forked from tlp19/ES-synth-returnSuccess4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetect.hpp
60 lines (50 loc) · 1.66 KB
/
detect.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <Arduino.h>
#include <U8g2lib.h>
#include <STM32FreeRTOS.h>
#include <math.h>
using namespace std;
extern const int REN_PIN;
extern const int OUT_PIN;
// Reading from the keyboard
extern volatile uint8_t keyArray[7];
extern SemaphoreHandle_t keyArrayMutex;
extern void setRow(uint8_t rowIdx);
/// Takes in as argument:
/// - int row: The row index of the Detect port in the key matrix
/// - int firstColumn: The column index of the Detect port in the key matrix
class Detect {
private:
int row;
int column;
bool state;
public:
Detect(int _row, int _column) {
row = _row;
column = _column;
}
/// Write a value to the output Detect port
void writeToPin(bool value) volatile {
setRow(row);
digitalWrite(REN_PIN,LOW);
digitalWrite(OUT_PIN,value);
digitalWrite(REN_PIN,HIGH);
delayMicroseconds(2);
digitalWrite(REN_PIN,LOW);
}
/// Analyse the output of the keymatrix read and update the state
void readFromPin() volatile {
// Get the row of the keyArray where the data about the Detect port is
uint8_t keyArrayR;
xSemaphoreTake(keyArrayMutex, portMAX_DELAY);
memcpy(&keyArrayR, (void*) &keyArray[row], sizeof(keyArray[row]));
xSemaphoreGive(keyArrayMutex);
// Take the binary number at the right column index
int currentPressedState = !((keyArrayR >> column) & 0x01);
// Set the new state value using atomic store
__atomic_store_n(&state, currentPressedState, __ATOMIC_RELAXED);
}
/// Returns if the input Detect port is HIGH or not
bool getState() volatile {
return state;
}
};