Files
espurna/code/espurna/button.h
Maxim Prokhorov 514bc938c2 settings: rework string<->enum conversion
Make sure we seamlessly handle 'convert' for the number and the string version.
And since it is a two-way map, update 'serialize' to use it as well instead of
either a simple static_cast<int> or duplicating the same strings used in 'convert'

Only string literals or static vars can be used in constexpr context,
but those can still be shoved into the flash region via PROGMEM.
Notably, PSTR(...) inside of a lambda is not a constexpr.

Some quirks to work out
- we don't 'enumerate' things through compiler, enum values may go
  missing since it is not a switch-case
- 'get' default value via query still requires us to know the settings
  key in the first place. and it still needs an explicit call to
  'serialize'
- sensor units are stringified as their display value.
  but, this also avoids two different 'string' versions of those
- EnumOptions struct instance may also be in PROGMEM, but one needs
  to be very careful to only allow aligned access to it's members
  (which currently means we can't use 8bit or 16bit 'enum class'es)
2021-12-23 12:51:43 +03:00

109 lines
2.1 KiB
C++

/*
BUTTON MODULE
Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
*/
#pragma once
#include <Arduino.h>
#include "libs/BasePin.h"
#include "libs/DebounceEvent.h"
#include <cstdint>
#include <cstddef>
#include <memory>
constexpr size_t ButtonsActionMax { 255ul };
constexpr size_t ButtonsPresetMax { 8ul };
constexpr size_t ButtonsMax { 32ul };
enum class ButtonProvider {
None,
Gpio,
Analog
};
enum class ButtonEvent {
None,
Pressed,
Released,
Click,
DoubleClick,
LongClick,
LongLongClick,
TripleClick
};
enum class ButtonAction {
None,
Toggle,
On,
Off,
AccessPoint,
Reset,
Pulse,
FactoryReset,
Wps,
SmartConfig,
BrightnessIncrease,
BrightnessDecrease,
DisplayOn,
Custom,
FanLow,
FanMedium,
FanHigh
};
struct ButtonActions {
ButtonAction pressed;
ButtonAction released;
ButtonAction click;
ButtonAction dblclick;
ButtonAction lngclick;
ButtonAction lnglngclick;
ButtonAction trplclick;
};
struct ButtonEventDelays {
ButtonEventDelays();
ButtonEventDelays(unsigned long debounce, unsigned long repeat, unsigned long lngclick, unsigned long lnglngclick);
unsigned long debounce;
unsigned long repeat;
unsigned long lngclick;
unsigned long lnglngclick;
};
using ButtonEventEmitterPtr = std::unique_ptr<debounce_event::EventEmitter>;
struct Button {
Button(ButtonActions&& actions, ButtonEventDelays&& delays);
Button(BasePinPtr&& pin, const debounce_event::types::Config& config,
ButtonActions&& actions, ButtonEventDelays&& delays);
bool state();
ButtonEvent loop();
ButtonEventEmitterPtr event_emitter;
ButtonActions actions;
ButtonEventDelays event_delays;
};
using ButtonEventHandler = void(*)(size_t id, ButtonEvent event);
void buttonSetCustomAction(ButtonEventHandler);
ButtonAction buttonAction(size_t id, const ButtonEvent event);
void buttonEvent(size_t id, ButtonEvent event);
void buttonOnEvent(ButtonEventHandler);
bool buttonAdd();
size_t buttonCount();
void buttonSetup();