Updated for single BLE Characteristic communication, replacing for 1 Char 1 Lamp

This commit is contained in:
Tempest 2025-09-19 20:35:25 +07:00
parent b4cfbbe60b
commit 91d4f59a9f
2 changed files with 28 additions and 21 deletions

View File

@ -97,16 +97,24 @@ async def set_full_matrix_on_ble(colorSeries):
colorSeries[24] = temp_color_12 colorSeries[24] = temp_color_12
# ===================================================================== # =====================================================================
# Ensure all characteristics are available before writing if DEBUG_MODE:
if len(ble_characteristics) != lampAmount: # Ensure all characteristics are available before writing
print(f"Mismatch in lamp amount. Expected {lampAmount}, got {len(ble_characteristics)}.") print(f"Confirmed DEBUG set to true.")
return if len(ble_characteristics) != lampAmount:
print(f"Constructed the following matrix data: {colorSeries}") print(f"Mismatch in lamp amount. Expected {lampAmount}, got {len(ble_characteristics)}.")
# Write each byte string to its corresponding characteristic return
for i, char in enumerate(ble_characteristics): print(f"Constructed the following matrix data: {colorSeries}")
value_to_write = colorSeries[i] # Write each byte string to its corresponding characteristic
print(f"Setting Lamp {i} ({char.uuid}) to {value_to_write.hex()}") for i, char in enumerate(ble_characteristics):
await ble_client.write_gatt_char(char.uuid, value_to_write) value_to_write = colorSeries[i]
print(f"Setting Lamp {i} ({char.uuid}) to {value_to_write.hex()}")
await ble_client.write_gatt_char(char.uuid, value_to_write)
else:
print(f"Confirmed DEBUG set to false.")
value_to_write = b"".join([color for color in colorSeries])
print(value_to_write)
print(f"Setting lamps to {value_to_write.hex()}")
await ble_client.write_gatt_char(ble_characteristics[0].uuid, value_to_write)
async def connect_to_ble_device(): async def connect_to_ble_device():

View File

@ -15,20 +15,19 @@
bool debugMode = true; bool debugMode = true;
int bleCharCount; int bleCharCount;
const int channelPerLamp = 4; const int channelPerLamp = 4;
int expectedLampCount = 25; const int expectedLampCount = 25;
//DMX_PACKET_SIZE = 100; const int dmxPacketSize = channelPerLamp * expectedLampCount + 1; //
struct Button { struct Button {
const uint8_t PIN; const uint8_t PIN;
uint32_t numberKeyPresses; uint32_t numberKeyPresses;
bool pressed; bool pressed;
}; };
uint8_t dmxData[101] = {0}; uint8_t dmxData[DMX_PACKET_SIZE] = {0};
BLEServer* pServer = NULL; BLEServer* pServer = NULL;
bool deviceConnected = false; bool deviceConnected = false;
bool oldDeviceConnected = false; bool oldDeviceConnected = false;
uint32_t value = 0;
uint16_t SERVICE_UUID = 20241115; uint16_t SERVICE_UUID = 20241115;
const int panelAmount = 25; const int panelAmount = 25;
@ -53,7 +52,7 @@ const int modeAmount = 16;
uint8_t brightnessMax = 20; uint8_t brightnessMax = 20;
uint8_t universalBrightness = 10; uint8_t universalBrightness = 10;
uint8_t dataSeq[modeAmount][101] = uint8_t dataSeq[modeAmount][DMX_PACKET_SIZE] =
{ {
{ {
0, 0,
@ -295,8 +294,8 @@ void setup() {
bleCharCount = (panelAmount * debugMode) + !debugMode; bleCharCount = (panelAmount * debugMode) + !debugMode;
Serial.printf("\nCalculating BLE MTU ..."); Serial.printf("\nCalculating BLE MTU ...");
uint16_t bleMTU = ((panelAmount * 3) / bleCharCount) + 3; uint16_t bleMTU = ((panelAmount * 3) / bleCharCount) + 3;
// Serial.printf("\nSetting BLE MTU to %i bytes... ", bleMTU); Serial.printf("\nSetting BLE MTU to %i bytes... ", bleMTU);
// BLEDevice::setMTU(bleMTU + 3); BLEDevice::setMTU(bleMTU + 3);
Serial.printf("Done!\n"); Serial.printf("Done!\n");
for (uint32_t i = 0; i < bleCharCount; i++){ for (uint32_t i = 0; i < bleCharCount; i++){
@ -364,7 +363,7 @@ void loop() {
serialRead(); serialRead();
if (modeOld != mode){ if (modeOld != mode){
Serial.printf("\nChanging Lighting Preset to Preset %d", mode); Serial.printf("\nChanging Lighting Preset to Preset %d", mode);
uint8_t lampData[75]; uint8_t lampData[DMX_PACKET_SIZE / 4 * 3];
Serial.printf("\nDetected preset %i size: %i", mode, sizeof(dataSeq[mode])); Serial.printf("\nDetected preset %i size: %i", mode, sizeof(dataSeq[mode]));
for (int i = 0; i < sizeof(dataSeq[mode]); i++){ for (int i = 0; i < sizeof(dataSeq[mode]); i++){
dmxData[i] = dataSeq[mode][i]; dmxData[i] = dataSeq[mode][i];
@ -376,7 +375,7 @@ void loop() {
lampData[j] = dataSeq[mode][i]; lampData[j] = dataSeq[mode][i];
} }
}; };
pCharacteristics[0]->setValue(lampData, 75); pCharacteristics[0]->setValue(lampData, expectedLampCount * 3);
} }
Serial.printf("\nConstructing DMX Payload with size "); Serial.printf("\nConstructing DMX Payload with size ");
for (int i = 0; i < bleCharCount; i++){ for (int i = 0; i < bleCharCount; i++){
@ -388,7 +387,7 @@ void loop() {
int lampSum = i*3 + j; int lampSum = i*3 + j;
int dmxAddress = (lampSum / 3) * 4 + lampSum % 3 + 1; int dmxAddress = (lampSum / 3) * 4 + lampSum % 3 + 1;
dmxData[dmxAddress] = packet; dmxData[dmxAddress] = packet;
Serial.printf("[[%i,%i] %i - %i] ",i , j, dmxAddress, packet); // Serial.printf("[[%i,%i] %i - %i] ",i , j, dmxAddress, packet);
}; };
}; };
Serial.printf("\n"); Serial.printf("\n");
@ -397,6 +396,6 @@ void loop() {
// Wait until the packet is finished being sent before proceeding. // Wait until the packet is finished being sent before proceeding.
dmx_wait_sent(DMX_NUM_1, DMX_TIMEOUT_TICK); dmx_wait_sent(DMX_NUM_1, DMX_TIMEOUT_TICK);
// Now write the packet synchronously! // Now write the packet synchronously!
dmx_write(DMX_NUM_1, dmxData, 100); dmx_write(DMX_NUM_1, dmxData, DMX_PACKET_SIZE);
dmx_send(DMX_NUM_1); dmx_send(DMX_NUM_1);
} }