-
Notifications
You must be signed in to change notification settings - Fork 0
/
thing_4_outputs.c
334 lines (300 loc) · 8.85 KB
/
thing_4_outputs.c
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
* thing_4_outputs.c
*
* Created on: Apr 20, 2021
* Author: Krzysztof Zurek
* e-mail: [email protected]
*/
#include <inttypes.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/timers.h"
#include "esp_system.h"
#include "esp_log.h"
#include "driver/gpio.h"
#include "simple_web_thing_server.h"
#include "thing_4_outputs.h"
//button GPIO
#define GPIO_OUTPUT_1 (CONFIG_OUTPUT_1_GPIO)
#define GPIO_OUTPUT_2 (CONFIG_OUTPUT_2_GPIO)
#define GPIO_OUTPUT_3 (CONFIG_OUTPUT_3_GPIO)
#define GPIO_OUTPUT_4 (CONFIG_OUTPUT_4_GPIO)
#define GPIO_OUTPUT_MASK (1ULL << GPIO_OUTPUT_1)|(1ULL << GPIO_OUTPUT_2)|\
(1ULL << GPIO_OUTPUT_3)|(1ULL << GPIO_OUTPUT_4)
#define CNT_MAX_OUT 1000
//xSemaphoreHandle DRAM_ATTR output_sem;
xSemaphoreHandle DRAM_ATTR thing_mux;
static int32_t DRAM_ATTR output_counter = 0;
thing_t *iot_4_outputs = NULL;
property_t *prop_output_1, *prop_output_2, *prop_output_3, *prop_output_4;
property_t *prop_output_counter, *prop_out_on_off;
at_type_t iot_4_outputs_type, output_1_prop_type, output_3_prop_type;
at_type_t output_4_prop_type;
at_type_t output_counter_prop_type, on_off_prop_type;
static bool out_1, out_2, out_3, out_4;
static bool on_off_state = false;
/* *****************************************************************
*
* turn the device ON or OFF
*
* *****************************************************************/
int16_t set_output(char *name, char *new_value_str){
int8_t output_nr = -1;
bool new_value = false;
bool prev_on_state;
int16_t result = 0;
output_counter++;
if (output_counter > CNT_MAX_OUT){
output_counter = 0;
}
if (strcmp(name, prop_output_1 -> id) == 0){
output_nr = 1;
}
else if (strcmp(name, prop_output_2 -> id) == 0){
output_nr = 2;
}
else if (strcmp(name, prop_output_3 -> id) == 0){
output_nr = 3;
}
else if (strcmp(name, prop_output_4 -> id) == 0){
output_nr = 4;
}
else{
return -1;
}
if (strcmp(new_value_str, "true") == 0){
new_value = true;
}
if (output_nr >= 0){
switch(output_nr){
case 1:
if (new_value == true){
gpio_set_level(GPIO_OUTPUT_1, 1);
if (out_1 == false){
out_1 = true;
result = 1;
}
}
else{
gpio_set_level(GPIO_OUTPUT_1, 0);
if (out_1 == true){
out_1 = false;
result = 1;
}
}
break;
case 2:
if (new_value == true){
gpio_set_level(GPIO_OUTPUT_2, 1);
if (out_2 == false){
out_2 = true;
result = 1;
}
}
else{
gpio_set_level(GPIO_OUTPUT_2, 0);
if (out_2 == true){
out_2 = false;
result = 1;
}
}
break;
case 3:
if (new_value == true){
gpio_set_level(GPIO_OUTPUT_3, 1);
if (out_3 == false){
out_3 = true;
result = 1;
}
}
else{
gpio_set_level(GPIO_OUTPUT_3, 0);
if (out_3 == true){
out_3 = false;
result = 1;
}
}
break;
case 4:
if (new_value == true){
gpio_set_level(GPIO_OUTPUT_4, 1);
if (out_4 == false){
out_4 = true;
result = 1;
}
}
else{
gpio_set_level(GPIO_OUTPUT_4, 0);
if (out_4 == true){
out_4 = false;
result = 1;
}
}
break;
}
}
prev_on_state = on_off_state;
if ((out_1 == true)||(out_2 == true)||(out_3 == true)||(out_4 == true)){
on_off_state = true;
}
else{
on_off_state = false;
}
if (prev_on_state != on_off_state){
inform_all_subscribers_prop(prop_out_on_off);
}
if (output_counter%10 == 0){
inform_all_subscribers_prop(prop_output_counter);
}
return result;
}
/*************************************************************
*
* main button function
*
* ************************************************************/
void outputs_fun(void *pvParameter){
uint32_t cnt = 0;
printf("Outputs task is ready\n");
for(;;){
vTaskDelay(30000 / portTICK_PERIOD_MS);
cnt++;
//printf("Output cnt: %i\n", cnt);
}
}
/*******************************************************************
*
* initialize button's GPIO
*
* ******************************************************************/
void init_outputs_io(void){
gpio_config_t io_conf;
//interrupt on both edges
io_conf.intr_type = GPIO_PIN_INTR_DISABLE;
//bit mask of the pins
io_conf.pin_bit_mask = GPIO_OUTPUT_MASK;
//set as output mode
io_conf.mode = GPIO_MODE_OUTPUT;
//enable pull-up mode
io_conf.pull_up_en = 0;
io_conf.pull_down_en = 0;
gpio_config(&io_conf);
gpio_set_level(GPIO_OUTPUT_1, 0);
gpio_set_level(GPIO_OUTPUT_2, 0);
gpio_set_level(GPIO_OUTPUT_3, 0);
gpio_set_level(GPIO_OUTPUT_4, 0);
}
/*****************************************************************
*
* Initialize button thing and all it's properties and event
*
* ****************************************************************/
thing_t *init_4_outputs(void){
out_1 = false;
out_2 = false;
out_3 = false;
out_4 = false;
//vSemaphoreCreateBinary(output_sem);
//xSemaphoreTake(output_sem, 0);
init_outputs_io();
thing_mux = xSemaphoreCreateMutex();
//create button thing
iot_4_outputs = thing_init();
iot_4_outputs -> id = "4-outputs";
iot_4_outputs -> at_context = things_context;
iot_4_outputs -> model_len = 2000;
//set @type
iot_4_outputs_type.at_type = "MultiLevelSwitch";
iot_4_outputs_type.next = NULL;
set_thing_type(iot_4_outputs, &iot_4_outputs_type);
iot_4_outputs -> description = "4 outputs";
//create output-1 property
prop_output_1 = property_init(NULL, NULL);
prop_output_1 -> id = "out-1";
prop_output_1 -> description = "output-1 value";
output_1_prop_type.at_type = "BooleanProperty";
output_1_prop_type.next = NULL;
prop_output_1 -> at_type = &output_1_prop_type;
prop_output_1 -> type = VAL_BOOLEAN;
prop_output_1 -> value = &out_1;
prop_output_1 -> title = "Output-1";
prop_output_1 -> read_only = false;
prop_output_1 -> set = set_output;
prop_output_1 -> mux = thing_mux;
add_property(iot_4_outputs, prop_output_1); //add property to thing
//create output-2 property
prop_output_2 = property_init(NULL, NULL);
prop_output_2 -> id = "out-2";
prop_output_2 -> description = "output-2 value";
prop_output_2 -> at_type = &output_1_prop_type;
prop_output_2 -> type = VAL_BOOLEAN;
prop_output_2 -> value = &out_2;
prop_output_2 -> title = "Output-2";
prop_output_2 -> read_only = false;
prop_output_2 -> set = set_output;
prop_output_2 -> mux = thing_mux;
add_property(iot_4_outputs, prop_output_2); //add property to thing
//create output-3 property
prop_output_3 = property_init(NULL, NULL);
prop_output_3 -> id = "out-3";
prop_output_3 -> description = "output-3 value";
prop_output_3 -> at_type = &output_1_prop_type;
prop_output_3 -> type = VAL_BOOLEAN;
prop_output_3 -> value = &out_3;
prop_output_3 -> title = "Output-3";
prop_output_3 -> read_only = false;
prop_output_3 -> set = set_output;
prop_output_3 -> mux = thing_mux;
add_property(iot_4_outputs, prop_output_3); //add property to thing
//create output-4 property
prop_output_4 = property_init(NULL, NULL);
prop_output_4 -> id = "out-4";
prop_output_4 -> description = "output-4 value";
prop_output_4 -> at_type = &output_1_prop_type;
prop_output_4 -> type = VAL_BOOLEAN;
prop_output_4 -> value = &out_4;
prop_output_4 -> title = "Output-4";
prop_output_4 -> read_only = false;
prop_output_4 -> set = set_output;
prop_output_4 -> mux = thing_mux;
add_property(iot_4_outputs, prop_output_4); //add property to thing
//create pushed property
prop_out_on_off = property_init(NULL, NULL);
prop_out_on_off -> id = "on-off";
prop_out_on_off -> description = "outputs value";
on_off_prop_type.at_type = "OnOffProperty";
on_off_prop_type.next = NULL;
prop_out_on_off -> at_type = &on_off_prop_type;
prop_out_on_off -> type = VAL_BOOLEAN;
prop_out_on_off -> value = &on_off_state;
prop_out_on_off -> title = "ON-OFF";
prop_out_on_off -> read_only = true;
prop_out_on_off -> set = NULL;
prop_out_on_off -> mux = thing_mux;
on_off_state = false;
add_property(iot_4_outputs, prop_out_on_off); //add property to thing
//create on-off counter property
prop_output_counter = property_init(NULL, NULL);
prop_output_counter -> id = "counter";
prop_output_counter -> description = "output counter";
output_counter_prop_type.at_type = "LevelProperty";
output_counter_prop_type.next = NULL;
prop_output_counter -> at_type = &output_counter_prop_type;
prop_output_counter -> type = VAL_INTEGER;
prop_output_counter -> value = &output_counter;
prop_output_counter -> max_value.int_val = CNT_MAX_OUT;
prop_output_counter -> min_value.int_val = 0;
prop_output_counter -> title = "Counter";
prop_output_counter -> read_only = true;
prop_output_counter -> set = NULL;
prop_output_counter -> mux = thing_mux;
add_property(iot_4_outputs, prop_output_counter); //add property to thing
//if (output_sem != NULL){
xTaskCreate(&outputs_fun, "outputs_task",
configMINIMAL_STACK_SIZE * 4, NULL, 0, NULL);
//}
return iot_4_outputs;
}