-
Notifications
You must be signed in to change notification settings - Fork 0
/
TinyArcade.h
99 lines (84 loc) · 3.51 KB
/
TinyArcade.h
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
/*
TinyArcade.h - Last modified 6 September 2016
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Written by Ben Rose for TinyCircuits.
The latest version of this library can be found at https://tinycircuits.com/
*/
#ifndef TINYARCADE_H
#define TINYARCADE_H
#define TinyArcadePin1 45 //Left Button
#define TinyArcadePin2 44 //Right Button
#define TinyArcadePinX 42 //Analog Joystick x-axis
#define TinyArcadePinY 1 //Analog Joystick y-axis
#define TinyArcadePinUp 42 //Joystick UP
#define TinyArcadePinDown 19 //Joystick DOWN
#define TinyArcadePinLeft 25 //Joystick LEFT
#define TinyArcadePinRight 15 //Joystick RIGHT
const uint8_t TAJoystickUp = 1 << 0; //Mask
const uint8_t TAJoystickDown = 1 << 1; //Mask
const uint8_t TAJoystickLeft = 1 << 2; //Mask
const uint8_t TAJoystickRight = 1 << 3; //Mask
const uint8_t TAButton1 = 1 << 0; //Mask
const uint8_t TAButton2 = 1 << 1; //Mask
uint8_t arcadeVersion = 1;
void arcadeInit() {
pinMode(4, INPUT_PULLUP);
if (!digitalRead(4)) {
arcadeVersion = 2;
}
analogWrite(A0, analogRead(A0));//Set audio output to avoid picking up noise, even though audio isn't used
pinMode(TinyArcadePin2, INPUT_PULLUP);
pinMode(TinyArcadePin1, INPUT_PULLUP);
if (arcadeVersion == 2) {
pinMode(TinyArcadePinUp, INPUT_PULLUP);
pinMode(TinyArcadePinDown, INPUT_PULLUP);
pinMode(TinyArcadePinLeft, INPUT_PULLUP);
pinMode(TinyArcadePinRight, INPUT_PULLUP);
}
}
uint8_t checkButton(uint8_t btn) {
uint8_t rVal = 0;
if ((btn & TAButton1) && !digitalRead(TinyArcadePin1))
rVal |= TAButton1;
if ((btn & TAButton2) && !digitalRead(TinyArcadePin2))
rVal |= TAButton2;
return rVal;
}
uint8_t checkJoystick(uint8_t joystickDir) {
if (arcadeVersion == 1) {
int analogVal = analogRead(TinyArcadePinY);
uint8_t rVal = 0;
if ((joystickDir & TAJoystickUp) && analogVal < 512 - 250)
rVal |= TAJoystickUp;
else if ((joystickDir & TAJoystickDown) && analogVal > 512 + 250)
rVal |= TAJoystickDown;
analogVal = analogRead(TinyArcadePinX);
if ((joystickDir & TAJoystickLeft) && analogVal > 512 + 250)
rVal |= TAJoystickLeft;
else if ((joystickDir & TAJoystickRight) && analogVal < 512 - 250)
rVal |= TAJoystickRight;
return rVal;
} else if (arcadeVersion == 2) {
uint8_t rVal = 0;
if ((joystickDir & TAJoystickUp) && !digitalRead(TinyArcadePinUp))
rVal |= TAJoystickUp;
if ((joystickDir & TAJoystickDown) && !digitalRead(TinyArcadePinDown))
rVal |= TAJoystickDown;
if ((joystickDir & TAJoystickLeft) && !digitalRead(TinyArcadePinLeft))
rVal |= TAJoystickLeft;
if ((joystickDir & TAJoystickRight) && !digitalRead(TinyArcadePinRight))
rVal |= TAJoystickRight;
return rVal;
}
}
#endif