This repository has been archived by the owner on Jun 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coral.h
213 lines (199 loc) · 5.41 KB
/
coral.h
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#ifndef CORAL_H_
#define CORAL_H_
#include <string>
#include <vector>
#include <memory>
#include <new>
#include <memory>
#include <scoped_allocator>
#include <Arduino.h>
using namespace std;
typedef void (*func_ptr_void)(void);
typedef void (*func_ptr_int)(int);
typedef int (*func_ptr_return_int)(void);
typedef void (*func_ptr_color)(int, int, int);
/*
* General base class for all devices
*/
class Device {
private:
protected:
func_ptr_void on_func; // function pointer to user-defined on function
func_ptr_void off_func; // function pointer to user-defined off function
public:
string id; // Unique id for device
string type; // type of device, such as "switch", "slider", "rgb", "sensor"
int status; // on or off
vector<string> states;
// Constructor:
Device(const string& d_id, const string& d_type, func_ptr_void d_on_func, func_ptr_void d_off_func);
// Deconstructor:
~Device(){}
// return status:
inline int get_status() { return status; }
// Sets user-defined on function:
void set_on_func(func_ptr_void func_to_call) {
on_func = func_to_call;
}
// Sets user-defined off function:
void set_off_func(func_ptr_void func_to_call) {
off_func = func_to_call;
}
// To be implemented by the device type:
virtual void on() {
status = 1;
if (on_func)
on_func();
}
virtual void off() {
status = 0;
if (off_func)
off_func();
}
};
/*
* Class for all devices that maintain a basic on/off state
* Examples: lights, doorlock
*/
class Switch: public Device {
public:
Switch(const string& d_id, const string& d_type, func_ptr_void d_on_func = 0, func_ptr_void d_off_func = 0) :
Device(d_id, d_type, d_on_func, d_off_func) {};
void on() {
status = 1;
if (on_func)
on_func();
}
void off() {
status = 0;
if (off_func)
off_func();
}
};
/*
* Class for all devices that maintain a basic on/off state and a sliding variable state
* Examples: lights with dimmer, servo
*/
class Slider: public Device {
private:
int slider_val;
func_ptr_int slider_func;
public:
Slider(const string& d_id, const string& d_type, func_ptr_void d_on_func = 0, func_ptr_void d_off_func = 0, func_ptr_int d_slider_func = 0) :
Device(d_id, d_type, d_on_func, d_off_func), slider_func(d_slider_func) {};
void set_slider_func(func_ptr_int func_to_call) {
slider_func = func_to_call;
}
void update_slider(int val) {
slider_val = val;
if (slider_func)
slider_func(val);
}
int value() {
return slider_val;
}
void on() {
status = 1;
slider_val = 100;
if (on_func)
on_func();
}
void off() {
status = 0;
slider_val = 0;
if (off_func)
off_func();
}
};
/*
* Class for all devices that maintain a basic on/off state and a rgb values
* Examples: lights
*/
class RGB_device: public Device {
private:
func_ptr_color color_func;
public:
RGB_device(const string& d_id, const string& d_type, func_ptr_void d_on_func = 0, func_ptr_void d_off_func = 0, func_ptr_color d_color_func = 0) :
Device(d_id, d_type, d_on_func, d_off_func), color_func(d_color_func) {};
int r;
int g;
int b;
void set_color_func(func_ptr_color func_to_call) {
color_func = func_to_call;
}
void red(int val) {
r = val;
}
void blue(int val) {
g = val;
}
void green(int val) {
b = val;
}
void init_colors(int red, int green, int blue) {
r = red;
g = green;
b = blue;
}
void set_colors(int red, int green, int blue) {
init_colors(red,green,blue);
if (color_func)
color_func(r, g, b);
}
void on() {
status = 1;
r = 100;
g = 100;
b = 100;
if (color_func)
color_func(r,g,b);
}
void off() {
status = 0;
r = 0;
g = 0;
b = 0;
if (color_func)
color_func(0,0,0);
}
};
/*
* Class for all devices that are sensor related
* Examples: temperature sensor
*/
class Sensor: public Device {
private:
func_ptr_return_int read_func;
public:
Sensor(const string& d_id, const string& d_type, func_ptr_void d_on_func = 0, func_ptr_void d_off_func = 0, func_ptr_return_int d_sense_func = 0) :
Device(d_id, d_type, d_on_func, d_off_func), read_func(d_sense_func) {};
// func_to_call should return an int value that can then be returned by read()
void set_read_func(func_ptr_return_int func_to_call) {
read_func = func_to_call;
}
int read() {
if (read_func)
return read_func();
return -1;
}
void on() {
status = 1;
if (on_func)
on_func();
}
void off() {
status = 0;
if (off_func)
off_func();
}
};
// Function prototypes:
void add_switch(const string& id, func_ptr_void d_on_func, func_ptr_void d_off_func);
void add_slider(const string& id, func_ptr_void d_on_func, func_ptr_void d_off_func, func_ptr_int d_slider_func);
void add_rgb(const string& id, func_ptr_void d_on_func, func_ptr_void d_off_func, func_ptr_color d_color_func);
void add_sensor(const string& id, func_ptr_void d_on_func, func_ptr_void d_off_func);
void make_device_list(String& list);
void coral_setup();
Device* get_device(string device_id);
int parse_cmd(String cmd);
#endif // #ifndef _CORAL_H_