Clean up examples directory.

This commit is contained in:
2021-11-02 19:55:35 +00:00
parent d6e0648abf
commit 5c2a2736dc
7 changed files with 70 additions and 1 deletions

View File

@ -0,0 +1,31 @@
// A button box example using all of the button types.
// This is the code used in the Black Box project.
// TODO: add link to blog post, once it exists.
#include <Bounce2.h>
#include <Joystick.h>
Joystick joystick;
void setup() {
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
joystick.Init();
// 3 toggle switches that emit a press every time they are toggled.
// (in either direction)
joystick.AddButton(2, BUTTON_PULSED_DOUBLE_ACTION);
joystick.AddButton(3, BUTTON_PULSED_DOUBLE_ACTION);
joystick.AddButton(4, BUTTON_PULSED_DOUBLE_ACTION);
// This button will stay 'held down' as long as the switch is on.
joystick.AddButton(6, BUTTON_LATCHED);
// This button will emit a short press every time it is pushed, but
// only one per push.
joystick.AddButton(8, BUTTON_PULSED);
}
void loop () {
joystick.Update();
}

View File

@ -0,0 +1,59 @@
// An example sketch using the joystick library.
// 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 <Bounce2.h>
#include <Joystick.h>
bool debug = false;
Joystick joystick(debug);
// Number of connected buttons.
int num_buttons = 5;
// Last known state of each pin.
int states[10] = {-1, -1, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW};
void parsePins(bool init=false) {
// Input: figure out which pins have changed.
bool something_pressed = false;
for (int i = 2; i < 2 + num_buttons; i++) {
int newstate = digitalRead(i);
if (newstate != states[i]) {
states[i] = newstate;
if (!init) {
joystick.PressButton(i-2);
something_pressed = true;
}
}
}
// Output: Actually press the appropriate buttons, wait, then clear them all.
if (something_pressed) {
joystick.Write();
joystick.ReleaseAllButtons();
joystick.Write();
}
}
void InitInputs() {
for (int i = 2; i < 2 + num_buttons; i++) {
pinMode(i, INPUT_PULLUP);
}
parsePins(true);
}
void setup() {
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
joystick.Init();
InitInputs();
}
void loop() {
parsePins();
if (debug) delay(900);
}

View File

@ -0,0 +1,23 @@
// This is the same 5-toggle-switch example from switch_panel.ino, using the
// new API. So much easier!
#include <Bounce2.h>
#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();
}