-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensor.cpp
82 lines (66 loc) · 2.03 KB
/
sensor.cpp
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "sensor.h"
#include "log.h"
#include <Arduino.h>
static unsigned long long sensors = 0;
static unsigned long long sensors_last = 0;
static unsigned long long sensor_latches = 0;
static unsigned long debounce[64] = {0};
void sensor_setup() {
for (byte pin = min(SENSOR_ROW_OUT_START, SENSOR_COL_IN_START); pin <= 45; pin++) {
pinMode(pin, pin % 2 == SENSOR_ROW_OUT_START % 2 ? OUTPUT : INPUT);
}
logg(TRACE, "Sensor setup complete");
}
// Timing suggests this never takes more than 2ms (pre debounce)
void sensor_refresh() {
sensors = 0;
const unsigned long current_millis = millis(); // Saves computation time
for (byte row = 0; row < 8; row++) {
for (byte i = 0; i < 8; i++) {
digitalWrite(SENSOR_ROW_OUT_START + i * 2, row == i ? HIGH : LOW);
}
for (byte col = 0; col < 7; col++) {
const boolean status = digitalRead(SENSOR_COL_IN_START + col * 2);
const byte index = row * 7 + col;
if (status != (boolean)(sensors_last & (1ULL << index))) {
debounce[index] = millis();
sensors_last ^= (1ULL << index);
} else if (current_millis - debounce[index] > DEBOUNCE_TIME && status) {
sensors |= 1ULL << index;
}
}
}
logg(TRACE, "Sensor refresh complete");
}
boolean sensor_read(byte sensor) {
return sensors & (1ULL << sensor);
}
static void sensor_update_latch(byte sensor, boolean inverse) {
if (sensor_read(sensor) ^ inverse) {
sensor_latches |= 1ULL << sensor;
} else {
sensor_latches &= ~(1ULL << sensor);
}
}
boolean sensor_read_latch(byte sensor) {
const boolean ret = sensor_read(sensor)
&& !(sensor_latches & (1ULL << sensor));
sensor_update_latch(sensor, false);
return ret;
}
boolean sensor_read_latch_inverse(byte sensor) {
const boolean ret = !sensor_read(sensor)
&& !(sensor_latches & (1ULL << sensor));
sensor_update_latch(sensor, true);
return ret;
}
byte trough_count() {
byte troughs = 0;
while (sensor_read(27 - troughs)) {
troughs++;
}
if (!SENSOR_trough_ready) {
return 0;
}
return troughs + 1;
}