-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
64 lines (48 loc) · 1.44 KB
/
main.c
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
#include <stdlib.h>
#include "hal/pin.h"
#include "hal/serial.h"
#include "utils/bit.h"
/* NOTE: BYTE DEFINITION - BIT-TO-PIN MAPPING
< output > < input >
bits: [b7][b6][b5][b4] [b3][b2][b1][b0]
pins: [D5][D4][D3][D2] [A3][A2][A1][A0]
*/
uint8_t g_PinStates;
void setup(void)
{
setPinMode(D2, OUTPUT);
setPinMode(D3, OUTPUT);
setPinMode(D4, OUTPUT);
setPinMode(D5, OUTPUT);
setPinMode(A0, INPUT_PULLUP);
setPinMode(A1, INPUT_PULLUP);
setPinMode(A2, INPUT_PULLUP);
setPinMode(A3, INPUT_PULLUP);
serialSetup(9600);
g_PinStates = 0b00000000;
}
void loop(void)
{
// wait to receive new output states from client
g_PinStates = serialReceiveWithBusyWait();
// write new output states
digitalWrite(D2, BIT_IS_SET(g_PinStates, 4));
digitalWrite(D3, BIT_IS_SET(g_PinStates, 5));
digitalWrite(D4, BIT_IS_SET(g_PinStates, 6));
digitalWrite(D5, BIT_IS_SET(g_PinStates, 7));
// read new input states (negated bc of pull-up)
BIT_OVERRIDE_WITH_VAL(g_PinStates, 0, !digitalRead(A0));
BIT_OVERRIDE_WITH_VAL(g_PinStates, 1, !digitalRead(A1));
BIT_OVERRIDE_WITH_VAL(g_PinStates, 2, !digitalRead(A2));
BIT_OVERRIDE_WITH_VAL(g_PinStates, 3, !digitalRead(A3));
// send updated input states back to client
serialSendWithBusyWait(g_PinStates);
}
int main (void)
{
setup();
while(true) {
loop();
}
return EXIT_SUCCESS;
}