-
Notifications
You must be signed in to change notification settings - Fork 6
/
esp-homekit-motor.c
309 lines (280 loc) · 11.3 KB
/
esp-homekit-motor.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
#include <stdio.h>
#include <espressif/esp_wifi.h>
#include <espressif/esp_sta.h>
#include <esp/uart.h>
#include <esp8266.h>
#include <FreeRTOS.h>
#include <task.h>
#include <queue.h>
#include <homekit/homekit.h>
#include <homekit/characteristics.h>
#include <wifi_config.h>
#include "button.h"
#define DURATION 10
static QueueHandle_t window_queue = NULL;
typedef enum {
window_command_idle,
window_command_open,
window_command_close
} window_command;
typedef enum {
window_state_idle,
window_state_opening,
window_state_closing,
} window_state;
static int duration = 0; //seconds
static window_state local_state = window_state_idle;
static int local_current_position = 0;
static int local_target_position = 0;
void on_update_target_position(homekit_characteristic_t *ch, homekit_value_t value, void *context);
homekit_value_t getter_current_position();
homekit_value_t getter_position_state();
homekit_value_t getter_target_position();
homekit_characteristic_t current_position = HOMEKIT_CHARACTERISTIC_(CURRENT_POSITION, 0, .getter=getter_current_position);
homekit_characteristic_t target_position = HOMEKIT_CHARACTERISTIC_(TARGET_POSITION, 0, .callback=HOMEKIT_CHARACTERISTIC_CALLBACK(on_update_target_position), .getter=getter_target_position);
homekit_characteristic_t position_state = HOMEKIT_CHARACTERISTIC_(POSITION_STATE, 0, .getter=getter_position_state);
const int led_gpio = 2;
const int up_gpio = 12;
const int down_gpio = 4;
const int button1_gpio = 0; //14=D5 //0=button //9=reset //5=D1
const int button2_gpio = 5;
#define start_up() gpio_write(up_gpio, 1);
#define stop_up() gpio_write(up_gpio, 0);
#define start_down() gpio_write(down_gpio, 1);
#define stop_down() gpio_write(down_gpio, 0);
#define led_on() gpio_write(led_gpio, 0);
#define led_off() gpio_write(led_gpio, 1);
void gpio_init() {
gpio_enable(led_gpio, GPIO_OUTPUT);
gpio_enable(up_gpio, GPIO_OUTPUT);
gpio_enable(down_gpio, GPIO_OUTPUT);
gpio_enable(button1_gpio, GPIO_INPUT);
gpio_enable(button2_gpio, GPIO_INPUT);
stop_up();
stop_down();
led_off();
}
void led_identify_task(void *_args) {
for (int i=0; i<3; i++) {
for (int j=0; j<2; j++) {
led_on();
vTaskDelay(100 / portTICK_PERIOD_MS);
led_off();
vTaskDelay(100 / portTICK_PERIOD_MS);
}
vTaskDelay(250 / portTICK_PERIOD_MS);
}
led_off();
vTaskDelete(NULL);
}
void led_identify(homekit_value_t _value) {
printf("LED identify\n");
xTaskCreate(led_identify_task, "LED identify", 128, NULL, 2, NULL);
}
void send_window_command(window_command cmd) {
xQueueSendToBack(window_queue, &cmd, 10/portTICK_PERIOD_MS);
}
void window_task_commands(void *context) {
printf("Window Task Commands initialized\n");
window_command cmd;
while(1) {
if (xQueueReceive(window_queue, &cmd, 100/portTICK_PERIOD_MS)) {
switch(cmd) {
case window_command_idle: {
printf("command IDLE received\n");
}
case window_command_open: {
printf("command OPEN received\n");
switch(local_state) {
case window_state_idle:
start_up();
homekit_characteristic_notify(&position_state, HOMEKIT_UINT8(window_state_opening));
local_state = window_state_opening;
break;
case window_state_opening:
break;
case window_state_closing:
stop_down();
local_state = window_state_idle;
vTaskDelay(50 / portTICK_PERIOD_MS);
homekit_characteristic_notify(&target_position, HOMEKIT_UINT8(local_target_position));
break;
}
break;
}
case window_command_close: {
switch(local_state) {
printf("command CLOSE received\n");
case window_state_idle:
start_down();
homekit_characteristic_notify(&position_state, HOMEKIT_UINT8(window_state_closing));
local_state = window_state_closing;
break;
case window_state_opening:
stop_up();
local_state = window_state_idle;
vTaskDelay(50 / portTICK_PERIOD_MS);
homekit_characteristic_notify(&target_position, HOMEKIT_UINT8(local_target_position));
break;
case window_state_closing:
break;
}
break;
}
}
}
}
}
void window_task_state(void *context) {
TickType_t xLastWakeTime = xTaskGetTickCount();
const TickType_t xFrequency = duration*10 / portTICK_PERIOD_MS;
printf("Window Task State initialized with Delay: %d ticks\n", xFrequency);
while(1) {
vTaskDelayUntil(&xLastWakeTime, xFrequency);
switch(local_state) {
case window_state_idle:
break;
case window_state_opening:
local_current_position++;
if (local_current_position > 100) local_current_position = 100;
homekit_characteristic_notify(¤t_position, HOMEKIT_UINT8(local_current_position));
printf("Current Position: %d\n", local_current_position);
if (local_current_position == local_target_position) {
stop_up();
homekit_characteristic_notify(¤t_position, HOMEKIT_UINT8(local_current_position));
homekit_characteristic_notify(&position_state, HOMEKIT_UINT8(window_state_idle));
local_state = window_state_idle;
}
break;
case window_state_closing:
local_current_position--;
if (local_current_position < 0) local_current_position = 0;
homekit_characteristic_notify(¤t_position, HOMEKIT_UINT8(local_current_position));
printf("Current Position: %d\n", local_current_position);
if (local_current_position == local_target_position) {
stop_down();
homekit_characteristic_notify(¤t_position, HOMEKIT_UINT8(local_current_position));
homekit_characteristic_notify(&position_state, HOMEKIT_UINT8(window_state_idle));
local_state = window_state_idle;
}
break;
}
}
}
void window_init(int dur) {
local_state = window_state_idle;
local_current_position = 0;
local_target_position = 0;
duration = dur; //seconds
window_queue = xQueueCreate(1, sizeof(window_command));
xTaskCreate(window_task_commands, "Window Task Commands", 256, NULL, 2, NULL);
xTaskCreate(window_task_state, "Window Task State", 256, NULL, 2, NULL);
}
void button_callback(uint8_t gpio_num, button_event_t event) {
switch(local_state) {
case window_state_idle:
if (gpio_num == button1_gpio) {
printf("Button: Setting homekit target position to 100\n");
homekit_characteristic_notify(&target_position, HOMEKIT_UINT8(100));
}
else if (gpio_num == button2_gpio) {
printf("Button: Setting homekit target position to 0\n");
homekit_characteristic_notify(&target_position, HOMEKIT_UINT8(0));
}
break;
case window_state_opening:
case window_state_closing:
stop_up(); stop_down();
local_target_position = local_current_position;
homekit_characteristic_notify(&target_position, HOMEKIT_UINT8(local_target_position));
homekit_characteristic_notify(¤t_position, HOMEKIT_UINT8(local_current_position));
homekit_characteristic_notify(&position_state, HOMEKIT_UINT8(window_state_idle));
local_state = window_state_idle;
break;
}
}
void on_update_target_position(homekit_characteristic_t *ch, homekit_value_t value, void *context) {
if (value.format != homekit_format_uint8) {
printf("Invalid value format: %d\n", value.format);
return;
};
local_target_position = value.int_value;
printf("HOMEKIT: Setting target position to %d\n", local_target_position);
if (local_target_position > local_current_position) {
printf("HOMEKIT: Sending Window OPEN\n");
send_window_command(window_command_open);
}
else if (local_target_position < local_current_position) {
printf("HOMEKIT: Sending Window CLOSE\n");
send_window_command(window_command_close);
}
else {
printf("HOMEKIT: No need to change position\n");
}
return;
}
homekit_value_t getter_current_position() {
return HOMEKIT_UINT8(local_current_position);
}
homekit_value_t getter_position_state() {
return HOMEKIT_UINT8(local_state);
}
homekit_value_t getter_target_position() {
return HOMEKIT_UINT8(local_target_position);
}
homekit_characteristic_t name = HOMEKIT_CHARACTERISTIC_(NAME, "ESP Homekit Motor");
homekit_accessory_t *accessories[] = {
HOMEKIT_ACCESSORY(.id=1, .category=homekit_accessory_category_window, .services=(homekit_service_t*[]){
HOMEKIT_SERVICE(ACCESSORY_INFORMATION, .characteristics=(homekit_characteristic_t*[]){
&name,
HOMEKIT_CHARACTERISTIC(MANUFACTURER, "iTead"),
HOMEKIT_CHARACTERISTIC(SERIAL_NUMBER, "037A2BABF19D"),
HOMEKIT_CHARACTERISTIC(MODEL, "Sonoff T1 EU"),
HOMEKIT_CHARACTERISTIC(FIRMWARE_REVISION, "0.1"),
HOMEKIT_CHARACTERISTIC(IDENTIFY, led_identify),
NULL
}),
HOMEKIT_SERVICE(WINDOW, .primary=true, .characteristics=(homekit_characteristic_t*[]){
HOMEKIT_CHARACTERISTIC(NAME, "ESP Homekit Motor"),
¤t_position,
&target_position,
&position_state,
NULL
}),
NULL
}),
NULL
};
homekit_server_config_t config = {
.accessories = accessories,
.password = "111-11-111"
};
void buttons_init() {
if (button_create(button1_gpio, 0, 4000, button_callback)) {
printf("Failed to initialize button\n");
}
if (button_create(button2_gpio, 0, 4000, button_callback)) {
printf("Failed to initialize button\n");
}
}
void create_accessory_name() {
uint8_t macaddr[6];
sdk_wifi_get_macaddr(STATION_IF, macaddr);
int name_len = snprintf(NULL, 0, "ESP Homekit Motor %02X:%02X:%02X",
macaddr[3], macaddr[4], macaddr[5]);
char *name_value = malloc(name_len+1);
snprintf(name_value, name_len+1, "ESP Homekit Motor %02X:%02X:%02X",
macaddr[3], macaddr[4], macaddr[5]);
name.value = HOMEKIT_STRING(name_value);
}
void on_wifi_ready() {
homekit_server_init(&config);
window_init(DURATION);
buttons_init();
}
void user_init(void) {
uart_set_baud(0, 115200);
create_accessory_name();
gpio_init();
wifi_config_init("ESP-Homekit-Motor", NULL, on_wifi_ready);
}