-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPaddleWar.ino
620 lines (536 loc) · 27.3 KB
/
PaddleWar.ino
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
/**
Demo/Game sketch for TinyRFModulator and DisplayFoundation library project.
---------------------------------------------------------------------------
This game was originally created by Maarten Janssen in 2015 and is converted to be able to use with TDisplayFoundation
and TVOut. This is a demo project for TDisplayFoundation and TinyRFModulator.
Serveral things are changed:
- Reading controller values in percents
- Added defines and macros, makes it easier to convert the code
- Rebuild or remove graphics (monochrome)
- Created some Keen artwork (monochrome)
REQUIREMENTS
- MCU with 32Kb program space
- TVOut library
- TV with RF input and TinyRFModulator
OR
TV with composite input
- 10K Potentiometer as paddle input
HOW TO CONNECT
_____________
ARDUINO | PIEZO SPEAKER OR RCA CONNECTOR____
NANO/ GND o-------------------------------\ --------------------| |______
ATMEGA328 | _|_/| __/---|______)
D11 o ------- AUDIO OUT ------------[__ | --[_1K_RESISTOR_]--| ____|
| \|
| ____
D9 o ------- VIDEO SYNC ----|__1K RESITOR__]-------- | |______
| |----TV--------|______)
D7 o ------- VIDEO SIGNAL --|__470 OHM RESISTOR_]----| ___|
| | RCA CONNECTOR
GND o ---------------------------------------------------- TV-----/
| _|_ | _
___ 1uF |_______| |_____ PADDLE
| | + --| |_____| 10K POTENTIOMETER LINEAIR
AO o ------------------|---------------|_| (not a taper pot!)
|
______________|
TODO:
- Auto alignment to any screensize
* Maarten wrote:
* Demo sketch for the LM15SGFNZ07 LCD display library.
* The LM15SGFNZ07 is an LCD with a 101x80 pixels resolution, capable of displaying 4096 colors. It is found in Siemens
* mobile phones (models A65, M55, C60, MC60, S55, A60).
*
* This demo lets you play the PaddleWar! game from the Commander
* Keen series.
*
* Connect a potentionmeter to A0, +5v and GND to control your paddle. Simple sounds are provided if you
* connect a small speaker between pins 11 and GND. The game will start automatically and end when either you or the
* Arduino reach 21 points. Reset your Arduino for another game.
* Coded by Maarten Janssen in 2015.
* Latest version of the library can be downloaded from my GitHub page: http://github.com/DhrBaksteen
*
*/
#include <TVout.h>
#include "DisplayFoundation.h"
TVout TV;
// Size of TV screen in pixels.
// ---------------------------
// It is not guaranteed these settings will work with every TV out
// there. If it doesn't work correctly, try different values. Keep
// in mind it will fit available memory!
#define videoWidth() 100
#define videoHeight() 80 // Must be divisable by 8!
//#define videoWidth() 128
//#define videoHeight() 96 // Must be divisable by 8!
#define COLOR_FG 1
#define COLOR_BG 0
#define PIN_LCD_CS 9
#define PIN_LCD_RST 5
#define PIN_LCD_RS 4
#define PIN_SND_SPEAKER 7
#define PIN_CTRL_INPUT A0 // Paddle ctrl, 10k pot
#define PIN_CTRL_MIN 16
#define PIN_CTRL_MAX 230
#define PIN_CTRL_SCALE (PIN_CTRL_MAX-PIN_CTRL_MIN)
#define PIN_CTRL_INVERT true
#define VAL_POS_SIZE 12
#define VAL_POS_MIN 2
#define VAL_POS_MAX (99-VAL_POS_SIZE)
#define VAL_POS_SCALE (VAL_POS_MAX-VAL_POS_MIN)
#define VAL_PAD_WIDTH (VAL_POS_SIZE+VAL_POS_MIN)
#define VAL_PAD_HEIGHT 3
#define VAL_PAD_POSY 71
#define PIN_LCD_LED1 3
#define PIN_LCD_LED2 6
// Some conversion macro's
#define displayInit() displayFoundry.onSetPixel = setFontPixelEventHandler;\
displayFoundry.setBG( DF_PIXEL_CLEAR );\
displayFoundry.begin( videoWidth(), videoHeight() );\
TV.begin(_PAL, videoWidth(), videoHeight());
#define displayClear(color) displayFoundry.clear(color)
#define displayDrawLine(x1,y1,x2,y2,color) displayFoundry.drawLine(x1,y1,x2,y2,color)
#define displayDrawBitmap(x,y,w,h,bmp) displayFoundry.drawBitmap(bmp,x,y,w,h)
#define displayDrawRect(x1,y1,x2,y2,color) displayFoundry.drawRect( x1,y1,x2,y2,color )
#define displayPrint(s,x,y,fg,bg) displayFoundry.gotoXY(x,y); displayFoundry.print(s)
#define displayFillRect(x,y,w,h,color) displayFoundry.drawFillRect(x,y,x+w,y+h,color)
#define displayDrawBall(x,y,h,w) displayFoundry.drawFillRect(x,y,x+2,y+2,1);\
displayFoundry.drawRect(x,y,x+4,y+4,0) // displayFoundry.drawCircle(x,y,4,4)
#define displayDrawPaddle(x,y,w,h) displayFoundry.drawFillRect(x,y,x+w,y+h-1)
#define displayGetFontWidth() displayFoundry.getFontWidth()
#define displayGetFontSpacing() displayFoundry.getCharSpacing()
#define tone(pin,freq,ms) TV.tone(freq,ms)
#define delay(n) TV.delay(n)
// ** bitmaps **
// image converted with use of https://javl.github.io/image2cpp/
const uint8_t bmpLogo[] PROGMEM =
{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x0c,
0x0c, 0x00, 0x00, 0xc0, 0xff, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x0c, 0x00, 0x00, 0xc0, 0xc3, 0x00,
0x00, 0x00, 0x00, 0x0c, 0x0c, 0x00, 0x00, 0xc0, 0xc3, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x0c, 0x00,
0x00, 0xc0, 0xc3, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x0c, 0x00, 0x00, 0xc0, 0xc3, 0x00, 0x00, 0x00,
0x00, 0x0c, 0x0c, 0x00, 0x00, 0xc0, 0xff, 0x30, 0xf0, 0xf0, 0xc3, 0xcc, 0x0c, 0x30, 0xf0, 0xc0,
0xfe, 0x30, 0xf0, 0xf0, 0xc3, 0xcc, 0x0c, 0x30, 0xf0, 0xc0, 0xc0, 0xcc, 0xcc, 0xcc, 0xc3, 0x0c,
0x0c, 0xcc, 0xcc, 0xc0, 0xc0, 0xcc, 0xcc, 0xcc, 0xc3, 0x0c, 0x0c, 0xcc, 0xcc, 0xc0, 0xc0, 0xfc,
0xcc, 0xcc, 0xc3, 0xcc, 0xcc, 0xfc, 0xf0, 0xc0, 0xc0, 0xfc, 0xcc, 0xcc, 0xc3, 0xcc, 0xcc, 0xfc,
0xf0, 0xc0, 0xc0, 0xcc, 0xcc, 0xcc, 0xc3, 0x0f, 0x3c, 0xcc, 0xcc, 0x00, 0xc0, 0xcc, 0xcc, 0xcc,
0xc3, 0x0f, 0x3c, 0xcc, 0xcc, 0x00, 0xc0, 0xcc, 0xf0, 0xf0, 0xf3, 0xcc, 0x0c, 0xcc, 0xcc, 0xc0,
0xc0, 0xcc, 0xf0, 0xf0, 0xf3, 0xcc, 0x0c, 0xcc, 0xcc, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0xff, 0xff, 0xfc, 0x00, 0xfe, 0x00, 0x3f, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xf0, 0xd8,
0xfe, 0x1c, 0x1f, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xe1, 0xfc, 0x7f, 0x0f, 0x0f, 0xff, 0xff, 0xc0,
0x00, 0x00, 0x07, 0xfe, 0x3f, 0x87, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xfe, 0x1f, 0xc7,
0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0x1f, 0xc3, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00,
0x3f, 0xf7, 0xff, 0xe3, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xf9, 0x8f, 0xff, 0xfc, 0x00,
0x00, 0x00, 0x0c, 0x00, 0xff, 0xff, 0x8f, 0xf1, 0xfe, 0x00, 0x00, 0x00, 0x1e, 0x00, 0xef, 0xff,
0x0f, 0xf1, 0xfe, 0x00, 0x00, 0x00, 0x1e, 0x01, 0xdf, 0xff, 0x8f, 0xf1, 0xff, 0x00, 0x00, 0x00,
0x0c, 0x83, 0xc3, 0xff, 0x8f, 0xf1, 0xff, 0x00, 0x00, 0x00, 0x00, 0x43, 0x83, 0xff, 0x8f, 0xf1,
0xff, 0x80, 0x00, 0x00, 0x02, 0x02, 0x03, 0xf1, 0xff, 0xff, 0xdf, 0x80, 0x00, 0x00, 0x01, 0x16,
0x21, 0xdf, 0xff, 0xff, 0xf7, 0x80, 0x00, 0x00, 0x00, 0x84, 0x63, 0xf0, 0x00, 0x00, 0x1f, 0xc0,
0x00, 0x00, 0x00, 0x44, 0x6f, 0x00, 0x00, 0x00, 0x03, 0xc0, 0x00, 0x00, 0x00, 0x26, 0x47, 0x80,
0x00, 0x00, 0x03, 0xc0, 0x00, 0x00, 0x00, 0x04, 0x03, 0x00, 0x00, 0x00, 0x01, 0xe0, 0x00, 0x00,
0x00, 0x05, 0x07, 0x02, 0xff, 0x86, 0x01, 0xe0, 0x00, 0x00, 0x00, 0x06, 0x8e, 0x07, 0xff, 0xef,
0x01, 0xe0, 0x00, 0x00, 0x00, 0x07, 0xae, 0x0f, 0xbf, 0xff, 0x01, 0xe0, 0x30, 0x00, 0x00, 0x07,
0xfe, 0x0e, 0x1f, 0xff, 0x01, 0xe0, 0x78, 0x00, 0x00, 0x05, 0xfe, 0x1c, 0xcf, 0xff, 0x01, 0xe0,
0x78, 0x00, 0x00, 0x00, 0xff, 0x1d, 0xef, 0xff, 0x01, 0xe1, 0x30, 0x00, 0x00, 0x01, 0xff, 0x0d,
0xf6, 0x07, 0x01, 0xe2, 0x00, 0x00, 0x00, 0x05, 0xcf, 0x1d, 0xf7, 0x79, 0x81, 0xc0, 0x00, 0x00,
0x00, 0x04, 0x07, 0x1d, 0xf7, 0x79, 0x81, 0xc8, 0x80, 0x00, 0x00, 0x06, 0x87, 0x1d, 0x17, 0x65,
0x81, 0xc1, 0x00, 0x00, 0x00, 0x03, 0x27, 0x0e, 0x09, 0x45, 0x01, 0xc2, 0x00, 0x00, 0x00, 0x03,
0x93, 0x8e, 0x16, 0x83, 0x01, 0xc4, 0x00, 0x00, 0x00, 0x03, 0x83, 0x0f, 0xf2, 0xff, 0x01, 0x80,
0x00, 0x00, 0x00, 0x01, 0xc7, 0x0f, 0xf9, 0xff, 0x01, 0x80, 0x00, 0x00, 0x00, 0x01, 0xcf, 0x0f,
0xff, 0xfe, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0xff, 0x0f, 0xff, 0xfe, 0x01, 0x80, 0x00, 0x00,
0x00, 0x00, 0xff, 0x1f, 0xff, 0xfc, 0x81, 0x80, 0x00, 0x00, 0x00, 0x00, 0x7f, 0x1f, 0xff, 0x7c,
0x41, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0x1f, 0xf8, 0xf8, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00,
0x7f, 0x1f, 0xff, 0xf0, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x0e, 0xff, 0xe0, 0x0c, 0x00,
0x00, 0x00, 0x00, 0x00, 0x1c, 0x0f, 0xff, 0xc0, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f,
0x9f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xc0, 0x00, 0x04, 0x00, 0x00,
0x03, 0x1e, 0xa1, 0xef, 0xff, 0xc0, 0x00, 0x02, 0x00, 0x00, 0x07, 0x80, 0x07, 0x03, 0xff, 0xde,
0x06, 0x01, 0x00, 0x00, 0x07, 0x80, 0x08, 0x00, 0x00, 0x01, 0x0f, 0x10, 0x80, 0x00, 0x03, 0x07,
0x10, 0x00, 0x07, 0xf8, 0x9f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x10, 0x3d, 0xc4,
0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x40, 0x20, 0xfc, 0xc2, 0x60, 0x00, 0x00, 0x00, 0x20, 0x00,
0x81, 0x43, 0xfc, 0x40, 0xf0, 0x00, 0x00, 0x00, 0x40, 0x00, 0xff, 0x83, 0xfc, 0xc0, 0xf0, 0x00,
0x00, 0x00, 0x40, 0x00, 0x00, 0x03, 0xf8, 0xc0, 0x60, 0x00, 0x00, 0x00, 0x40, 0xff, 0x00, 0x03,
0xf3, 0x80, 0x00, 0x00, 0x00, 0x00, 0x43, 0xff, 0x80, 0x03, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
0x87, 0xff, 0x80, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8f, 0xff, 0xf0, 0x00, 0x01, 0x00,
0x00, 0x00, 0x00, 0x00, 0x9f, 0xff, 0xf0, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x9f, 0xff,
0xf0, 0x1f, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xe0, 0x3f, 0x3e, 0x00, 0x00, 0x00
};
// image converted with use of https://javl.github.io/image2cpp/
const uint8_t bmpPaddleWar[] PROGMEM =
{
0xe3, 0x39, 0xc8, 0xe8, 0x4c, 0xe4, 0xf7, 0xad, 0x68, 0x88, 0x5e, 0xf4, 0x94, 0xa5, 0x28, 0x88,
0x52, 0x94, 0x94, 0xa5, 0x28, 0xe8, 0x52, 0x94, 0xf7, 0xa5, 0x28, 0x8b, 0x5e, 0xf4, 0xe4, 0xa5,
0x28, 0x8f, 0xd2, 0xc0, 0x84, 0xad, 0x68, 0x8c, 0xd2, 0xa4, 0x84, 0xb9, 0xce, 0xe8, 0x52, 0x94
};
// image converted with use of https://javl.github.io/image2cpp/
const uint8_t bmpWon[] PROGMEM =
{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x60, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0xc0, 0xc0, 0x00, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xc0, 0x00,
0x63, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xc0, 0x00, 0x63, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0xc0, 0xc0, 0x00, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xc0, 0x00, 0x60, 0x00,
0x00, 0x00, 0x00, 0x0f, 0xf0, 0xc0, 0xc0, 0x40, 0x63, 0x3f, 0xf1, 0xff, 0x8f, 0xcf, 0xf0, 0xc0,
0xc0, 0xe0, 0x63, 0x3f, 0xf9, 0xff, 0xcf, 0xcc, 0x30, 0xc0, 0xc1, 0xf0, 0x63, 0x18, 0x18, 0xc0,
0xcc, 0x0c, 0x30, 0xc0, 0xc3, 0xb8, 0x63, 0x18, 0x18, 0xc0, 0xcc, 0x0c, 0xf0, 0xc0, 0xc7, 0x1c,
0x63, 0x18, 0x18, 0xc0, 0xce, 0x0f, 0xe0, 0xc0, 0xce, 0x0e, 0x63, 0x18, 0x18, 0xc0, 0xce, 0x0f,
0x80, 0xc0, 0xdc, 0x07, 0x63, 0x18, 0x18, 0xc0, 0xcc, 0x0c, 0xc0, 0x00, 0xf8, 0x03, 0xe3, 0x18,
0x18, 0xc0, 0xcc, 0x0c, 0x60, 0x00, 0xf0, 0x01, 0xe3, 0x18, 0x18, 0xc0, 0xcf, 0xcc, 0x38, 0xc0,
0xe0, 0x00, 0xe3, 0x18, 0x18, 0xc0, 0xcf, 0xcc, 0x18, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0xff, 0xff, 0xfc, 0x00, 0xfe, 0x00, 0x3f, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xf0, 0xd8,
0xfe, 0x1c, 0x1f, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xe1, 0xfc, 0x7f, 0x0f, 0x0f, 0xff, 0xff, 0xc0,
0x00, 0x00, 0x07, 0xfe, 0x3f, 0x87, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xfe, 0x1f, 0xc7,
0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0x1f, 0xc3, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00,
0x3f, 0xf7, 0xff, 0xe3, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xf9, 0x8f, 0xff, 0xfc, 0x00,
0x00, 0x00, 0x0c, 0x00, 0xff, 0xff, 0x8f, 0xf1, 0xfe, 0x00, 0x30, 0x00, 0x1e, 0x00, 0xef, 0xff,
0x0f, 0xf1, 0xfe, 0x00, 0x78, 0x00, 0x1e, 0x01, 0xdf, 0xff, 0x8f, 0xf1, 0xff, 0x00, 0x78, 0x00,
0x0c, 0x83, 0xc3, 0xff, 0x8f, 0xf1, 0xff, 0x01, 0x30, 0x00, 0x00, 0x43, 0x83, 0xff, 0x8f, 0xf1,
0xff, 0x82, 0x00, 0x00, 0x02, 0x02, 0x03, 0xf1, 0xff, 0xff, 0xdf, 0x80, 0x00, 0x00, 0x01, 0x16,
0x21, 0xdf, 0xff, 0xff, 0xf7, 0x88, 0x80, 0x00, 0x00, 0x84, 0x63, 0xf0, 0x00, 0x00, 0x1f, 0xc1,
0x00, 0x00, 0x00, 0x44, 0x6f, 0x00, 0x00, 0x00, 0x03, 0xc2, 0x00, 0x00, 0x00, 0x26, 0x47, 0x80,
0x00, 0x00, 0x03, 0xc4, 0x00, 0x00, 0x00, 0x04, 0x03, 0x00, 0x00, 0x00, 0x01, 0xe0, 0x00, 0x00,
0x00, 0x05, 0x07, 0x02, 0xff, 0x86, 0x01, 0xe0, 0x00, 0x00, 0x00, 0x06, 0x8e, 0x07, 0xff, 0xef,
0x01, 0xe0, 0x00, 0x00, 0x00, 0x07, 0xae, 0x0f, 0xbf, 0xff, 0x01, 0xe0, 0x00, 0x00, 0x00, 0x07,
0xfe, 0x0e, 0x1f, 0xff, 0x01, 0xe0, 0x00, 0x00, 0x00, 0x05, 0xfe, 0x1c, 0xcf, 0xff, 0x01, 0xe0,
0x00, 0x00, 0x00, 0x00, 0xff, 0x1d, 0xef, 0xff, 0x01, 0xe0, 0x00, 0x00, 0x00, 0x01, 0xff, 0x0d,
0xf7, 0xff, 0x01, 0xe0, 0x00, 0x00, 0x00, 0x05, 0xcf, 0x1d, 0xf7, 0xf7, 0x81, 0xc0, 0x00, 0x00,
0x00, 0x04, 0x07, 0x1d, 0xf7, 0x8f, 0x81, 0xc0, 0x00, 0x00, 0x00, 0x06, 0x87, 0x1d, 0x17, 0xfb,
0x81, 0xc0, 0x00, 0x00, 0x00, 0x03, 0x27, 0x0e, 0x09, 0x07, 0x01, 0xc0, 0x00, 0x00, 0x00, 0x03,
0x93, 0x8e, 0x16, 0xff, 0x01, 0xc0, 0x00, 0x00, 0x00, 0x03, 0x83, 0x0f, 0xf2, 0xff, 0x01, 0x80,
0x00, 0x00, 0x00, 0x01, 0xc7, 0x0f, 0xf9, 0xff, 0x01, 0x80, 0x00, 0x00, 0x00, 0x01, 0xcf, 0x0f,
0xff, 0xfe, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0xff, 0x0f, 0xff, 0xfe, 0x01, 0x80, 0x00, 0x00,
0x00, 0x00, 0xff, 0x1f, 0x7f, 0xfc, 0x81, 0x80, 0x00, 0x00, 0x00, 0x00, 0x7f, 0x1f, 0xbf, 0x7c,
0x41, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0x1f, 0xc0, 0xf8, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00,
0x7f, 0x1f, 0xff, 0xf0, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x0e, 0xff, 0xe0, 0x0c, 0x00,
0x00, 0x00, 0x00, 0x00, 0x1c, 0x0f, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f,
0x9f, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xc0, 0x00, 0x04, 0x00, 0x00,
0x00, 0x00, 0x01, 0xef, 0xff, 0xc0, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x07, 0x03, 0xff, 0xcc,
0x00, 0x01, 0x00, 0x00, 0x00, 0x04, 0x48, 0x00, 0xff, 0xd9, 0x00, 0x00, 0x80, 0x00, 0x00, 0x08,
0x90, 0x00, 0x00, 0x18, 0x80, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x1c, 0x60, 0x04,
0x00, 0x00, 0x00, 0x22, 0x20, 0x00, 0x00, 0x0c, 0x10, 0x02, 0x60, 0x00, 0x00, 0x44, 0x20, 0x00,
0x01, 0xef, 0x08, 0x00, 0xf0, 0x00, 0x00, 0x00, 0x40, 0x00, 0x03, 0xff, 0x04, 0x00, 0xf0, 0x00,
0x03, 0x00, 0x40, 0x08, 0x00, 0x3f, 0x00, 0x00, 0x60, 0x00, 0x07, 0x80, 0x40, 0x10, 0x03, 0xff,
0x02, 0x00, 0x00, 0x00, 0x07, 0x80, 0x40, 0x20, 0x00, 0x07, 0x7e, 0x00, 0x00, 0x00, 0x03, 0x00,
0x80, 0x20, 0x01, 0xff, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x20, 0x00, 0x01, 0x7f, 0x80,
0x00, 0x00, 0x00, 0x00, 0xff, 0xc0, 0x00, 0xfe, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0xff, 0xfe,
0x00, 0x00, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xc0, 0x07, 0xff, 0x80, 0x00, 0x00
};
// image converted with use of https://javl.github.io/image2cpp/
const uint8_t bmpLose[] PROGMEM =
{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0xc0, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xc3, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xc3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0xc0, 0xc3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xc3, 0x00, 0x00, 0x03,
0xf8, 0x0f, 0xe0, 0x07, 0xf8, 0xc0, 0xff, 0x8c, 0x0c, 0x7f, 0xf8, 0x0f, 0xe7, 0xe7, 0xf8, 0xc0,
0xff, 0xcc, 0x0c, 0xff, 0x18, 0xfc, 0x67, 0xe6, 0x18, 0xc0, 0xc0, 0xcc, 0x0c, 0xc3, 0x19, 0xfc,
0x66, 0x06, 0x18, 0xc0, 0xc0, 0xcc, 0x0c, 0xc3, 0x19, 0x8c, 0x66, 0x06, 0x78, 0xc0, 0xc0, 0xcc,
0x0c, 0xc3, 0x19, 0x8c, 0x67, 0x07, 0xf0, 0xc0, 0xc0, 0xcc, 0x0c, 0xc0, 0x19, 0x80, 0x67, 0x07,
0xc0, 0xc0, 0xc0, 0xcc, 0x0c, 0xc0, 0x19, 0x80, 0x66, 0x06, 0x60, 0x00, 0xc0, 0xcc, 0x0c, 0xc0,
0x19, 0x80, 0x66, 0x06, 0x30, 0x00, 0xff, 0xcf, 0xfc, 0xc0, 0x19, 0x80, 0x67, 0xe6, 0x1c, 0xc0,
0xff, 0x87, 0xfc, 0xc0, 0x19, 0x80, 0x67, 0xe6, 0x0c, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x0c, 0x00,
0x00, 0x00, 0x00, 0x01, 0x80, 0x00, 0x0c, 0x3e, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x03, 0xc0, 0x00,
0x38, 0x7c, 0x3f, 0xc0, 0x00, 0x00, 0x00, 0x03, 0xc0, 0x00, 0x78, 0x7c, 0x3f, 0xe0, 0x00, 0x00,
0x00, 0x01, 0x90, 0x00, 0xf0, 0xf8, 0x3f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x08, 0x01, 0xff, 0xff,
0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x40, 0x03, 0xf0, 0xf8, 0x7f, 0xfc, 0x00, 0x00, 0x00, 0x00,
0x22, 0x03, 0xf0, 0xf8, 0x7f, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x10, 0x07, 0xf0, 0xf8, 0x7f, 0xfe,
0x00, 0x00, 0x00, 0x00, 0x08, 0x07, 0xf0, 0xf8, 0x7f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x04, 0x07,
0xf0, 0xf8, 0x7f, 0xf4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xf0, 0xf8, 0x7f, 0xf1, 0x00, 0x00,
0x00, 0x00, 0x01, 0x0f, 0xc0, 0x00, 0x7f, 0xf4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8f, 0x80, 0x00,
0x0f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4f, 0x07, 0xe3, 0xc7, 0xfa, 0x00, 0x00, 0x00, 0x00,
0x00, 0x0f, 0x0e, 0xf7, 0x73, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x05, 0x4e, 0x08, 0x7c, 0x3b, 0xfd,
0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x0f, 0x3f, 0x99, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4e,
0x09, 0x7c, 0xbd, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x08, 0x7c, 0x3d, 0xfd, 0x00, 0x00,
0x00, 0x00, 0x00, 0x8e, 0x0c, 0xce, 0x7d, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x01, 0x0e, 0x07, 0xb7,
0xfd, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x02, 0x06, 0x0f, 0x8f, 0xfd, 0xf6, 0x00, 0x00, 0x06, 0x00,
0x04, 0x06, 0x0f, 0xef, 0xfd, 0xe3, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x06, 0x07, 0xff, 0xfd, 0xe3,
0x00, 0x00, 0x0f, 0x00, 0x10, 0x06, 0x07, 0xff, 0xfd, 0xe2, 0x00, 0x00, 0x06, 0x00, 0x20, 0x06,
0x07, 0x83, 0xfd, 0xe2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x43, 0x07, 0x00, 0x7d, 0xf6, 0x00, 0x00,
0x00, 0x00, 0x00, 0x43, 0x0b, 0x7f, 0x7c, 0xfe, 0x00, 0x00, 0x09, 0x00, 0x01, 0x43, 0x91, 0xff,
0xfc, 0xfc, 0x00, 0x00, 0x09, 0x00, 0x04, 0xf1, 0xe1, 0xff, 0xf8, 0x7c, 0x00, 0x00, 0x01, 0x00,
0x0a, 0xc0, 0x00, 0xff, 0xe0, 0x38, 0x00, 0x00, 0x01, 0x00, 0x09, 0x40, 0x00, 0x00, 0x10, 0x00,
0x00, 0x00, 0x09, 0x00, 0x45, 0x00, 0x03, 0x7f, 0xf6, 0x00, 0x00, 0x00, 0x00, 0x03, 0x43, 0x00,
0x04, 0x5f, 0xf1, 0x00, 0x00, 0x00, 0x01, 0x05, 0x28, 0x00, 0x18, 0x5f, 0xf0, 0xc0, 0x00, 0x00,
0x00, 0x02, 0x30, 0x00, 0x30, 0x1f, 0xc0, 0x70, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0xc0, 0x00,
0x00, 0x08, 0x00, 0x00, 0x09, 0x00, 0x80, 0x01, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x01, 0x00,
0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x01, 0x00, 0x00, 0x06, 0x01, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x08, 0x00, 0x80, 0x00, 0x01, 0x00, 0x00, 0x0c,
0x02, 0x80, 0x0c, 0x00, 0x80, 0x00, 0x01, 0x00, 0x00, 0x08, 0x02, 0x00, 0x08, 0x00, 0x40, 0x00,
0x01, 0x00, 0x00, 0x18, 0x04, 0x00, 0x08, 0x00, 0x40, 0x00, 0x01, 0x00, 0x00, 0x10, 0x04, 0x00,
0x03, 0x70, 0x20, 0x00, 0x01, 0x00, 0x00, 0x10, 0x00, 0x00, 0x07, 0xfc, 0x20, 0x00, 0x00, 0x00,
0x00, 0x10, 0x0f, 0xf8, 0x07, 0xfe, 0x20, 0x00, 0x01, 0x00, 0x00, 0x10, 0x1f, 0xff, 0xe7, 0xfe,
0x60, 0x00, 0x00, 0x00, 0x00, 0x10, 0x3f, 0xff, 0x1f, 0xfe, 0x20, 0x00, 0x00, 0x00, 0x00, 0x11,
0xbf, 0xf0, 0xff, 0xfe, 0x20, 0x00, 0x00, 0x00, 0x00, 0x16, 0xaf, 0xef, 0xff, 0xf9, 0xe0, 0x00
};
// ** CODE STARTS HERE **
int paddleBottom;
float paddleTop;
int p1Score;
int p2Score;
int hold;
float ballX;
float ballY;
float ballDx;
float ballDy;
// Event handlers
uint8_t getFontPixelEventHandler( TDisplayFoundation* poSender,
DFC_UINT_TYPE x,
DFC_UINT_TYPE y )
{
// Gets a pixel
return TV.get_pixel( x,y );
}
void setFontPixelEventHandler( TDisplayFoundation* poSender,
DFC_UINT_TYPE x,
DFC_UINT_TYPE y,
uint8_t iSetMode )
{
// Sets a pixel
if( TV.get_pixel( x,y ) != iSetMode )
{ TV.set_pixel( x,y, iSetMode ); }
}
void gameReset()
{
paddleBottom = 0;
paddleTop = 43;
p1Score = 0;
p2Score = 0;
hold = 0;
ballX = 48;
ballY = 42;
ballDx = 0.5;
ballDy = -0.5;
}
void displayScore()
{
uint8_t iPosX = 88-((p2Score > 9)?(displayGetFontWidth()+displayGetFontSpacing()):0);
displayFillRect(1, 2, 12, 7, COLOR_BG);
displayFillRect(iPosX, 2, 12, 7, COLOR_BG);
if( p1Score==0 )
{ displayPrint( "0", 2, 2, COLOR_FG, COLOR_BG); }
else { displayPrint( p1Score, 2, 2, COLOR_FG, COLOR_BG); }
if( p2Score==0 )
{ displayPrint( "0", iPosX ,2, COLOR_FG, COLOR_BG); }
else { displayPrint( p2Score, iPosX, 2, COLOR_FG, COLOR_BG); }
//88-((p2Score>9)?getFontWidth():0), 2, COLOR_FG, COLOR_BG);
}
void gameClear()
{
//displayClear( COLOR_BG );
displayFillRect( 0, 21, videoWidth(), 38, COLOR_FG );
displayDrawBitmap( 13, 0, 74, 80, bmpLogo );
delay(5000);
gameReset();
displayClear( COLOR_BG );
displayDrawLine(0, 10, videoWidth(), 10, COLOR_FG);
displayDrawLine(0, 75, videoWidth(), 75, COLOR_FG);
//displayPrint("0", 2, 2, COLOR_FG, COLOR_BG);
//displayPrint("0", 88, 2, COLOR_FG, COLOR_BG);
//displayDrawBitmap(32, 1, 37, 8, bmpPaddleWar);
displayDrawBitmap(27, 1, 46, 8, bmpPaddleWar);
displayScore();
ballDx = (random(2) - 0.5) / 2;
ballDy = random(2) - 0.5;
hold = 100;
}
/**
* Check whether the paddle hits the ball and make it bounce off.
*/
void bouncePaddle(int paddleX)
{
int collision = ballX - paddleX + 4;
if (collision > 0 && collision < 16) {
// If the ball is hit reverse Y direction and determine
// X direction from where the ball hit the paddle
ballDy = -ballDy;
tone( PIN_SND_SPEAKER, 1200, 100);
if (collision < 4) {
ballDx = -0.5;
} else if (collision < 8) {
ballDx = -0.25;
} else if (collision < 12) {
ballDx = 0.25;
} else {
ballDx = 0.5;
}
}
}
/**
* Either the player or the Arduino missed the ball.
*/
void miss()
{
if( ballY <= 11 )
{
// Update player score. End game if 21.
p1Score ++;
tone( PIN_SND_SPEAKER , 600, 100);
displayScore();
if( p1Score == 21 )
{ gameEnd(true); }
} else {
// Update Arduino's score. End game if 21.
p2Score ++;
tone( PIN_SND_SPEAKER, 100, 500);
displayScore();
if (p2Score == 21)
{ gameEnd(false); }
}
// Set up next ball.
ballY = 42;
ballX = 48;
ballDx = (random(2) - 0.5) / 2;
hold = 100;
}
/**
* Show win or lose message when game ends and stop.
*/
void gameEnd(bool bHasWon)
{
/*
for (int i = 1; i < 180; i += 2)
{ displayDrawLine(0, i, i, 0, COLOR_BG); }
displayFillRect(20, 22, 60, 36, COLOR_BG);
displayDrawRect(20, 22, 60, 36, COLOR_FG);
//displayDrawBitmap(32, 25, 37, 8, bmpPaddleWar);
displayDrawBitmap(27, 0, 46, 8, bmpPaddleWar);
displayDrawLine(20, 35, 79, 35, COLOR_FG);
if( bHasWon )
{ displayPrint("You won!", 34, 43, COLOR_FG, COLOR_BG); }
else { displayPrint("You lose!", 32, 43, COLOR_FG, COLOR_BG); }
*/
displayClear( COLOR_BG );
displayFillRect( 0, 21, 100, 38, COLOR_FG );
displayDrawBitmap( 13, 0, 74, 80, bHasWon?bmpWon:bmpLose );
// Wait ten seconds
delay( 10000 );
// Reset game
gameClear();
}
int getPaddleValPerc()
{
int i = analogRead( PIN_CTRL_INPUT );
if( i >= PIN_CTRL_MIN )
{
if( i >= PIN_CTRL_MAX )
{ i=PIN_CTRL_MAX; }
i-=PIN_CTRL_MIN;
}
else { i=0; }
if( i > 0 )
{
i = (uint8_t)round( i * (100.0 / (PIN_CTRL_SCALE)) );
// allow only even values
if( i > 0 && i % 2 != 0 )
{ ++i; }
}
#if defined( PIN_CTRL_INVERT )
#if PIN_CTRL_INVERT == true
return 100-i;
#endif
#endif
return i;
}
int getPaddlePos()
{
int i = VAL_POS_MIN+( getPaddleValPerc() * (100.0 / VAL_POS_SCALE ) );
return i;
}
/**
* Initialize LCD and PaddleWar game.
*/
void setup()
{
// Serial setup
Serial.begin(9600);
// init the lcd
displayInit();
delay(1000);
// Set backlight
//pinMode( PIN_LCD_LED1 , OUTPUT );
//pinMode( PIN_LCD_LED2 , OUTPUT );
pinMode( PIN_CTRL_INPUT, INPUT_PULLUP );
// Maximum brightness
//analogWrite( PIN_LCD_LED1, 0xFF );
//analogWrite( PIN_LCD_LED2, 0xFF );
// Clear playfield and score
gameClear();
}
/**
* Main game loop.
*/
void loop()
{
// Update player paddle position and remove old sprites.
int newpaddleBottom = getPaddlePos();
if (paddleBottom != newpaddleBottom)
{
displayFillRect(paddleBottom, VAL_PAD_POSY, VAL_PAD_WIDTH, VAL_PAD_HEIGHT, COLOR_BG );
paddleBottom = newpaddleBottom;
}
// fillRect: x, y, w, h, c
displayFillRect((int)paddleTop, 12, VAL_PAD_WIDTH, VAL_PAD_HEIGHT, COLOR_BG );
displayFillRect((int)ballX, (int)ballY, 4, 4, COLOR_BG);
if (hold == 0)
{
// Move Arduino's paddle.
if (ballX < paddleTop + 5 && paddleTop > 0)
{ paddleTop -= 0.25; }
else {
if (ballX > paddleTop + 5 && paddleTop < 89)
{ paddleTop += 0.25; }
}
// Horizontal ball movement and bounce.
ballX += ballDx;
if (ballX >= 97)
{
ballX = 97;
ballDx = -ballDx;
tone( PIN_SND_SPEAKER, 1000, 100);
}
else {
if (ballX <= 0)
{
ballX = 0;
ballDx = -ballDx;
tone( PIN_SND_SPEAKER, 1000, 100);
}
}
// Vertical ball movement.
ballY += ballDy;
if (ballY > 68 && ballY < 71 && ballDy > 0)
{
// Bounce off player's paddle.
bouncePaddle(paddleBottom);
}
else {
if (ballY < 15 && ballY > 11 && ballDy < 0)
{
// Bounce off Arduino's paddle.
bouncePaddle(paddleTop);
} else {
if (ballY >= 71 || ballY <= 11)
{
// Player or Arduino missed the ball.
miss();
}
}
}
}
else { hold --; }
// Paint ball and paddles.
if (hold == 0)
{ displayDrawBall((int)ballX, (int)ballY, 4, 4 ); }
displayDrawPaddle((int)paddleTop, 12, 12, 3);
displayDrawPaddle(paddleBottom, 71, 12, 3);
delay(10);
}