From f2d7f48d4368e8891062f08dedb5a2748908d8a2 Mon Sep 17 00:00:00 2001 From: Anna Wiggins Date: Fri, 6 Nov 2015 12:07:37 -0500 Subject: [PATCH] Add an equivalent example using the new Joystick API. Also add pin initializing code to Joystick::AddButton() --- Joystick.cpp | 5 ++++- Joystick.h | 2 +- examples/switch_panel.ino | 3 +++ examples/switch_panel_improved.ino | 22 ++++++++++++++++++++++ 4 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 examples/switch_panel_improved.ino diff --git a/Joystick.cpp b/Joystick.cpp index c518999..8b4c30a 100644 --- a/Joystick.cpp +++ b/Joystick.cpp @@ -21,7 +21,10 @@ void Joystick::Init() { 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].type = type; _buttons[_num_buttons].last_state = digitalRead(pin); diff --git a/Joystick.h b/Joystick.h index 7f7ab07..b97e542 100644 --- a/Joystick.h +++ b/Joystick.h @@ -31,7 +31,7 @@ class Joystick { void Init(); void Update(); - void AddButton(uint8_t pin, ButtonType type); + void AddButton(uint8_t pin, ButtonType type, bool pullup=false); void AddAxis(uint8_t pin); // These functions are deprecated and may become private in a future diff --git a/examples/switch_panel.ino b/examples/switch_panel.ino index 72867d1..5dff3ec 100644 --- a/examples/switch_panel.ino +++ b/examples/switch_panel.ino @@ -2,6 +2,9 @@ // In this example, we have 5 toggle switches that are configured // as double-action pulsed buttons - that is, whenever they are toggled, they // emit a short button press. +// +// This example uses the *old* Joystick API. You should strongly prefer the +// other, better examples in this directory! #include diff --git a/examples/switch_panel_improved.ino b/examples/switch_panel_improved.ino new file mode 100644 index 0000000..cc36a30 --- /dev/null +++ b/examples/switch_panel_improved.ino @@ -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 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(); +}