-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtinyJoypadUtils.cpp
More file actions
575 lines (523 loc) · 15.9 KB
/
tinyJoypadUtils.cpp
File metadata and controls
575 lines (523 loc) · 15.9 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
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
//
// This file works for TinyJoypad compatible devices.
//
// If not compiled for ATTiny85 (meaning __AVR_ATtiny85__ is not defined),
// generic functions are used instead of direct port access, which
// makes it possible to use an Arduino or Mega2560 (or many others)
// for debugging with serial output or even hardware breakpoints.
//
#include <Arduino.h>
#include "TinyJoypadUtils.h"
#if defined(__AVR_ATtiny85__)
#include <ssd1306xled.h>
#else
// include Adafruit library and immediately create an object
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 display( 128, 64, &Wire, -1 );
uint8_t *_adafruitBuffer;
uint8_t _column{0};
uint8_t _row{0};
// flag if vertical addressing mode is enabled
bool _verticalAddressingModeEnabled{false};
// these functions are only required if a screenshot should be printed as a hexdump to the serial port
#ifdef _ENABLE_SERIAL_SCREENSHOT_
// include serial output functions
#include "SerialHexTools.h"
#endif
#endif
// buffered analog joystick inputs
uint16_t _analogJoystickX;
uint16_t _analogJoystickY;
/*-------------------------------------------------------*/
// function for initializing the TinyJoypad (ATtiny85) and other microcontrollers
void InitTinyJoypad()
{
#if defined(__AVR_ATtiny85__)
// not using 'pinMode()' here saves ~100 bytes of flash!
// configure A0, A3 and D1 as input
SOUND_PORT_DDR &= ~( ( 1 << PB5) | ( 1 << PB3 ) | ( 1 << PB1 ) );
// configure A2 (aka SOUND_PIN) as output
SOUND_PORT_DDR |= ( 1 << SOUND_PIN );
#elif !defined( USE_KEYBOARD_INPUT )
// use 'pinMode()' for simplicity's sake... any other micro controller has enough flash :)
pinMode( LEFT_RIGHT_BUTTON, INPUT );
pinMode( UP_DOWN_BUTTON, INPUT );
pinMode( FIRE_BUTTON, INPUT );
// configure SOUND_PIN as output (Pin D12 on Arduino UNO R3 and Pin D10 on Arduino Mega 2560 )
pinMode( SOUND_PIN, OUTPUT );
// prepare serial port for debugging output
Serial.begin( 115200 );
#endif
}
#ifdef USE_KEYBOARD_INPUT
bool leftKeyPressed{ false };
bool rightKeyPressed{ false };
bool upKeyPressed{ false };
bool downKeyPressed{ false };
bool actionKeyPressed{ false };
/*-------------------------------------------------------*/
void handleKeys()
{
if (_kbhit())
{
auto key = _getch();
switch( key )
{
case 'a':
case 'A':
leftKeyPressed = true;
break;
case 'd':
case 'D':
rightKeyPressed = true;
break;
case 'w':
case 'W':
upKeyPressed = true;
break;
case 's':
case 'S':
downKeyPressed = true;
break;
case ' ':
actionKeyPressed = true;
break;
default:
break;
// cursor keys
case 224:
key = _getch();
switch (key)
{
case 75:
leftKeyPressed = true;
break;
case 77:
rightKeyPressed = true;
break;
case 72:
upKeyPressed = true;
break;
case 80:
downKeyPressed = true;
break;
default:
break;
}
}
}
}
#endif
/*-------------------------------------------------------*/
bool isLeftPressed()
{
#ifdef USE_KEYBOARD_INPUT
handleKeys();
auto key = leftKeyPressed;
leftKeyPressed = false;
return( key );
#else
uint16_t inputX = analogRead( LEFT_RIGHT_BUTTON );
return( ( inputX >= ANALOG_UPPER_LIMIT_MIN ) && ( inputX < ANALOG_UPPER_LIMIT_MAX ) );
#endif
}
/*-------------------------------------------------------*/
bool isRightPressed()
{
#ifdef USE_KEYBOARD_INPUT
handleKeys();
auto key = rightKeyPressed;
rightKeyPressed = false;
return( key );
#else
uint16_t inputX = analogRead( LEFT_RIGHT_BUTTON );
return( ( inputX > ANALOG_LOWER_LIMIT_MIN ) && ( inputX < ANALOG_LOWER_LIMIT_MAX ) );
#endif
}
/*-------------------------------------------------------*/
bool isUpPressed()
{
#ifdef USE_KEYBOARD_INPUT
handleKeys();
auto key = upKeyPressed;
upKeyPressed = false;
return(key);
#else
uint16_t inputY = analogRead( UP_DOWN_BUTTON );
return( ( inputY > ANALOG_LOWER_LIMIT_MIN ) && ( inputY < ANALOG_LOWER_LIMIT_MAX ) );
#endif
}
/*-------------------------------------------------------*/
bool isDownPressed()
{
#ifdef USE_KEYBOARD_INPUT
handleKeys();
auto key = downKeyPressed;
downKeyPressed = false;
return(key);
#else
uint16_t inputY = analogRead( UP_DOWN_BUTTON );
return( ( inputY >= ANALOG_UPPER_LIMIT_MIN ) && ( inputY < ANALOG_UPPER_LIMIT_MAX ) );
#endif
}
/*-------------------------------------------------------*/
bool isFirePressed()
{
#ifdef USE_KEYBOARD_INPUT
handleKeys();
auto key = actionKeyPressed;
actionKeyPressed = false;
return( key );
#else
return( digitalRead( FIRE_BUTTON ) == 0 );
#endif
}
/*-------------------------------------------------------*/
// wait until all buttons are released
void waitUntilButtonsReleased()
{
while( isLeftPressed() || isRightPressed() || isUpPressed() || isDownPressed() || isFirePressed() );
}
/*-------------------------------------------------------*/
// wait until all buttons are released and wait a little delay
void waitUntilButtonsReleased( const uint8_t delayTime )
{
waitUntilButtonsReleased();
_delay_ms( delayTime );
}
/*-------------------------------------------------------*/
// read analog joystick inputs into internal variables
void readAnalogJoystick()
{
_analogJoystickX = analogRead( LEFT_RIGHT_BUTTON );
_analogJoystickY = analogRead( UP_DOWN_BUTTON );
}
/*-------------------------------------------------------*/
bool wasLeftPressed()
{
return( ( _analogJoystickX >= ANALOG_UPPER_LIMIT_MIN ) && ( _analogJoystickX < ANALOG_UPPER_LIMIT_MAX ) );
}
/*-------------------------------------------------------*/
bool wasRightPressed()
{
return( ( _analogJoystickX > ANALOG_LOWER_LIMIT_MIN ) && ( _analogJoystickX < ANALOG_LOWER_LIMIT_MAX ) );
}
/*-------------------------------------------------------*/
bool wasUpPressed()
{
return( ( _analogJoystickY > ANALOG_LOWER_LIMIT_MIN ) && ( _analogJoystickY < ANALOG_LOWER_LIMIT_MAX ) );
}
/*-------------------------------------------------------*/
bool wasDownPressed()
{
return( ( _analogJoystickY >= ANALOG_UPPER_LIMIT_MIN ) && ( _analogJoystickY < ANALOG_UPPER_LIMIT_MAX ) );
}
/*-------------------------------------------------------*/
uint16_t getAnalogValueX()
{
return( _analogJoystickX );
}
/*-------------------------------------------------------*/
uint16_t getAnalogValueY()
{
return( _analogJoystickY );
}
/*-------------------------------------------------------*/
#if defined(__AVR_ATtiny85__) /* codepath for ATtiny85 */
void __attribute__ ((noinline)) _variableDelay_us( uint8_t delayValue )
#else
void _variableDelay_us(uint8_t delayValue)
#endif
{
while ( delayValue-- != 0 )
{
_delay_us( 1 );
}
}
/*-------------------------------------------------------*/
// This code was originaly borrowed from Daniel C's Tiny-invaders :)
// Code optimization by sbr
void Sound( const uint8_t freq, const uint8_t dur )
{
#if !defined( NO_SOUND )
for ( uint8_t t = 0; t < dur; t++ )
{
#if defined(__AVR_ATtiny85__) /* codepath for ATtiny85 */
if ( freq != 0 ){ SOUND_PORT = SOUND_PORT | ( 1 << SOUND_PIN); }
_variableDelay_us( 255 - freq );
SOUND_PORT = SOUND_PORT & ~( 1 << SOUND_PIN );
_variableDelay_us( 255 - freq );
#else
if ( freq != 0 ){ digitalWrite( SOUND_PIN, 1 ); }
_variableDelay_us( 255 - freq );
digitalWrite( SOUND_PIN, 0 );
_variableDelay_us( 255 - freq );
#endif
}
#endif
}
/*-------------------------------------------------------*/
void InitDisplay()
{
#if defined(__AVR_ATtiny85__) /* codepath for ATtiny85 */
#if defined( _SSD1306XLED_TINY_INIT_SUPPORTED_ )
// library supports shorter init method
SSD1306.ssd1306_tiny_init();
#else
// use standard init method
SSD1306.ssd1306_init();
#endif
#else
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
// Address 0x3D for 128x64
if( !display.begin(SSD1306_SWITCHCAPVCC, 0x3C))
{
// extended the error message
Serial.println(F("SSD1306 allocation failed - 1024 bytes for frame buffer required!")); for(;;);
}
// reset display coordinates
_column = 0;
_row = 0;
// get raw image buffer
_adafruitBuffer = display.getBuffer();
#endif
}
/*-------------------------------------------------------*/
void EnableVerticalAddressingMode()
{
#if defined(__AVR_ATtiny85__) /* codepath for ATtiny85 */
SSD1306.ssd1306_send_command_start();
SSD1306.ssd1306_send_byte( 0x20 ); SSD1306.ssd1306_send_byte( 0x01 );
SSD1306.ssd1306_send_byte( 0x21 ); SSD1306.ssd1306_send_byte( 0x00 ); SSD1306.ssd1306_send_byte( 0x7f );
SSD1306.ssd1306_send_byte( 0x22 ); SSD1306.ssd1306_send_byte( 0x00 ); SSD1306.ssd1306_send_byte( 0x07 );
SSD1306.ssd1306_send_command_stop();
#else
_verticalAddressingModeEnabled = true;
// reset display coordinates
_column = 0;
_row = 0;
#endif
}
/*-------------------------------------------------------*/
void DisableVerticalAddressingMode()
{
#if defined(__AVR_ATtiny85__) /* codepath for ATtiny85 */
SSD1306.ssd1306_send_command_start();
SSD1306.ssd1306_send_byte( 0x20 ); SSD1306.ssd1306_send_byte( 0x00 );
SSD1306.ssd1306_send_byte( 0x21 ); SSD1306.ssd1306_send_byte( 0x00 ); SSD1306.ssd1306_send_byte( 0x7f );
SSD1306.ssd1306_send_byte( 0x22 ); SSD1306.ssd1306_send_byte( 0x00 ); SSD1306.ssd1306_send_byte( 0x07 );
SSD1306.ssd1306_send_command_stop();
#else
_verticalAddressingModeEnabled = false;
// reset display coordinates
_column = 0;
_row = 0;
#endif
}
/*-------------------------------------------------------*/
// This code will init the display for row <y>
void PrepareDisplayRow( uint8_t y )
{
#if defined(__AVR_ATtiny85__) /* codepath for ATtiny85 */
// initialize image transfer to segment 'y'
SSD1306.ssd1306_send_command(0xb0 + y);
#ifdef _USE_SH1106_
// SH1106 internally uses 132 pixels/line,
// output is (always?) centered, so we need to start at position 2
SSD1306.ssd1306_send_command(0x02);
SSD1306.ssd1306_send_command(0x10);
#else
// classic SSD1306 supports only 128 pixels/line, so we start at 0
SSD1306.ssd1306_send_command(0x00);
SSD1306.ssd1306_send_command(0x10);
#endif
SSD1306.ssd1306_send_data_start();
#else /* codepath for any Adafruit_SSD1306 supported MCU */
// address the display buffer
_row = y;
#endif
}
/*-------------------------------------------------------*/
void StartSendPixels()
{
#if defined(__AVR_ATtiny85__) /* codepath for ATtiny85 */
SSD1306.ssd1306_send_data_start();
#endif
}
#if !defined(__AVR_ATtiny85__) /* codepath for ATtiny85 */
/*-------------------------------------------------------*/
// writes the pixles and handles the addressing of columns and rows
void writePixelsToAdafruitBuffer( uint8_t pixels )
{
_adafruitBuffer[_column + _row * 128] = pixels;
if ( _verticalAddressingModeEnabled )
{
_row++;
if ( _row > 7 )
{
_row = 0;
_column++;
if ( _column > 127 )
{
_column = 0;
}
}
}
else
{
_column++;
if ( _column > 127 )
{
_column = 0;
_row++;
if ( _row > 7 )
{
_row = 0;
}
}
}
}
#endif
/*-------------------------------------------------------*/
void SendPixels( uint8_t pixels )
{
#if defined(__AVR_ATtiny85__) /* codepath for ATtiny85 */
// send a byte directly to the SSD1306
SSD1306.ssd1306_send_byte( pixels );
#else /* codepath for any Adafruit_SSD1306 supported MCU */
// write pixels directly into the buffer
writePixelsToAdafruitBuffer( pixels );
#endif
}
/*-------------------------------------------------------*/
void StopSendPixels()
{
#if defined(__AVR_ATtiny85__) /* codepath for ATtiny85 */
SSD1306.ssd1306_send_data_stop();
#endif
}
/*-------------------------------------------------------*/
// This code will finish a row (only on Tiny85)
void FinishDisplayRow()
{
#if defined(__AVR_ATtiny85__)
// this line appears to be optional, as it was never called during the intro screen...
// but hey, we still have some bytes left ;)
SSD1306.ssd1306_send_data_stop();
#endif
}
/*-------------------------------------------------------*/
void DisplayBuffer()
{
#if !defined(__AVR_ATtiny85__) /* codepath for any Adafruit_SSD1306 supported MCU */
// display buffer (not necessary)
display.display();
// slow down fast microcontrollers
#if defined(_VARIANT_ARDUINO_ZERO_)
// wait 100ms to compensate for extremely fast hardware i2c
delay( 100 );
#endif
#ifndef _SERIAL_SCREENSHOT_NO_AUTO_SHOT_
// check for screenshot request
CheckForSerialScreenshot();
#endif
#endif
}
/*-------------------------------------------------------*/
// Output is one hex byte per pixel. To get the actual image perform the following steps:
// (1) The output can be converted to binary with 'https://tomeko.net/online_tools/hex_to_file.php?lang=en' online.
// (2) Then import the file with IrfanView (https://www.irfanview.com/): Open as -> RAW file...
// (3) Set Image width to 64 and Image height to 128, 8 BPP -> OK
// (4) Rotate and mirror the result as needed :)
void SerialScreenshot()
{
#if !defined(__AVR_ATtiny85__) /* codepath for any Adafruit_SSD1306 supported MCU */
#ifdef _ENABLE_SERIAL_SCREENSHOT_
// print a short header
Serial.println( F("\r\nThis is a TinyJoypad screenshot. Output is one hex byte per pixel. To get the actual image perform the following steps:") );
Serial.println( F("(1) The output can be converted to binary with 'https://tomeko.net/online_tools/hex_to_file.php?lang=en' online.") );
Serial.println( F("(2) Then import the file with IrfanView (https://www.irfanview.com/): Open as -> RAW file...") );
Serial.println( F("(3) Set Image width to 64 and Image height to 128, 8 BPP -> OK") );
Serial.println( F("(4) Rotate and mirror the result as needed :)\r\n") );
Serial.println( F("Hint: If you only get partial screenshots, try using a terminal program to capture the serial output.") );
// output the full buffer as a hexdump to the serial port
printScreenBufferToSerial( display.getBuffer(), 128, 8 );
#endif
#endif
}
/*-------------------------------------------------------*/
// Perform a screenshot if
// [x] enabled and
// [x] trigger condition met
void CheckForSerialScreenshot()
{
#if !defined(__AVR_ATtiny85__) /* codepath for any Adafruit_SSD1306 supported MCU */
#ifdef _ENABLE_SERIAL_SCREENSHOT_
if ( _SERIAL_SCREENSHOT_TRIGGER_CONDITION_ )
{
// perform the screenshot
SerialScreenshot();
}
#endif
#endif
}
///////////////////////////////////////////////////////////////////////////////////
// serial output without clustering the code with #if !defined(__AVR_ATtiny85__)...
/*-------------------------------------------------------*/
void serialPrint( const char *text )
{
#ifdef USE_SERIAL_PRINT
Serial.print( text );
#endif
}
/*-------------------------------------------------------*/
void serialPrintln( const char *text )
{
#ifdef USE_SERIAL_PRINT
Serial.println( text );
#endif
}
/*-------------------------------------------------------*/
void serialPrint( const __FlashStringHelper *text )
{
#ifdef USE_SERIAL_PRINT
Serial.print( text );
#endif
}
/*-------------------------------------------------------*/
void serialPrintln( const __FlashStringHelper *text )
{
#ifdef USE_SERIAL_PRINT
Serial.println( text );
#endif
}
/*-------------------------------------------------------
void serialPrint( const unsigned int number )
{
#ifdef USE_SERIAL_PRINT
Serial.print( number );
#endif
}
*/
/*-------------------------------------------------------
void serialPrintln( const unsigned int number )
{
#ifdef USE_SERIAL_PRINT
Serial.println( number );
#endif
}
*/
/*-------------------------------------------------------*/
void serialPrint( const int number )
{
#ifdef USE_SERIAL_PRINT
Serial.print( number );
#endif
}
/*-------------------------------------------------------*/
void serialPrintln( const int number )
{
#ifdef USE_SERIAL_PRINT
Serial.println( number );
#endif
}