4ac463f50a59d2986b7b44f6c4f3b25e6cef0ce1
[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 bool connected = false;
7 static int32_t base4 = 0;
8 static int32_t base5 = 0;
9 static int32_t event4 = 0;
10 static int32_t event5 = 0;
11
12 void main() {
13     Hal_init();
14     Hal_gpioEnable(gpioHandler);
15     Pulsecounter_setDeviceName("PULS-CNTR");
16     Pulsecounter_start();
17     Hal_idleLoop();
18 }
19
20 static void gpioHandler(uint8_t id) {
21     uint8_t i;
22
23     switch (id) {
24     case 0:
25         Pulsecounter_accept(true);
26             Hal_greenLedOn();
27             Hal_redLedOn();
28             Hal_delay(10);
29             Hal_greenLedOff();
30             Hal_redLedOff();
31         Hal_tickStart(15000, tickHandler);
32         break;
33     case 1:
34         event4++;
35         if (connected)
36             Pulsecounter_event4_indicate();
37         Hal_greenLedOn();
38         Hal_delay(10);
39         Hal_greenLedOff();
40         break;
41     case 2:
42         event5++;
43         if (connected)
44             Pulsecounter_event5_indicate();
45         Hal_redLedOn();
46         Hal_delay(10);
47         Hal_redLedOff();
48         break;
49     default:
50         for (i = 0; i < 5; i++) {
51             Hal_greenLedOn();
52             Hal_redLedOn();
53             Hal_delay(10);
54             Hal_greenLedOff();
55             Hal_redLedOff();
56             Hal_delay(10);
57         }
58     }
59 }
60
61 static void tickHandler(void) {
62     uint8_t i;
63
64     Hal_tickStop();
65     if (connected)
66         return;
67     for (i = 0; i < 3; i++) {
68         Hal_greenLedOn();
69         Hal_delay(50);
70         Hal_redLedOn();
71         Hal_delay(50);
72         Hal_redLedOff();
73         Hal_delay(50);
74         Hal_greenLedOff();
75     }
76     Pulsecounter_accept(false);
77 }
78
79 /* -------- SCHEMA CALLBACKS -------- */
80
81 void Pulsecounter_connectHandler(void) {
82     connected = true;
83     Hal_connected();
84     Hal_redLedOn();
85     Hal_delay(100);
86     Hal_redLedOff();
87     Hal_greenLedOn();
88     Hal_delay(100);
89     Hal_greenLedOff();
90 }
91
92 void Pulsecounter_disconnectHandler(void) {
93     connected = false;
94     Hal_greenLedOn();
95     Hal_delay(100);
96     Hal_greenLedOff();
97     Hal_redLedOn();
98     Hal_delay(100);
99     Hal_redLedOff();
100     Hal_tickStart(15000, tickHandler);
101     Hal_disconnected();
102 }
103
104 void Pulsecounter_event4_fetch(Pulsecounter_event4_t* const output) {
105     *output = base4 + event4;
106 }
107
108 void Pulsecounter_event5_fetch(Pulsecounter_event5_t* const output) {
109     *output = base5 + event5;
110 }
111
112 void Pulsecounter_base4_fetch(Pulsecounter_base4_t* const output) {
113     *output = base4;
114 }
115
116 void Pulsecounter_base4_store(Pulsecounter_base4_t* const input) {
117     base4 = *input - event4;
118 }
119
120 void Pulsecounter_base5_fetch(Pulsecounter_base5_t* const output) {
121     *output = base5;
122 }
123
124 void Pulsecounter_base5_store(Pulsecounter_base5_t* const input) {
125     base5 = *input - event5;
126 }