Skip to content

Commit

Permalink
Experiment with wled udp and serial .ino tweaks for esp32
Browse files Browse the repository at this point in the history
  • Loading branch information
jsilveira committed Dec 29, 2021
1 parent 21288fe commit 55d1aaf
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 82 deletions.
154 changes: 104 additions & 50 deletions arduino/conga-esp32/conga-esp32.ino
Original file line number Diff line number Diff line change
@@ -1,60 +1,30 @@
#define FASTLED_ESP32_I2S
#include <FastLED.h>

// How many leds in your strip?
// #define NUM_LEDS 150
#define NUM_LEDS 345
#define NUM_LEDS 600

// For led chips like Neopixels, which have a data line, ground, and power, you just
// need to define DATA_PIN. For led chipsets that are SPI based (four wires - data, clock,
// ground, and power), like the LPD8806 define both DATA_PIN and CLOCK_PIN
#define DATA_PIN 2
//#define DATA_PIN2 7
#define DATA_PIN2 19
//#define DATA_PIN3 8
#define CLOCK_PIN 13

// Define the array of leds
CRGB leds[NUM_LEDS];
// CRGB leds2[NUM_LEDS];
CRGB leds[NUM_LEDS/2];
CRGB leds2[NUM_LEDS/2];
// CRGB leds3[NUM_LEDS];


int stripSize = NUM_LEDS/2;

// This variable is persisted even after reseting the arduino. That allows cycling through
// different programs of light
__attribute__((section(".noinit"))) unsigned int program;

void setup() {
program = (program + 1) % 2;
//program = 0;

// Uncomment/edit one of the following lines for your leds arrangement.
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
// FastLED.addLeds<WS2812B, DATA_PIN2, GRB>(leds2, NUM_LEDS);
// FastLED.addLeds<WS2812B, DATA_PIN3, GRB>(leds3, NUM_LEDS);

FastLED.setMaxPowerInVoltsAndMilliamps(5, 300);

Serial.begin(3*500000); // set up Serial library at 500000 bps, the same than in Node.js

Serial.println("Hello world!"); // prints hello with ending line break

for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB::Black;
}

leds[0] = CRGB::Black;
leds[1] = CRGB::Red;
leds[2] = CRGB::Green;
leds[3] = CRGB::Blue;

// leds2[1] = CRGB::Red;
// leds2[2] = CRGB::Green;
// leds2[3] = CRGB::Blue;

// leds3[1] = CRGB::Red;
// leds3[2] = CRGB::Green;
// leds3[3] = CRGB::Blue;

FastLED.show();
}

byte ENCODING_POS_RGB = 1;
byte ENCODING_POS_VGA = 2;
Expand All @@ -78,19 +48,19 @@ byte vgaGreen(byte vga) {
}

void writeLeds(int pos, byte r, byte g, byte b) {
// if (pos < 150) {
if (pos < stripSize) {
leds[pos].red = r;
leds[pos].green = g;
leds[pos].blue = b;
// } else if(pos < 300) {
// leds2[pos].red = r;
// leds2[pos].green = g;
// leds2[pos].blue = b;
} else if(pos >= stripSize) {
leds2[pos-stripSize].red = r;
leds2[pos-stripSize].green = g;
leds2[pos-stripSize].blue = b;
// } else {
// leds3[pos].red = r;
// leds3[pos].green = g;
// leds3[pos].blue = b;
// }
}
}

void writeLedsHSB(int pos, byte h, byte s, byte b) {
Expand All @@ -99,10 +69,6 @@ void writeLedsHSB(int pos, byte h, byte s, byte b) {
}
}


int stripSize = NUM_LEDS;


boolean connected = false;
void reconnect() {
connected = false;
Expand Down Expand Up @@ -143,6 +109,7 @@ void readLedsFromSerial() {
lastConnectionTime = millis();
} else {
drainSerial();
Serial.println("RECONNECT");
delay(50);
}
}
Expand Down Expand Up @@ -204,25 +171,112 @@ void readLedsFromSerial() {
return reconnect();
}
} else if (encoding == ENCODING_RGB) {
int j = stripSize;
int j = NUM_LEDS;
char data[3 * j];
int total = Serial.readBytes(data, 3 * j);
if (total == 3 * j) {
for (int i = 0; i < j; i++) {
writeLeds(i, data[i * 3], data[1 + i * 3], data[2 + i * 3]);
}
} else {
Serial.println("WRONGSIZE");
return reconnect();
}
} else {
Serial.println("WRONG ENCODING");
return reconnect();
}

FastLED.show();

//FastLEDshowESP32();

Serial.println("OK"); // ASCII printable characters

// Protocolo que entiende node.js
}

boolean programInitialized = false;



#define FASTLED_SHOW_CORE 1

static TaskHandle_t FastLEDshowTaskHandle = 0;
static TaskHandle_t userTaskHandle = 0;

void FastLEDshowESP32()
{
if (userTaskHandle == 0) {
const TickType_t xMaxBlockTime = pdMS_TO_TICKS( 200 );
// -- Store the handle of the current task, so that the show task can
// notify it when it's done
//noInterrupts();
userTaskHandle = xTaskGetCurrentTaskHandle();

// -- Trigger the show task
xTaskNotifyGive(FastLEDshowTaskHandle);

// -- Wait to be notified that it's done
ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS( 100 ));

//interrupts();
userTaskHandle = 0;
}
}

void FastLEDshowTask(void *pvParameters)
{
const TickType_t xMaxBlockTime = pdMS_TO_TICKS( 500 );
// -- Run forever...
for(;;) {
// -- Wait for the trigger
ulTaskNotifyTake(pdTRUE,portMAX_DELAY);

// -- Do the show (synchronously)

FastLED.show();

// -- Notify the calling task
xTaskNotifyGive(userTaskHandle);
}
}


void setup() {
xTaskCreatePinnedToCore(FastLEDshowTask, "FastLEDshowTask", 10000, NULL,3, &FastLEDshowTaskHandle, FASTLED_SHOW_CORE);

program = (program + 1) % 2;
//program = 0;

// Uncomment/edit one of the following lines for your leds arrangement.
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS/2);
FastLED.addLeds<WS2812B, DATA_PIN2, GRB>(leds2, NUM_LEDS/2);
// FastLED.addLeds<WS2812B, DATA_PIN3, GRB>(leds3, NUM_LEDS);

FastLED.setMaxPowerInVoltsAndMilliamps(5, 300);

Serial.begin(3*500000); // set up Serial library at 500000 bps, the same than in Node.js
Serial.setRxBufferSize(40960);

Serial.println("Hello world!"); // prints hello with ending line break

for (int i = 0; i < stripSize; i++) {
leds[i] = CRGB::Black;
}

leds[0] = CRGB::White;
leds[1] = CRGB::Red;
leds[2] = CRGB::Green;
leds[3] = CRGB::Blue;

// leds2[1] = CRGB::Red;
// leds2[2] = CRGB::Green;
// leds2[3] = CRGB::Blue;

// leds3[1] = CRGB::Red;
// leds3[2] = CRGB::Green;
// leds3[3] = CRGB::Blue;

//FastLEDshowESP32();
FastLED.show();
}
1 change: 1 addition & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"dgram": "^1.0.1",
"lodash": "^4.17.15",
"moment": "^2.24.0",
"node-fetch": "^2.6.6",
"performance-now": "^2.1.0",
"pino": "^5.15.0",
"pino-pretty": "^3.5.0",
Expand Down
31 changes: 22 additions & 9 deletions server/setups/conga.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"serial1": {
"type": "serial",
"params": {
"numberOfLights": 345,
"numberOfLights": 45,
"baudRate": 1500000,
"devicePortWindows": "COM3",
"devicePortUnix": "/dev/ttyACM0"
}
Expand All @@ -15,23 +16,35 @@
"type": "serial",
"params": {
"numberOfLights": 300,
"baudRate": 1500000,
"devicePortWindows": "COM4",
"devicePortUnix": "/dev/ttyACM0"
}
},
"serialTest": {
"type": "serial",
"params": {
"numberOfLights": 600,
"baudRate": 1500000,
"devicePortWindows": "COM5",
"devicePortUnix": "/dev/ttyACM0"
}
},
"udp": {
"type": "udp-wled",
"params": {
"numberOfLights": 300,
"ip": "192.168.0.177",
"udpPort": 21324
}
}
},
"lightsToDevicesMapping": [
{
"from": 0,
"to": 345,
"to":600,
"baseIndex": 0,
"deviceName": "serial1"
},
{
"from": 345,
"to":645,
"baseIndex": -300,
"deviceName": "serial2"
"deviceName": "serialTest"
}
]
}
4 changes: 4 additions & 0 deletions server/src/devices/serial.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ module.exports = class LightDeviceSerial extends LightDevice {
this.updateStatus(this.STATUS_RUNNING);
} else if (data === "OK") {
//logger.info(`ACK`);
} else if (data === "RECONNECT") {
logger.info("RECONNECT request")
this.sendInitialKick();
return;
} else if (data === "ARDUINOSTART") {
logger.info("ARDUINOSTART");
return;
Expand Down
58 changes: 37 additions & 21 deletions server/src/devices/udp-wled.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ const dns = require('dns');
const dgram = require("dgram");
const now = require("performance-now");
const logger = require("pino")({ prettyPrint: true });
const fetch = require('node-fetch');

const { LightDevice } = require("./base");
const { WLEDRGBEncoder } = require("./encodings");

module.exports = class LightDeviceUDPWLED extends LightDevice {
constructor({ numberOfLights, ip, name, udpPort }) {
super(numberOfLights, "E " + (name || ip));
super(numberOfLights, "WLED " + (name || ip));

this.expectedIp = ip;
this.name = name;
Expand All @@ -33,9 +34,22 @@ module.exports = class LightDeviceUDPWLED extends LightDevice {

this.packageCount = 0;

this.updateStatus(this.STATUS_CONNECTING);
this.setupCommunication();
}

async fetchDeviceInfo() {
try {
let res = await (await fetch(`http://${this.expectedIp}/json/info`, {timeout: 2000})).json()
this.updateStatus(this.STATUS_RUNNING);
this.connected = true;
this.lastFps = res.leds.fps;
} catch(err) {
this.updateStatus(this.STATUS_ERROR);
}
setTimeout(() => this.fetchDeviceInfo(), 1000);
}

sendNextFrame() {
if (this.connected && this.freshData) {
const data = this.encoder.encode(this.state)
Expand Down Expand Up @@ -121,6 +135,8 @@ module.exports = class LightDeviceUDPWLED extends LightDevice {
this.sendNextFrame();
}
}, 16);

this.fetchDeviceInfo();
}

// handleListening() {
Expand Down Expand Up @@ -171,23 +187,23 @@ module.exports = class LightDeviceUDPWLED extends LightDevice {
};
//
//
var client = dgram.createSocket('udp4');
const _ = require('lodash')
let i = 0;
let send = () => {
i = (i+1) % 120;
let payload = Buffer.from([
2, 5,
100, 50, 50,
... _.flatten(new Array(i*1).fill([0,0,0])),
// 0, 0, 50,
// 0, 255, Math.floor(Math.random()*255),
255, 0, 255,
255, 0, 255,
255, 0, 255,
255, 0, 255,
]);
client.send(payload, 0, payload.length, 21666, '192.168.0.255');
}

setTimeout(send, 20);
// var client = dgram.createSocket('udp4');
// const _ = require('lodash')
// let i = 0;
// let send = () => {
// i = (i+1) % 120;
// let payload = Buffer.from([
// 2, 5,
// 100, 50, 50,
// ... _.flatten(new Array(i*1).fill([0,0,0])),
// // 0, 0, 50,
// // 0, 255, Math.floor(Math.random()*255),
// 255, 0, 255,
// 255, 0, 255,
// 255, 0, 255,
// 255, 0, 255,
// ]);
// client.send(payload, 0, payload.length, 21666, '192.168.0.255');
// }
//
// setTimeout(send, 20);
Loading

0 comments on commit 55d1aaf

Please sign in to comment.