-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbekant.ino
266 lines (227 loc) · 5 KB
/
bekant.ino
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
#include "lin.h"
#ifdef ESP8266
#include <ESP8266WiFi.h>
#include <aREST.h>
#include <aREST_UI.h>
// Pin settings for Sparkfun ESP8266 Thing Dev
#define UP_BTN 0
#define DOWN_BTN 13
#define LED 5
#define LED_ON LOW
#define TX_PIN 1
#define RX_PIN 3
#else
// Standard arduino settings
#define UP_BTN 7
#define DOWN_BTN 8
#define LED 13
#define LED_ON HIGH
#define TX_PIN 1
#define RX_PIN 0
#endif
Lin lin(Serial, TX_PIN);
#ifdef ESP8266
aREST_UI rest = aREST_UI();
const char * ssid = "Linksays";
WiFiServer server(80);
#endif
int height = 0;
enum class Command {
NONE,
UP,
DOWN,
};
Command user_cmd = Command::NONE;
int up(bool pushed) {
if(pushed) {
digitalWrite(LED, LED_ON);
user_cmd = Command::UP;
} else {
digitalWrite(LED, !LED_ON);
user_cmd = Command::NONE;
}
}
int down(bool pushed) {
if(pushed) {
digitalWrite(LED, LED_ON);
user_cmd = Command::DOWN;
} else {
digitalWrite(LED, !LED_ON);
user_cmd = Command::NONE;
}
}
#ifdef ESP8266
int restCallback(String answer) {
return rest.buttonCallback(answer);
}
#endif
void setup() {
// put your setup code here, to run once:
lin.begin(19200);
pinMode(UP_BTN, INPUT_PULLUP);
pinMode(DOWN_BTN, INPUT_PULLUP);
pinMode(LED, OUTPUT);
digitalWrite(LED, !LED_ON);
#ifdef ESP8266
rest.title("Desk");
rest.variable("Height", &height);
rest.label("Height");
rest.set_name("esp8266");
rest.buttonFunction("Up", &up);
rest.buttonFunction("Down", &down);
rest.function("ui_callback", &restCallback);
WiFi.mode(WIFI_AP);
//WiFi.softAP(ssid, password);
WiFi.softAP(ssid);
server.begin();
#endif
}
unsigned long t = 0;
void delay_until(unsigned long ms) {
unsigned long end = t + (1000 * ms);
unsigned long d = end - micros();
// crazy long delay; probably negative wrap-around
// just return
if ( d > 1000000 ) {
t = micros();
return;
}
if (d > 15000) {
unsigned long d2 = (d-15000)/1000;
delay(d2);
d = end - micros();
}
delayMicroseconds(d);
t = end;
}
enum class State {
OFF,
STARTING,
UP,
DOWN,
STOPPING1,
STOPPING2,
};
State state = State::OFF;
void loop() {
// put your main code here, to run repeatedly:
uint8_t empty[] = { 0, 0, 0 };
uint8_t node_a[4] = { 0, 0, 0, 0 };
uint8_t node_b[4] = { 0, 0, 0, 0 };
uint8_t cmd[3] = { 0, 0, 0 };
uint8_t res = 0;
// Send ID 11
lin.send(0x11, empty, 3, 2);
delay_until(5);
// Recv from ID 08
res = lin.recv(0x08, node_a, 3, 2);
delay_until(5);
// Recv from ID 09
res = lin.recv(0x09, node_b, 3, 2);
delay_until(5);
// Send ID 10, 6 times
for (uint8_t i = 0; i < 6; i++) {
lin.send(0x10, 0, 0, 2);
delay_until(5);
}
// Send ID 1
lin.send(0x01, 0, 0, 2);
delay_until(5);
uint16_t enc_a = node_a[0] | (node_a[1] << 8);
uint16_t enc_b = node_b[0] | (node_b[1] << 8);
uint16_t enc_target = enc_a;
height = enc_a;
// Send ID 12
switch (state) {
case State::OFF:
cmd[2] = 0xFC; // 0b11111100
break;
case State::STARTING:
cmd[2] = 0xC4; // 0b10100100
break;
case State::UP:
enc_target = min(enc_a, enc_b);
cmd[2] = 0x86; // 0b10000110
break;
case State::DOWN:
enc_target = max(enc_a, enc_b);
cmd[2] = 0x85; // 0b10000101
break;
case State::STOPPING1:
cmd[2] = 0x87; // 0b10000111
break;
case State::STOPPING2:
cmd[2] = 0x84; // 0b10000100
break;
}
cmd[0] = enc_target & 0xFF;
cmd[1] = enc_target >> 8;
lin.send(0x12, cmd, 3, 2);
// read buttons and compute next state
#ifndef ESP8266
if (digitalRead(UP_BTN) == 0) { // UP
up(true);
} else if (digitalRead(DOWN_BTN) == 0) { // DOWN
down(true);
} else {
up(false);
}
#endif
switch (state) {
case State::OFF:
if (user_cmd != Command::NONE) {
if ( node_a[2] == 0x60 && node_b[2] == 0x60) {
state = State::STARTING;
}
}
break;
case State::STARTING:
//if( node_a[2] == 0x02 && node_b[2] == 0x02) {
switch(user_cmd) {
case Command::NONE:
state = State::OFF;
break;
case Command::UP:
state = State::UP;
break;
case Command::DOWN:
state = State::DOWN;
break;
}
//}
break;
case State::UP:
if (user_cmd != Command::UP) {
state = State::STOPPING1;
}
break;
case State::DOWN:
if (user_cmd != Command::DOWN) {
state = State::STOPPING1;
}
break;
case State::STOPPING1:
state = State::STOPPING2;
break;
case State::STOPPING2:
if ( node_a[2] == 0x60 && node_b[2] == 0x60) {
state = State::OFF;
}
break;
default:
state = State::OFF;
break;
}
#ifdef ESP8266
// Use the remaining time to service clients
WiFiClient client = server.available();
if(client) {
while(!client.available()) {
delay(1);
}
rest.handle(client);
}
#endif
// Wait the remaining 150 ms in the cycle
delay_until(150);
}