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
|
/**
* @file controller.cpp
* @brief The main smartwatch code.
*
* Copyright (C) 2019 Clyne Sullivan
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <bluefruit.h>
#include "rtc.hpp"
#include "sharp.hpp"
#include "vibrate.hpp"
#include "widget.hpp"
BLEUart bleuart;
void handlePacket(void);
void app_error_handler_bare([[maybe_unused]] uint32_t error_code)
{
Serial.println("Received an error");
while (1);
}
void setup(void)
{
Serial.begin(115200);
while (!Serial)
delay(10);
Vibrate::begin();
RTC::begin();
Sharp::begin();
Bluefruit.begin();
// Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4
Bluefruit.setTxPower(-20);
Bluefruit.setName("smartwatch");
// Configure and start the BLE Uart service
bleuart.begin();
// Advertising packet
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
Bluefruit.Advertising.addTxPower();
Bluefruit.Advertising.addService(bleuart);
Bluefruit.ScanResponse.addName();
Bluefruit.Advertising.restartOnDisconnect(true);
Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds
Sharp::addWidget<TimeWidget>();
Sharp::addWidget<NotificationWidget>("Welcome to smartwatch");
}
static int touchToCoord(int val)
{
return val / 70 * (SHARP_HEIGHT / 10);
}
void loop(void)
{
if (bleuart.available())
handlePacket();
static int last = 0;
static bool scrolled = false;
int val = analogRead(A5);
if (val >= 10) {
if (last == 0) {
scrolled = false;
last = val;
}
auto diff = val - last;
if (std::abs(diff) > 50) {
Sharp::setScroll(touchToCoord(diff));
scrolled = true;
}
} else {
if (last != 0 && !scrolled) {
Sharp::sendInput(touchToCoord(last));
delay(100);
}
last = 0;
Sharp::setScroll();
}
delay(10);
}
static void addNewNote(const char *buf)
{
unsigned int count = (buf[1] - '0') * 10 + (buf[2] - '0');
Sharp::addWidget<NoteWidget>(buf + 3, count);
}
void handlePacket(void)
{
char buf[64];
unsigned int i;
for (i = 0; bleuart.available() && i < 63; i++)
buf[i] = bleuart.read();
buf[i] = '\0';
switch (buf[0]) {
case 'L':
Vibrate::pulse();
Sharp::addWidget<NotificationWidget>(buf + 1);
break;
case 'T':
Vibrate::pulse();
Sharp::addWidget<NotificationWidget>("Time updated");
RTC::setTicks(std::atoi(buf + 1) * 60);
break;
case 'N':
Vibrate::pulse();
addNewNote(buf);
default:
break;
}
}
|