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 {
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;
58 virtual bool readValue() {
61 if (this->activeLogic == ActiveTouch) {
62 int value = touchRead(this->pin);
63 result = value <= touchLimit;
66 value = touchRead(this->pin);
67 result = value <= touchLimit;
68 LOGI(
"touch pin: %d value %d (limit: %d) -> %s", this->pin, value,
69 touchLimit, result ?
"true" :
"false");
80 virtual void process() {
82 bool value = readValue();
83 if (this->actionOn !=
nullptr && this->actionOff !=
nullptr) {
85 if (value != this->lastState) {
88 if ((value && this->activeLogic == ActiveHigh) ||
89 (!value && this->activeLogic == ActiveLow)) {
90 this->actionOn(
true, this->pin, this->ref);
92 this->actionOff(
false, this->pin, this->ref);
94 this->lastState = value;
96 }
else if (this->activeLogic == ActiveChange) {
99 if (value != this->lastState &&
millis() > this->debounceTimeout) {
102 this->actionOn(active, this->pin, this->ref);
103 this->lastState = value;
107 bool active = (this->activeLogic == ActiveLow) ? !value : value;
109 (active != this->lastState ||
millis() > this->debounceTimeout)) {
112 this->actionOn(active, this->pin, this->ref);
113 this->lastState = active;
123 selfAudioActions =
this;
134 insertAction(action);
138 void add(
int pin,
void (*actionOn)(
bool pinStatus,
int pin,
void *ref),
139 ActiveLogic activeLogic = ActiveLow,
void *ref =
nullptr) {
140 add(pin, actionOn,
nullptr, activeLogic, ref);
144 void add(
int pin,
void (*actionOn)(
bool pinStatus,
int pin,
void *ref),
145 void (*actionOff)(
bool pinStatus,
int pin,
void *ref),
146 ActiveLogic activeLogicPar = ActiveLow,
void *ref =
nullptr) {
147 LOGI(
"ActionLogic::add pin: %d / logic: %d", pin, activeLogicPar);
150 setupPin(pin, activeLogicPar);
155 action.actionOn = actionOn;
156 action.actionOff = actionOff;
157 action.activeLogic = activeLogicPar;
160 action.touchLimit = touchLimit;
162 insertAction(action);
164 LOGW(
"pin %d -> Ignored", pin);
172 p_action->enabled = enabled;
185 actions[pos]->process();
187 if (pos >= actions.size()) {
194 for (
Action *action : actions) {
201 for (
Action *action : actions) {
202 if (action->id() ==
id) {
212 for (
Action *action : actions) {
213 if (action->id() ==
id) {
231 for (Action *act : actions){
238 int debounceDelayValue = DEBOUNCE_DELAY;
239 int touchLimit = TOUCH_LIMIT;
240 bool use_pin_interrupt =
false;
241 bool use_pin_mode =
true;
242 Vector<Action*> actions{0};
244 void insertAction(Action& action){
248 delete(actions[idx]);
249 actions[idx] = &action;
252 actions.push_back(&action);
258 void setupPin(
int pin, ActiveLogic logic) {
261 if (logic == ActiveLow) {
262 pinMode(pin, INPUT_PULLUP);
263 LOGI(
"pin %d -> INPUT_PULLUP", pin);
266 LOGI(
"pin %d -> INPUT", pin);
270 #if !defined(IS_MIN_DESKTOP)
271 if (use_pin_interrupt) {
272 attachInterrupt(digitalPinToInterrupt(pin), audioActionsISR, CHANGE);
static int digitalRead(int pin)
e.g. for AudioActions
Definition: NoArduino.h:206