aboutsummaryrefslogtreecommitdiffstats
path: root/source/controller.cpp
blob: d9ac9611f22a5dca3180dffc454a33e2bb452b10 (plain)
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
// sharp takes < ~0.05uA

#include <bluefruit.h>

#include "rtc.hpp"
#include "sharp.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);

	Serial.println(F("Initializing..."));

	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  

	Serial.println(F("Ready."));
	Sharp::addWidget<TimeWidget>();
	Sharp::addWidget<NotificationWidget>("Welcome to smartwatch");
}

void loop(void)
{
	if (bleuart.available())
		handlePacket();

	delay(10);
}

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':
		//RTC::setMessage(buf + 1);
		break;
	case 'T':
		Sharp::addWidget<NotificationWidget>("Time updated");
		RTC::setTicks(std::atoi(buf + 1) * 60);
		break;
	default:
		break;
	}
}