Skip to content

Instantly share code, notes, and snippets.

@sblantipodi
Created April 21, 2021 18:12
Show Gist options
  • Save sblantipodi/2a70f68a9c953626a752468cf2144fd6 to your computer and use it in GitHub Desktop.
Save sblantipodi/2a70f68a9c953626a752468cf2144fd6 to your computer and use it in GitHub Desktop.
Stripped GW Firmware
/**
* Dynamic PIN Template
*/
struct PINUtil {
template<uint8_t DYNAMIC_DATA_PIN = DATA_PIN>
void init(int ledToUse) {
FastLED.addLeds<CHIPSET, DYNAMIC_DATA_PIN, COLOR_ORDER>(leds, ledToUse);
}
};
PINUtil pinUtil;
/**
* Setup function
*/
void setup() {
// Bootsrap setup() with Wifi and MQTT functions
#if defined(ESP32)
xTaskCreatePinnedToCore(
tcpTask, /* Task function. */
"tcpTask", /* name of task. */
8048, /* Stack size of task */
NULL, /* parameter of the task */
5, /* priority of the task */
NULL, /* Task handle to keep track of created task */
0);
xTaskCreatePinnedToCore(
serialTask, /* Task function. */
"serialTask", /* name of task. */
8048, /* Stack size of task */
NULL, /* parameter of the task */
5 , /* priority of the task */
NULL, /* Task handle to keep track of created task */
1);
#endif
}
/**
* START CALLBACK
* @param topic MQTT topic
* @param payload MQTT payload
* @param length MQTT message length
*/
void callback(char *topic, byte *payload, unsigned int length) {
int myLeds[NUM_LEDS] = {};
char delimiters[] = ",";
char *ptr;
int index = 0;
ptr = strtok(reinterpret_cast<char *>(payload), delimiters);
int numLedFromLuciferin = atoi(ptr);
ptr = strtok(NULL, delimiters);
if (numLedFromLuciferin == 0) {
effect = Effect::solid;
} else {
if (dynamicLedNum != numLedFromLuciferin) {
setNumLed(numLedFromLuciferin);
}
while (ptr != NULL) {
myLeds[index] = atoi(ptr);
leds[index].r = (myLeds[index] >> 16 & 0xFF);
leds[index].g = (myLeds[index] >> 8 & 0xFF);
leds[index].b = (myLeds[index] >> 0 & 0xFF);
index++;
ptr = strtok(NULL, delimiters);
}
}
framerateCounter++;
lastStream = millis();
espMultiCoreSemaphore = true;
#if defined(ESP32)
delay(1);
#endif
}
/**
* Main task for ESP32, pinned to CORE0
*/
#if defined(ESP32)
void feedTheDog(){
// feed dog
TIMERG0.wdt_wprotect=TIMG_WDT_WKEY_VALUE; // write enable
TIMERG0.wdt_feed=1; // feed dog
TIMERG0.wdt_wprotect=0; // write protect
}
/**
* Pinned on CORE0, max performance with TCP
* @param parameter
*/
void tcpTask(void * parameter) {
while(true) {
EVERY_N_MILLISECONDS(500) {
feedTheDog();
}
// Contains WiFi and MQTT keep alive code
mainLoop();
delay(1);
}
}
/**
* Pinned on CORE1, max performance for Serial
* @param parameter
*/
void serialTask(void * parameter) {
while(true) {
EVERY_N_MILLISECONDS(500) {
feedTheDog();
}
if (espMultiCoreSemaphore) {
espMultiCoreSemaphore = false;
FastLED.show();
}
delay(1);
}
}
#endif
void loop() {
#if defined(ESP32)
delay(1);
EVERY_N_MILLISECONDS(500) {
feedTheDog();
}
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment