-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathM5Peripheral.ino
More file actions
97 lines (87 loc) · 2.96 KB
/
Copy pathM5Peripheral.ino
File metadata and controls
97 lines (87 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <M5StickCPlus.h>
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#include <BLE2902.h>
#define SERVICE_UUID "FE55"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
#define SERVER_NAME "M5StickC"
/* ================ BLE ================ */
BLEServer* pServer = NULL;
BLECharacteristic* pCharacteristic = NULL;
bool deviceConnected = false;
class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setCursor(0, 0);
M5.Lcd.setTextSize(2);
M5.Lcd.println("connect");
deviceConnected = true;
};
void onDisconnect(BLEServer* pServer) {
M5.Lcd.setCursor(0, 0);
M5.Lcd.setTextSize(2);
M5.Lcd.println("disconnect");
deviceConnected = false;
}
};
class MyCallbacks: public BLECharacteristicCallbacks {
void onRead(BLECharacteristic *pCharacteristic) {
M5.Lcd.println("read");
std::string value = pCharacteristic->getValue();
M5.Lcd.println(value.c_str());
}
void onWrite(BLECharacteristic *pCharacteristic) {
std::string value = pCharacteristic->getValue();
M5.Lcd.setCursor(0, 160);
M5.Lcd.setTextSize(3);
M5.Lcd.println("written");
M5.Lcd.setTextSize(5);
M5.Lcd.println(value.c_str());
}
};
/* ================ Properties ================ */
int counter = 0;
/* ================ Arduino ================ */
void setup() {
M5.begin();
M5.Lcd.setCursor(0, 0);
M5.Lcd.setTextSize(2);
M5.Lcd.println("Start!");
setupBle();
}
void loop() {
if (deviceConnected) {
if(M5.BtnB.wasPressed()) {
pCharacteristic->setValue(counter);
pCharacteristic->notify();
M5.Lcd.setCursor(0, 60);
M5.Lcd.setTextSize(3);
M5.Lcd.println("notify");
M5.Lcd.setTextSize(5);
M5.Lcd.println(counter);
counter += 1;
}
}
M5.update();
}
/* ================ BLE ================ */
void setupBle() {
BLEDevice::init(SERVER_NAME);
BLEServer *pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
BLEService *pService = pServer->createService(SERVICE_UUID);
pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE |
BLECharacteristic::PROPERTY_WRITE_NR |
BLECharacteristic::PROPERTY_NOTIFY |
BLECharacteristic::PROPERTY_INDICATE
);
pCharacteristic->setCallbacks(new MyCallbacks());
pCharacteristic->addDescriptor(new BLE2902());
pService->start();
BLEAdvertising *pAdvertising = pServer->getAdvertising();
pAdvertising->start();
}