-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathir_opponent_sensors.cpp
62 lines (51 loc) · 1.35 KB
/
ir_opponent_sensors.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
#include <Arduino.h>
#include <cstdint>
#include "ir_opponent_sensors.h"
#define IR_LED_FREQ_HZ (38000)
void initIrOpponentSensorPins(void) {
pinMode(PIN_IR_LED, OUTPUT);
for (uint8_t i = 0 ; i < irSensorCount; i++) {
pinMode(irSensorPin[i], INPUT_PULLUP);
}
}
IrSensor_t readIrSensors(void) {
enableIrSensorTx();
IrSensor_t sensorData = 0;
for (uint8_t i = 0; i < irSensorCount; i++) {
if (digitalRead(irSensorPin[i]) == LOW) {
sensorData |= (1 << i);
}
}
disableIrSensorTx();
return sensorData;
}
void enableIrSensorTx(void) {
tone(PIN_IR_LED, IR_LED_FREQ_HZ);
}
void disableIrSensorTx(void) {
noTone(PIN_IR_LED);
}
#define PERIOD_IR_UPDATE 100
bool irSensorsNeedUpdate(){
/*
Curr Prev Next
| | |
. . . . . . . . . . . . . . .
cpn ncp pnc : execute
cnp pcn npc : not yet
check by using all three variables in unsigned form and subtracting p from n and c and seeing if c >= n, then execute
*/
// Assure unconditional execution the first time.
static uint32_t previousTime = 0;
static uint32_t nextTime = 0;
bool ret = false;
uint32_t currentTime = millis();
uint32_t c = currentTime - previousTime;
uint32_t n = nextTime - previousTime;
if(c > 0 && c >= n){
previousTime = currentTime;
nextTime = currentTime + PERIOD_IR_UPDATE;
return true;
}
return false;
}