2 #include "AudioBasic/Collections/Vector.h"
3 #include "AudioTools/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;
45 bool lastState =
true;
50 int touchLimit = TOUCH_LIMIT;
55 if (this->activeLogic == ActiveTouch) {
56 int value = touchRead(this->pin);
57 result = value <= touchLimit;
60 value = touchRead(this->pin);
61 result = value <= touchLimit;
62 LOGI(
"touch pin: %d value %d (limit: %d) -> %s", this->pin, value,
63 touchLimit, result ?
"true" :
"false");
76 bool value = readValue();
77 if (this->actionOn !=
nullptr && this->actionOff !=
nullptr) {
79 if (value != this->lastState) {
82 if ((value && this->activeLogic == ActiveHigh) ||
83 (!value && this->activeLogic == ActiveLow)) {
84 this->actionOn(
true, this->pin, this->ref);
86 this->actionOff(
false, this->pin, this->ref);
88 this->lastState = value;
90 }
else if (this->activeLogic == ActiveChange) {
93 if (value != this->lastState &&
millis() > this->debounceTimeout) {
96 this->actionOn(active, this->pin, this->ref);
97 this->lastState = value;
101 bool active = (this->activeLogic == ActiveLow) ? !value : value;
103 (active != this->lastState ||
millis() > this->debounceTimeout)) {
106 this->actionOn(active, this->pin, this->ref);
107 this->lastState = active;
117 selfAudioActions =
this;
122 void add(
int pin,
void (*actionOn)(
bool pinStatus,
int pin,
void *ref),
123 ActiveLogic activeLogic = ActiveLow,
void *ref =
nullptr) {
124 add(pin, actionOn,
nullptr, activeLogic, ref);
128 void add(
int pin,
void (*actionOn)(
bool pinStatus,
int pin,
void *ref),
129 void (*actionOff)(
bool pinStatus,
int pin,
void *ref),
130 ActiveLogic activeLogicPar = ActiveLow,
void *ref =
nullptr) {
131 LOGI(
"ActionLogic::add pin: %d / logic: %d", pin, activeLogicPar);
134 setupPin(pin, activeLogicPar);
139 p_action->actionOn = actionOn;
140 p_action->actionOff = actionOff;
141 p_action->activeLogic = activeLogicPar;
147 action.actionOn = actionOn;
148 action.actionOff = actionOff;
149 action.activeLogic = activeLogicPar;
153 action.touchLimit = touchLimit;
155 actions.push_back(action);
158 LOGW(
"pin %d -> Ignored", pin);
166 p_action->enabled = enabled;
179 actions[pos].process();
181 if (pos >= actions.size()) {
188 for (
Action &action : actions) {
195 for (
Action &action : actions) {
196 if (action.pin == pin) {
213 int debounceDelayValue = DEBOUNCE_DELAY;
214 int touchLimit = TOUCH_LIMIT;
215 bool use_pin_interrupt =
false;
216 bool use_pin_mode =
true;
222 void setupPin(
int pin, ActiveLogic logic) {
225 if (logic == ActiveLow) {
226 pinMode(pin, INPUT_PULLUP);
227 LOGI(
"pin %d -> INPUT_PULLUP", pin);
230 LOGI(
"pin %d -> INPUT", pin);
234 #if !defined(IS_MIN_DESKTOP)
235 if (use_pin_interrupt) {
236 attachInterrupt(digitalPinToInterrupt(pin), audioActionsISR, CHANGE);
int digitalRead(int pin)
e.g. for AudioActions
Definition: NoArduino.h:205