Prototype for 4-byte BLE communication

This commit is contained in:
Tempest 2024-11-04 21:14:20 +07:00 committed by Glahera
parent 359eb64f30
commit 785a4212f5
2 changed files with 2 additions and 345 deletions

View File

@ -70,8 +70,8 @@ void setup() {
void loop() {
// notify changed value
if (deviceConnected) {
btmessage = pCharacteristic->getValue().c_str();
mode = btmessage;
btmessage = pCharacteristic->getData();
mode = btmessage;
}
// disconnecting
if (!deviceConnected && oldDeviceConnected) {

View File

@ -1,343 +0,0 @@
// Include Section
#include "esp_dmx.h"
#define INTERRUPT_PIN 0
struct Button {
const uint8_t PIN;
uint32_t numberKeyPresses;
bool pressed;
};
struct Panel {
int led0;
int led1;
int led2;
int led3 = 0;
};
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
BLEServer* pServer = NULL;
BLECharacteristic* pCharacteristic = NULL;
bool deviceConnected = false;
bool oldDeviceConnected = false;
uint32_t value = 0;
uint32_t 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;
}
};
// Defining BOOT button on ESP32 as our built-in button.
Button button1 = {INTERRUPT_PIN, 0, false};
int mode = 0;
const int modeAmount = 16;
uint8_t brightnessMax = 20;
uint8_t universalBrightness = 10;
uint8_t dataSeq[modeAmount][DMX_PACKET_SIZE] =
{
{
0,
0,0,universalBrightness,0,
0,0,universalBrightness,0,
0,0,universalBrightness,0,
0,0,universalBrightness,0,
0,0,universalBrightness,0,
0,0,universalBrightness,0,
0,0,universalBrightness,0,
0,0,universalBrightness,0,
0,0,universalBrightness,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0
},
{
0,
0,universalBrightness,0,0,
0,universalBrightness,0,0,
0,universalBrightness,0,0,
0,universalBrightness,0,0,
0,universalBrightness,0,0,
0,universalBrightness,0,0,
0,universalBrightness,0,0,
0,universalBrightness,0,0,
0,universalBrightness,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0
},
{
0,
universalBrightness,0,0,0,
universalBrightness,0,0,0,
universalBrightness,0,0,0,
universalBrightness,0,0,0,
universalBrightness,0,0,0,
universalBrightness,0,0,0,
universalBrightness,0,0,0,
universalBrightness,0,0,0,
universalBrightness,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0
},
{
0,
universalBrightness,0,0,0, // Orange
0,universalBrightness,0,0, // White
0,universalBrightness,0,0, // White
0,0,universalBrightness,0, // Blue
0,0,universalBrightness,0,
0,0,universalBrightness,0,
0,0,universalBrightness,0,
0,0,universalBrightness,0,
0,universalBrightness,0,0,
//End Inner Round
//Start Outer Round
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0
},
{
0,
//Start Inner Round
0,0,universalBrightness,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
//End Inner Round
//Start Outer Round
universalBrightness,0,0,0,
universalBrightness,0,0,0,
universalBrightness,0,0,0,
universalBrightness,0,0,0,
universalBrightness,0,0,0,
universalBrightness,0,0,0,
universalBrightness,0,0,0,
universalBrightness,0,0,0,
universalBrightness,0,0,0,
universalBrightness,0,0,0,
universalBrightness,0,0,0,
universalBrightness,0,0,0,
universalBrightness,0,0,0,
universalBrightness,0,0,0,
universalBrightness,0,0,0,
universalBrightness,0,0,0
}
}
;
void IRAM_ATTR isr() {
button1.pressed = true;
};
void ledBlink(int interval, int pinNumber) {
digitalWrite(
pinNumber,
!digitalRead(pinNumber)
);
delay(interval);
};
void dmxSetup() {
const dmx_port_t dmx_num = DMX_NUM_1;
Serial.printf("\nSetting up DMX Port %d", dmx_num);
// First, use the default DMX configuration...
dmx_config_t config = DMX_CONFIG_DEFAULT;
// Declare Personality RGBW
const int personality_count = 1;
Serial.print("\nDefining DMX Personality... ");
dmx_personality_t personalities[] = {
{4, "RGBW"}
};
Serial.print("Done");
Serial.print("\nInstalling DMX Driver... ");
// ...install the DMX driver...
dmx_driver_install(dmx_num, &config, personalities, personality_count);
Serial.print("Done");
// ...and then set the communication pins!
const int tx_pin = 23;
const int rx_pin = 22;
const int rts_pin = 21;
Serial.printf("\nSetting up pin %d as Transmit Pin, pin %d as Receive Pin and pin %d as RTS Pin... ", tx_pin, rx_pin, rts_pin);
dmx_set_pin(dmx_num, tx_pin, rx_pin, rts_pin);
Serial.print("Done\n");
}
void serialRead(){
String incomingByte;
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.readStringUntil('\r\n');
Serial.print("\nI received: ");
Serial.print(incomingByte);
mode = incomingByte.toInt();
}
}
void setup() {
Serial.begin(115200);
Serial.print("\nIf you receive this message, ESP32 module has finished setting up Serial Interface for communication.");
// Create the BLE Device
BLEDevice::init("Pupilometer LED Billboard");
// 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("\nWaiting a client connection to notify...");
pinMode(INTERRUPT_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(INTERRUPT_PIN), isr, RISING);
delay(2000);
dmxSetup();
const dmx_port_t dmx_num = DMX_NUM_1;
Serial.println("Welcome to Pupilometer LED Billboard!");
}
void loop() {
// Send the DMX packet.
int modeOld = mode;
// notify changed value
if (deviceConnected) {
btmessage = pCharacteristic->getData();
// Serial.print(btmessage);
mode = btmessage.toInt();
}
// 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;
}
serialRead();
dmx_send(DMX_NUM_1);
// Preparing next packet
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
};
if (modeOld != mode){
Serial.printf("\nCurrently received data is %p", btmessage);
Serial.printf("\nChanging Lighting Preset to Preset %d", mode);
}
// 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);
serialRead();
}