From cd3d4237fe81b5ccf1eb0a18eba06086d948e63b Mon Sep 17 00:00:00 2001 From: Fisch Date: Wed, 1 Apr 2020 11:37:09 +0200 Subject: [PATCH] separate button handling --- .../mixercontroller_w5100_pio/src/button.cpp | 50 +++++++++++++++++++ .../mixercontroller_w5100_pio/src/button.h | 25 ++++++++++ .../src/{main.ino => main.cpp} | 47 ++++++++++++----- 3 files changed, 109 insertions(+), 13 deletions(-) create mode 100644 controller/mixercontroller_w5100_pio/src/button.cpp create mode 100644 controller/mixercontroller_w5100_pio/src/button.h rename controller/mixercontroller_w5100_pio/src/{main.ino => main.cpp} (93%) diff --git a/controller/mixercontroller_w5100_pio/src/button.cpp b/controller/mixercontroller_w5100_pio/src/button.cpp new file mode 100644 index 0000000..dd450b3 --- /dev/null +++ b/controller/mixercontroller_w5100_pio/src/button.cpp @@ -0,0 +1,50 @@ +#include "button.h" + +#define DEBOUNCETIME 20 //time to not check for inputs after key press +#define BUTTONTIMEHOLD 750 //time for button hold +#define BUTTONTIMEHOLDLONG 3000 //time for button long hold + +Button::Button() { + +} + +void Button::update(long millis, boolean state) +{ + //example: state=!digitalRead(PIN_BTNLEFT) + //Short press (true when button short pressed, on release) + _button=false; + + //long press (true when button is held down for BUTTONTIMEHOLD, on time elapsed) + _button_hold=false; + + if (millis-_millis_lastinput>DEBOUNCETIME) //Button debouncing + { + if (_timebuttonpressed == 0 && state){ //first time pressed down. (low when pressed) + _timebuttonpressed=millis; //set time of button press + _millis_lastinput=millis; //for debouncing + }else if(_timebuttonpressed != 0 && !state){ //button released (was pressed) + if (millis-_timebuttonpressed < BUTTONTIMEHOLD){ //short press + _button=true; + } + _timebuttonpressed=0; //re-enable after short press and release from hold + _millis_lastinput=millis; //for debouncing + }else if(millis-_timebuttonpressed >= BUTTONTIMEHOLD && _timebuttonpressed>0){ //held down long enough and not already hold triggered + _button_hold=true; + _timebuttonpressed=-1; //-1 as flag for hold triggered + } + } + + if ( _button ) { + _millis_lastinput=millis; //for debouncing + } + +} + +boolean Button::buttonPressed() +{ + return _button; +} +boolean Button::buttonHold() +{ + return _button_hold; +} diff --git a/controller/mixercontroller_w5100_pio/src/button.h b/controller/mixercontroller_w5100_pio/src/button.h new file mode 100644 index 0000000..2d11636 --- /dev/null +++ b/controller/mixercontroller_w5100_pio/src/button.h @@ -0,0 +1,25 @@ +#ifndef BUTTON_H +#define BUTTON_H + +#include + +class Button +{ + public: + Button(); + void update(long millis, boolean state); //millis: current time, state: state of button + boolean buttonPressed(); //true (once after update) when button released and not hold down long enough + boolean buttonHold(); //true (once after update) when button held down for long enough. + private: + long _millis_lastinput; + long _timebuttonpressed; + + //Short press (true when button short pressed, on release) + boolean _button=false; + + //long press (true when button is held down for BUTTONTIMEHOLD, on time elapsed) + boolean _button_hold=false; + +}; + +#endif diff --git a/controller/mixercontroller_w5100_pio/src/main.ino b/controller/mixercontroller_w5100_pio/src/main.cpp similarity index 93% rename from controller/mixercontroller_w5100_pio/src/main.ino rename to controller/mixercontroller_w5100_pio/src/main.cpp index 5e02f11..269c27c 100644 --- a/controller/mixercontroller_w5100_pio/src/main.ino +++ b/controller/mixercontroller_w5100_pio/src/main.cpp @@ -1,9 +1,3 @@ -/* - * TODO: - * bestehende steuerung aus espcontroller übernehmen und testen - * topics und handler implementieren - */ - #include #include @@ -11,6 +5,13 @@ #include #endif +void reconnect(); +uint32_t Wheel(byte WheelPos); +boolean srRead(uint8_t pbit); +void srWrite(uint8_t pbit, boolean state); +void callback(char* topic, byte* payload, unsigned int length); +void sendData(); + #define LEDPIN 9 //PB1 = D9 = Pin15 Adafruit_NeoPixel leds = Adafruit_NeoPixel(9, LEDPIN, NEO_GRB + NEO_KHZ800); @@ -38,10 +39,13 @@ long last_serialdebug=0; #define INTERVAL_SERIALDEBUG 200 //Inputs +#include "button.h" + #define PIN_BUTTON A3 //A3 = PC3, defining PCx as pin doesnt work #define PIN_ENCA A2 //A2 = PC2 #define PIN_ENCB A1 //A1 = PC1 -#define BUTTON_RELEASE_DEBOUNCE 10 //minimum time after button release to reenable triggering + +Button button_knob; boolean button_flag=false; //true if button pressed boolean button_released=true; @@ -100,6 +104,7 @@ uint8_t error=0; void setup() { pinMode(PIN_BUTTON,INPUT_PULLUP); + button_knob = Button(); pinMode(PIN_POT,INPUT); @@ -196,6 +201,7 @@ void loop() { //Serial Input ############################################## + /*For debugging while (Serial.available() > 0) { int _value = Serial.parseInt(); if (Serial.read() == '\n') { @@ -206,12 +212,13 @@ void loop() { srWrite(_value,!srRead(_value)); } - } + */ //Inputs ################################################### poti_read=poti_read*POTIFILTER + (1.0-POTIFILTER)*analogRead(PIN_POT); //read poti + /* if (!digitalRead(PIN_BUTTON)){ //button pressed if (button_released){ button_released=false; //flag: not released @@ -222,7 +229,21 @@ void loop() { }else if(!button_flag && !button_released){ //button released and flag has been cleared last_button_released=loopmillis; button_released=true; + }*/ + + button_knob.update(millis(),!digitalRead(PIN_BUTTON)); + + if (button_knob.buttonPressed()){ + Serial.println("Button Pressed"); + }else if(button_knob.buttonHold()){ + Serial.println("Button hold"); } + + + + + + //Read Encoder to velocity "volEncVel" int volEncVel=0; @@ -370,19 +391,19 @@ void callback(char* topic, byte* payload, unsigned int length) { -void srWrite(uint8_t pin, boolean state){ +void srWrite(uint8_t pbit, boolean state){ //change bit to state if (state==true){ - srbits |= 1UL << pin; //set bit + srbits |= 1UL << pbit; //set bit }else{ - srbits &= ~(1UL << pin); //clear bit + srbits &= ~(1UL << pbit); //clear bit } digitalWrite(SRLATCH, LOW); shiftOut(SRDATA, SRCLOCK, MSBFIRST, srbits>>8); shiftOut(SRDATA, SRCLOCK, MSBFIRST, srbits); digitalWrite(SRLATCH, HIGH); } -boolean srRead(uint8_t pin){ - return (srbits >> pin) & 1U; +boolean srRead(uint8_t pbit){ //get state at bit + return (srbits >> pbit) & 1U; }