160 lines
4.5 KiB
C++
160 lines
4.5 KiB
C++
#include <BLEDevice.h>
|
|
#include <BLEServer.h>
|
|
#include <BLEUtils.h>
|
|
#include <BLE2902.h>
|
|
|
|
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 <Adafruit_GFX.h>
|
|
#include <Adafruit_NeoMatrix.h>
|
|
#include <Adafruit_NeoPixel.h>
|
|
|
|
#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 = color<matrix.Color(255,255,255)?color+1:0;
|
|
|
|
portDISABLE_INTERRUPTS();
|
|
matrix.show();
|
|
portENABLE_INTERRUPTS();
|
|
delay(100);
|
|
}
|
|
}
|
|
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
|
|
// Create the BLE Device
|
|
BLEDevice::init("ESP32 Chat Test"); // Give it a name
|
|
|
|
// Create the BLE Server
|
|
BLEServer *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_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);
|
|
|
|
}
|