-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActions.h
More file actions
135 lines (115 loc) · 3.29 KB
/
Actions.h
File metadata and controls
135 lines (115 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#ifndef ACTIONS_H
#define ACTIONS_H
#include <Adafruit_SleepyDog.h>
class Actions;
#include "Defaults.h"
#include "Cli.h"
#include "Logger.h"
#include "Dispatcher.h"
#include "Utils.h"
#include "Relay.h"
#include "Keys.h"
#include "CBUS.h"
#include "CBUSConfig.h"
#include "AudioBoard.h"
#define ACTIVITY_IDLE -1
class Actions {
Relay * relay;
AudioBoard * audio;
CBUS * cbus;
CBUSConfig * config;
Dispatcher<Actions> * dispatcher;
Keys * keys;
void (*keepAlive)();
int runningCount;
public:
Actions() : relay(nullptr),
audio(nullptr),
dispatcher(nullptr),
keys(nullptr),
keepAlive(nullptr),
cbus(nullptr),
config(nullptr),
runningCount(ACTIVITY_IDLE){
};
// Initialize all static members
void init(Relay * r, AudioBoard * a, CBUS * cbus, CBUSConfig * c, Keys * k, Dispatcher<Actions> * d, void (*wdtCb)()){
this->audio = a;
this->relay = r;
this->keys = k;
this->dispatcher = d;
this->keepAlive = wdtCb;
this->cbus = cbus;
this->config = c;
};
void checkPushButtonActivity(){
if(runningCount>0){
trace.log("Actions", "checkPushButtonActivity. Activity running", runningCount);
runningCount--; //Decrement 1 and keep going
return;
}
if(runningCount==0){
//Last tick -> disable activity
trace.log("Actions", "checkPushButtonActivity", "Activity completed");
relay->off();
audio->stopPlaying();
runningCount = ACTIVITY_IDLE;
return;
}
}
void checkKeyAction(){
if(keys->isOn()){
trace.log("Actions", "checkKeysAction", "Key pressed");
if(runningCount == ACTIVITY_IDLE){ //Action is IDLE, start activity
trace.log("Actions", "checkKeysAction", "Activating relay & default audio");
relay->on();
audio->play(config->getDefaultAudio());
runningCount = SEC_TO_TICKS(15);
}
return;
}
};
void checkCBUSCommandAction(){
int nodeNumber, eventNumber;
auto cmd = cbus->getEvent(&nodeNumber, &eventNumber);
if(!cmd){
trace.log("Actions", "No command received");
return;
}
if(nodeNumber != config->getNodeNumber()){
trace.log("Actions", "Ignoring Event from Node: ", nodeNumber);
return;
}
//Check if event number is mapped to the relay
if(eventNumber == config->getRelayEventNumber()){
if(cmd == ACON){
trace.log("Actions", "Event for activation of relay received");
relay->on();
} else {
if(cmd == ACOF){
trace.log("Actions", "Event for deactivation of relay received");
relay->off();
}
}
}
//Check if event number is mapped to any audio file
char * track = config->getAudioByEventNumber(eventNumber);
if(track){
if(cmd == ACON){
trace.log("Actions", "Event for activation of audio received");
audio->play(track);
return;
} else {
if(cmd == ACOF){
trace.log("Actions", "Event for deactivation of audio received");
audio->stopPlaying();
return;
}
}
}
// The event comes from a recognized node, but it is not mapped to any action here
trace.log("Actions", "Unmapped event: ", eventNumber);
return;
};
};
#endif