FastledXLemming-BLE...
 
Notifications
Clear all

FastledXLemming-BLE esp32


mbohben
(@mbohben)
Active Member
Joined: 10 months ago
Posts: 7
Topic starter  

just straight to the topic, any help on getting fastled to work over lemmings-ble-gamepad?

there is my sample code, tested with matrix and direct button. its works well. but cant figure out how to make fastled connect to simhub over esp32.

*edit: cant upload ino file

Spoiler
code
/*
 * This code programs a number of pins on an ESP32 as buttons on a BLE gamepad
 *
 * It uses arrays to cut down on code
 *
 * Before using, adjust the numOfButtons, buttonPins and physicalButtons to suit your senario
 *
 */

#include 
#include  //  https://github.com/lemmingDev/ESP32-BLE-Gamepad 
#include 

BleGamepad bleGamepad("F1esp32", "mbohben", 100);

#define numOfButtons 21

byte previousButtonStates[numOfButtons];
byte currentButtonStates[numOfButtons];
byte buttonPins[numOfButtons] = {13, 12, 14, 27, 26, 25, 33, 32, 35, 34, 39, 36, 23, 22, 1, 3, 21, 19, 18, 5, 17};
byte physicalButtons[numOfButtons] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21};

///////fastled/////

#define BUF_SIZE 64
#define REV_LIGHTS_COUNT 16

#define WS2812_PIN 12

char simHubMessageBuf[BUF_SIZE];
BluetoothSerial bleGamepad;
CRGB leds[REV_LIGHTS_COUNT];

const byte ColorNone[3] = {0, 0, 0};
const byte ColorBlue[3] = {0, 0, 168};
const byte ColorGreen[3] = {0, 168, 0};
const byte ColorYellow[3] = {168, 168, 0};
const byte ColorRed[3] = {168, 0, 0};

int spd;
int revs;

//////////////////

void setup()
{
    for (byte currentPinIndex = 0; currentPinIndex < numOfButtons; currentPinIndex++)
    {
        pinMode(buttonPins[currentPinIndex], INPUT_PULLUP);
        previousButtonStates[currentPinIndex] = HIGH;
        currentButtonStates[currentPinIndex] = HIGH;
    }

    BleGamepadConfiguration bleGamepadConfig;
    Serial.begin(115200);
    bleGamepadConfig.setAutoReport(false);
    bleGamepadConfig.setButtonCount(numOfButtons);
    Serial.println("setup");
    memset(simHubMessageBuf, 0x0, BUF_SIZE);
    bleGamepad.begin(&bleGamepadConfig);

    FastLED.addLeds<NEOPIXEL, WS2812_PIN>(leds, REV_LIGHTS_COUNT);

    // changing bleGamepadConfig after the begin function has no effect, unless you call the begin function again
}

//////////////////

void loop()
{
    if (bleGamepad.isConnected())
    {
        for (byte currentIndex = 0; currentIndex < numOfButtons; currentIndex++)
        {
            currentButtonStates[currentIndex] = digitalRead(buttonPins[currentIndex]);

            if (currentButtonStates[currentIndex] != previousButtonStates[currentIndex])
            {
                if (currentButtonStates[currentIndex] == HIGH)
                {
                    bleGamepad.press(physicalButtons[currentIndex]);
                }
                else
                {
                    bleGamepad.release(physicalButtons[currentIndex]);
                }
            }
        }

        if (currentButtonStates != previousButtonStates)
        {
            for (byte currentIndex = 0; currentIndex < numOfButtons; currentIndex++)
            {
                previousButtonStates[currentIndex] = currentButtonStates[currentIndex];
            }

            bleGamepad.sendReport();
        }

        delay(20);
    }
    
    if (EEBlue.available() > 0) {
    EEBlue.readBytesUntil('{', simHubMessageBuf, BUF_SIZE);
    int readCount = EEBlue.readBytesUntil('}', simHubMessageBuf, BUF_SIZE);
    simHubMessageBuf[min(readCount - 1, BUF_SIZE - 1)] = 0x0;
    processMessage();
    memset(simHubMessageBuf, 0x0, BUF_SIZE);
  }
  FastLED.show();
}

////////////fastled/////////

void processMessage() {
  char msgType = simHubMessageBuf[0];

  switch (msgType) {
    case 'R': {
      sscanf(&simHubMessageBuf[1], "%d", &revs);
      int numOfLightsToShow = round((revs / 100.0f) * REV_LIGHTS_COUNT);
      for(int i=0; i < REV_LIGHTS_COUNT; i++) {
        const byte *color = i < numOfLightsToShow ? ledColor(i) : ColorNone;
        leds[i].setRGB(color[0], color[1], color[2]);
      }
      break;
    }
    case 'S':
      sscanf(&simHubMessageBuf[1], "%d", &spd);
      break;
  }

  Serial.print("Revs: ");
  Serial.println(revs);
  Serial.print("Speed: ");
  Serial.println(spd);
}

const byte* ledColor(int index) {
  switch(index) {
    case 7:
      return ColorBlue;
    case 6:
    case 5:
      return ColorRed;
    case 4:
    case 3:
      return ColorYellow;
    default: 
      return ColorGreen;  
  }
}

 


Quote
Topic Tags
mbohben
(@mbohben)
Active Member
Joined: 10 months ago
Posts: 7
Topic starter  

UP please


ReplyQuote
mbohben
(@mbohben)
Active Member
Joined: 10 months ago
Posts: 7
Topic starter  

help me please


ReplyQuote
mbohben
(@mbohben)
Active Member
Joined: 10 months ago
Posts: 7
Topic starter  

current proggress:

-button worked well

-leds seems worked, need to figure out the custom serial protocol (didn't work).

to do:

-back to the main topic (ws2812 how to get it work on esp32)

-add something like (tm1638, oled, or small lcd to get worked on)

 

Spoiler
Code
#include 
#include "BluetoothSerial.h"
#include 
#include 
#include 

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

BleGamepad bleGamepad("F1Wheel", "mbohben", 100); // Shows how you can customise the device name, manufacturer name and initial battery level

//def
#define ROWS 5
#define COLS 4
#define BUF_SIZE 64
#define REV_LIGHTS_COUNT 8
#define WS2812_PIN 5

//fastled
char simHubMessageBuf[BUF_SIZE];
BluetoothSerial EEBlue;
CRGB leds[REV_LIGHTS_COUNT];

const byte ColorNone[3] = {0, 0, 0};
const byte ColorBlue[3] = {0, 0, 168};
const byte ColorGreen[3] = {0, 168, 0};
const byte ColorYellow[3] = {168, 168, 0};
const byte ColorRed[3] = {168, 0, 0};

int spd;
int revs;

//keypad
uint8_t rowPins[ROWS] = {13, 12, 14, 27, 26}; // ESP32 pins used for rows      --> adjust to suit --> Pinout on board: R1, R2, R3, R4
uint8_t colPins[COLS] = {15, 4, 16, 17}; // ESP32 pins used for columns   --> adjust to suit --> Pinout on board: Q1, Q2, Q3, Q4
uint8_t keymap[ROWS][COLS] =
    {
      {1,2,3,4},
      {5,6,7,8},
      {9,10,11,12},
      {13,14,15,16},
      {17,18,19,20}
};

Keypad customKeypad = Keypad(makeKeymap(keymap), rowPins, colPins, ROWS, COLS);

void setup() {
  BleGamepadConfiguration bleGamepadConfig;
    bleGamepadConfig.setAutoReport(false);
    bleGamepadConfig.setControllerType(CONTROLLER_TYPE_GAMEPAD);
    bleGamepadConfig.setButtonCount(20);
    bleGamepadConfig.setHatSwitchCount(0);
    bleGamepadConfig.setWhichSpecialButtons(false, false, false, false, false, false, false, false);
    bleGamepadConfig.setWhichAxes(false, false, false, false, false, false, false, false);
    bleGamepadConfig.setWhichSimulationControls(false, false, false, false, false);
    Serial.begin(115200);
    Serial.println("setup");
  memset(simHubMessageBuf, 0x0, BUF_SIZE);
  bleGamepad.begin(&bleGamepadConfig);

  FastLED.addLeds<NEOPIXEL, WS2812_PIN>(leds, REV_LIGHTS_COUNT);
}

void loop() {
  if (EEBlue.available() > 0) {
    EEBlue.readBytesUntil('{', simHubMessageBuf, BUF_SIZE);
    int readCount = EEBlue.readBytesUntil('}', simHubMessageBuf, BUF_SIZE);
    simHubMessageBuf[min(readCount - 1, BUF_SIZE - 1)] = 0x0;
    processMessage();
    memset(simHubMessageBuf, 0x0, BUF_SIZE);
  }
  FastLED.show();
  KeypadUpdate();
  delay(10);
}


void processMessage() {
  char msgType = simHubMessageBuf[0];

  switch (msgType) {
    case 'R': {
      sscanf(&simHubMessageBuf[1], "%d", &revs);
      int numOfLightsToShow = round((revs / 100.0f) * REV_LIGHTS_COUNT);
      for(int i=0; i < REV_LIGHTS_COUNT; i++) {
        const byte *color = i < numOfLightsToShow ? ledColor(i) : ColorNone;
        leds[i].setRGB(color[0], color[1], color[2]);
      }
      break;
    }
    case 'S':
      sscanf(&simHubMessageBuf[1], "%d", &spd);
      break;
  }

  Serial.print("Revs: ");
  Serial.println(revs);
  Serial.print("Speed: ");
  Serial.println(spd);
}

const byte* ledColor(int index) {
  switch(index) {
    case 7:
      return ColorBlue;
    case 6:
    case 5:
      return ColorRed;
    case 4:
    case 3:
      return ColorYellow;
    default: 
      return ColorGreen;  
  }
}

void KeypadUpdate()
{
    customKeypad.getKeys();

    for (int i = 0; i < LIST_MAX; i++) // Scan the whole key list.      //LIST_MAX is provided by the Keypad library and gives the number of buttons of the Keypad instance
    {
        if (customKeypad.key[i].stateChanged) // Only find keys that have changed state.
        {
            uint8_t keystate = customKeypad.key[i].kstate;

            if (bleGamepad.isConnected())
            {
                if (keystate == PRESSED)
                {
                    bleGamepad.press(customKeypad.key[i].kchar);
                } // Press or release button based on the current state
                if (keystate == RELEASED)
                {
                    bleGamepad.release(customKeypad.key[i].kchar);
                }

                bleGamepad.sendReport(); // Send the HID report after values for all button states are updated, and at least one button state had changed
            }
        }
    }
}
This post was modified 2 days ago by mbohben

ReplyQuote
Share: