-
Notifications
You must be signed in to change notification settings - Fork 180
/
test.ino
139 lines (109 loc) · 4.59 KB
/
test.ino
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright (c) Sandeep Mistry. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//#define SHOW_FREE_MEMORY
#ifdef SHOW_FREE_MEMORY
#include <MemoryFree.h>
#endif
// Import libraries (BLEPeripheral depends on SPI)
#include <SPI.h>
#include <BLEPeripheral.h>
//custom boards may override default pin definitions with BLEPeripheral(PIN_REQ, PIN_RDY, PIN_RST)
BLEPeripheral blePeripheral = BLEPeripheral();
// create service
BLEService testService = BLEService("fff0");
// create counter characteristic
BLEUnsignedShortCharacteristic testCharacteristic = BLEUnsignedShortCharacteristic("fff1", BLERead | BLEWrite | BLEWriteWithoutResponse | BLENotify /*| BLEIndicate*/);
// create user description descriptor for characteristic
BLEDescriptor testDescriptor = BLEDescriptor("2901", "counter");
// last counter update time
unsigned long long lastSent = 0;
void setup() {
Serial.begin(9600);
#if defined (__AVR_ATmega32U4__)
delay(5000); //5 seconds delay for enabling to see the start up comments on the serial board
#endif
blePeripheral.setLocalName("test");
#if 1
blePeripheral.setAdvertisedServiceUuid(testService.uuid());
#else
const char manufacturerData[4] = {0x12, 0x34, 0x56, 0x78};
blePeripheral.setManufacturerData(manufacturerData, sizeof(manufacturerData));
#endif
// set device name and appearance
blePeripheral.setDeviceName("Test");
blePeripheral.setAppearance(0x0080);
// add service, characteristic, and decriptor to peripheral
blePeripheral.addAttribute(testService);
blePeripheral.addAttribute(testCharacteristic);
blePeripheral.addAttribute(testDescriptor);
// assign event handlers for connected, disconnected to peripheral
blePeripheral.setEventHandler(BLEConnected, blePeripheralConnectHandler);
blePeripheral.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler);
// assign event handlers for characteristic
testCharacteristic.setEventHandler(BLEWritten, characteristicWritten);
testCharacteristic.setEventHandler(BLESubscribed, characteristicSubscribed);
testCharacteristic.setEventHandler(BLEUnsubscribed, characteristicUnsubscribed);
// set initial value for characteristic
testCharacteristic.setValue(0);
// begin initialization
blePeripheral.begin();
Serial.println(F("BLE Peripheral"));
#ifdef SHOW_FREE_MEMORY
Serial.print(F("Free memory = "));
Serial.println(freeMemory());
#endif
}
void loop() {
BLECentral central = blePeripheral.central();
if (central) {
// central connected to peripheral
Serial.print(F("Connected to central: "));
Serial.println(central.address());
// reset counter value
testCharacteristic.setValue(0);
while (central.connected()) {
// central still connected to peripheral
if (testCharacteristic.written()) {
// central wrote new value to characteristic
Serial.println(F("counter written, reset"));
// reset counter value
lastSent = 0;
testCharacteristic.setValue(0);
}
if (millis() > 1000 && (millis() - 1000) > lastSent) {
// atleast one second has passed since last increment
lastSent = millis();
// increment characteristic value
testCharacteristic.setValue(testCharacteristic.value() + 1);
Serial.print(F("counter = "));
Serial.println(testCharacteristic.value(), DEC);
}
}
// central disconnected
Serial.print(F("Disconnected from central: "));
Serial.println(central.address());
}
}
void blePeripheralConnectHandler(BLECentral& central) {
// central connected event handler
Serial.print(F("Connected event, central: "));
Serial.println(central.address());
}
void blePeripheralDisconnectHandler(BLECentral& central) {
// central disconnected event handler
Serial.print(F("Disconnected event, central: "));
Serial.println(central.address());
}
void characteristicWritten(BLECentral& central, BLECharacteristic& characteristic) {
// characteristic value written event handler
Serial.print(F("Characteristic event, writen: "));
Serial.println(testCharacteristic.value(), DEC);
}
void characteristicSubscribed(BLECentral& central, BLECharacteristic& characteristic) {
// characteristic subscribed event handler
Serial.println(F("Characteristic event, subscribed"));
}
void characteristicUnsubscribed(BLECentral& central, BLECharacteristic& characteristic) {
// characteristic unsubscribed event handler
Serial.println(F("Characteristic event, unsubscribed"));
}