Skip to content

Instantly share code, notes, and snippets.

@nmwilk
Created February 21, 2021 14:27
Show Gist options
  • Save nmwilk/8b0ba9bd3744e0e747ce8f743ebed185 to your computer and use it in GitHub Desktop.
Save nmwilk/8b0ba9bd3744e0e747ce8f743ebed185 to your computer and use it in GitHub Desktop.
Example of shows WS2812 leds using ESP32 over Bluetooth with SimHub.
#include "BluetoothSerial.h"
#include <FastLED.h>
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
#define BUF_SIZE 64
#define REV_LIGHTS_COUNT 8
#define WS2812_PIN 12
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;
void setup() {
Serial.begin(115200);
Serial.println("setup");
memset(simHubMessageBuf, 0x0, BUF_SIZE);
EEBlue.begin("BT-DASH");
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();
}
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;
}
}
@dixionfph
Copy link

Hi nmwilk, I can't get the fastled to work with simhub using bluetooth. Is there any setting I need to change in simhub?

@gatohdo
Copy link

gatohdo commented Apr 3, 2022

hi how are you?, I upload the code, get the bluetooth conection, but i dont know what i need to do in the simhub custom serial....can you share a video tutorial please?

@mbohben
Copy link

mbohben commented Jun 12, 2022

how to get this worked with ble-gamepad? since ble-gamepad independently worked on the serial port too without arduino bluetoothserial.h

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment