-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTinyJoypadUtils.cpp
376 lines (332 loc) · 11.4 KB
/
TinyJoypadUtils.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
//
// 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;
// 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 );
#else
// 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
}
/*-------------------------------------------------------*/
bool isLeftPressed()
{
uint16_t inputX = analogRead( LEFT_RIGHT_BUTTON );
return( ( inputX >= ANALOG_UPPER_LIMIT_MIN ) && ( inputX < ANALOG_UPPER_LIMIT_MAX ) );
}
/*-------------------------------------------------------*/
bool isRightPressed()
{
uint16_t inputX = analogRead( LEFT_RIGHT_BUTTON );
return( ( inputX > ANALOG_LOWER_LIMIT_MIN ) && ( inputX < ANALOG_LOWER_LIMIT_MAX ) );
}
/*-------------------------------------------------------*/
bool isUpPressed()
{
uint16_t inputY = analogRead( UP_DOWN_BUTTON );
return( ( inputY > ANALOG_LOWER_LIMIT_MIN ) && ( inputY < ANALOG_LOWER_LIMIT_MAX ) );
}
/*-------------------------------------------------------*/
bool isDownPressed()
{
uint16_t inputY = analogRead( UP_DOWN_BUTTON );
return( ( inputY >= ANALOG_UPPER_LIMIT_MIN ) && ( inputY < ANALOG_UPPER_LIMIT_MAX ) );
}
/*-------------------------------------------------------*/
bool isFirePressed()
{
return( digitalRead( FIRE_BUTTON ) == 0 );
}
/*-------------------------------------------------------*/
// 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 );
}
/*-------------------------------------------------------*/
void __attribute__ ((noinline)) _variableDelay_us( uint8_t delayValue )
{
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 )
{
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
}
}
/*-------------------------------------------------------*/
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(;;);
}
#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
adafruitBuffer = display.getBuffer() + ( y * 128 );
#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 to the buffer
*adafruitBuffer++ = pixels;
#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
}