some debugging, notes on BT protocol
[pulsecounter.git] / Pulsecounter-Prog.c
1 #include "Pulsecounter.h"
2 #include "Hal.h"
3
4 static void gpioHandler(uint8_t id);
5 static void tickHandler(void);
6 static int32_t cold = 0;
7 static int32_t hot  = 0;
8 static bool connected = false;
9 static bool updatable = false;
10
11 void main() {
12     Hal_init();
13     Hal_gpioEnable(gpioHandler);
14     Pulsecounter_setDeviceName("PULS-CNTR");
15     Pulsecounter_start();
16     Hal_idleLoop();
17 }
18
19 static void gpioHandler(uint8_t id) {
20     uint8_t i;
21
22     switch (id) {
23     case 0:
24         /* Pulsecounter_accept(true); */
25         updatable = true;
26         if (connected) {
27             Pulsecounter_coldTick_indicate();
28             Hal_delay(100);
29             Pulsecounter_hotTick_indicate();
30         }
31         Hal_greenLedOn();
32         Hal_redLedOn();
33         Hal_delay(10);
34         Hal_greenLedOff();
35         Hal_redLedOff();
36         Hal_tickStart(15000, tickHandler);
37         break;
38     case 1:
39         cold++;
40         if (connected)
41             Pulsecounter_coldTick_indicate();
42         Hal_greenLedOn();
43         Hal_delay(10);
44         Hal_greenLedOff();
45         break;
46     case 2:
47         hot++;
48         if (connected)
49             Pulsecounter_hotTick_indicate();
50         Hal_redLedOn();
51         Hal_delay(10);
52         Hal_redLedOff();
53         break;
54     default:
55         for (i = 0; i < 5; i++) {
56             Hal_greenLedOn();
57             Hal_redLedOn();
58             Hal_delay(10);
59             Hal_greenLedOff();
60             Hal_redLedOff();
61             Hal_delay(10);
62         }
63     }
64 }
65
66 static void tickHandler(void) {
67     uint8_t i;
68
69     Hal_tickStop();
70     if (connected)
71         return;
72     for (i = 0; i < 3; i++) {
73         Hal_greenLedOn();
74         Hal_delay(50);
75         Hal_redLedOn();
76         Hal_delay(50);
77         Hal_redLedOff();
78         Hal_delay(50);
79         Hal_greenLedOff();
80     }
81     updatable = false;
82     /* Pulsecounter_accept(false); */
83 }
84
85 /* -------- SCHEMA CALLBACKS -------- */
86
87 void Pulsecounter_connectHandler(void) {
88     connected = true;
89     Hal_tickStop();
90     Hal_connected();
91     Hal_redLedOn();
92     Hal_delay(100);
93     Hal_redLedOff();
94     Hal_greenLedOn();
95     Hal_delay(100);
96     Hal_greenLedOff();
97 }
98
99 void Pulsecounter_disconnectHandler(void) {
100     connected = false;
101     updatable = false;
102     Hal_greenLedOn();
103     Hal_delay(100);
104     Hal_greenLedOff();
105     Hal_redLedOn();
106     Hal_delay(100);
107     Hal_redLedOff();
108     /* Hal_tickStart(15000, tickHandler); */
109     Hal_disconnected();
110 }
111
112 void Pulsecounter_coldTick_fetch(Pulsecounter_coldTick_t* const output) {
113     *output = cold;
114 }
115
116 void Pulsecounter_hotTick_fetch(Pulsecounter_hotTick_t* const output) {
117     *output = hot;
118 }
119
120 void Pulsecounter_coldSet_store(Pulsecounter_coldSet_t* const input) {
121     Hal_greenLedOn();
122     Hal_delay(100);
123     Hal_greenLedOff();
124     Hal_delay(100);
125     Hal_greenLedOn();
126     Hal_delay(100);
127     Hal_greenLedOff();
128     // if (updatable)
129         cold = *input;
130 }
131
132 void Pulsecounter_hotSet_store(Pulsecounter_hotSet_t* const input) {
133     Hal_redLedOn();
134     Hal_delay(100);
135     Hal_redLedOff();
136     Hal_delay(100);
137     Hal_redLedOn();
138     Hal_delay(100);
139     Hal_redLedOff();
140     // if (updatable)
141         hot = *input;
142 }