Finalized BLE communication

This commit is contained in:
Tempest 2024-11-15 11:46:47 +07:00
parent 2a30a98144
commit 245eb45ff1
2 changed files with 63 additions and 39 deletions

BIN
src/lightingFirmware/.DS_Store vendored Normal file

Binary file not shown.

View File

@ -1,7 +1,7 @@
// Include Section
#include "esp_dmx.h"
#include "UUID.h"
#define INTERRUPT_PIN 0
struct Button {
@ -23,7 +23,9 @@ struct Panel {
#include <BLE2902.h>
BLEServer* pServer = NULL;
BLECharacteristic* pCharacteristic = NULL;
BLECharacteristic* pCharacteristic1 = NULL;
BLECharacteristic* pCharacteristic2 = NULL;
bool deviceConnected = false;
bool oldDeviceConnected = false;
uint32_t value = 0;
@ -32,9 +34,10 @@ 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"
#define CHARACTERISTIC_UUIDS {"beb5483e-36e1-4688-b7f5-ea07361b26a8","beb5483e-36e1-4688-b7f5-ea07361b26a9","beb5483e-36e1-4688-b7f5-ea07361b26b0"}
uint16_t SERVICE_UUID = 20241115;
const int panelAmount = 25;
BLECharacteristic* pCharacteristics[panelAmount];
char* CHARACTERISTIC_UUIDS[panelAmount];
class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
@ -45,21 +48,17 @@ class MyServerCallbacks: public BLEServerCallbacks {
deviceConnected = false;
}
};
//Function for setting up BLE Characteristic
const int panelAmount = 3;
BLECharacteristic* pCharacteristics[] = {};
void setupCharacteristic(pService){
for(int i = 0; i < panelAmount - 1; i++){
pCharacteristics[i] = pService->createCharacteristic(
CHARACTERISTIC_UUIDS[i],
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE |
BLECharacteristic::PROPERTY_NOTIFY |
BLECharacteristic::PROPERTY_INDICATE
);
};
};
//void setupCharacteristic(BLEService *pService){
// for(int i = 0; i < panelAmount - 1; i++){
// pCharacteristics[i] = pService->createCharacteristic(
// CHARACTERISTIC_UUIDS[i],
// BLECharacteristic::PROPERTY_READ |
// BLECharacteristic::PROPERTY_WRITE |
// BLECharacteristic::PROPERTY_NOTIFY |
// BLECharacteristic::PROPERTY_INDICATE
// );
// };
//};
// Defining BOOT button on ESP32 as our built-in button.
@ -281,20 +280,26 @@ void setup() {
pServer->setCallbacks(new MyServerCallbacks());
// Create the BLE Service
BLEService *pService = pServer->createService(SERVICE_UUID);
BLEService *pService = pServer->createService(SERVICE_UUID,52);
// Create a BLE Characteristic
pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
for (uint32_t i = 0; i < panelAmount; i++){
UUID uuid;
uuid.seed(i+1);
uuid.generate();
Serial.printf("Creating BLE Characteristic with UUID %s ...", uuid.toCharArray());
pCharacteristics[i] = pService->createCharacteristic(
i+1,
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());
// pCharacteristics[i]->addDescriptor(new BLE2902());
Serial.printf("Done\n");
};
// Start the service
pService->start();
@ -305,7 +310,6 @@ void setup() {
pAdvertising->setScanResponse(false);
pAdvertising->setMinPreferred(0x0); // set value to 0x00 to not advertise this parameter
BLEDevice::startAdvertising();
Serial.println("\nWaiting a client connection to notify...");
pinMode(INTERRUPT_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(INTERRUPT_PIN), isr, RISING);
@ -318,13 +322,16 @@ void loop() {
dmx_send(DMX_NUM_1);
// Save Old Mode
int modeOld = mode;
uint8_t* btMessage[panelAmount];
uint8_t dmxData[DMX_PACKET_SIZE] = {0};
// notify changed value
if (deviceConnected) {
Serial.printf("\nI received the following bytes: ");
uint8_t* btMessage = pCharacteristic->getData();
for (int i = 0; i < sizeof(btMessage); i++){
Serial.printf("%d ",btMessage[i]);
for (int i = 0; i < panelAmount;i++){
btMessage[i] = pCharacteristics[i]->getData();
for (int j = 0; j < sizeof(btMessage[i]); j++){
Serial.printf("%d ",btMessage[i][j]);
};
};
}
// disconnecting
@ -349,8 +356,25 @@ void loop() {
if (modeOld != mode){
Serial.printf("\nChanging Lighting Preset to Preset %d", mode);
}
// Serial.printf("\nConstructing Payload using ");
if (deviceConnected){
// Serial.printf("Bluetooth Data ...");
for (int i = 0; i < panelAmount; i++){
for (int j = 0; j < sizeof(btMessage[i]); j++){
dmxData[i*4 + j + 1] = btMessage[i][j];
// Serial.printf(".");
};
};
}
else{
// Serial.printf("Preset Data ...");
for (int i = 0; i < sizeof(dataSeq[mode]); i++){
dmxData[i] = dataSeq[mode][i];
};
};
// Serial.printf(" Done");
// Wait until the packet is finished being sent before proceeding.
dmx_wait_sent(DMX_NUM_1, DMX_TIMEOUT_TICK);
// Now write the packet synchronously!
dmx_write(DMX_NUM_1, dataSeq[mode], 100);
dmx_write(DMX_NUM_1, dmxData, 100);
}