Add an equivalent example using the new Joystick API. Also add pin initializing code to Joystick::AddButton()

This commit is contained in:
Anna Rose 2015-11-06 12:07:37 -05:00
parent c2771502e3
commit f2d7f48d43
4 changed files with 30 additions and 2 deletions

View File

@ -21,7 +21,10 @@ void Joystick::Init() {
if (_debug) Serial.println("DEBUG: Joystick library initialized."); if (_debug) Serial.println("DEBUG: Joystick library initialized.");
} }
void Joystick::AddButton(uint8_t pin, ButtonType type) { void Joystick::AddButton(uint8_t pin, ButtonType type, bool pullup) {
if (pullup) pinMode(pin, INPUT_PULLUP);
else pinMode(pin, INPUT);
_buttons[_num_buttons].pin = pin; _buttons[_num_buttons].pin = pin;
_buttons[_num_buttons].type = type; _buttons[_num_buttons].type = type;
_buttons[_num_buttons].last_state = digitalRead(pin); _buttons[_num_buttons].last_state = digitalRead(pin);

View File

@ -31,7 +31,7 @@ class Joystick {
void Init(); void Init();
void Update(); void Update();
void AddButton(uint8_t pin, ButtonType type); void AddButton(uint8_t pin, ButtonType type, bool pullup=false);
void AddAxis(uint8_t pin); void AddAxis(uint8_t pin);
// These functions are deprecated and may become private in a future // These functions are deprecated and may become private in a future

View File

@ -2,6 +2,9 @@
// In this example, we have 5 toggle switches that are configured // In this example, we have 5 toggle switches that are configured
// as double-action pulsed buttons - that is, whenever they are toggled, they // as double-action pulsed buttons - that is, whenever they are toggled, they
// emit a short button press. // emit a short button press.
//
// This example uses the *old* Joystick API. You should strongly prefer the
// other, better examples in this directory!
#include <Joystick.h> #include <Joystick.h>

View File

@ -0,0 +1,22 @@
// This is the same 5-toggle-switch example from switch_panel.ino, using the
// new API. So much easier!
#include <Joystick.h>
Joystick joystick;
void setup() {
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
joystick.Init();
joystick.AddButton(2, BUTTON_PULSED_DOUBLE_ACTION, true);
joystick.AddButton(3, BUTTON_PULSED_DOUBLE_ACTION, true);
joystick.AddButton(4, BUTTON_PULSED_DOUBLE_ACTION, true);
joystick.AddButton(5, BUTTON_PULSED_DOUBLE_ACTION, true);
joystick.AddButton(6, BUTTON_PULSED_DOUBLE_ACTION, true);
}
void loop () {
joystick.Update();
}