separate button handling
This commit is contained in:
parent
0e5ffebaa7
commit
cd3d4237fe
|
@ -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;
|
||||||
|
}
|
|
@ -0,0 +1,25 @@
|
||||||
|
#ifndef BUTTON_H
|
||||||
|
#define BUTTON_H
|
||||||
|
|
||||||
|
#include <Arduino.h>
|
||||||
|
|
||||||
|
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
|
|
@ -1,9 +1,3 @@
|
||||||
/*
|
|
||||||
* TODO:
|
|
||||||
* bestehende steuerung aus espcontroller übernehmen und testen
|
|
||||||
* topics und handler implementieren
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
|
||||||
#include <Adafruit_NeoPixel.h>
|
#include <Adafruit_NeoPixel.h>
|
||||||
|
@ -11,6 +5,13 @@
|
||||||
#include <avr/power.h>
|
#include <avr/power.h>
|
||||||
#endif
|
#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
|
#define LEDPIN 9 //PB1 = D9 = Pin15
|
||||||
Adafruit_NeoPixel leds = Adafruit_NeoPixel(9, LEDPIN, NEO_GRB + NEO_KHZ800);
|
Adafruit_NeoPixel leds = Adafruit_NeoPixel(9, LEDPIN, NEO_GRB + NEO_KHZ800);
|
||||||
|
|
||||||
|
@ -38,10 +39,13 @@ long last_serialdebug=0;
|
||||||
#define INTERVAL_SERIALDEBUG 200
|
#define INTERVAL_SERIALDEBUG 200
|
||||||
|
|
||||||
//Inputs
|
//Inputs
|
||||||
|
#include "button.h"
|
||||||
|
|
||||||
#define PIN_BUTTON A3 //A3 = PC3, defining PCx as pin doesnt work
|
#define PIN_BUTTON A3 //A3 = PC3, defining PCx as pin doesnt work
|
||||||
#define PIN_ENCA A2 //A2 = PC2
|
#define PIN_ENCA A2 //A2 = PC2
|
||||||
#define PIN_ENCB A1 //A1 = PC1
|
#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_flag=false; //true if button pressed
|
||||||
boolean button_released=true;
|
boolean button_released=true;
|
||||||
|
@ -100,6 +104,7 @@ uint8_t error=0;
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
pinMode(PIN_BUTTON,INPUT_PULLUP);
|
pinMode(PIN_BUTTON,INPUT_PULLUP);
|
||||||
|
button_knob = Button();
|
||||||
|
|
||||||
pinMode(PIN_POT,INPUT);
|
pinMode(PIN_POT,INPUT);
|
||||||
|
|
||||||
|
@ -196,6 +201,7 @@ void loop() {
|
||||||
|
|
||||||
|
|
||||||
//Serial Input ##############################################
|
//Serial Input ##############################################
|
||||||
|
/*For debugging
|
||||||
while (Serial.available() > 0) {
|
while (Serial.available() > 0) {
|
||||||
int _value = Serial.parseInt();
|
int _value = Serial.parseInt();
|
||||||
if (Serial.read() == '\n') {
|
if (Serial.read() == '\n') {
|
||||||
|
@ -206,12 +212,13 @@ void loop() {
|
||||||
srWrite(_value,!srRead(_value));
|
srWrite(_value,!srRead(_value));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
//Inputs ###################################################
|
//Inputs ###################################################
|
||||||
poti_read=poti_read*POTIFILTER + (1.0-POTIFILTER)*analogRead(PIN_POT); //read poti
|
poti_read=poti_read*POTIFILTER + (1.0-POTIFILTER)*analogRead(PIN_POT); //read poti
|
||||||
|
|
||||||
|
/*
|
||||||
if (!digitalRead(PIN_BUTTON)){ //button pressed
|
if (!digitalRead(PIN_BUTTON)){ //button pressed
|
||||||
if (button_released){
|
if (button_released){
|
||||||
button_released=false; //flag: not released
|
button_released=false; //flag: not released
|
||||||
|
@ -222,8 +229,22 @@ void loop() {
|
||||||
}else if(!button_flag && !button_released){ //button released and flag has been cleared
|
}else if(!button_flag && !button_released){ //button released and flag has been cleared
|
||||||
last_button_released=loopmillis;
|
last_button_released=loopmillis;
|
||||||
button_released=true;
|
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"
|
//Read Encoder to velocity "volEncVel"
|
||||||
int volEncVel=0;
|
int volEncVel=0;
|
||||||
int _volEnc=volEnc.read();
|
int _volEnc=volEnc.read();
|
||||||
|
@ -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){
|
if (state==true){
|
||||||
srbits |= 1UL << pin; //set bit
|
srbits |= 1UL << pbit; //set bit
|
||||||
}else{
|
}else{
|
||||||
srbits &= ~(1UL << pin); //clear bit
|
srbits &= ~(1UL << pbit); //clear bit
|
||||||
}
|
}
|
||||||
digitalWrite(SRLATCH, LOW);
|
digitalWrite(SRLATCH, LOW);
|
||||||
shiftOut(SRDATA, SRCLOCK, MSBFIRST, srbits>>8);
|
shiftOut(SRDATA, SRCLOCK, MSBFIRST, srbits>>8);
|
||||||
shiftOut(SRDATA, SRCLOCK, MSBFIRST, srbits);
|
shiftOut(SRDATA, SRCLOCK, MSBFIRST, srbits);
|
||||||
digitalWrite(SRLATCH, HIGH);
|
digitalWrite(SRLATCH, HIGH);
|
||||||
}
|
}
|
||||||
boolean srRead(uint8_t pin){
|
boolean srRead(uint8_t pbit){ //get state at bit
|
||||||
return (srbits >> pin) & 1U;
|
return (srbits >> pbit) & 1U;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue