From bda937a889e53266a4438b79a3d6370fb9313ad9 Mon Sep 17 00:00:00 2001 From: Tempest Date: Thu, 18 Sep 2025 21:44:13 +0700 Subject: [PATCH] Remove misc file and modified firmware flow to commit data to BLE buffer --- .../SimpleBleDevice/SimpleBleDevice.ino | 88 ------------------- src/lightingFirmware/SimpleBleDevice/ci.json | 6 -- .../esp32_test0/esp32_test0.ino | 86 +++++++++--------- 3 files changed, 46 insertions(+), 134 deletions(-) delete mode 100644 src/lightingFirmware/SimpleBleDevice/SimpleBleDevice.ino delete mode 100644 src/lightingFirmware/SimpleBleDevice/ci.json diff --git a/src/lightingFirmware/SimpleBleDevice/SimpleBleDevice.ino b/src/lightingFirmware/SimpleBleDevice/SimpleBleDevice.ino deleted file mode 100644 index 45cfc64..0000000 --- a/src/lightingFirmware/SimpleBleDevice/SimpleBleDevice.ino +++ /dev/null @@ -1,88 +0,0 @@ -#include -#include -#include -#include - -BLEServer* pServer = NULL; -BLECharacteristic* pCharacteristic = NULL; -bool deviceConnected = false; -bool oldDeviceConnected = false; -uint32_t value = 0; -String btmessage; - -// See the following for generating UUIDs: -// https://www.uuidgenerator.net/ - -#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b" -#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8" - - -class MyServerCallbacks: public BLEServerCallbacks { - void onConnect(BLEServer* pServer) { - deviceConnected = true; - }; - - void onDisconnect(BLEServer* pServer) { - deviceConnected = false; - } -}; - - - -void setup() { - Serial.begin(115200); - - // Create the BLE Device - BLEDevice::init("ESP32"); - - // Create the BLE Server - pServer = BLEDevice::createServer(); - pServer->setCallbacks(new MyServerCallbacks()); - - // Create the BLE Service - BLEService *pService = pServer->createService(SERVICE_UUID); - - // Create a BLE Characteristic - pCharacteristic = pService->createCharacteristic( - CHARACTERISTIC_UUID, - BLECharacteristic::PROPERTY_READ | - BLECharacteristic::PROPERTY_WRITE | - BLECharacteristic::PROPERTY_NOTIFY | - BLECharacteristic::PROPERTY_INDICATE - ); - - // https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml - // Create a BLE Descriptor - pCharacteristic->addDescriptor(new BLE2902()); - - // Start the service - pService->start(); - - // Start advertising - BLEAdvertising *pAdvertising = BLEDevice::getAdvertising(); - pAdvertising->addServiceUUID(SERVICE_UUID); - pAdvertising->setScanResponse(false); - pAdvertising->setMinPreferred(0x0); // set value to 0x00 to not advertise this parameter - BLEDevice::startAdvertising(); - Serial.println("Waiting a client connection to notify..."); -} - -void loop() { - // notify changed value - if (deviceConnected) { - btmessage = pCharacteristic->getData(); - mode = btmessage; - } - // disconnecting - if (!deviceConnected && oldDeviceConnected) { - delay(500); // give the bluetooth stack the chance to get things ready - pServer->startAdvertising(); // restart advertising - Serial.println("Start advertising"); - oldDeviceConnected = deviceConnected; - } - // connecting - if (deviceConnected && !oldDeviceConnected) { - // do stuff here on connecting - oldDeviceConnected = deviceConnected; - } -} \ No newline at end of file diff --git a/src/lightingFirmware/SimpleBleDevice/ci.json b/src/lightingFirmware/SimpleBleDevice/ci.json deleted file mode 100644 index d33a23f..0000000 --- a/src/lightingFirmware/SimpleBleDevice/ci.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "requires": [ - "CONFIG_BT_ENABLED=y", - "CONFIG_BLUEDROID_ENABLED=y" - ] -} diff --git a/src/lightingFirmware/esp32_test0/esp32_test0.ino b/src/lightingFirmware/esp32_test0/esp32_test0.ino index cc657f8..b3e568f 100644 --- a/src/lightingFirmware/esp32_test0/esp32_test0.ino +++ b/src/lightingFirmware/esp32_test0/esp32_test0.ino @@ -15,13 +15,15 @@ bool debugMode = true; int bleCharCount; const int channelPerLamp = 4; +int expectedLampCount = 25; +//DMX_PACKET_SIZE = 100; struct Button { const uint8_t PIN; uint32_t numberKeyPresses; bool pressed; }; -uint8_t dmxData[DMX_PACKET_SIZE] = {0}; +uint8_t dmxData[101] = {0}; BLEServer* pServer = NULL; bool deviceConnected = false; @@ -51,7 +53,7 @@ const int modeAmount = 16; uint8_t brightnessMax = 20; uint8_t universalBrightness = 10; -uint8_t dataSeq[modeAmount][DMX_PACKET_SIZE] = +uint8_t dataSeq[modeAmount][101] = { { 0, @@ -286,22 +288,22 @@ void setup() { // Create the BLE Service BLEService *pService = pServer->createService(SERVICE_UUID,52); - const bool debugMode = true; + const bool debugMode = false; // Serial.printf(debugMode); // Create a BLE Characteristic Serial.printf("\nCalculating BLE Charateristic Count"); bleCharCount = (panelAmount * debugMode) + !debugMode; - Serial.printf("\nCalculating BLE MTU"); + Serial.printf("\nCalculating BLE MTU ..."); uint16_t bleMTU = ((panelAmount * 3) / bleCharCount) + 3; // Serial.printf("\nSetting BLE MTU to %i bytes... ", bleMTU); // BLEDevice::setMTU(bleMTU + 3); Serial.printf("Done!\n"); for (uint32_t i = 0; i < bleCharCount; i++){ - UUID uuid; - uuid.seed(i+1); - uuid.generate(); - Serial.printf("Creating BLE Characteristic with UUID %s ...", uuid.toCharArray()); + //UUID uuid; + //uuid.seed(i+1); + //uuid.generate(); + //Serial.printf("Creating BLE Characteristic with UUID %s ...", BLEUUID(i+1)); pCharacteristics[i] = pService->createCharacteristic( i+1, @@ -311,8 +313,10 @@ void setup() { BLECharacteristic::PROPERTY_NOTIFY | BLECharacteristic::PROPERTY_INDICATE ); + Serial.printf("Created BLE Characteristic with UUID %s ...", pCharacteristics[i]->getUUID().toString().c_str()); + // pCharacteristics[i]->addDescriptor(new BLE2902()); - Serial.printf("Done\n"); +// Serial.printf("Done\n"); }; // Start the service @@ -332,17 +336,10 @@ void loop() { int modeOld = mode; int msgSize; uint8_t* btMessage[bleCharCount]; + // uint8_t dmxData[DMX_PACKET_SIZE] = {0}; // notify changed value if (deviceConnected) { - for (int i = 0; i < bleCharCount;i++){ - btMessage[i] = pCharacteristics[i]->getData(); - msgSize = pCharacteristics[i]->getLength(); - Serial.printf("\nI have received %i bytes.", msgSize); - //for (int j = 0; j < msgSize; j++){ - // Serial.printf("[%i - %d] ",j,btMessage[i][j]); - //}; - //Serial.printf("\n"); - } + } // disconnecting if (!deviceConnected && oldDeviceConnected) { @@ -357,36 +354,45 @@ void loop() { oldDeviceConnected = deviceConnected; } - serialRead(); +// Serial.printf("\nConstructing Payload using "); +// Serial.printf("Bluetooth Data ..."); if (button1.pressed){ if (mode < modeAmount - 1){mode++;} else {mode = 0;}; // Increment the value of each slot, excluding the start code. button1.pressed = false; // Reset button status to FALSE }; -// Serial.printf("\nConstructing Payload using "); - if (deviceConnected){ -// Serial.printf("Bluetooth Data ..."); - Serial.printf("\nConstructing DMX Payload: "); - for (int i = 0; i < bleCharCount; i++){ - for (int j = 0; j < msgSize; j++){ - int packet = btMessage[i][j]; - int k = i*3 + j; - int dmxAddress = (k / 3) * 4 + k % 3 + 1; - dmxData[dmxAddress] = packet; - Serial.printf("[[%i,%i] %i - %i] ",i , j, dmxAddress, packet); - }; + serialRead(); + if (modeOld != mode){ + Serial.printf("\nChanging Lighting Preset to Preset %d", mode); + uint8_t lampData[75]; + Serial.printf("\nDetected preset %i size: %i", mode, sizeof(dataSeq[mode])); + for (int i = 0; i < sizeof(dataSeq[mode]); i++){ + dmxData[i] = dataSeq[mode][i]; + int sublampIndex = i % 4; + //Serial.printf("[%i]", sublampIndex, j); + if (sublampIndex > 0) { + int j = (i / 4) * 3 + sublampIndex - 1; + Serial.printf("[%i](%i)", j, sublampIndex); + lampData[j] = dataSeq[mode][i]; + } }; - Serial.printf("\n"); + pCharacteristics[0]->setValue(lampData, 75); } - else{ -// Serial.printf("Preset Data ..."); - if (modeOld != mode){ - Serial.printf("\nChanging Lighting Preset to Preset %d", mode); - for (int i = 0; i < sizeof(dataSeq[mode]); i++){ - dmxData[i] = dataSeq[mode][i]; - }; - } + Serial.printf("\nConstructing DMX Payload with size "); + for (int i = 0; i < bleCharCount; i++){ + btMessage[i] = pCharacteristics[i]->getData(); + msgSize = pCharacteristics[i]->getLength(); + Serial.printf("%i bytes ", msgSize); + for (int j = 0; j < msgSize; j++){ + int packet = btMessage[i][j]; + int lampSum = i*3 + j; + int dmxAddress = (lampSum / 3) * 4 + lampSum % 3 + 1; + dmxData[dmxAddress] = packet; + Serial.printf("[[%i,%i] %i - %i] ",i , j, dmxAddress, packet); + }; }; + Serial.printf("\n"); + // Serial.printf(" Done"); // Wait until the packet is finished being sent before proceeding. dmx_wait_sent(DMX_NUM_1, DMX_TIMEOUT_TICK);