f5071784b56c41126d9d58559242d7bf0ea0b090
[pulsecounter.git] / Pulsecounter-Prog.c
1 #include "Pulsecounter.h"
2 #include "Hal.h"
3
4 static void buttonHandler(void);
5 static void tickHandler(void);
6 static bool connected = false;
7 static int32_t buttonCnt = 0;
8
9 void main() {
10     Hal_init();
11     Hal_buttonEnable(buttonHandler);
12     Pulsecounter_setDeviceName("PULS-CNTR");
13     Pulsecounter_start();
14     Hal_idleLoop();
15 }
16
17 static void buttonHandler(void) {
18     uint8_t i;
19
20     buttonCnt++;
21     if (connected)
22         Pulsecounter_event3_indicate();
23     else
24         Pulsecounter_accept(true);
25     for (i = 0; i < 3; i++) {
26         Hal_greenLedOn();
27         Hal_redLedOn();
28         Hal_delay(10);
29         Hal_greenLedOff();
30         Hal_redLedOff();
31     }
32     Hal_tickStart(5000, tickHandler);
33 }
34
35 static void tickHandler(void) {
36     uint8_t i;
37
38     Hal_tickStop();
39     if (connected)
40         return;
41     for (i = 0; i < 3; i++) {
42         Hal_greenLedOn();
43         Hal_delay(50);
44         Hal_redLedOn();
45         Hal_delay(50);
46         Hal_redLedOff();
47         Hal_delay(50);
48         Hal_greenLedOff();
49     }
50     Pulsecounter_accept(false);
51 }
52
53 /* -------- SCHEMA CALLBACKS -------- */
54
55 void Pulsecounter_connectHandler(void) {
56     connected = true;
57     Hal_connected();
58     Hal_redLedOn();
59     Hal_delay(100);
60     Hal_redLedOff();
61     Hal_greenLedOn();
62     Hal_delay(100);
63     Hal_greenLedOff();
64 }
65
66 void Pulsecounter_disconnectHandler(void) {
67     connected = false;
68     Hal_greenLedOn();
69     Hal_delay(100);
70     Hal_greenLedOff();
71     Hal_redLedOn();
72     Hal_delay(100);
73     Hal_redLedOff();
74     Hal_tickStart(5000, tickHandler);
75     Hal_disconnected();
76 }
77
78 void Pulsecounter_event3_fetch(Pulsecounter_event3_t* const output) {
79     *output = buttonCnt;
80 }
81
82 void Pulsecounter_event4_fetch(Pulsecounter_event4_t* const output) {
83     *output = 4;
84 }
85
86 void Pulsecounter_event5_fetch(Pulsecounter_event5_t* const output) {
87     *output = 5;
88 }