Move button pressing logic to it's own file

This commit is contained in:
Reed Nightingale 2020-04-25 12:00:19 -07:00
parent 5596c0c8ab
commit 8af59b0a85
5 changed files with 37 additions and 34 deletions

7
button_press_e.h Normal file
View File

@ -0,0 +1,7 @@
#pragma once
enum ButtonPress_e : uint8_t {
NotPressed,
ShortPress,
LongPress
};

7
menu.h
View File

@ -1,6 +1,7 @@
#pragma once
#include <stdint.h>
#include "button_press_e.h"
#include "point.h"
enum MenuReturn_e : uint8_t {
@ -9,12 +10,6 @@ enum MenuReturn_e : uint8_t {
ExitedNoRedraw
};
enum ButtonPress_e : uint8_t {
NotPressed,
ShortPress,
LongPress
};
struct Menu_t {
void (*const initMenu)();//Any initial draw routines or state initialization
MenuReturn_e (*const runMenu)(const ButtonPress_e tuner_button,

View File

@ -1,4 +1,5 @@
#include <Arduino.h>
#include "button_timing.h"
#include "pin_definitions.h"
#include "push_button.h"
@ -8,3 +9,26 @@ bool IsButtonPressed()
//and reads low when pressed down
return !digitalRead(PIN_ENC_PUSH_BUTTON);
}
ButtonPress_e CheckTunerButton(){
if (!IsButtonPressed()){
return ButtonPress_e::NotPressed;
}
delay(DEBOUNCE_DELAY_MS);
if (!IsButtonPressed()){//debounce
return ButtonPress_e::NotPressed;
}
uint16_t down_time = 0;
while(IsButtonPressed() && (down_time < LONG_PRESS_TIME_MS)){
delay(LONG_PRESS_POLL_TIME_MS);
down_time += LONG_PRESS_POLL_TIME_MS;
}
if(down_time < LONG_PRESS_TIME_MS){
return ButtonPress_e::ShortPress;
}
else{
return ButtonPress_e::LongPress;
}
}

View File

@ -1,3 +1,5 @@
#pragma once
bool IsButtonPressed(); //returns true if the encoder button is pressed
#include "button_press_e.h"
ButtonPress_e CheckTunerButton();

View File

@ -30,7 +30,6 @@
* Si5351 object to control the clocks.
*/
#include <Wire.h>
#include "button_timing.h"
#include "encoder.h"
#include "menu.h"
#include "menu_main.h"
@ -86,30 +85,6 @@ void checkPTT(){
stopTx();
}
//check if the encoder button was pressed
ButtonPress_e checkButton(){
if (!IsButtonPressed()){
return ButtonPress_e::NotPressed;
}
delay(DEBOUNCE_DELAY_MS);
if (!IsButtonPressed()){//debounce
return ButtonPress_e::NotPressed;
}
uint16_t down_time = 0;
while(IsButtonPressed() && (down_time < LONG_PRESS_TIME_MS)){
delay(LONG_PRESS_POLL_TIME_MS);
down_time += LONG_PRESS_POLL_TIME_MS;
}
if(down_time < LONG_PRESS_TIME_MS){
return ButtonPress_e::ShortPress;
}
else{
return ButtonPress_e::LongPress;
}
}
/**
* The settings are read from EEPROM. The first time around, the values may not be
* present or out of range, in this case, some intelligent defaults are copied into the
@ -167,7 +142,7 @@ void setup()
setFrequency(globalSettings.vfoA.frequency);
//Run initial calibration routine if button is pressed during power up
if(IsButtonPressed()){
if(ButtonPress_e::NotPressed != CheckTunerButton()){
LoadDefaultSettings();
setupTouch();
SetActiveVfoMode(VfoMode_e::VFO_MODE_USB);
@ -201,7 +176,7 @@ void loop(){
return;
}
ButtonPress_e tuner_button = checkButton();
ButtonPress_e tuner_button = CheckTunerButton();
Point touch_point;
ButtonPress_e touch_button = checkTouch(&touch_point);
int16_t knob = enc_read();