-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathklik.c
291 lines (247 loc) · 6.44 KB
/
klik.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
/*
* File: klik.c
* Project: Klik
* -----
* This source code is released under BSD-3 license.
* Check LICENSE file for full list of conditions and disclaimer.
* -----
* Copyright 2022 - 2023 M.Kusiak (timax)
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "pico/stdlib.h"
#include "button.h"
#include "serial.h"
#include "led.h"
#include "servo.h"
#include "request.h"
#include "config.h"
#define BUTTON_PIN 26
#define LED_BLUE 20
#define LED_GREEN 19
#define LED_RED 18
#define SERVO_PIN 21
#define RESPONSE_VALUE_STRING "\"value\":\""
#define TAP_BREAK_TIME 500
#define BREAK_TIME 1000
typedef enum
{
KLIK_STATE_SETUP,
KLIK_STATE_CONNECTING,
KLIK_STATE_CONNECTION_ERROR,
KLIK_STATE_REQUEST_ERROR,
KLIK_STATE_WORKING,
KLIK_STATE_UNDEFINED
} state_t;
typedef enum
{
KLIK_MODE_OFF,
KLIK_MODE_ON,
KLIK_MODE_TAP,
KLIK_MODE_DOUBLE_TAP
} klik_mode_t;
/**
* @brief Blinks led diode according to state.
*
* @param state current device state.
*/
void diodeSetState(state_t state)
{
static bool firstTimeSetupDone;
static ledDiode_t led;
static struct repeating_timer timer;
if (!firstTimeSetupDone)
{
ledDiodeSetup(&led, LED_BLUE, LED_GREEN, LED_RED);
firstTimeSetupDone = true;
}
else
{
cancel_repeating_timer(&timer);
ledDiodeDim(&led);
}
switch (state)
{
case KLIK_STATE_SETUP:
ledCycle(&led, &timer, 200, COLOR_GREEN, COLOR_BLUE);
break;
case KLIK_STATE_CONNECTING:
ledCycle(&led, &timer, 200, COLOR_BLUE, COLOR_RED);
break;
case KLIK_STATE_CONNECTION_ERROR:
ledBlink(&led, &timer, COLOR_RED, 1000);
break;
case KLIK_STATE_REQUEST_ERROR:
ledBlink(&led, &timer, COLOR_BLUE, 1000);
break;
case KLIK_STATE_WORKING:
ledBlink(&led, &timer, COLOR_GREEN, 5000);
break;
case KLIK_STATE_UNDEFINED:
break;
}
}
/**
* @brief Gets the value from request.
*
* @param response request response.
* @return int8_t value from request,
* if value negative then bad request or overfilled value.
*/
int8_t getValueFromResponse(char *response)
{
int8_t value = 0;
char *valueString = strstr(response, RESPONSE_VALUE_STRING);
if (!valueString)
return -1;
valueString += strlen(RESPONSE_VALUE_STRING);
while (isdigit(valueString[0]))
{
value *= 10;
value += valueString[0] - '0';
valueString++;
}
return value;
}
/**
* @brief Moves servo to corner positions by value.
*
* @param max true if to max allowed angle, false to position 0.
* @param maxAngle maximum angle of motion.
*/
void moveServoByValue(bool max, uint8_t maxAngle)
{
float angle = (float)maxAngle;
if (max)
servoMoveToAngle(SERVO_PIN, angle);
else
servoMoveToAngle(SERVO_PIN, 0);
}
/**
* @brief Moves servo back and forth.
*
* @param count how many times of back and forth motion.
* @param maxAngle maximum angle of motion.
*/
void servoTap(uint8_t count, uint8_t maxAngle)
{
bool tapState = false;
uint8_t cycles = (2 * count) + 1;
for (int i = 0; i < cycles; i++)
{
moveServoByValue(tapState, maxAngle);
tapState = !tapState;
sleep_ms(TAP_BREAK_TIME);
}
}
/**
* @brief Updates config if data present on usb serial.
* There's no USB serial interrupt, so it must be called manually.
*/
void usbSerialUpdateConfig()
{
char *configLine = serialUsbGetLastLine();
if (configLine)
configHandler(configLine);
}
/**
* @brief main(), lol.
*/
int main()
{
config_t config;
char *request, *response;
int8_t responseValue;
stdio_init_all();
/*
* INITIAL SETUP
*/
diodeSetState(KLIK_STATE_SETUP);
configLoad(&config);
configApplyDefaults(false);
serialUartInit();
serialUartSetInterruptHandler(configUartInterruptHandler);
servoSetup(SERVO_PIN);
buttonSet(BUTTON_PIN);
/*
* CONNECT TO WI-FI
*/
diodeSetState(KLIK_STATE_CONNECTING);
if (!requestSetup(config.ssid, config.password))
{
diodeSetState(KLIK_STATE_CONNECTION_ERROR);
goto error;
}
/*
* SEND INITIAL REQUEST
*/
diodeSetState(KLIK_STATE_WORKING);
request = requestPrepareGET(config.username, config.feedName, config.apiKey);
response = requestSend(request);
responseValue = getValueFromResponse(response);
if (responseValue < 0)
{
diodeSetState(KLIK_STATE_REQUEST_ERROR);
goto error;
}
/*
* LOOP PHRASE
* At this phrase everything should be working.
*/
while (true)
{
request = requestPrepareGET(config.username, config.feedName, config.apiKey);
response = requestSend(request);
responseValue = getValueFromResponse(response);
/*
* Overwrite responseValue if button pressed
*/
if (buttonReadState(BUTTON_PIN) &&
(responseValue == KLIK_MODE_ON || responseValue == KLIK_MODE_OFF))
{
responseValue = !responseValue;
request = requestPreparePOST(responseValue, config.username, config.feedName, config.apiKey);
requestSend(request);
}
switch (responseValue)
{
case KLIK_MODE_OFF:
moveServoByValue(KLIK_MODE_OFF, config.angleMax);
break;
case KLIK_MODE_ON:
moveServoByValue(KLIK_MODE_ON, config.angleMax);
break;
case KLIK_MODE_TAP:
servoTap(1, config.angleMax);
request = requestPreparePOST(KLIK_MODE_OFF, config.username, config.feedName, config.apiKey);
requestSend(request);
break;
case KLIK_MODE_DOUBLE_TAP:
servoTap(2, config.angleMax);
request = requestPreparePOST(KLIK_MODE_OFF, config.username, config.feedName, config.apiKey);
requestSend(request);
break;
default:
break;
}
usbSerialUpdateConfig();
sleep_ms(BREAK_TIME);
}
/*
* ERROR
* End here if anything fails during first 3 phrases.
* Device will blink LED, with error code.
*/
error:
responseValue = KLIK_MODE_OFF;
while (1)
{
usbSerialUpdateConfig();
if (buttonReadState(BUTTON_PIN))
responseValue = !responseValue;
moveServoByValue(responseValue, config.angleMax);
sleep_ms(BREAK_TIME);
}
return 0;
}