update only after button press
[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             Pulsecounter_hotTick_indicate();
29         }
30         Hal_greenLedOn();
31         Hal_redLedOn();
32         Hal_delay(10);
33         Hal_greenLedOff();
34         Hal_redLedOff();
35         Hal_tickStart(15000, tickHandler);
36         break;
37     case 1:
38         cold++;
39         if (connected)
40             Pulsecounter_coldTick_indicate();
41         Hal_greenLedOn();
42         Hal_delay(10);
43         Hal_greenLedOff();
44         break;
45     case 2:
46         hot++;
47         if (connected)
48             Pulsecounter_hotTick_indicate();
49         Hal_redLedOn();
50         Hal_delay(10);
51         Hal_redLedOff();
52         break;
53     default:
54         for (i = 0; i < 5; i++) {
55             Hal_greenLedOn();
56             Hal_redLedOn();
57             Hal_delay(10);
58             Hal_greenLedOff();
59             Hal_redLedOff();
60             Hal_delay(10);
61         }
62     }
63 }
64
65 static void tickHandler(void) {
66     uint8_t i;
67
68     Hal_tickStop();
69     if (connected)
70         return;
71     for (i = 0; i < 3; i++) {
72         Hal_greenLedOn();
73         Hal_delay(50);
74         Hal_redLedOn();
75         Hal_delay(50);
76         Hal_redLedOff();
77         Hal_delay(50);
78         Hal_greenLedOff();
79     }
80     updatable = false;
81     /* Pulsecounter_accept(false); */
82 }
83
84 /* -------- SCHEMA CALLBACKS -------- */
85
86 void Pulsecounter_connectHandler(void) {
87     connected = true;
88     Hal_connected();
89     Hal_redLedOn();
90     Hal_delay(100);
91     Hal_redLedOff();
92     Hal_greenLedOn();
93     Hal_delay(100);
94     Hal_greenLedOff();
95 }
96
97 void Pulsecounter_disconnectHandler(void) {
98     connected = false;
99     updatable = false;
100     Hal_greenLedOn();
101     Hal_delay(100);
102     Hal_greenLedOff();
103     Hal_redLedOn();
104     Hal_delay(100);
105     Hal_redLedOff();
106     /* Hal_tickStart(15000, tickHandler); */
107     Hal_disconnected();
108 }
109
110 void Pulsecounter_coldTick_fetch(Pulsecounter_coldTick_t* const output) {
111     *output = cold;
112 }
113
114 void Pulsecounter_hotTick_fetch(Pulsecounter_hotTick_t* const output) {
115     *output = hot;
116 }
117
118 void Pulsecounter_coldSet_store(Pulsecounter_coldSet_t* const input) {
119     if (updatable)
120         cold = *input;
121 }
122
123 void Pulsecounter_hotSet_store(Pulsecounter_hotSet_t* const input) {
124     if (updatable)
125         hot = *input;
126 }