-
Notifications
You must be signed in to change notification settings - Fork 3
/
logicdata.c
233 lines (207 loc) · 9.73 KB
/
logicdata.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
/* MIT License
*
* Copyright (c) 2022 ma-lwa-re
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "math.h"
#include "logicdata.h"
static const char *LOGICDATA_TAG = "logicdata";
uint8_t desk_sleep = true;
response_frame_t response_frame = {
.random = 0x00,
.reserved0 = {0x00},
.direction = DESK_DOWN,
.reserved1 = {0x00, 0x00, 0xFF},
.action = DESK_IDLE,
.reserved2 = {0x01},
.checksum = 0x00
};
status_frame_t *status_frame = NULL;
void desk_wake_up() {
uint8_t cafebabe[] = {0xCA, 0xFE, 0xBA, 0xBE};
uart_write_bytes(UART_PORT, cafebabe, sizeof(cafebabe));
ESP_LOGI(LOGICDATA_TAG, "Waking up desk!");
}
void desk_move(uint8_t direction) {
if(response_frame.action == DESK_IDLE) {
response_frame.direction = direction;
response_frame.action = DESK_MOVE;
if(desk_sleep) {
desk_wake_up();
}
}
}
void desk_move_up() {
if(response_frame.action == DESK_IDLE) {
ESP_LOGI(LOGICDATA_TAG, "Moving desk up!");
}
desk_move(DESK_UP);
}
void desk_move_down() {
if(response_frame.action == DESK_IDLE) {
ESP_LOGI(LOGICDATA_TAG, "Moving desk down!");
}
desk_move(DESK_DOWN);
}
void desk_stop() {
if(response_frame.action != DESK_MOVE) {
return;
}
ESP_LOGI(LOGICDATA_TAG, "Stopping desk!");
response_frame.action = DESK_STOP;
vTaskDelay(10);
response_frame.action = DESK_IDLE;
}
void desk_handle_lin_frame(lin_frame_t *lin_frame, uint8_t *event_data, uint8_t event_size) {
desk_sleep = false;
uint8_t protected_id = lin_frame->protected_id & 0x3F;
if(protected_id == LIN_PROTECTED_ID_SYNC) {
if(event_size > LIN_HEADER_SIZE) {
ESP_LOGI(LOGICDATA_TAG, "Pairing sequence %d%%", (uint8_t)((lin_frame->data[0] / 7.0) * 100));
ESP_LOG_BUFFER_HEX_LEVEL(LOGICDATA_TAG, event_data, event_size, ESP_LOG_DEBUG);
}
} else if(protected_id == LIN_PROTECTED_ID_MOVE) {
if(response_frame.action != DESK_IDLE) {
response_frame.random = rand() % 0xFF;
response_frame.checksum = checksum((uint8_t*) &response_frame, lin_frame->protected_id);
uart_write_bytes(UART_PORT, &response_frame, sizeof(response_frame));
}
} else if(protected_id == LIN_PROTECTED_ID_STATUS) {
if(event_size < (LIN_HEADER_SIZE + LIN_DATA_SIZE + LIN_CHECKSUM_SIZE)) {
ESP_LOGW(LOGICDATA_TAG, "Status event too small");
ESP_LOG_BUFFER_HEX_LEVEL(LOGICDATA_TAG, event_data, event_size, ESP_LOG_WARN);
return;
}
status_frame = (status_frame_t*) lin_frame;
if(status_frame->ready == DESK_READY) {
uint8_t new_desk_height = round(((lin_frame->data[3] << 8) + lin_frame->data[4]) / 10.0);
if(new_desk_height != current_desk_height) {
if(current_desk_height == 0xFF) {
target_desk_height = new_desk_height;
}
current_desk_height = new_desk_height;
desk_percentage = round((lin_frame->data[5] / 255.0) * 100);
ESP_LOGI(LOGICDATA_TAG, "Desk height %dcm @ %d%%", current_desk_height, desk_percentage);
}
} else if(status_frame->ready == DESK_NOT_READY) {
if(status_frame->status == DESK_PAIRING) {
switch(status_frame->status_code) {
case 0x00:
ESP_LOGW(LOGICDATA_TAG, "Synchronizing");
break;
case 0x01:
ESP_LOGE(LOGICDATA_TAG, "Desk error, need to be reset!");
break;
default:
ESP_LOGE(LOGICDATA_TAG, "Unknown status (0x%02x)!", lin_frame->data[5]);
break;
}
} else if(status_frame->status == DESK_ERROR) {
switch(status_frame->error_code) {
case 0x01:
ESP_LOGE(LOGICDATA_TAG, "Firmware Error: Disconnect the Power Unit "
"from the Mains. Then, disconnect System from the Power "
"Unit. Reconnect the system again, then operate the DM "
"System as normal.");
break;
case 0x02:
ESP_LOGE(LOGICDATA_TAG, "Motor Over Current: Release all "
"Keys and wait for 5 seconds. Then, try again.");
break;
case 0x03:
ESP_LOGE(LOGICDATA_TAG, "DC Over Voltage: Release all "
"Keys and wait for 5 seconds. Then, try again.");
break;
case 0x08:
ESP_LOGE(LOGICDATA_TAG, "Impulse Detection Timeout: Perform a "
"Position Reset Procedure (see System Manual)");
break;
case 0x0B:
ESP_LOGE(LOGICDATA_TAG, "Speed cannot be achieved: Release all "
"Keys and wait for 5 seconds. Then, try again.");
break;
case 0x0C:
ESP_LOGE(LOGICDATA_TAG, "Power Stage Overcurrent: Release all "
"Keys and wait for 5 seconds. Then, try again.");
break;
case 0x0D:
ESP_LOGE(LOGICDATA_TAG, "DC Under Voltage: Release all "
"Keys and wait for 5 seconds. Then, try again.");
break;
case 0x0E:
ESP_LOGE(LOGICDATA_TAG, "Critical DC Over Voltage: Release all "
"Keys and wait for 5 seconds. Then, try again.");
break;
case 0x0F:
ESP_LOGE(LOGICDATA_TAG, "Strain Gauge is defective: Release all "
"Keys and wait for 5 seconds. Then, try again. "
"Contact LOGICDATA if the problem persists. "
"Do not operate the DM System if components are broken.");
break;
case 0x11:
ESP_LOGE(LOGICDATA_TAG, "Error during pairing sequence: Disconnect "
"the Power Unit from the Mains. Then, disconnect System "
"from the Power Unit. Reconnect the system again, then "
"operate the DM System as normal. If this fails, perform "
"a factory reset (see DM System Manual).");
break;
case 0x12:
ESP_LOGE(LOGICDATA_TAG, "Parameterization or firmware of different "
"Actuators in the Table System are incompatible: Re- "
"paramaterize the Actuators. Contact LOGICDATA for further "
"information.");
break;
case 0x13:
ESP_LOGE(LOGICDATA_TAG, "Too many / too few Actuators connected: "
"Connect the correct number of Actuators (as specified in "
"setup).");
break;
case 0x14:
ESP_LOGE(LOGICDATA_TAG, "Motor short circuit and/or open load: "
"Contact LOGICDATA.");
break;
case 0x15:
ESP_LOGE(LOGICDATA_TAG, "Firmware Error: Disconnect the Power Unit "
"from the Mains. Then, disconnect System from the Power "
"Unit. Reconnect the system again, then operate the DM "
"System as normal.");
break;
case 0x16:
ESP_LOGE(LOGICDATA_TAG, "Power Unit overload: Release all "
"Keys and wait for 5 seconds. Then, try again.");
break;
case 0x17:
ESP_LOGE(LOGICDATA_TAG, "Motor Under Voltage: Release all "
"Keys and wait for 5 seconds. Then, try again.");
break;
default:
ESP_LOGE(LOGICDATA_TAG, "Unknown error code! (0x%02x)", status_frame->error_code);
break;
}
}
} else {
ESP_LOGE(LOGICDATA_TAG, "Unknown state (0x%02x)!", status_frame->ready);
}
}
desk_sleep = true;
}