-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.cpp
384 lines (329 loc) · 11.2 KB
/
main.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#include <Arduino.h>
#include <HardwareSerial.h>
#include <WiFi.h>
#include "vectors.h"
//Plotter Interface
#define CLEAR_TO_SEND_IN_PIN 21
#define TX_OUT_PIN 22
#define RX_IN_PIN 23
//LEDs
#define LED1_PIN 13
#define LED2_PIN 12
#define LED3_PIN 14
#define LED4_PIN 27
#define LED5_PIN 26
#define NR_OF_LEDS 5
//Buttons
#define BUT1_PIN 25
#define BUT2_PIN 33
#define BUT3_PIN 32
#define BUT4_PIN 35
#define BUT5_PIN 34
//Delay
#define SWIPE_DELAY 50
//Dart Game
#define DART_COUNTDOWN_DELAY 1000
//WiFi
#define WIFI_MODE WIFI_STA // WIFI_STA or WIFI_AP
#define WIFI_SSID "xHain"
#include "wifi_password.h" //make sure to create wifi_password.h, see wifi_password.h.example file
#define WIFI_INPUT_BUFFER_SIZE 255
#define WIFI_CHANNEL 5
#define WIFI_MAX_CONNECTIONS 1
#define HOSTNAME "harryplotter"
#define LISTEN_PORT 1337
IPAddress local_IP(192,168,4,1);
IPAddress gateway(192,168,4,13);
IPAddress subnet(255,255,255,0);
WiFiServer wifiServer(LISTEN_PORT);
WiFiClient client;
char wifi_input_buffer[WIFI_INPUT_BUFFER_SIZE];
HardwareSerial PlotterSerial(1);
void wifi_ap() {
Serial.println("Starting Wifi...");
Serial.print("Setting soft-AP configuration ... ");
Serial.println(WiFi.softAPConfig(local_IP, gateway, subnet) ? "Ready" : "Failed!");
Serial.print("Setting soft-AP ... ");
Serial.println(WiFi.softAP(WIFI_SSID, WIFI_PASSWORD, WIFI_CHANNEL, false, WIFI_MAX_CONNECTIONS) ? "Ready" : "Failed!");
Serial.print("Soft-AP IP address = ");
Serial.println(WiFi.softAPIP());
}
void wifi_connect() {
Serial.print("Connecting to ");
Serial.print(WIFI_SSID);
WiFi.setHostname(HOSTNAME);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("Hostname: ");
Serial.println(HOSTNAME);
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
}
void setup() {
//Configure Pins
pinMode(LED_BUILTIN, OUTPUT);
pinMode(CLEAR_TO_SEND_IN_PIN, INPUT);
pinMode(LED1_PIN, OUTPUT);
pinMode(LED2_PIN, OUTPUT);
pinMode(LED3_PIN, OUTPUT);
pinMode(LED4_PIN, OUTPUT);
pinMode(LED5_PIN, OUTPUT);
pinMode(BUT1_PIN, INPUT_PULLUP);
pinMode(BUT2_PIN, INPUT_PULLUP);
pinMode(BUT3_PIN, INPUT_PULLUP);
pinMode(BUT4_PIN, INPUT_PULLUP);
pinMode(BUT5_PIN, INPUT_PULLUP);
Serial.begin(9600);
while (!Serial) {
; // wait for Serial port to connect.
}
Serial.println("Starting up...");
PlotterSerial.begin(9600, SERIAL_8N1, RX_IN_PIN, TX_OUT_PIN);//Baud,Mode, RX Pin (In), TX Pin (Out)
if (WIFI_MODE == WIFI_AP) {
wifi_ap();
} else {
wifi_connect();
}
Serial.print("Starting TCP server on port ");
Serial.println(LISTEN_PORT);
wifiServer.begin();
}
uint8_t button_pressed(){
if(!digitalRead(BUT1_PIN)) return 1;
else if(!digitalRead(BUT2_PIN)) return 2;
else if(!digitalRead(BUT3_PIN)) return 3;
else if(!digitalRead(BUT4_PIN)) return 4;
else if(!digitalRead(BUT5_PIN)) return 5;
else return 0;
}
void leds_off(){
digitalWrite(LED1_PIN,LOW);
digitalWrite(LED2_PIN,LOW);
digitalWrite(LED3_PIN,LOW);
digitalWrite(LED4_PIN,LOW);
digitalWrite(LED5_PIN,LOW);
}
void leds_on(){
digitalWrite(LED1_PIN,HIGH);
digitalWrite(LED2_PIN,HIGH);
digitalWrite(LED3_PIN,HIGH);
digitalWrite(LED4_PIN,HIGH);
digitalWrite(LED5_PIN,HIGH);
}
void leds_flash_on_off(){
leds_on();
delay(500);
leds_off();
delay(500);
}
void leds_swipe_until_user_input(){
int leds[] = {LED1_PIN,LED2_PIN,LED3_PIN,LED4_PIN,LED5_PIN};
int current_led=0;
int current_swipe = 0;
//do the effect
while(!button_pressed() && !(client=wifiServer.available())){//return if pressed, or client connects, save client object
//set the current led according to swipe
if (current_swipe == 0) digitalWrite(leds[current_led],HIGH);
else if (current_swipe == 1) digitalWrite(leds[current_led],LOW);
current_led++;
//update status variables
if (current_led == NR_OF_LEDS){
current_led = 0;
current_swipe++;
if (current_swipe==2) current_swipe = 0;
}
delay(SWIPE_DELAY);
}
//Switch everything off before returning
digitalWrite(LED1_PIN,LOW);
digitalWrite(LED2_PIN,LOW);
digitalWrite(LED3_PIN,LOW);
digitalWrite(LED4_PIN,LOW);
digitalWrite(LED5_PIN,LOW);
}
void test_buttons_and_led(){
if(!digitalRead(BUT1_PIN)) digitalWrite(LED1_PIN,HIGH);
else digitalWrite(LED1_PIN,LOW);
if(!digitalRead(BUT2_PIN)) digitalWrite(LED2_PIN,HIGH);
else digitalWrite(LED2_PIN,LOW);
if(!digitalRead(BUT3_PIN)) digitalWrite(LED3_PIN,HIGH);
else digitalWrite(LED3_PIN,LOW);
if(!digitalRead(BUT4_PIN)) digitalWrite(LED4_PIN,HIGH);
else digitalWrite(LED4_PIN,LOW);
if(!digitalRead(BUT5_PIN)) digitalWrite(LED5_PIN,HIGH);
else digitalWrite(LED5_PIN,LOW);
}
void send_buffered(const char input_chars[]){
String input = input_chars;
const uint max_blocksize = 70;
uint inlen = input.length();
// buffer for storing the string fragment
String fragment;
// number of chars that will be read into buffer
//uint readlen;
ulong full_blocks = inlen/max_blocksize;
//handle full blocks
for (int current_block=0; current_block < full_blocks; current_block++){
fragment = input.substring(current_block*max_blocksize, current_block*max_blocksize+max_blocksize);
PlotterSerial.flush();//wait for all bytes to be sent from previous block in esp's internal buffer
while(digitalRead(CLEAR_TO_SEND_IN_PIN)) {delay(50);}//check if plotter is ready to receive new block
PlotterSerial.write(fragment.c_str());//send block
}
//handle last partial block
if(full_blocks*max_blocksize < inlen){
fragment = input.substring(full_blocks*max_blocksize, inlen);//copy last partial block
PlotterSerial.flush();//wait for all bytes to be sent from previous block in esp's internal buffer
while(digitalRead(CLEAR_TO_SEND_IN_PIN)) {delay(50);}//check if plotter is ready to receive new block
PlotterSerial.write(fragment.c_str());//send block
}
}
void print_id(){
Serial.println("Getting ID");
PlotterSerial.write("OI;");
delay(100);
Serial.println("Response:");
Serial.println(PlotterSerial.readStringUntil('\r'));
}
void autofeed_paper(){
PlotterSerial.write("PG;");
}
void dart_game(){
uint target_middle_x =5350;
uint target_middle_y = 3750;
char stringBuffer[100];
//init/select pen
send_buffered("IN;SP2;");
//go to target position
sprintf(stringBuffer,"PA%d,%d;",target_middle_x,target_middle_y);
send_buffered(stringBuffer);
//draw target
send_buffered("CI400;CI800;CI1600;CI2400;");
//draw info text
send_buffered("PA500,200;LBTry to hit the target: Press button now to launch arrow. Press+hold again to hit the target.;");
delay(20000);//adjust this at least to the time it takes to plot the previous stuff
//go to "text view" position
//go to score position
sprintf(stringBuffer,"PA%d,%d;",100,7000);
send_buffered(stringBuffer);
//wait for button press and release
while(!button_pressed()){delay(50);}
while(button_pressed()){delay(50);}
// //draw countdown
// send_buffered("LB3 ;");
// delay(DART_COUNTDOWN_DELAY);
// send_buffered("LB2 ;");
// delay(DART_COUNTDOWN_DELAY);
// send_buffered("LB1 ;");
// delay(DART_COUNTDOWN_DELAY);
// send_buffered("LBGo! ;");
// delay(DART_COUNTDOWN_DELAY);
//move pen and wait for any button
uint score = 0;
uint current_position_x=0;
uint current_position_y=0;
for (int i=0; i<5; i++){
while(!button_pressed()){
current_position_x = target_middle_x + random(-2400,2400);
current_position_y = target_middle_y + random(-2400,2400);
sprintf(stringBuffer,"PA%d,%d;",current_position_x,current_position_y);
send_buffered(stringBuffer);
delay(250);
}
//plot arrow
send_buffered("LB<-------<<;");
delay(1000);
//determine score
score += 3000- sqrt(pow(target_middle_x - current_position_x*1.0,2)+ pow(target_middle_y - current_position_y*1.0,2)*1.0);
// wait until button is released
while(button_pressed()){delay(50);}
}
//go to score position
sprintf(stringBuffer,"PA%d,%d;",1000,1000);
send_buffered(stringBuffer);
//plot score text
sprintf(stringBuffer,"LBYour score is: %d Thanks for playing. :)\n\nx-hain.de, Gruenberger Str. 16, 10243 Berlin;",score);
send_buffered(stringBuffer);
//eject paper
autofeed_paper();
while(button_pressed());//wait for button to be released, so we dont immediately go to game again
}
void loop() {
int received_data_count = 0;
for (int i = 0; i < 3; i++) leds_flash_on_off();
while(1){
//lure people close with flashing lights
leds_swipe_until_user_input();
//Check for client connection
if (client) {
Serial.println("Client connected");
leds_on();//indicate active connection
while (client.connected()) {//while someone is connected
delay(10); //wait for some data to arrive
received_data_count = 0;
while (client.available()>0 && received_data_count<(WIFI_INPUT_BUFFER_SIZE-1)) {//while data is available and input buffer not full
//put available char into input buffer
wifi_input_buffer[received_data_count] = client.read();
received_data_count++;
}
if (received_data_count > 0){//if something was received
//add extra null terminator for safety
wifi_input_buffer[received_data_count] = 0;
//send data to plotter
Serial.println("Received data over WiFi:");
Serial.println(wifi_input_buffer);
//Send data to plotter
send_buffered(wifi_input_buffer);
//send acknowledgement to that data was sent to plotter
if (client.connected()) client.write("OK");
}
} //end while connection
client.stop();
client = 0; //reset global client variable
Serial.println("Client disconnected");
leds_off();//indicate disconnect
}//end if client
//check what is pressed
delay(50); //debounce wait for the button
if(!digitalRead(BUT1_PIN)) {
digitalWrite(LED1_PIN,HIGH);
send_buffered(vader);
send_buffered(logo_for_vader);
autofeed_paper();
}else if(!digitalRead(BUT2_PIN)) {
digitalWrite(LED2_PIN,HIGH);
//send_buffered(xHain_flyer_cccamp);
//send_buffered(xHain_flyer_cccamp_party);
send_buffered(flyer_36c3);
autofeed_paper();
}else if(!digitalRead(BUT3_PIN)){
digitalWrite(LED3_PIN,HIGH);
dart_game();
//send_buffered(dodekaeder1);
//send_buffered(dodekaeder2);
//send_buffered(dodekaeder3);
//autofeed_paper();
}
else if(!digitalRead(BUT4_PIN)) {
digitalWrite(LED4_PIN,HIGH);
send_buffered(rick);
//send_buffered(dode_info_en1);
//send_buffered(dode_info_en2);
//send_buffered(dode_info_en3);
//send_buffered(dode_info_en_red);
autofeed_paper();
}
else if(!digitalRead(BUT5_PIN)) {
digitalWrite(LED5_PIN,HIGH);
//send_buffered(dode_info_de1);
//send_buffered(dode_info_de2);
//send_buffered(dode_info_de3);
//send_buffered(dode_info_de_red);
//autofeed_paper();
}
}
}