Added Joystick.h
Setup PCF buttons to activate via joystick Todo: Finish rotary encoder to work via joystick Todo: make code more efficient
This commit is contained in:
parent
8ba82c0262
commit
dad952c9a5
@ -15,3 +15,4 @@ framework = arduino
|
|||||||
lib_deps =
|
lib_deps =
|
||||||
xreef/PCF8574 library@^2.2.0
|
xreef/PCF8574 library@^2.2.0
|
||||||
ellsclytn/Rotary@0.0.0-alpha+sha.d1fef10209
|
ellsclytn/Rotary@0.0.0-alpha+sha.d1fef10209
|
||||||
|
mheironimus/Joystick@^2.0.7
|
||||||
|
106
src/main.cpp
106
src/main.cpp
@ -1,37 +1,43 @@
|
|||||||
//Need to create an array to house all of the button states. Define the array using a variable so I can just change a number at the top of the code.
|
|
||||||
|
|
||||||
#include "Arduino.h"
|
#include "Arduino.h"
|
||||||
#include "PCF8574.h"
|
#include "PCF8574.h"
|
||||||
#include "Rotary.h"
|
#include "Rotary.h"
|
||||||
|
#include "Joystick.h"
|
||||||
//Setup I/O Expander Chips
|
|
||||||
PCF8574 pcf1(0x20);
|
|
||||||
//PCF8574 pcf2(0x38);
|
|
||||||
//Setup Rotary Encoders
|
|
||||||
Rotary r1 = Rotary(4, 5);
|
|
||||||
|
|
||||||
//setup variables
|
//setup variables
|
||||||
//numPCFButtons is greater than 8, its expected to be using 2 PCF8572 chips (multiples of 8)
|
//numPCFButtons is greater than 8, its expected to be using 2 PCF8572 chips (multiples of 8)
|
||||||
int numPCFButtons = 8;
|
const int numPCFButtons = 8;
|
||||||
//int numPCFButtons = 16;
|
const int numRotaries = 3;
|
||||||
unsigned long btnTime[32] = {0};
|
unsigned long btnTime[numPCFButtons] = {0};
|
||||||
unsigned long btnPressedTime[8] = {0};
|
unsigned long rotTime[numRotaries] = {0};
|
||||||
int btnState[32] = {0};
|
unsigned long btnPressedTime[numPCFButtons] = {0};
|
||||||
|
unsigned long rotPressedTime[numRotaries] = {0};
|
||||||
|
int btnState[numPCFButtons] = {0};
|
||||||
|
int rotState[numRotaries] = {0};
|
||||||
const unsigned long gButtonDelta = 15;
|
const unsigned long gButtonDelta = 15;
|
||||||
const unsigned long gButtonPressedDelta = 100;
|
const unsigned long gButtonPressedDelta = 100;
|
||||||
/*
|
bool debug = false;
|
||||||
#define int RELEASED 0
|
|
||||||
#define int PRESSSED 1
|
//Setup I/O Expander Chips
|
||||||
#define int HOLD 1
|
PCF8574 pcf1(0x20);
|
||||||
*/
|
|
||||||
|
//Setup Rotary Encoders
|
||||||
|
Rotary r1 = Rotary(4, 5);
|
||||||
|
Rotary r2 = Rotary(6, 7);
|
||||||
|
Rotary r3 = Rotary(8, 9);
|
||||||
|
|
||||||
|
//Setup Joystick
|
||||||
|
Joystick_ joy(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_GAMEPAD,
|
||||||
|
32, 0, // Button Count, Hat Switch Count
|
||||||
|
true, true, 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
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
|
|
||||||
pinMode(LED_BUILTIN, OUTPUT); //debug LED
|
pinMode(LED_BUILTIN, OUTPUT); //debug LED
|
||||||
|
|
||||||
Serial.begin(9600);
|
|
||||||
while (!Serial); // Leonardo needs this in order to see serial output at startup
|
|
||||||
|
|
||||||
//INIT Rotary Encoder
|
//INIT Rotary Encoder
|
||||||
r1.begin(true);
|
r1.begin(true);
|
||||||
|
|
||||||
@ -48,7 +54,10 @@ void setup() {
|
|||||||
pcf1.digitalWrite(i, LOW);
|
pcf1.digitalWrite(i, LOW);
|
||||||
}
|
}
|
||||||
|
|
||||||
Serial.println("Ready");
|
//Joystick start
|
||||||
|
joy.begin();
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void processBtn1(int i) {
|
void processBtn1(int i) {
|
||||||
@ -72,8 +81,9 @@ void processBtn1(int i) {
|
|||||||
btnTime[i] = millis() + gButtonDelta;
|
btnTime[i] = millis() + gButtonDelta;
|
||||||
//Are we still pressing the button? If so, skip checking for the button state
|
//Are we still pressing the button? If so, skip checking for the button state
|
||||||
if (btnState[i] == 0) {
|
if (btnState[i] == 0) {
|
||||||
//joystick.button(i).push
|
//joystick.button(i).push <--psudo code
|
||||||
//Start Debug Code
|
//Start Debug Code
|
||||||
|
joy.pressButton(i);
|
||||||
digitalWrite(LED_BUILTIN, HIGH);
|
digitalWrite(LED_BUILTIN, HIGH);
|
||||||
Serial.write("Button: ");
|
Serial.write("Button: ");
|
||||||
Serial.print(i);
|
Serial.print(i);
|
||||||
@ -93,8 +103,9 @@ void processBtn1(int i) {
|
|||||||
// btnState[i] = 0;
|
// btnState[i] = 0;
|
||||||
// break;
|
// break;
|
||||||
default:
|
default:
|
||||||
//joystick.button(i).letgo
|
//joystick.button(i).letgo <--psudo code
|
||||||
//Start Debug Code
|
//Start Debug Code
|
||||||
|
joy.releaseButton(i);
|
||||||
digitalWrite(LED_BUILTIN, LOW);
|
digitalWrite(LED_BUILTIN, LOW);
|
||||||
Serial.write("Button: ");
|
Serial.write("Button: ");
|
||||||
Serial.print(i);
|
Serial.print(i);
|
||||||
@ -133,7 +144,6 @@ void specialBtnOnStart(int i) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void specialBtnAlwaysOn(int i) {
|
void specialBtnAlwaysOn(int i) {
|
||||||
//This is a sample function that shows how to simulate a button press.
|
//This is a sample function that shows how to simulate a button press.
|
||||||
//This is mainly for always on switches. When you turn the switch on, it will simulate a button press
|
//This is mainly for always on switches. When you turn the switch on, it will simulate a button press
|
||||||
@ -157,7 +167,6 @@ void specialBtnAlwaysOn(int i) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
//Start PCF8574 Button pressed detection section
|
//Start PCF8574 Button pressed detection section
|
||||||
@ -171,55 +180,26 @@ void loop()
|
|||||||
//Serial.println(en1 == DIR_CW ? "Right" : "Left");
|
//Serial.println(en1 == DIR_CW ? "Right" : "Left");
|
||||||
if (en1 == DIR_CW) {
|
if (en1 == DIR_CW) {
|
||||||
Serial.println("RIGHT");
|
Serial.println("RIGHT");
|
||||||
btnState[9] = 1;
|
rotState[1] = 1;
|
||||||
btnTime[9] = millis() + gButtonDelta;
|
rotTime[1] = millis() + gButtonDelta;
|
||||||
digitalWrite(LED_BUILTIN, LOW);
|
digitalWrite(LED_BUILTIN, LOW); //This can be written for joystick.letgo. This is in case the button is still being pressed, it will let go before pressing it again
|
||||||
digitalWrite(LED_BUILTIN, HIGH);
|
digitalWrite(LED_BUILTIN, HIGH);
|
||||||
} else {
|
} else {
|
||||||
Serial.println("LEFT");
|
Serial.println("LEFT");
|
||||||
btnState[9] = 1;
|
rotState[1] = 1;
|
||||||
btnTime[9] = millis() + gButtonDelta;
|
rotTime[1] = millis() + gButtonDelta;
|
||||||
digitalWrite(LED_BUILTIN, LOW);
|
digitalWrite(LED_BUILTIN, LOW); //This can be written for joystick.letgo. This is in case the button is still being pressed, it will let go before pressing it again
|
||||||
digitalWrite(LED_BUILTIN, HIGH);
|
digitalWrite(LED_BUILTIN, HIGH);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (btnState[9] == 1) {
|
if (rotState[1] == 1) {
|
||||||
if (millis() >= btnTime[9]) {
|
if (millis() >= rotTime[1]) {
|
||||||
Serial.println("Encoder Off");
|
Serial.println("Encoder Off");
|
||||||
digitalWrite(LED_BUILTIN,LOW);
|
digitalWrite(LED_BUILTIN,LOW);
|
||||||
btnState[9] = 0;
|
rotState[1] = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//End PCF8574 Button Pressed detection section
|
//End PCF8574 Button Pressed detection section
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
time = millis();
|
|
||||||
Serial.println(time);
|
|
||||||
delay(500);
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
for (int i = 0; i<8; i++) {
|
|
||||||
if (pcf1.digitalRead(i) == HIGH){
|
|
||||||
digitalWrite(LED_BUILTIN,HIGH);
|
|
||||||
delay(50);
|
|
||||||
} else {
|
|
||||||
digitalWrite(LED_BUILTIN,LOW);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
Loading…
Reference in New Issue
Block a user