Move from button to gpio

This commit is contained in:
fvanroie 2020-05-02 00:15:54 +02:00
parent 1a45648146
commit 7650a2ed81
4 changed files with 184 additions and 110 deletions

View File

@ -1,90 +0,0 @@
#include "Arduino.h"
#include "ArduinoLog.h"
#include "AceButton.h"
#include "hasp_conf.h"
#include "lv_conf.h"
#include "hasp_mqtt.h" // testing memory consumption
#include "hasp_button.h"
#include "hasp_dispatch.h"
using namespace ace_button;
static AceButton * button[HASP_NUM_INPUTS]; // Connect your button between pin 2 and GND
static void button_event_cb(AceButton * button, uint8_t eventType, uint8_t buttonState)
{
char buffer[8];
switch(eventType) {
case 0: // AceButton::kEventPressed:
memcpy_P(buffer, PSTR("DOWN"), sizeof(buffer));
break;
case 2: // AceButton::kEventClicked:
memcpy_P(buffer, PSTR("SHORT"), sizeof(buffer));
break;
// case AceButton::kEventDoubleClicked:
// memcpy_P(buffer, PSTR("DOUBLE"), sizeof(buffer));
// break;
case 4: // AceButton::kEventLongPressed:
memcpy_P(buffer, PSTR("LONG"), sizeof(buffer));
break;
case 5: // AceButton::kEventRepeatPressed:
return; // Fix needed for switches
memcpy_P(buffer, PSTR("HOLD"), sizeof(buffer));
break;
case 1: // AceButton::kEventReleased:
memcpy_P(buffer, PSTR("UP"), sizeof(buffer));
break;
}
Log.verbose(F("BTNS: setup(): ready"));
dispatch_button(button->getId(), buffer);
}
void buttonSetup(void)
{
ButtonConfig * buttonConfig = ButtonConfig::getSystemButtonConfig();
buttonConfig->setEventHandler(button_event_cb);
// Features
buttonConfig->setFeature(ButtonConfig::kFeatureClick);
buttonConfig->setFeature(ButtonConfig::kFeatureLongPress);
buttonConfig->setFeature(ButtonConfig::kFeatureRepeatPress);
// buttonConfig->setFeature(ButtonConfig::kFeatureDoubleClick);
// buttonConfig->setFeature(ButtonConfig::kFeatureSuppressClickBeforeDoubleClick);
// Delays
buttonConfig->setClickDelay(LV_INDEV_DEF_LONG_PRESS_TIME);
buttonConfig->setDoubleClickDelay(LV_INDEV_DEF_LONG_PRESS_TIME);
buttonConfig->setLongPressDelay(LV_INDEV_DEF_LONG_PRESS_TIME);
buttonConfig->setRepeatPressDelay(LV_INDEV_DEF_LONG_PRESS_TIME);
buttonConfig->setRepeatPressInterval(LV_INDEV_DEF_LONG_PRESS_REP_TIME);
// button[0] = new Button(2);
#if defined(ARDUINO_ARCH_ESP8266)
button[1] = new AceButton(3, HIGH, 1);
#else
//button[1] = new AceButton(3, HIGH, 1);
#endif
pinMode (PC13, INPUT_PULLUP);
button[0] = new AceButton(buttonConfig, PC13, HIGH, 0);
pinMode (PA0, INPUT_PULLUP);
button[1] = new AceButton(buttonConfig, PA0, HIGH, 1);
pinMode (PD15, INPUT);
button[2] = new AceButton(buttonConfig, PD15, HIGH, 2);
Log.verbose(F("BTNS: setup(): ready"));
}
void IRAM_ATTR buttonLoop(void)
{
// Should be called every 4-5ms or faster, for the default debouncing time
// of ~20ms.
for(uint8_t i = 0; i < HASP_NUM_INPUTS; i++) {
if(button[i]) button[i]->check();
}
}

View File

@ -1,10 +0,0 @@
#if HASP_USE_BUTTON
#ifndef HASP_BUTTON_H
#define HASP_BUTTON_H
void buttonSetup(void);
void IRAM_ATTR buttonLoop(void);
#endif
#endif

View File

@ -1,14 +1,166 @@
#include <Arduino.h>
#include "ArduinoJson.h"
#include "Arduino.h"
#include "ArduinoLog.h"
#include "AceButton.h"
#include "lv_conf.h" // For timing defines
#include "hasp_conf.h"
#include "hasp_gpio.h"
#include "hasp_dispatch.h"
#define HASP_NUM_GPIO_CONFIG 5
uint8_t gpioUsedInputCount = 0;
uint16_t gpioConfig[HASP_NUM_GPIO_CONFIG];
using namespace ace_button;
static AceButton * button[HASP_NUM_INPUTS];
static void gpio_event_cb(AceButton * button, uint8_t eventType, uint8_t buttonState)
{
char buffer[16];
switch(eventType) {
case 0: // AceButton::kEventPressed:
memcpy_P(buffer, PSTR("DOWN"), sizeof(buffer));
break;
case 2: // AceButton::kEventClicked:
memcpy_P(buffer, PSTR("SHORT"), sizeof(buffer));
break;
case AceButton::kEventDoubleClicked:
memcpy_P(buffer, PSTR("DOUBLE"), sizeof(buffer));
break;
case 4: // AceButton::kEventLongPressed:
memcpy_P(buffer, PSTR("LONG"), sizeof(buffer));
break;
case 5: // AceButton::kEventRepeatPressed:
// return; // Fix needed for switches
memcpy_P(buffer, PSTR("HOLD"), sizeof(buffer));
break;
case 1: // AceButton::kEventReleased:
memcpy_P(buffer, PSTR("UP"), sizeof(buffer));
break;
default:
memcpy_P(buffer, PSTR("UNKNOWN"), sizeof(buffer));
}
dispatch_button(button->getId(), buffer);
}
void aceButtonSetup(void)
{
ButtonConfig * buttonConfig = ButtonConfig::getSystemButtonConfig();
buttonConfig->setEventHandler(gpio_event_cb);
// Features
buttonConfig->setFeature(ButtonConfig::kFeatureClick);
buttonConfig->setFeature(ButtonConfig::kFeatureLongPress);
buttonConfig->setFeature(ButtonConfig::kFeatureRepeatPress);
// buttonConfig->setFeature(ButtonConfig::kFeatureDoubleClick);
// buttonConfig->setFeature(ButtonConfig::kFeatureSuppressClickBeforeDoubleClick);
// Delays
buttonConfig->setClickDelay(LV_INDEV_DEF_LONG_PRESS_TIME);
buttonConfig->setDoubleClickDelay(LV_INDEV_DEF_LONG_PRESS_TIME);
buttonConfig->setLongPressDelay(LV_INDEV_DEF_LONG_PRESS_TIME);
buttonConfig->setRepeatPressDelay(LV_INDEV_DEF_LONG_PRESS_TIME);
buttonConfig->setRepeatPressInterval(LV_INDEV_DEF_LONG_PRESS_REP_TIME);
}
void IRAM_ATTR gpioLoop(void)
{
// Should be called every 4-5ms or faster, for the default debouncing time of ~20ms.
for(uint8_t i = 0; i < gpioUsedInputCount; i++) {
if(button[i]) button[i]->check();
}
}
void gpioAddButton(uint8_t pin, uint8_t input_mode, uint8_t default_state, uint8_t channel)
{
uint8_t i;
for(i = 0; i < HASP_NUM_INPUTS; i++) {
if(!button[i]) {
button[i] = new AceButton(pin, default_state, channel);
if(button[i]) {
pinMode(pin, input_mode);
ButtonConfig * buttonConfig = button[i]->getButtonConfig();
buttonConfig->setEventHandler(gpio_event_cb);
buttonConfig->setFeature(ButtonConfig::kFeatureClick);
buttonConfig->setFeature(ButtonConfig::kFeatureDoubleClick);
buttonConfig->setFeature(ButtonConfig::kFeatureLongPress);
buttonConfig->setFeature(ButtonConfig::kFeatureRepeatPress);
buttonConfig->setFeature(ButtonConfig::kFeatureSuppressClickBeforeDoubleClick);
Log.verbose(F("GPIO: Button%d created on pin %d (channel %d) mode %d default %d"), i, pin, channel, input_mode,default_state);
gpioUsedInputCount = i + 1;
return;
}
}
}
Log.error(F("GPIO: Failed to create Button%d pin %d (channel %d). All %d slots available are in use!"), i, pin,
channel, HASP_NUM_INPUTS);
}
void gpioSetup()
{
aceButtonSetup();
//gpioConfig[0] = PD15 * 256 + 5 + (INPUT << 3);
for(uint8_t i = 0; i < HASP_NUM_GPIO_CONFIG; i++) {
uint8_t pin = (gpioConfig[i] >> 8) & 0xFF;
uint8_t channel = gpioConfig[i] & 0b111; // 3bit
uint8_t input_mode = (gpioConfig[i] >> 3) & 0b11; // 2bit gpio mode
uint8_t gpiotype = (gpioConfig[i] >> 5) & 0b111; // 3bit
uint8_t default_state = gpioConfig[i] & 0b1; // 1bit: 0=LOW, 1=HIGH
switch(input_mode) {
case 1:
input_mode = OUTPUT;
break;
case 2:
input_mode = INPUT_PULLUP;
break;
case 3:
input_mode = INPUT_PULLDOWN;
break;
default:
input_mode = INPUT;
}
switch(gpiotype) {
case HASP_GPIO_SWITCH:
case HASP_GPIO_BUTTON:
gpioAddButton(pin, input_mode, default_state, channel);
break;
case HASP_GPIO_RELAY:
pinMode(pin, OUTPUT);
break;
// case HASP_GPIO_LED:
case HASP_GPIO_PWM:
case HASP_GPIO_BACKLIGHT:
pinMode(pin, OUTPUT);
#if defined(ARDUINO_ARCH_ESP32)
// configure LED PWM functionalitites
ledcSetup(channel, 20000, 10);
// attach the channel to the GPIO to be controlled
ledcAttachPin(pin, channel);
#endif
break;
}
}
/*
#if defined(ARDUINO_ARCH_ESP8266)
pinMode(D1, OUTPUT);
pinMode(D2, INPUT_PULLUP);
#endif
#if defined(STM32_CORE_VERSION)
#if defined(STM32F4xx)
pinMode(HASP_OUTPUT_PIN, OUTPUT);
pinMode(HASP_INPUT_PIN, INPUT_PULLUP);
pinMode(HASP_INPUT_PIN, INPUT);
#endif
*/
}

View File

@ -1,3 +1,25 @@
#ifndef HASP_GPIO_H
#define HASP_GPIO_H
#include "ArduinoJson.h"
void gpioSetup();
#ifdef __cplusplus
extern "C" {
#endif
void gpioSetup(void);
void IRAM_ATTR gpioLoop(void);
enum lv_hasp_gpio_type_t {
HASP_GPIO_SWITCH = 0,
HASP_GPIO_BUTTON = 1,
HASP_GPIO_RELAY = 2,
HASP_GPIO_PWM = 3,
HASP_GPIO_BACKLIGHT = 4,
};
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif