Bug fixes.

This commit is contained in:
Anna Rose 2021-11-24 05:40:28 +00:00
parent 690afdbce5
commit a9c80c2ed5
4 changed files with 33 additions and 28 deletions

View File

@ -38,6 +38,7 @@ Joystick::Joystick(bool debug) {
void Joystick::Init() { void Joystick::Init() {
Serial.begin(115200); Serial.begin(115200);
delay(100); delay(100);
if (_debug) Serial.println("Joystick serial communication initialized.");
} }
void Joystick::AddButton(uint8_t pin, ButtonType type, bool pullup) { void Joystick::AddButton(uint8_t pin, ButtonType type, bool pullup) {
@ -68,6 +69,8 @@ void Joystick::AddEncoder(uint8_t pin1, uint8_t pin2, ButtonType type) {
Serial.println(type); Serial.println(type);
} }
} }
if (_debug) Serial.println("Added an encoder.");
} }
void Joystick::AddAxis(uint8_t pin) { void Joystick::AddAxis(uint8_t pin) {
@ -165,6 +168,10 @@ void Joystick::_addButton(ButtonType type, Reader* reader) {
button = new PulsedButton(_virtual_buttons, reader, true, true); button = new PulsedButton(_virtual_buttons, reader, true, true);
_virtual_buttons += 2; _virtual_buttons += 2;
break; break;
case BUTTON_LATCHED_MOMENTARY:
button = new LatchedButton(_virtual_buttons, reader);
_virtual_buttons++;
break;
default: default:
return; return;
} }

View File

@ -1,25 +1,19 @@
#include "Matrix.h" #include "Matrix.h"
Matrix::Matrix(uint8_t* columns, bool inverted) { Matrix::Matrix(uint8_t* columns, uint8_t num_columns, bool inverted) {
this->columns = columns; this->active_pin = 255; // sentinel value, highest possible 8-bit number
this->num_columns = sizeof(columns);
this->active_pin = num_columns;
this->inverted = inverted; this->inverted = inverted;
for (uint8_t i = 0; i < num_columns; i++) { for (uint8_t i = 0; i < num_columns; i++) {
pinMode(columns[i], OUTPUT); pinMode(columns[i], OUTPUT);
if (inverted) digitalWrite(columns[i], HIGH); _disable(columns[i]);
else digitalWrite(columns[i], LOW);
} }
} }
void Matrix::Activate(uint8_t column) { void Matrix::Activate(uint8_t pin) {
if (column > num_columns) return; // TODO: throw an error here?
uint8_t pin = columns[column];
if (pin == active_pin) return; if (pin == active_pin) return;
if (active_pin != num_columns) { if (active_pin != 255) {
_disable(active_pin); _disable(active_pin);
} }

View File

@ -8,16 +8,14 @@
class Matrix { class Matrix {
public: public:
Matrix(uint8_t *columns, bool inverted=true); Matrix(uint8_t *columns, uint8_t num_columns, bool inverted=true);
void Activate(uint8_t column); void Activate(uint8_t column);
private: private:
void _enable(uint8_t pin); void _enable(uint8_t pin);
void _disable(uint8_t pin); void _disable(uint8_t pin);
uint8_t* columns;
uint8_t active_pin; uint8_t active_pin;
uint8_t num_columns;
bool inverted; bool inverted;
}; };

View File

@ -1,5 +1,6 @@
// An example sketch using the joystick library, demonstrating recommended usage // An example sketch using the joystick library, demonstrating recommended usage
// of all features. // of all features.
// This code loads the sketch in debug mode. Connect to the Arduino's serial port at 115200 baud to read the output.
#include <Joystick.h> #include <Joystick.h>
#include <Matrix.h> #include <Matrix.h>
@ -18,17 +19,16 @@ using namespace admux;
#define BTN3 6 #define BTN3 6
#define BTN4 7 #define BTN4 7
// pins 8-13 are attached to a 3x3 scanning input matrix. Pins 8-10 are the row pins, 11-13 are the column pins // pins 8-13 are attached to a 3x3 scanning input matrix. Pins 8-10 are the row pins, 11-13 are the column pins.
// However, note that the column pins are 0-indexed; the actual pins are defined in the Matrix object later. #define MATRIX1 8,11
#define MATRIX1 8,0 #define MATRIX2 9,11
#define MATRIX2 9,0 #define MATRIX3 10,11
#define MATRIX3 10,0 #define MATRIX4 8,12
#define MATRIX4 8,1 #define MATRIX5 9,12
#define MATRIX5 9,1 #define MATRIX6 10,12
#define MATRIX6 10,1 #define MATRIX7 8,13
#define MATRIX7 8,2 #define MATRIX8 9,13
#define MATRIX8 9,2 #define MATRIX9 10,13
#define MATRIX9 10,2
// A multiplexer is attached to pins A1-A5 // A multiplexer is attached to pins A1-A5
#define MUX_SIG A1 // the multiplexer's signal pin is on Analog 1 #define MUX_SIG A1 // the multiplexer's signal pin is on Analog 1
@ -40,14 +40,18 @@ using namespace admux;
#define MUX_BTN3 2 #define MUX_BTN3 2
bool debug = false; bool debug = true;
Joystick js(debug); Joystick js(debug);
void setup() { void setup() {
js.Init();
Serial.println("Adding encoder.");
js.AddEncoder(ENCODER, ENCODER_PULSED_SPLIT); js.AddEncoder(ENCODER, ENCODER_PULSED_SPLIT);
// Different types of button programming are available. BUTTON_PASSTHRU simply passes on the button's // Different types of button programming are available. BUTTON_PASSTHRU simply passes on the button's
// real state, and will be the most common, but you can also change how the button works in software like so... // real state, and will be the most common, but you can also change how the button works in software like so...
Serial.println("Adding directly connected buttons.");
// With BUTTON_PULSED, no matter how long the button stays held down, it will only send a button press for 250ms // With BUTTON_PULSED, no matter how long the button stays held down, it will only send a button press for 250ms
// then deactivate until pressed again. // then deactivate until pressed again.
@ -69,8 +73,9 @@ void setup() {
// See http://blog.komar.be/how-to-make-a-keyboard-the-matrix/ for a very detailed discussion of how these work. // See http://blog.komar.be/how-to-make-a-keyboard-the-matrix/ for a very detailed discussion of how these work.
// This is a 3x3 example, so we can get 9 buttons out of 6 inputs. // This is a 3x3 example, so we can get 9 buttons out of 6 inputs.
// For faster read results, always add buttons so that buttons with the same *column* are grouped together. // For faster read results, always add buttons so that buttons with the same *column* are grouped together.
Serial.println("Adding matrix.");
uint8_t cols[] = {11, 12, 13}; uint8_t cols[] = {11, 12, 13};
Matrix* matrix = new Matrix(cols); Matrix* matrix = new Matrix(cols, 3);
js.AddMatrixButton(MATRIX1, matrix, BUTTON_PASSTHRU); js.AddMatrixButton(MATRIX1, matrix, BUTTON_PASSTHRU);
js.AddMatrixButton(MATRIX2, matrix, BUTTON_PASSTHRU); js.AddMatrixButton(MATRIX2, matrix, BUTTON_PASSTHRU);
js.AddMatrixButton(MATRIX3, matrix, BUTTON_PASSTHRU); js.AddMatrixButton(MATRIX3, matrix, BUTTON_PASSTHRU);
@ -83,12 +88,13 @@ void setup() {
// another way to get more room for our inputs, this code adds a 16-channel multiplexer. // another way to get more room for our inputs, this code adds a 16-channel multiplexer.
// It only attaches 3 buttons, but could attach up to channel 15. // It only attaches 3 buttons, but could attach up to channel 15.
Serial.println("Adding multiplexer.");
Mux* mux = new Mux(Pin(MUX_SIG, INPUT_PULLUP, PinType::Digital), Pinset(MUX_ADDR)); Mux* mux = new Mux(Pin(MUX_SIG, INPUT_PULLUP, PinType::Digital), Pinset(MUX_ADDR));
js.AddMuxButton(MUX_BTN1, mux, BUTTON_PASSTHRU); js.AddMuxButton(MUX_BTN1, mux, BUTTON_PASSTHRU);
js.AddMuxButton(MUX_BTN2, mux, BUTTON_PASSTHRU); js.AddMuxButton(MUX_BTN2, mux, BUTTON_PASSTHRU);
js.AddMuxButton(MUX_BTN3, mux, BUTTON_PASSTHRU); js.AddMuxButton(MUX_BTN3, mux, BUTTON_PASSTHRU);
js.Init(); Serial.println("Example joystick fully configured.");
} }
void loop() { void loop() {