-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ardwiino.ino
126 lines (113 loc) · 3.46 KB
/
Ardwiino.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
/*
* Project Ardwiino
* @author Sebastiaan
* @link /github.com/whitebird/ardwiino
* @license LGPLv3 - Copyright (c) 2018 Sebastiaan Jansen
*
* This file is part of the Ardwiino software.
*
* This program 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 3 of the License, or
* (at your option) any later version.
*
* This program 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 program. If not, see <http://www.gnu.org/licenses/>.
*
* Description: Wii accessories as native game controller
* Based on the Nintendo Extension Controller Library by David Madison
* and ased on the ArduinoJoystickLibrary by Matthew Heironimus
*/
#include <Joystick.h>
#include <NintendoExtensionCtrl.h>
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_GAMEPAD,
7, 1, // Button Count, Hat Switch Count
true, false, false, // X and Y, but no Z Axis
false, false, false, // No Rx, Ry, or Rz
false, false, // No rudder or throttle
false, false, false); // No accelerator, brake, or steering
GuitarController guitar;
const int joyMin = 0;
const int joyMax = 63;
const int deadzone = 15;
const int joyLow = joyMin + deadzone;
const int joyHigh = joyMax - deadzone;
void setup()
{
Serial.begin(115200);
guitar.begin();
Joystick.begin();
// Whammy range, about 15 - 27
Joystick.setXAxisRange(15, 27);
while (!guitar.connect())
{
Serial.println("Guitar controller not detected!");
delay(1000);
}
}
void loop()
{
boolean success = guitar.update(); // Get new data from the controller
Serial.println(success);
if (!success)
{ // Ruh roh
Serial.println("Controller disconnected!");
delay(1000);
}
else
{
Joystick.setButton(0, guitar.fretGreen());
Joystick.setButton(1, guitar.fretRed());
Joystick.setButton(2, guitar.fretYellow());
Joystick.setButton(3, guitar.fretBlue());
Joystick.setButton(4, guitar.fretOrange());
Joystick.setButton(5, guitar.buttonPlus());
Joystick.setButton(6, guitar.buttonMinus());
// Read the whammy bar (0-31, starting around 15-16)
uint8_t whammy = guitar.whammyBar();
Joystick.setXAxis(whammy);
// Read the joystick axis (0-63 XY)
int joyX = guitar.joyX();
int joyY = guitar.joyY();
bool joystickUsed = true;
if (joyX < joyLow)
{ // down
Joystick.setHatSwitch(0, 90);
}
else if (joyY < joyLow)
{ // right
Joystick.setHatSwitch(0, 0);
}
else if (joyX > joyHigh)
{ // up
Joystick.setHatSwitch(0, 270);
}
else if (joyY > joyHigh)
{ // left
Joystick.setHatSwitch(0, 180);
}
else
{ // none
joystickUsed = false;
}
if (guitar.strumUp())
{
Joystick.setHatSwitch(0, 0);
}
else if (guitar.strumDown())
{
Joystick.setHatSwitch(0, 180);
}
else if (!joystickUsed)
{
Joystick.setHatSwitch(0, -1);
}
// Print all the values!
// guitar.printDebug();
}
}