-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.cpp
More file actions
171 lines (156 loc) · 3.96 KB
/
Copy pathClient.cpp
File metadata and controls
171 lines (156 loc) · 3.96 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <errno.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "Constants.h"
#include "SerialParsing.h"
const int8_t DEVICE_ID = 3;
bool Pressed = false;
int8_t count = 0;
bool this_buzzer_locked_in = false;
bool msg_start,msg_end;
uint8_t buffer_index;
int8_t received_device_id;
MessageType received_msg;
uint64_t buzz_in_time;
// Buffer for incoming message;
char buffer[bufferSize];
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
void UpdateClientDisplayDebug(){
display.setTextSize(1);
display.clearDisplay();
display.setCursor(0,0);
display.printf("Device ID:%d\n",DEVICE_ID);
display.printf("Pressed:%llu\n",buzz_in_time);
display.printf("Locked In:%d\n",this_buzzer_locked_in);
display.printf("%d\n",count++);
display.display();
}
void UpdateClientDisplay(){
display.setTextSize(8);
display.clearDisplay();
display.setCursor(0,0);
display.printf("%d\n",DEVICE_ID);
if(this_buzzer_locked_in){
display.invertDisplay(1);
display.display();
}
else{
display.invertDisplay(0);
display.display();
}
}
bool ClientAction(MessageType rec_msg, int8_t rec_device_id){
if(debug){
Serial.printf("Action:%d,%d\n",rec_msg,rec_device_id);
}
// We respond if the message if for this client
if(rec_device_id==DEVICE_ID){
switch(rec_msg){
case ALIVE:
SendMsgClient(DEVICE_ID,ALIVE,micros());
break;
case RESET:
buzz_in_time = 0;
Pressed = false;
this_buzzer_locked_in = false;
SendMsgClient(DEVICE_ID,RESET,0);
break;
case TIMING:
SendMsgClient(DEVICE_ID,TIMING,buzz_in_time);
break;
case LOCK_IN:
SendMsgClient(DEVICE_ID,LOCK_IN,0);
this_buzzer_locked_in = true;
break;
case INVALID:
default:
SendMsgClient(DEVICE_ID,INVALID,0);
break;
}
}
// Otherwise, we don't respond, and only set variables local to client
else if(rec_device_id==ALL_DEVICES){
switch(rec_msg){
case ALIVE:
break;
case RESET:
buzz_in_time = 0;
Pressed = false;
this_buzzer_locked_in = false;
break;
case TIMING:
break;
case LOCK_IN:
this_buzzer_locked_in = true;
break;
case INVALID:
default:
break;
}
}
else{
return false;
}
return true;
}
void IRAM_ATTR TooglePressed(){
if(!Pressed){
Pressed = true;
buzz_in_time = micros();
// Serial.printf("%dn",Pressed);
// Serial.printf("%llu\n",buzz_in_time);
}
}
void ScanForCommands(){
bool valid = false;
while(1){
if(debug || v_debug){
UpdateClientDisplayDebug();
}
else{
UpdateClientDisplay();
}
ReceiveChar(buffer,buffer_index,msg_start,msg_end);
if(msg_end && msg_start){
valid = ParseMsgClient(buffer,received_device_id,received_msg);
// WE got a valid message from the server. Perform an action
if(valid){
ClientAction(received_msg,received_device_id);
}
// We got an invalid message. send an INVALID response
received_msg = INVALID;
received_device_id = INVALID_DEVICE;
msg_start = false;
msg_end = false;
buffer_index = 0;
valid = true;
break;
}
}
}
void setup() {
buzz_in_time = 0;
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.setTextSize(1);
display.setTextColor(WHITE);
display.clearDisplay();
display.setCursor(0,0);
display.printf("Device ID:%d\n",DEVICE_ID);
display.printf("Pressed:%llu\n",buzz_in_time);
display.display();
Serial.begin(115200);
pinMode(ENABLE_PIN,OUTPUT);
pinMode(TRIGGER_PIN,INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(TRIGGER_PIN), TooglePressed, FALLING);
digitalWrite(ENABLE_PIN,LOW);
received_msg = INVALID;
received_device_id = INVALID_DEVICE;
msg_start = false;
msg_end = false;
buffer_index = 0;
}
void loop() {
ScanForCommands();
}