Cockpit-Switch-Panel/cockpit_switches.ino

56 lines
1.3 KiB
Arduino
Raw Normal View History

2020-09-15 10:28:54 +10:00
#include <EasyTransfer.h>
#define BUTTONS 16
#define FAUXBUTTONS 5
const long speedLimit = 10;
unsigned long lastSend = 0;
int buttonPins[BUTTONS] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, A0, A1, A2, A3 };
int analogButtonPin = A4;
EasyTransfer ET;
struct SEND_DATA_STRUCTURE{
//THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO
int16_t joyx;
int16_t joyy;
int8_t joyz;
int16_t joyxr;
int16_t joyyr;
int8_t joyzr;
uint32_t buttonState;
};
SEND_DATA_STRUCTURE controllerState;
void setup() {
Serial.begin(2000000);
ET.begin(details(controllerState), &Serial);
for (int i = 0; i < BUTTONS; i++) { pinMode(buttonPins[i], INPUT_PULLUP); }
pinMode(analogButtonPin, INPUT);
2020-09-15 10:27:21 +10:00
}
2020-09-15 10:28:54 +10:00
uint32_t readButtons(){
uint32_t buttonStateNow = 0;
for (int i=0; i<BUTTONS; i++) {
bitWrite(buttonStateNow, i, !digitalRead(buttonPins[i]));
2020-09-15 10:27:21 +10:00
}
2020-09-15 10:28:54 +10:00
int fauxButton = analogRead(analogButtonPin) / 205;
for (int i=0; i<FAUXBUTTONS; i++) {
bool buttonPressed = false;
fauxButton == i ? buttonPressed = true : buttonPressed = false;
bitWrite(buttonStateNow, i + BUTTONS, buttonPressed);
}
return buttonStateNow;
2020-09-15 10:27:21 +10:00
}
2020-09-15 10:28:54 +10:00
void loop(){
controllerState.buttonState = readButtons();
if ((millis() - lastSend) > speedLimit ) {
ET.sendData();
lastSend = millis();
2020-09-15 10:27:21 +10:00
}
}