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)
14 extern "C" void pinMode(
int,
int);
22 static AudioActions *selfAudioActions =
nullptr;
31 enum ActiveLogic : uint8_t {
40 void (*actionOn)(
bool pinStatus,
int pin,
void *ref) =
nullptr;
41 void (*actionOff)(
bool pinStatus,
int pin,
void *ref) =
nullptr;
43 unsigned long debounceTimeout = 0;
44 ActiveLogic activeLogic = ActiveHigh;
45 bool lastState =
true;
50 int touchLimit = TOUCH_LIMIT;
56 virtual bool readValue() {
59 if (this->activeLogic == ActiveTouch) {
60 int value = touchRead(this->pin);
61 result = value <= touchLimit;
64 value = touchRead(this->pin);
65 result = value <= touchLimit;
66 LOGI(
"touch pin: %d value %d (limit: %d) -> %s", this->pin, value,
67 touchLimit, result ?
"true" :
"false");
78 virtual void process() {
80 bool value = readValue();
81 if (this->actionOn !=
nullptr && this->actionOff !=
nullptr) {
83 if (value != this->lastState) {
86 if ((value && this->activeLogic == ActiveHigh) ||
87 (!value && this->activeLogic == ActiveLow)) {
88 this->actionOn(
true, this->pin, this->ref);
90 this->actionOff(
false, this->pin, this->ref);
92 this->lastState = value;
94 }
else if (this->activeLogic == ActiveChange) {
97 if (value != this->lastState &&
millis() > this->debounceTimeout) {
100 this->actionOn(active, this->pin, this->ref);
101 this->lastState = value;
105 bool active = (this->activeLogic == ActiveLow) ? !value : value;
107 (active != this->lastState ||
millis() > this->debounceTimeout)) {
110 this->actionOn(active, this->pin, this->ref);
111 this->lastState = active;
121 selfAudioActions =
this;
132 insertAction(action);
136 void add(
int pin,
void (*actionOn)(
bool pinStatus,
int pin,
void *ref),
137 ActiveLogic activeLogic = ActiveLow,
void *ref =
nullptr) {
138 add(pin, actionOn,
nullptr, activeLogic, ref);
142 void add(
int pin,
void (*actionOn)(
bool pinStatus,
int pin,
void *ref),
143 void (*actionOff)(
bool pinStatus,
int pin,
void *ref),
144 ActiveLogic activeLogicPar = ActiveLow,
void *ref =
nullptr) {
145 LOGI(
"ActionLogic::add pin: %d / logic: %d", pin, activeLogicPar);
148 setupPin(pin, activeLogicPar);
153 action.actionOn = actionOn;
154 action.actionOff = actionOff;
155 action.activeLogic = activeLogicPar;
158 action.touchLimit = touchLimit;
160 insertAction(action);
162 LOGW(
"pin %d -> Ignored", pin);
170 p_action->enabled = enabled;
183 actions[pos]->process();
185 if (pos >= actions.size()) {
192 for (
Action *action : actions) {
199 for (
Action *action : actions) {
200 if (action->id() ==
id) {
210 for (
Action *action : actions) {
211 if (action->id() ==
id) {
229 for (Action *act : actions){
236 int debounceDelayValue = DEBOUNCE_DELAY;
237 int touchLimit = TOUCH_LIMIT;
238 bool use_pin_interrupt =
false;
239 bool use_pin_mode =
true;
240 Vector<Action*> actions{0};
242 void insertAction(Action& action){
246 delete(actions[idx]);
247 actions[idx] = &action;
250 actions.push_back(&action);
256 void setupPin(
int pin, ActiveLogic logic) {
259 if (logic == ActiveLow) {
260 pinMode(pin, INPUT_PULLUP);
261 LOGI(
"pin %d -> INPUT_PULLUP", pin);
264 LOGI(
"pin %d -> INPUT", pin);
268 #if !defined(IS_MIN_DESKTOP)
269 if (use_pin_interrupt) {
270 attachInterrupt(digitalPinToInterrupt(pin), audioActionsISR, CHANGE);
int digitalRead(int pin)
e.g. for AudioActions
Definition: NoArduino.h:206