-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
171 lines (141 loc) · 4.59 KB
/
main.cpp
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/* mbed Microcontroller Library
* Copyright (c) 2006-2013 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "mbed.h"
#include "ble/BLE.h"
#include <string>
#include "ble/services/UARTService.h"
#define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console;
* it will have an impact on code-size and power consumption. */
#if NEED_CONSOLE_OUTPUT
#define DEBUG(...) { printf(__VA_ARGS__); }
#else
#define DEBUG(...) /* nothing */
#endif /* #if NEED_CONSOLE_OUTPUT */
BLEDevice ble;
DigitalOut led1(LED1);
UARTService *uartServicePtr;
Serial interLink(P0_2, P0_1); // tx, rx```
uint8_t h = 12;
uint8_t m = 0;
uint8_t s = 0;
uint8_t alarm_h;
uint8_t alarm_m;
uint8_t alarm_s;
bool alarm=false;
void sendNeo(uint8_t r, uint8_t g, uint8_t b) {
interLink.printf("%02d,%02d,%02d\n",r,g,b);
}
void sendTime() {
// this runs every second
// Increment the time
s++;
if(s>59) {
m++;
if(m>59) {
h++;
if(h>12) {
h=1;
}
m=0;
}
s=0;
}
// if we have an alarm enabled
if(alarm) {
if(alarm_s==s && alarm_m==m && alarm_h==h) {
// set the alarm off!
alarm=false;
interLink.printf("|\n"); // Send an alarm
}
}
//-----This block maps the time onto the neopixel
uint8_t temp_s = (s/5)%60;
if(temp_s==0) temp_s=12;
uint8_t temp_m = (m/5)%60;
if(temp_m==0) temp_m=12;
uint8_t temp_h = h%13;
if(h>12) temp_h++;
//-----------------------------------------
sendNeo(temp_h,temp_s,temp_m);
}
void periodicCallback() {
sendTime();
}
void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
{
DEBUG("Disconnected!\n\r");
DEBUG("Restarting the advertising process\n\r");
ble.startAdvertising();
}
void onDataWritten(const GattWriteCallbackParams *params)
{
if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) {
uint16_t bytesRead = params->len;
DEBUG("received %u bytes\n\r", bytesRead);
ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead);
uint8_t identifier = *params->data;
if(identifier==1) {
h = *(params->data+1);
m = *(params->data+2);
s = *(params->data+3);
}
else if (identifier==2) {
alarm_h = *(params->data+1);
alarm_m = *(params->data+2);
alarm_s = *(params->data+3);
alarm = 1;
}
else if (identifier==3) {
interLink.printf("!\n");
}
else if (identifier==4) {
interLink.printf("*\n");
}
else {
int n;
interLink.printf(";");
for(n=0; n<(bytesRead); n++) {
interLink.printf("%c", *(params->data+n));
}
interLink.printf("\n");
}
}
}
int main(void)
{
interLink.baud(115200);
led1 = 1;
Ticker ticker;
ticker.attach(periodicCallback, 1);
DEBUG("Initialising the nRF51822\n\r");
ble.init();
ble.onDisconnection(disconnectionCallback);
ble.onDataWritten(onDataWritten);
/* setup advertising */
ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME,
(const uint8_t *)"JoshWatch", sizeof("JoshWatch") - 1);
ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
(const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
ble.setAdvertisingInterval(500); /* 1000ms; in multiples of 0.625ms. */
ble.startAdvertising();
UARTService uartService(ble);
uartServicePtr = &uartService;
while (true) {
ble.waitForEvent();
}
}