2#include "AudioTools/CoreAudio/AudioBasic/Collections/Vector.h"
3#include "AudioTools/CoreAudio/AudioLogger.h"
10#define DEBOUNCE_DELAY 500
13#if defined(IS_MIN_DESKTOP)
14extern "C" void pinMode(
int,
int);
22static AudioActions* selfAudioActions =
nullptr;
31 enum ActiveLogic : uint8_t {
42 void (*actionOn)(
bool pinStatus,
int pin,
void* ref) =
nullptr;
43 void (*actionOff)(
bool pinStatus,
int pin,
void* ref) =
nullptr;
45 unsigned long debounceTimeout = 0;
46 ActiveLogic activeLogic = ActiveHigh;
47 bool lastState =
true;
52 int touchLimit = TOUCH_LIMIT;
53 std::function<bool(
int)> read_cb =
nullptr;
55 virtual int id() {
return pin; }
57 virtual bool readValue() {
58#if defined(USE_TOUCH_READ)
60 if (this->activeLogic == ActiveTouch) {
61 int value = touchRead(this->pin);
62 result = value <= touchLimit;
65 value = touchRead(this->pin);
66 result = value <= touchLimit;
67 LOGI(
"touch pin: %d value %d (limit: %d) -> %s", this->pin, value,
68 touchLimit, result ?
"true" :
"false");
71 result = readPin(this->pin);
75 return this->readPin(this->pin);
79 virtual void process() {
81 bool value = readValue();
82 if (this->actionOn !=
nullptr && this->actionOff !=
nullptr) {
84 if (value != this->lastState) {
87 if ((value && this->activeLogic == ActiveHigh) ||
88 (!value && this->activeLogic == ActiveLow)) {
89 this->actionOn(
true, this->pin, this->ref);
91 this->actionOff(
false, this->pin, this->ref);
93 this->lastState = value;
95 }
else if (this->activeLogic == ActiveChange) {
98 if (value != this->lastState &&
millis() > this->debounceTimeout) {
101 this->actionOn(active, this->pin, this->ref);
102 this->lastState = value;
106 bool active = (this->activeLogic == ActiveLow) ? !value : value;
108 (active != this->lastState ||
millis() > this->debounceTimeout)) {
111 this->actionOn(active, this->pin, this->ref);
112 this->lastState = active;
120 bool readPin(
int pin) {
131 selfAudioActions =
this;
142 void add(
int pin,
void (*actionOn)(
bool pinStatus,
int pin,
void* ref),
143 ActiveLogic activeLogic = ActiveLow,
void* ref =
nullptr) {
144 add(pin, actionOn,
nullptr, activeLogic, ref);
148 void add(
int pin,
void (*actionOn)(
bool pinStatus,
int pin,
void* ref),
149 void (*actionOff)(
bool pinStatus,
int pin,
void* ref),
150 ActiveLogic activeLogicPar = ActiveLow,
void* ref =
nullptr) {
151 LOGI(
"ActionLogic::add pin: %d / logic: %d", pin, activeLogicPar);
154 setupPin(pin, activeLogicPar);
159 action.actionOn = actionOn;
160 action.actionOff = actionOff;
161 action.activeLogic = activeLogicPar;
164 action.touchLimit = touchLimit;
165 action.read_cb = read_cb;
167 insertAction(action);
169 LOGW(
"pin %d -> Ignored", pin);
177 p_action->enabled = enabled;
187 if (actions.empty())
return;
189 actions[pos]->process();
191 if (pos >= actions.size()) {
198 for (
Action* action : actions) {
205 for (
Action* action : actions) {
206 if (action->id() == id) {
216 for (
Action* action : actions) {
217 if (action->id() == id) {
235 for (Action* act : actions) {
245 int debounceDelayValue = DEBOUNCE_DELAY;
246 int touchLimit = TOUCH_LIMIT;
247 bool use_pin_interrupt =
false;
248 bool use_pin_mode =
true;
250 std::function<bool(
int)> read_cb =
nullptr;
252 void insertAction(Action& action) {
256 delete (actions[idx]);
257 actions[idx] = &action;
260 actions.push_back(&action);
266 void setupPin(
int pin, ActiveLogic logic) {
269 if (logic == ActiveLow) {
270 pinMode(pin, INPUT_PULLUP);
271 LOGI(
"pin %d -> INPUT_PULLUP", pin);
274 LOGI(
"pin %d -> INPUT", pin);
278#if defined(ARDUINO) && !defined(IS_MIN_DESKTOP)
279 if (use_pin_interrupt) {
280 attachInterrupt(digitalPinToInterrupt(pin), audioActionsISR, CHANGE);
int digitalRead(int pin)
e.g. for AudioActions
Definition NoArduino.h:201