forked from dekuNukem/Poke-O-Matic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pom_pcb.c
1583 lines (1420 loc) · 31.1 KB
/
pom_pcb.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
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <LiquidCrystal.h>
#include <stdio.h>
#include <stdlib.h>
#include <my_button.h>
#include <SD.h>
#include <stdint.h>
#define UNDEFINED -1
#define LCD_RS 26
#define LCD_EN 27
#define LCD_D4 28
#define LCD_D5 0
#define LCD_D6 1
#define LCD_D7 2
#define A_BUTTON_PIN 4
#define B_BUTTON_PIN 5
#define X_BUTTON_PIN 6
#define Y_BUTTON_PIN 7
#define RIGHT_SHOULDER_BUTTON_PIN UNDEFINED
#define LEFT_SHOULDER_BUTTON_PIN UNDEFINED
#define UP_BUTTON_PIN 8
#define DOWN_BUTTON_PIN 9
#define LEFT_BUTTON_PIN 14
#define RIGHT_BUTTON_PIN 15
#define DS_START_BUTTON_PIN UNDEFINED
#define HOME_BUTTON_PIN UNDEFINED
#define DS_SELECT_BUTTON_PIN UNDEFINED
#define POWER_BUTTON_PIN 3
#define TOUCH_SCREEN_X_PIN 23
#define TOUCH_SCREEN_Y_PIN A14
#define TOUCH_SCREEN_Y_SENSE_PIN 19
#define C_PAD_X_PIN 21
#define C_PAD_Y_PIN 22
#define LIGHT_SENSOR_PIN A10
#define AUDIO_IN_PIN UNDEFINED
#define BUZZER_PIN 32
#define BLUE_LED_PIN 31
#define BUTTON_PRESSED LOW
#define WALKING_MODE 0
#define FISHING_MODE 1
#define HORDE_MODE 2
#define CLONE_MODE 3
#define SR_MODE 4
#define EGG_MODE 5
#define RELEASE_MODE 6
#define FISHING_TIMEOUT 8000
#define WALKING_BUTTON_PINPRESS_DURATION 300
#define QUICKBALL_PAGE 0
#define QUICKBALL_POS 0
#define BACKUPBALL_PAGE 0
#define BACKUPBALL_POS 1
#define BATTLE_BUTTON_PINPRESS_DELAY 900
#define CATCH_TIMEOUT 50000
#define NO_TIMEOUT -1
#define DARK 0
#define BRIGHT 1
#define RETRY_ATTEMPT 3
#define SCREEN_DARK 0
#define SCREEN_BRIGHT 1
#define CHECK_SCRREN_DELAY 40
#define CURSOR_MODE 0
#define CURSOR_DARK_THRESHOLD 1
#define CURSOR_NICKNAME 2
#define CURSOR_BLACKOUT_MODE 3
#define CURSOR_RUN 4
#define ABILITY_NOT_STAT_MODIFYING 0
#define ABILITY_STAT_MODIFYING 1
#define ABILITY_DROUGHT 2
#define ALWAYS_CATCH 3
#define NEVER_CATCH 4
#define SET_TO_DEBUG() lcd.setCursor(9, 1)
#define SET_TO_LAST() lcd.setCursor(15, 1)
#define SET_TO_BEGINNING() lcd.setCursor(0, 0)
#define SET_TO_BEGINNING_ROW2() lcd.setCursor(0, 1)
#define CLEAR_LCD() lcd.clear()
#define LED_ON(a) digitalWrite(a, HIGH)
#define LED_OFF(a) digitalWrite(a, LOW)
#define MODE_NUM 7
#define READ_LIGHT_SENSOR() analogRead(LIGHT_SENSOR_PIN)
#define EGG_DARK_THRESHOLD 150
#define NAME_BUF_SIZE 13
#define CLEAR_NAME_BUF() memset(name_buf, 0, NAME_BUF_SIZE)
#define CYCLE_PERIOD 7000
//5170
#define C_PAD_DEFAULT_POTENTIAL 132
#define ERR_CHECK_SCREEN_PREMATURE_RETURN 0
#define ERR_WUE_PREMATURE_RETURN 1
#define ERR_FLEE_FAILED 2
#define ERR_SWEET_SCENT_SEL 3
#define ERR_FISHING_FALSE_BITE 4
LiquidCrystal lcd(LCD_RS, LCD_EN, LCD_D4, LCD_D5, LCD_D6, LCD_D7);
char current_mode = EGG_MODE;
unsigned char dark_threshold = 20;
char nickname_enabled = 0;
char blackout_mode = ABILITY_NOT_STAT_MODIFYING;
char check_screen_delay_enabled = 1;
unsigned int encounter_count = 0;
unsigned char catch_retry_count = 0;
unsigned char shiny_caught_count = 0;
char error_occurred = 0;
long last_blackout = 0;
char egg_in_pocket = 0;
char hatched_in_pocket = 0;
char free_slot = 5;
char egg_request_interval = 25;
int total_hatched = 0;
int total_obtained = 0;
char pkmn_in_box = 0;
char name_buf[NAME_BUF_SIZE];
char walk_timer = 10;
char timer_updated = 0;
char egg_ui_set = 0;
int release_request = 30;
char is_just_exited_daycare = 0;
my_button sf_up_button(33, 0);
my_button sf_down_button(25, 0);
my_button sf_left_button(UNDEFINED, 0);
my_button sf_right_button(24, 0);
void setup()
{
lcd.begin(16, 2);
analogReadResolution(10);
analogWriteResolution(9);
pinMode(TOUCH_SCREEN_X_PIN, INPUT);
pinMode(TOUCH_SCREEN_Y_PIN, INPUT);
pinMode(TOUCH_SCREEN_Y_SENSE_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(BLUE_LED_PIN, OUTPUT);
pinMode(C_PAD_X_PIN, OUTPUT);
pinMode(C_PAD_Y_PIN, OUTPUT);
c_pad_reset();
do_UI();
CLEAR_LCD();
}
void loop()
{
do_mode(current_mode);
}
int is_shiny(long blackout_duration)
{
last_blackout = blackout_duration;
long shiny_threshold;
switch(blackout_mode)
{
case ABILITY_NOT_STAT_MODIFYING: shiny_threshold = 14200; break;
case ABILITY_STAT_MODIFYING: shiny_threshold = 15400; break;
case ABILITY_DROUGHT: shiny_threshold = 17000; break;
case ALWAYS_CATCH: return 1;
case NEVER_CATCH: return 0;
}
if(is_between(blackout_duration, 12100, 13200) || (blackout_duration > shiny_threshold))
{
LED_ON(BLUE_LED_PIN);
shiny_caught_count++;
return 1;
}
else
return 0;
}
void do_egg_UI()
{
CLEAR_LCD();
char cursor_pos = 3;
while(1)
{
lcd.setCursor(1, 0);
lcd.print("EiP");
lcd.setCursor(5, 0);
lcd.print("PiB");
lcd.setCursor(9, 0);
lcd.print("ERI");
lcd.setCursor(13, 0);
lcd.print("Go");
lcd.setCursor(1, 1);
lcd.print((int)egg_in_pocket);
lcd.setCursor(5, 1);
lcd.print((int)pkmn_in_box);
lcd.print(" ");
lcd.setCursor(9, 1);
lcd.print((int)egg_request_interval);
lcd.print(" ");
if(sf_right_button.uniquePress())
cursor_pos = (cursor_pos + 1) % 4;
if(sf_left_button.uniquePress())
{
cursor_pos - 1 < 0 ? cursor_pos = 4 : cursor_pos;
cursor_pos = (cursor_pos - 1) % 4;
}
switch(cursor_pos)
{
case 0:
lcd.setCursor(0, 0);
lcd.print(">");
lcd.setCursor(4, 0);
lcd.print(" ");
lcd.setCursor(8, 0);
lcd.print(" ");
lcd.setCursor(12, 0);
lcd.print(" ");
if(sf_up_button.uniquePress())
egg_in_pocket++;
if(sf_down_button.uniquePress())
egg_in_pocket--;
break;
case 1:
lcd.setCursor(0, 0);
lcd.print(" ");
lcd.setCursor(4, 0);
lcd.print(">");
lcd.setCursor(8, 0);
lcd.print(" ");
lcd.setCursor(12, 0);
lcd.print(" ");
if(sf_up_button.uniquePress())
pkmn_in_box++;
if(sf_down_button.uniquePress())
pkmn_in_box--;
break;
case 2:
lcd.setCursor(0, 0);
lcd.print(" ");
lcd.setCursor(4, 0);
lcd.print(" ");
lcd.setCursor(8, 0);
lcd.print(">");
lcd.setCursor(12, 0);
lcd.print(" ");
if(sf_up_button.uniquePress())
egg_request_interval += 5;
if(sf_down_button.uniquePress())
egg_request_interval -= 5;
break;
case 3:
lcd.setCursor(0, 0);
lcd.print(" ");
lcd.setCursor(4, 0);
lcd.print(" ");
lcd.setCursor(8, 0);
lcd.print(" ");
lcd.setCursor(12, 0);
lcd.print(">");
if(sf_up_button.uniquePress() || sf_down_button.uniquePress())
{
egg_ui_set = 1;
CLEAR_LCD();
return;
}
break;
}
egg_in_pocket > 5 ? egg_in_pocket = 5 : egg_in_pocket;
pkmn_in_box > 30 ? pkmn_in_box = 30 : pkmn_in_box;
egg_request_interval > 120 ? egg_request_interval = 120 : egg_request_interval;
egg_in_pocket < 0 ? egg_in_pocket = 0 : egg_in_pocket;
pkmn_in_box < 0 ? pkmn_in_box = 0 : pkmn_in_box;
egg_request_interval < 15 ? egg_request_interval = 15 : egg_request_interval;
free_slot = 5 - egg_in_pocket - hatched_in_pocket;
}
}
void do_release()
{
CLEAR_LCD();
while(1)
{
SET_TO_BEGINNING();
lcd.print("Release how");
SET_TO_BEGINNING_ROW2();
lcd.print("many?");
lcd.setCursor(8, 1);
lcd.print(release_request);
lcd.print(" ");
if(sf_up_button.uniquePress())
release_request++;
if(sf_down_button.uniquePress())
release_request--;
if(sf_right_button.uniquePress())
release_request+=30;
release_request >= 900 ? release_request = 900 : release_request;
release_request < 1 ? release_request = 1 : release_request;
if(sf_left_button.uniquePress())
break;
}
CLEAR_LCD();
SET_TO_BEGINNING();
lcd.print("0 Pokemon");
SET_TO_BEGINNING_ROW2();
lcd.print("released");
for(int i = 1; i <= release_request; i++)
{
press_once(A_BUTTON_PIN);
press_ntimes(UP_BUTTON_PIN, 2);
press_once(A_BUTTON_PIN);
delay(200);
press_once(UP_BUTTON_PIN);
press_once(A_BUTTON_PIN);
SET_TO_BEGINNING();
lcd.print(i);
lcd.print(" Pokemon");
SET_TO_BEGINNING_ROW2();
lcd.print("released");
delay(500);
press_ntimes(A_BUTTON_PIN, 2);
press_once(RIGHT_BUTTON_PIN);
if(i % 6 == 0)
{
for(int i = 0; i < 6; i++)
{
press(LEFT_BUTTON_PIN);
delay(100);
release(LEFT_BUTTON_PIN);
delay(100);
}
press_once(DOWN_BUTTON_PIN);
}
if(i % 30 == 0)
{
press_once(DOWN_BUTTON_PIN);
press_once(RIGHT_BUTTON_PIN);
press_once(DOWN_BUTTON_PIN);
}
}
}
void walk_n_step(char dir, int n)
{
release(dir);
for(int i = 0; i < n; i++)
{
press(dir);
delay(200);
release(dir);
delay(100);
}
}
int walk_n_step_hatching_protected(char dir, int n)
{
for(int i = 0; i < n; i++)
{
walk_n_step(dir, 1);
press_once_fast(B_BUTTON_PIN);
if(READ_LIGHT_SENSOR() < dark_threshold)
{
hatch_handler();
//log hatching on foot here
return 1;
}
}
return 0;
}
void cycle_for_egg()
{
if(!egg_ui_set)
do_egg_UI();
long now = millis();
int ls_value = READ_LIGHT_SENSOR();
if(is_between((now % (2*CYCLE_PERIOD + 400)), 0, CYCLE_PERIOD))
press(RIGHT_BUTTON_PIN);
if(is_between((now % (2*CYCLE_PERIOD + 400)), CYCLE_PERIOD, CYCLE_PERIOD+200))
release(RIGHT_BUTTON_PIN);
if(is_between((now % (2*CYCLE_PERIOD + 400)), CYCLE_PERIOD + 200, 2*CYCLE_PERIOD+200))
press(LEFT_BUTTON_PIN);
if(is_between((now % (2*CYCLE_PERIOD + 400)), 2*CYCLE_PERIOD+200, 2*CYCLE_PERIOD+400))
release(LEFT_BUTTON_PIN);
if(now % 500 < 100)
press(B_BUTTON_PIN);
else
release(B_BUTTON_PIN);
if(now % 1000 < 500)
{
if(timer_updated == 0)
{
(walk_timer+1) > egg_request_interval ? walk_timer : walk_timer++;
timer_updated = 1;
}
}
else
timer_updated = 0;
if(ls_value < dark_threshold)
hatch_handler();
if(free_slot <= 0 && hatched_in_pocket >= 5)
store_hatched();
if((walk_timer >= egg_request_interval) && (free_slot > 0))
{
walk_timer = 0;
ask_for_egg();
}
SET_TO_BEGINNING();
lcd.print((int)walk_timer);
lcd.print(' ');
lcd.setCursor(3, 0);
lcd.print("Egg:");
lcd.print((int)egg_in_pocket);
lcd.setCursor(9, 0);
lcd.print("PKMN:");
lcd.print((int)hatched_in_pocket);
lcd.setCursor(13, 1);
lcd.print(ls_value);
lcd.print(" ");
SET_TO_BEGINNING_ROW2();
lcd.print("Free:");
lcd.print((int)free_slot);
lcd.setCursor(7, 1);
lcd.print("T:");
lcd.print(total_hatched);
}
int check_hatch(int duration, int button)
{
long now = millis();
press(button);
while(millis() - now <= duration)
{
if(millis() % 500 < 100)
press(B_BUTTON_PIN);
else
release(B_BUTTON_PIN);
if(READ_LIGHT_SENSOR() < dark_threshold)
{
hatch_handler();
press(button);
// log hatching while biking asking for egg here
return 1;
}
}
return 0;
}
void store_hatched()
{
CLEAR_LCD();
SET_TO_BEGINNING();
lcd.print("storing ");
lcd.print((int)hatched_in_pocket);
lcd.print(" PKMN");
release_all();
delay(200);
while(check_hatch(CYCLE_PERIOD+1, RIGHT_BUTTON_PIN));
release_all();
press_once(Y_BUTTON_PIN);
press_once(LEFT_BUTTON_PIN);
delay(1000);
walk_n_step(LEFT_BUTTON_PIN, 10);
walk_n_step(UP_BUTTON_PIN, 3);
delay(3200);
walk_n_step(UP_BUTTON_PIN, 6);
walk_n_step(RIGHT_BUTTON_PIN, 2);
walk_n_step(UP_BUTTON_PIN, 1);
press_once(A_BUTTON_PIN);
delay(2000);
press_once(A_BUTTON_PIN);
delay(350);
press_once(A_BUTTON_PIN);
delay(350);
press_once(A_BUTTON_PIN);
delay(2500);
for(int i = 0; i < hatched_in_pocket; i++)
{
press_once(RIGHT_BUTTON_PIN);
press_once(A_BUTTON_PIN);
press_once(A_BUTTON_PIN);
delay(350);
if(pkmn_in_box >= 30)
{
press_once(RIGHT_BUTTON_PIN);
pkmn_in_box = 0;
}
press_once(A_BUTTON_PIN);
pkmn_in_box++;
SET_TO_BEGINNING_ROW2();
lcd.print("PKMN in box:");
lcd.print((int)pkmn_in_box);
lcd.print(" ");
}
press_once(B_BUTTON_PIN);
delay(600);
press_once(B_BUTTON_PIN);
delay(600);
press_once(B_BUTTON_PIN);
delay(600);
press_once(B_BUTTON_PIN);
delay(4000);
walk_n_step(LEFT_BUTTON_PIN, 2);
walk_n_step(DOWN_BUTTON_PIN, 7);
delay(3500);
walk_n_step(DOWN_BUTTON_PIN, 1);
free_slot += hatched_in_pocket;
hatched_in_pocket = 0;
walk_timer = egg_request_interval;
is_just_exited_daycare = 1;
CLEAR_LCD();
}
void hatch_handler()
{
CLEAR_LCD();
SET_TO_BEGINNING();
lcd.print("egg is hatching!");
release_all();
delay(18200);
if(nickname_enabled)
{
CLEAR_NAME_BUF();
sprintf(name_buf, "5 IV!");
give_nickname();
}
else
{
skip_nickname();
delay(4000);
}
CLEAR_LCD();
egg_in_pocket--;
hatched_in_pocket++;
total_hatched++;
}
void ask_for_egg()
{
long now = 0;
CLEAR_LCD();
SET_TO_BEGINNING();
lcd.print("asking for egg..");
release_all();
delay(200);
if(is_just_exited_daycare)
{
walk_n_step(LEFT_BUTTON_PIN, 5);
is_just_exited_daycare = 0;
}
else
{
while(check_hatch(CYCLE_PERIOD+1, RIGHT_BUTTON_PIN));
release_all();
press_once(Y_BUTTON_PIN);
press_once(LEFT_BUTTON_PIN);
delay(1000);
if(walk_n_step_hatching_protected(LEFT_BUTTON_PIN, 15))
{
press_once(Y_BUTTON_PIN);
press_once(LEFT_BUTTON_PIN);
delay(1000);
return;
}
}
walk_n_step(UP_BUTTON_PIN, 1);
press_once(A_BUTTON_PIN);
delay(500);
SET_TO_BEGINNING_ROW2();
if(READ_LIGHT_SENSOR() < EGG_DARK_THRESHOLD)
{
lcd.print("egg available");
total_obtained++;
free_slot--;
egg_in_pocket++;
// log egg available here
now = millis();
while(millis() - now < 4500)
press_once(A_BUTTON_PIN);
now = millis();
while(millis() - now < 3250)
press_once(B_BUTTON_PIN);
}
else
{
lcd.print("no egg");
// log egg unavailable here
now = millis();
while(millis() - now < 2800)
press_once(B_BUTTON_PIN);
}
walk_n_step(DOWN_BUTTON_PIN, 1);
press_once(Y_BUTTON_PIN);
press_once(LEFT_BUTTON_PIN);
delay(1000);
CLEAR_LCD();
}
void log_error(char err_no)
{
;
}
void do_UI()
{
char cursor_pos = 4;
SET_TO_BEGINNING();
lcd.print(" mode DS N Tm Go");
print_mode(current_mode);
print_DS();
print_nickname_en();
print_blackout_mode();
while(1)
{
if(sf_right_button.uniquePress())
cursor_pos = (cursor_pos + 1) % 5;
if(sf_left_button.uniquePress())
{
cursor_pos - 1 < 0 ? cursor_pos = 5 : cursor_pos;
cursor_pos = (cursor_pos - 1) % 5;
}
print_cursor(cursor_pos);
switch(cursor_pos)
{
case CURSOR_MODE:
if(sf_up_button.uniquePress())
current_mode = (current_mode + 1) % MODE_NUM;
if(sf_down_button.uniquePress())
{
current_mode - 1 < 0 ? current_mode = MODE_NUM : current_mode;
current_mode = (current_mode - 1) % MODE_NUM;
}
print_mode(current_mode);
break;
case CURSOR_DARK_THRESHOLD:
if(sf_up_button.uniquePress())
dark_threshold >= 60 ? dark_threshold : dark_threshold += 5;
if(sf_down_button.uniquePress())
dark_threshold <= 10 ? dark_threshold : dark_threshold -= 5;
print_DS();
break;
case CURSOR_NICKNAME:
if(sf_up_button.uniquePress() || sf_down_button.uniquePress())
nickname_enabled = (nickname_enabled + 1) % 2;
print_nickname_en();
break;
case CURSOR_BLACKOUT_MODE:
if(sf_up_button.uniquePress())
blackout_mode = (blackout_mode + 1) % 5;
if(sf_down_button.uniquePress())
{
blackout_mode - 1 < 0 ? blackout_mode = 5 : blackout_mode;
blackout_mode = (blackout_mode - 1) % 5;
}
print_blackout_mode();
break;
case CURSOR_RUN:
if(sf_up_button.uniquePress() || sf_down_button.uniquePress())
return;
current_mode == CLONE_MODE ? check_screen_delay_enabled = 0 : check_screen_delay_enabled = 1;
}
}
}
void do_clone()
{
int count = 0;
SET_TO_BEGINNING();
lcd.print("clone press");
lcd.setCursor(0, 1);
lcd.print("down to start");
while(!sf_down_button.uniquePress());
CLEAR_LCD();
SET_TO_BEGINNING();
lcd.print("Armed");
check_screen(NO_TIMEOUT);
CLEAR_LCD();
delay(3920);
press(POWER_BUTTON_PIN);
delay(300);
release(POWER_BUTTON_PIN);
SET_TO_BEGINNING();
lcd.print("restarting..");
delay(1000);
press_once(HOME_BUTTON_PIN);
delay(20000);
press_once(A_BUTTON_PIN);
delay(9000);
press_once(A_BUTTON_PIN);
delay(2000);
press_once(A_BUTTON_PIN);
CLEAR_LCD();
}
void log_shiny()
{
;
}
void do_horde()
{
SET_TO_BEGINNING();
lcd.print("horde");
press_once(X_BUTTON_PIN);
press_once(A_BUTTON_PIN);
delay(2000);
press_once(RIGHT_BUTTON_PIN);
press_once(A_BUTTON_PIN);
delay(1000);
press_once(DOWN_BUTTON_PIN);
press_once(A_BUTTON_PIN);
press_once(A_BUTTON_PIN);
delay(4500);
long duration = check_screen(CATCH_TIMEOUT);
if(duration == -1)
{
for (int i = 0; i < 50; i++)
press_once(B_BUTTON_PIN);
log_error(ERR_SWEET_SCENT_SEL);
return;
}
encounter_count++;
print_encounter_count();
SET_TO_DEBUG();
lcd.print(duration);
while(is_between(duration, 13500, 14700) || duration > 16000)
sound_buzzer();
do_run_away();
delay(1000);
}
void sound_buzzer()
{
if(millis() % 1000 < 500)
tone(BUZZER_PIN, 3000);
else
noTone(BUZZER_PIN);
}
void do_walking()
{
while(1)
{
SET_TO_BEGINNING();
lcd.print("walk ");
long blackout_duration = walk_until_encounter();
encounter_count++;
print_encounter_count();
if(is_shiny(blackout_duration))
{
try_catch();
delay(500);
continue;
}
else
{
do_run_away();
delay(500);
}
}
}
void try_catch()
{
SET_TO_BEGINNING();
lcd.print("catch");
throw_ball(QUICKBALL_PAGE, QUICKBALL_POS);
if(!is_caught())
{
do_false_swipe();
for(int i = 0; i < RETRY_ATTEMPT; i++)
{
throw_ball(BACKUPBALL_PAGE, BACKUPBALL_POS);
if(is_caught())
goto logging;
}
log_shiny();
do_alarm();
}
// now it's time to send logging request
logging:
log_shiny();
catch_retry_count = 0;
encounter_count = 0;
}
void do_alarm()
{
SET_TO_BEGINNING();
lcd.print("crap ");
while(1)
sound_buzzer();
}
int is_caught()
{
if(check_screen(CATCH_TIMEOUT) == -1)
{
catch_retry_count++;
print_catch_info();
return 0;
}
SET_TO_BEGINNING();
lcd.print("cnfrm");
if(nickname_enabled)
{
CLEAR_NAME_BUF();
sprintf(name_buf, "test");
give_nickname();
}
else
skip_nickname();
delay(1000);
press_once(A_BUTTON_PIN);
delay(5000);
print_catch_info();
return 1;
}
void skip_nickname()
{
delay(1000);
press_once(B_BUTTON_PIN);
press_once(B_BUTTON_PIN);
}
void give_nickname()
{
delay(1000);
press_once(A_BUTTON_PIN);
delay(3000);
enter_nickname(name_buf);
press_once(A_BUTTON_PIN);
delay(5500);
}
void enter_nickname(char* s)
{
for(int i = 0; i < 12; i++)
{
if(s[i] == 0)
{
select_enter();
return;
}
enter_letter(s[i], i);
}
}
void print_catch_info()
{
lcd.setCursor(4, 1);
lcd.print("C");
lcd.print(shiny_caught_count);
lcd.print("R");
lcd.print(catch_retry_count);
}
void throw_ball(char page_index, char ball_index)
{
delay(1000);
press_once(DOWN_BUTTON_PIN);
press_once(A_BUTTON_PIN);
delay(BATTLE_BUTTON_PINPRESS_DELAY);
press_once(RIGHT_BUTTON_PIN);
press_once(A_BUTTON_PIN);
delay(BATTLE_BUTTON_PINPRESS_DELAY);
reset_page();
goto_page_start(page_index);
select_ball(ball_index);
delay(BATTLE_BUTTON_PINPRESS_DELAY);
press_once(A_BUTTON_PIN);
}
void reset_page()
{
unsigned long now = millis();
while(millis() - now <= 2000)
press_once(LEFT_BUTTON_PIN);
for(int i=0; i<3; i++)
press_once(UP_BUTTON_PIN);
}
void select_ball(char ball_index)
{
unsigned char row = ball_index / 2;
unsigned char col = ball_index - 2 * row;
for(int i=0; i<row; i++)
press_once(DOWN_BUTTON_PIN);
for(int i=0; i<col; i++)
press_once(RIGHT_BUTTON_PIN);
press_once(A_BUTTON_PIN);
}
void goto_page_start(char page_index)
{
switch(page_index)
{
case 0: return;
case 1:
press_once(RIGHT_BUTTON_PIN);
press_once(RIGHT_BUTTON_PIN);
delay(BATTLE_BUTTON_PINPRESS_DELAY);
press_once(LEFT_BUTTON_PIN);
break;
case 2:
press_once(RIGHT_BUTTON_PIN);
press_once(RIGHT_BUTTON_PIN);
delay(BATTLE_BUTTON_PINPRESS_DELAY);
press_once(RIGHT_BUTTON_PIN);
delay(BATTLE_BUTTON_PINPRESS_DELAY);
press_once(LEFT_BUTTON_PIN);
break;
default: return;
}
}
int is_between(long val, long lower, long upper)
{
return (val >= lower && val < upper);
}
int walk_until_encounter()
{
unsigned long dark_start = 0;
unsigned char screen_state = SCREEN_BRIGHT;
while(1)
{
delay(CHECK_SCRREN_DELAY);
if(screen_state == SCREEN_BRIGHT)
{
press(B_BUTTON_PIN);
if(millis() % WALKING_BUTTON_PINPRESS_DURATION * 2 < WALKING_BUTTON_PINPRESS_DURATION)
{
release(RIGHT_BUTTON_PIN);
press(LEFT_BUTTON_PIN);
}
else
{
release(LEFT_BUTTON_PIN);
press(RIGHT_BUTTON_PIN);
}
}
int curr = READ_LIGHT_SENSOR();
lcd.setCursor(0, 1);
lcd.print(curr);
lcd.print(" ");
curr < dark_threshold ? curr = DARK : curr = BRIGHT;
if(screen_state == SCREEN_BRIGHT && curr == DARK)
{
screen_state = SCREEN_DARK;
release_all();
SET_TO_LAST();
lcd.print("D");
SET_TO_BEGINNING();
lcd.print("batt ");
dark_start = millis();
}
else if(screen_state == SCREEN_DARK && curr == BRIGHT)
{
unsigned long ret = millis() - dark_start;
if(ret < 10000)
{
SET_TO_LAST();
lcd.print("F");
SET_TO_DEBUG();
lcd.print(ret);
lcd.print(" ");
error_occurred = 1;