@mathieu Hi!
I have sent the initialization code to your e-mail (I have included your full commands there, but not here on the forum).
Basically, the commands are sent in a non-blocking fashion one-by one:
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#include <pgmspace.h>
const char* const commands[] PROGMEM = {
"|LB UID:lb0 X:20 Y:10\r\n",
...
...
...
};
const size_t numCommands = sizeof(commands) / sizeof(commands[0]);
namespace uuid {
static const char *SERVICE_UUID = "6E400001-B5A3-F393-E0A9-E50E24DCCA9E";
static const char *RX_CHARACTERISTIC_UUID = "6E400002-B5A3-F393-E0A9-E50E24DCCA9E";
static const char *TX_CHARACTERISTIC_UUID = "6E400003-B5A3-F393-E0A9-E50E24DCCA9E";
} // namespace uuid
// forward declare parser for incoming messages
void parseGuioMsg(const String &msg);
// setup done flag
bool setupDone = false;
// init active flag
bool initActive = false;
// send command period in msec
static const unsigned long sendCommandPeriod = 50;
// custom handling of server callbacks
class CustomBLEServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
Serial.println("Connected!");
};
void onDisconnect(BLEServer* pServer) {
Serial.println("Disconnected!");
// fix provided by BM
// restart advertising after disconnect, otherwise GUI-O cannot re-connect
if(setupDone) {
// restart advertising on disconnect
delay(500);
pServer->startAdvertising();
}
}
};
// custom handling of characteristic callbacks
class CustomBLECharacteristicCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
std::string msg = pCharacteristic->getValue();
// parse message string
parseGuioMsg(String(msg.c_str()));
}
};
// global ptr
BLECharacteristic *pTxCharacteristic;
void setup() {
// debug output
Serial.begin(115200);
// create device
BLEDevice::init("BasicBLE_NUS");
// create server and register callback
BLEServer *pServer = BLEDevice::createServer();
pServer->setCallbacks(new CustomBLEServerCallbacks());
// create service
BLEService *pService = pServer->createService(uuid::SERVICE_UUID);
// crate Tx characteristic and add descriptor
pTxCharacteristic = pService->createCharacteristic(uuid::TX_CHARACTERISTIC_UUID, BLECharacteristic::PROPERTY_NOTIFY);
pTxCharacteristic->addDescriptor(new BLE2902());
// crate Rx characteristic and register callback
BLECharacteristic *pRxCharacteristic = pService->createCharacteristic(uuid::RX_CHARACTERISTIC_UUID, BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_WRITE_NR);
pRxCharacteristic->setCallbacks(new CustomBLECharacteristicCallbacks());
// start the service and start advertising
pService->start();
pServer->getAdvertising()->start();
// setup done flag
setupDone = true;
}
void loop() {
static size_t currentCommandIndex = 0;
static unsigned long lastCommandTime = 0;
if (initActive) {
if (currentCommandIndex < numCommands) {
if (millis() - lastCommandTime > sendCommandPeriod) {
lastCommandTime = millis();
// retrieve and send the command
char commandBuffer[256];
strcpy_P(commandBuffer, (char*)pgm_read_ptr(&commands[currentCommandIndex]));
sendMsg(commandBuffer);
Serial.print("Sending: ");
Serial.println(commandBuffer);
currentCommandIndex++;
}
}
else {
initActive = false;
// hide loading screen
sendMsg("@hls\r\n");
}
}
}
/***************************/
/* IMPLEMENT YOUR GUI HERE */
/***************************/
void sendMsg(const String &msg) {
pTxCharacteristic->setValue(std::string(msg.c_str()));
pTxCharacteristic->notify();
delay(50);
}
void parseGuioMsg(const String &msg) {
if(msg.startsWith("@init")) {
Serial.println("GUI-O app is requesting INITIALIZATION!");
// clear screen and set background
sendMsg("@cls\r\n");
sendMsg("@guis BGC:#FFFFFF\r\n");
sendMsg("|SORI UID:sori2 HID:sori ORI:2 SEN:0\r\n");
// wait for orientation change (alternatively catch GUI-O screen orientation change event...)
delay(500);
// show loading screen
sendMsg("@sls\r\n");
initActive = true;
}
}
Can you please test it and see if it works for you?
You can try extending sendCommandPeriod if you have any issues... Also you can comment out the @sls and @hls commands to see exactly what is going on...
Best regards,
Kl3m3n