diff --git a/crossbow/crossbow.ino b/crossbow/crossbow.ino index 2492ebc..aae77ff 100644 --- a/crossbow/crossbow.ino +++ b/crossbow/crossbow.ino @@ -15,15 +15,21 @@ Copyright (c) 20xx, MPL Contributor1 contrib1@example.net #include "sbus.h" #ifdef ARDUINO_AVR_FEATHER32U4 - #define LORA_SS_PIN 8 - #define LORA_RST_PIN 4 - #define LORA_DI0_PIN 7 + #define LORA_SS_PIN 8 + #define LORA_RST_PIN 4 + #define LORA_DI0_PIN 7 + + #define BUTTON_0_PIN 9 + #define BUTTON_1_PIN 10 #elif defined(ARDUINO_SAMD_FEATHER_M0) - #define LORA_SS_PIN 8 - #define LORA_RST_PIN 4 - #define LORA_DI0_PIN 3 + #define LORA_SS_PIN 8 + #define LORA_RST_PIN 4 + #define LORA_DI0_PIN 3 + + #define BUTTON_0_PIN 9 //Please verify + #define BUTTON_1_PIN 10 //Please verify #else - #error please select hardware + #error please select hardware #endif /* @@ -60,6 +66,16 @@ uint32_t lastOledTaskTime = 0; #endif +#include "tactile.h" + +Tactile button0(BUTTON_0_PIN); +Tactile button1(BUTTON_1_PIN); + + // uint8_t buttonStates[2] = {HIGH, HIGH}; + // uint8_t previousButtonStates[2] = {HIGH, HIGH}; + // uint32_t buttonPressMillis[2] = {0, 0}; + // uint8_t buttonAction[2] = {BUTTON_ACTION_NONE, BUTTON_ACTION_NONE}; + #endif /* @@ -200,11 +216,7 @@ void setup(void) LORA_DI0_PIN ); - if (!LoRa.begin(getFrequencyForChannel(radioState.channel))) - { - #ifdef DEBUG_SERIAL - Serial.println("LoRa init failed. Check your connections."); - #endif + if (!LoRa.begin(getFrequencyForChannel(radioState.channel))) { while (true); } @@ -263,6 +275,13 @@ void setup(void) * Prepare Serial1 for S.Bus processing */ txInput.start(); + + /* + * Buttons on TX module + */ + button0.start(); + button1.start(); + #endif pinMode(LED_BUILTIN, OUTPUT); @@ -342,6 +361,10 @@ void loop(void) */ #ifdef DEVICE_MODE_TX + //Process buttons + button0.loop(); + button1.loop(); + txInput.recoverStuckFrames(); static uint32_t serialRestartMillis = 0; @@ -363,28 +386,14 @@ void loop(void) //In the beginning just keep jumping forward and try to resync over lost single frames if (radioState.failedDwellsCount < 6 && radioState.channelEntryMillis + RX_CHANNEL_DWELL_TIME < currentMillis) { radioState.failedDwellsCount++; - -#ifdef DEBUG_SERIAL - Serial.print("Sync forward on ch "); - Serial.print(radioState.channel); - Serial.print(" number "); - Serial.println(radioState.failedDwellsCount); -#endif - hopFrequency(&radioState, true, radioState.channel, radioState.channelEntryMillis + RX_CHANNEL_DWELL_TIME); LoRa.receive(); - } // If we are loosing more frames, start jumping in the opposite direction since probably we are completely out of sync now if (radioState.failedDwellsCount >= 6 && radioState.channelEntryMillis + (RX_CHANNEL_DWELL_TIME * 5) < currentMillis) { hopFrequency(&radioState, false, radioState.channel, radioState.channelEntryMillis + RX_CHANNEL_DWELL_TIME); //Start jumping in opposite direction to resync LoRa.receive(); - -#ifdef DEBUG_SERIAL - Serial.println("Sync backward"); -#endif - } #endif diff --git a/crossbow/tactile.cpp b/crossbow/tactile.cpp new file mode 100644 index 0000000..922da08 --- /dev/null +++ b/crossbow/tactile.cpp @@ -0,0 +1,42 @@ +#include "Arduino.h" +#include "tactile.h" + +Tactile::Tactile(uint8_t pin) { + _pin = pin; +} + +void Tactile::loop(void) { + + uint8_t pinState = digitalRead(_pin); + + //Press moment + if (pinState == LOW && _previousPinState == HIGH) { + _pressMillis = millis(); + } + + //Release moment + if (pinState == HIGH && _previousPinState == LOW) { + const uint32_t buttonTime = abs(millis() - _pressMillis); + + if (buttonTime > TACTILE_LONG_PRESS_TIME) { + _state = TACTILE_STATE_LONG_PRESS; + } else if (buttonTime > TACTILE_MIN_PRESS_TIME) { + _state = TACTILE_STATE_SHORT_PRESS; + } else { + _state = TACTILE_STATE_NONE; + } + + } else { + _state = TACTILE_STATE_NONE; + } + + _previousPinState = pinState; +} + +void Tactile::start(void) { + pinMode(_pin, INPUT_PULLUP); +} + +uint8_t Tactile::getState(void) { + return _state; +} \ No newline at end of file diff --git a/crossbow/tactile.h b/crossbow/tactile.h new file mode 100644 index 0000000..d95ac91 --- /dev/null +++ b/crossbow/tactile.h @@ -0,0 +1,30 @@ +#pragma once + +#ifndef TACTILE_H +#define TACTILE_H + +#include "Arduino.h" + +enum tactileStateFlags { + TACTILE_STATE_NONE, + TACTILE_STATE_SHORT_PRESS, + TACTILE_STATE_LONG_PRESS +}; + +#define TACTILE_MIN_PRESS_TIME 50 +#define TACTILE_LONG_PRESS_TIME 1000 + +class Tactile { + public: + Tactile(uint8_t pin); + void loop(void); + void start(void); + uint8_t getState(void); + private: + uint8_t _pin; + uint8_t _previousPinState = HIGH; + uint32_t _pressMillis = 0; + uint8_t _state = TACTILE_STATE_NONE; +}; + +#endif \ No newline at end of file