From 2ea5f20f62fc530e200deafa7439a20524cc5cb8 Mon Sep 17 00:00:00 2001 From: Dorian Zedler Date: Fri, 9 Oct 2020 00:11:55 +0200 Subject: [PATCH] added ble arduino sketch --- Arduino/OmobiDisplay/OmobiDisplay.ino | 160 ++++++++++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100644 Arduino/OmobiDisplay/OmobiDisplay.ino diff --git a/Arduino/OmobiDisplay/OmobiDisplay.ino b/Arduino/OmobiDisplay/OmobiDisplay.ino new file mode 100644 index 0000000..9c97df4 --- /dev/null +++ b/Arduino/OmobiDisplay/OmobiDisplay.ino @@ -0,0 +1,160 @@ +#include +#include +#include +#include + +BLECharacteristic *pCharacteristic; +bool deviceConnected = false; +float txValue = 0; +const int readPin = 32; // Use GPIO number. See ESP32 board pinouts +const int LED = 2; // Could be different depending on the dev board. I used the DOIT ESP32 dev board. + +//std::string rxValue; // Could also make this a global var to access it in loop() + +// See the following for generating UUIDs: +// https://www.uuidgenerator.net/ + +#define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID +#define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E" +#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E" + + +#include +#include +#include + +#define PIN 4 + +TaskHandle_t Task1; + +Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(8, 8, 2, 1, PIN, + NEO_TILE_TOP + NEO_TILE_LEFT + NEO_TILE_ROWS + NEO_TILE_PROGRESSIVE + + NEO_MATRIX_TOP + NEO_MATRIX_LEFT + NEO_MATRIX_ROWS + NEO_TILE_PROGRESSIVE, + NEO_GRB + NEO_KHZ800); + + +const uint16_t colors[] = { + matrix.Color(255, 0, 0), matrix.Color(0, 255, 0), matrix.Color(0, 0, 255) }; + +void Task1code(); + +int x = matrix.width(); +int pass = 0; +String scroll_text = "This is a test !!!"; + +void sendMessage(std::string message) { + + pCharacteristic->setValue(message); // Sending a test message +// pCharacteristic->setValue(txString); + + pCharacteristic->notify(); // Send the value to the app! +} + +class MyServerCallbacks: public BLEServerCallbacks { + void onConnect(BLEServer* pServer) { + deviceConnected = true; + }; + + void onDisconnect(BLEServer* pServer) { + deviceConnected = false; + } +}; + +class MyCallbacks: public BLECharacteristicCallbacks { + void onWrite(BLECharacteristic *pCharacteristic) { + std::string rxValue = pCharacteristic->getValue(); + + if (rxValue.length() > 0) { + Serial.println("*********"); + Serial.print("Received Value: "); + Serial.println(rxValue.c_str()); + Serial.println("*********"); + + scroll_text = String(rxValue.c_str()); + + sendMessage(rxValue); + } + } +}; + +void Task1code( void * pvParameters ) { + uint32_t color = 0; + for (;;) { + matrix.fillScreen(0); + matrix.setCursor(x, 0); + matrix.print(scroll_text.c_str()); + if (--x < (int)(-6 * scroll_text.length())) { + x = matrix.width(); + if (++pass >= 3) pass = 0; + matrix.setTextColor(colors[pass]); + } + + + matrix.drawPixel(0,0,color); + color = colorsetCallbacks(new MyServerCallbacks()); + + // Create the BLE Service + BLEService *pService = pServer->createService(SERVICE_UUID); + + // Create a BLE Characteristic + pCharacteristic = pService->createCharacteristic( + CHARACTERISTIC_UUID_TX, + BLECharacteristic::PROPERTY_NOTIFY + ); + + pCharacteristic->addDescriptor(new BLE2902()); + + BLECharacteristic *pCharacteristic = pService->createCharacteristic( + CHARACTERISTIC_UUID_RX, + BLECharacteristic::PROPERTY_READ | + BLECharacteristic::PROPERTY_WRITE + ); + + pCharacteristic->setCallbacks(new MyCallbacks()); + + // Start the service + pService->start(); + + // Start advertising + pServer->getAdvertising()->addServiceUUID(pService->getUUID()); + pServer->getAdvertising()->setScanResponse(true); + pServer->getAdvertising()->setMinPreferred(0x06); // functions that help with iPhone connections issue + pServer->getAdvertising()->setMinPreferred(0x12); + pServer->getAdvertising()->start(); + Serial.println("Waiting a client connection to notify..."); + + + + matrix.begin(); + matrix.setTextWrap(false); + matrix.setBrightness(40); + matrix.setTextColor(colors[0]); + + xTaskCreatePinnedToCore(Task1code, "Task1", 10000, NULL, 1, &Task1, 0); +} + +void loop() { + if (deviceConnected) { + sendMessage("PING"); + } + delay(1000); + +}