software:firmware
MakAir Firmware
keyboard.cpp
Go to the documentation of this file.
1 
10 #pragma once
11 
12 // INCLUDES ===================================================================
13 
14 // Associated header
15 #include "../includes/keyboard.h"
16 
17 // External
18 #include <OneButton.h>
19 
20 // Internal
21 #include "../includes/activation.h"
22 #include "../includes/buzzer.h"
23 #include "../includes/calibration.h"
24 #include "../includes/config.h"
25 #include "../includes/debug.h"
26 #include "../includes/main_controller.h"
27 #include "../includes/parameters.h"
28 
29 // INITIALISATION =============================================================
30 
31 static OneButton buttonAlarmOff(PIN_BTN_ALARM_OFF, false, false);
32 static OneButton buttonStart(PIN_BTN_START, false, false);
33 static OneButton buttonStop(PIN_BTN_STOP, false, false);
34 
35 // FUNCTIONS ==================================================================
36 
39 
42 
45 
48 
51 
54 
57 
60 
63 
65 void onStart() {
66  if (Calibration_Started()) {
67  // Restart calibration
69  } else {
70  // Or start the machine
72  }
73 }
74 
77 
78 void initKeyboard() {
79  // define the 3x3 matrix keyboard input and output
80  pinMode(PIN_OUT_COL1, OUTPUT);
81  pinMode(PIN_OUT_COL2, OUTPUT);
82  pinMode(PIN_OUT_COL3, OUTPUT);
83  digitalWrite(PIN_OUT_COL1, LOW);
84  digitalWrite(PIN_OUT_COL2, LOW);
85  digitalWrite(PIN_OUT_COL3, LOW);
86  pinMode(PIN_IN_ROW1, INPUT);
87  pinMode(PIN_IN_ROW2, INPUT);
88  pinMode(PIN_IN_ROW3, INPUT);
89 
90  buttonAlarmOff.attachClick(onAlarmOff);
91  buttonStart.attachClick(onStart);
92  buttonStop.attachClick(onStop);
93 }
94 
95 // current powered column of the matrix keyboard
97 uint16_t scanMatrixCounterC1R1 = 0;
98 uint16_t scanMatrixCounterC1R2 = 0;
99 uint16_t scanMatrixCounterC1R3 = 0;
106 #define SM_DEBOUNCE 2u // number of debounce ticks before trigger an action
107 #define SM_REPEAT 20u // number of ticks before starting continuous press action repeat
108 #define SM_PERIOD 15u // period of action repeat in case of a continuous press
109 
110 // fast solution: no hardware timer, no interrupt priority or atomicity issue. Close to Arduino
111 // philosophy.
113  if (1u == scanMatrixCurrentColumn) {
114  // Increase counter for each column and row
115  if (HIGH == digitalRead(PIN_IN_ROW1)) {
117  } else {
119  }
120  if (HIGH == digitalRead(PIN_IN_ROW2)) {
122  } else {
124  }
125  if (HIGH == digitalRead(PIN_IN_ROW3)) {
127  } else {
129  }
130  // first click (after debounce ticks) or
131  // later clicks if continuous press trigger action
134  && (0u == (scanMatrixCounterC1R1 - SM_REPEAT) % SM_PERIOD))) {
136  }
139  && (0u == (scanMatrixCounterC1R2 - SM_REPEAT) % SM_PERIOD))) {
141  }
144  && (0u == (scanMatrixCounterC1R3 - SM_REPEAT) % SM_PERIOD))) {
146  }
147  } else if (2u == scanMatrixCurrentColumn) {
148  if (HIGH == digitalRead(PIN_IN_ROW1)) {
150  } else {
152  }
153  if (HIGH == digitalRead(PIN_IN_ROW2)) {
155  } else {
157  }
158  if (HIGH == digitalRead(PIN_IN_ROW3)) {
160  } else {
162  }
163  // first click (after debounce ticks) or
164  // later clicks if continuous press trigger action
167  && (0u == (scanMatrixCounterC2R1 - SM_REPEAT) % SM_PERIOD))) {
169  }
172  && (0u == (scanMatrixCounterC2R2 - SM_REPEAT) % SM_PERIOD))) {
174  }
177  && (0u == (scanMatrixCounterC2R3 - SM_REPEAT) % SM_PERIOD))) {
179  }
180  } else if (3u == scanMatrixCurrentColumn) {
181  if (HIGH == digitalRead(PIN_IN_ROW1)) {
183  } else {
185  }
186  if (HIGH == digitalRead(PIN_IN_ROW2)) {
188  } else {
190  }
191  if (HIGH == digitalRead(PIN_IN_ROW3)) {
193  } else {
195  }
196  // first click (after debounce ticks) or
197  // later clicks if continuous press trigger action
200  && (0u == (scanMatrixCounterC3R1 - SM_REPEAT) % SM_PERIOD))) {
201  onCycleIncrease();
202  }
205  && (0u == (scanMatrixCounterC3R2 - SM_REPEAT) % SM_PERIOD))) {
206  onCycleDecrease();
207  }
208  // there is no button on col3 x row3
209  } else {
210  // Do nothing
211  }
212 
213  // next column
215  if (4u == scanMatrixCurrentColumn) {
217  }
218  digitalWrite(PIN_OUT_COL1, (1u == scanMatrixCurrentColumn) ? HIGH : LOW);
219  digitalWrite(PIN_OUT_COL2, (2u == scanMatrixCurrentColumn) ? HIGH : LOW);
220  digitalWrite(PIN_OUT_COL3, (3u == scanMatrixCurrentColumn) ? HIGH : LOW);
221 }
222 
223 void keyboardLoop() {
224 #ifndef DISABLE_BUTTONS
225  scanMatrixLoop();
226  buttonAlarmOff.tick();
227  buttonStart.tick();
228  buttonStop.tick();
229 #endif
230 }
231 
232 // cppcheck-suppress unusedFunction
ActivationController activationController
Instance of the activation controller.
Definition: activation.cpp:21
AlarmController alarmController
Instance of the alarm controller.
bool Calibration_Started()
Check if calibration mode is started.
void Calibration_Restart()
Restart calibration process.
void onStopButton()
Callback to call each time the stop button is pushed.
Definition: activation.cpp:37
void onStartButton()
Callback to call each time the start button is pushed.
Definition: activation.cpp:27
void snooze()
Snooze alarm for 2 minutes.
void onPeakPressureIncrease()
Increase the desired peak pressure.
void onPeakPressureDecrease()
Decrease the desired peak pressure.
void onCycleIncrease()
Increase the desired number of cycles per minute.
void onPlateauPressureIncrease()
Increase the desired plateau pressure.
void onCycleDecrease()
Decrease the desired number of cycles per minute.
void onPeepPressureIncrease()
Increase the minimal PEEP desired.
void onPeepPressureDecrease()
Decrease the minimal PEEP desired.
void onPlateauPressureDecrease()
Decrease the desired plateau pressure.
void onStart()
Handler of the button to start.
Definition: keyboard.cpp:65
uint16_t scanMatrixCounterC2R2
Definition: keyboard.cpp:101
uint16_t scanMatrixCurrentColumn
Definition: keyboard.cpp:96
void onCycleIncrease()
Handler of the button to increase the number of breathing cycles.
Definition: keyboard.cpp:56
static OneButton buttonAlarmOff(PIN_BTN_ALARM_OFF, false, false)
void onAlarmOff()
Handler of the button to stop alarm.
Definition: keyboard.cpp:62
uint16_t scanMatrixCounterC1R3
Definition: keyboard.cpp:99
void onStop()
Handler of the button to stop.
Definition: keyboard.cpp:76
void onPlateauPressureDecrease()
Handler of the button to decrease the plateau pressure.
Definition: keyboard.cpp:47
#define SM_PERIOD
Definition: keyboard.cpp:108
#define SM_REPEAT
Definition: keyboard.cpp:107
void onPeepPressureIncrease()
Handler of the button to increase the PEP pressure.
Definition: keyboard.cpp:50
uint16_t scanMatrixCounterC3R2
Definition: keyboard.cpp:104
uint16_t scanMatrixCounterC3R3
Definition: keyboard.cpp:105
void calibrateButtons()
Helper to calibrate analog values of buttons.
Definition: keyboard.cpp:233
void scanMatrixLoop()
Definition: keyboard.cpp:112
static OneButton buttonStart(PIN_BTN_START, false, false)
static OneButton buttonStop(PIN_BTN_STOP, false, false)
void initKeyboard()
Initialize keyboard abstraction.
Definition: keyboard.cpp:78
uint16_t scanMatrixCounterC2R3
Definition: keyboard.cpp:102
uint16_t scanMatrixCounterC1R2
Definition: keyboard.cpp:98
void onCycleDecrease()
Handler of the button to decrease the number of breathing cycles.
Definition: keyboard.cpp:59
uint16_t scanMatrixCounterC2R1
Definition: keyboard.cpp:100
void onPeepPressureDecrease()
Handler of the button to decrease the PEP pressure.
Definition: keyboard.cpp:53
void onPlateauPressureIncrease()
Handler of the button to increase the plateau pressure.
Definition: keyboard.cpp:44
void onPeakPressureIncrease()
Handler of the button to increase the crete pressure.
Definition: keyboard.cpp:38
void onPeakPressureDecrease()
Handler of the button to decrease the crete pressure.
Definition: keyboard.cpp:41
uint16_t scanMatrixCounterC3R1
Definition: keyboard.cpp:103
#define SM_DEBOUNCE
Definition: keyboard.cpp:106
void keyboardLoop()
Handle button events.
Definition: keyboard.cpp:223
uint16_t scanMatrixCounterC1R1
Definition: keyboard.cpp:97
MainController mainController
#define PIN_BTN_START
Definition: parameters.h:232
#define PIN_OUT_COL2
Definition: parameters.h:236
#define PIN_IN_ROW3
Definition: parameters.h:240
#define PIN_IN_ROW1
Definition: parameters.h:238
#define PIN_OUT_COL1
Definition: parameters.h:235
#define PIN_BTN_STOP
Definition: parameters.h:233
#define PIN_BTN_ALARM_OFF
Definition: parameters.h:231
#define PIN_IN_ROW2
Definition: parameters.h:239
#define PIN_OUT_COL3
Definition: parameters.h:237