-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelancontrol.c
337 lines (333 loc) · 11 KB
/
elancontrol.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
335
336
337
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/uart.h"
#include "hardware/irq.h"
#include <sendzpadcommand.pio.h>
#include <nec_transmit.h> // include the library headers
//Hardware UART Setup
#define BAUD_RATE 19200
#define DATA_BITS 8
#define STOP_BITS 1
#define PARITY UART_PARITY_NONE
// Pin Definitions
#define UART_TX_PIN 12
#define UART_RX_PIN 13
#define LED_PIN 25
#define CHANNEL_1_PIN 17
#define CHANNEL_2_PIN 18
#define CHANNEL_3_PIN 19
#define CHANNEL_4_PIN 20
#define CHANNEL_5_PIN 21
#define CHANNEL_6_PIN 22
// PIO Units
#define CHANNEL_1_PIO pio0
#define CHANNEL_2_PIO pio0
#define CHANNEL_3_PIO pio0
#define CHANNEL_4_PIO pio0
#define CHANNEL_5_PIO pio1
#define CHANNEL_6_PIO pio1
// Offsets, must match with above
#define CHANNEL_1_OFFSET pio0_offset
#define CHANNEL_2_OFFSET pio0_offset
#define CHANNEL_3_OFFSET pio0_offset
#define CHANNEL_4_OFFSET pio0_offset
#define CHANNEL_5_OFFSET pio1_offset
#define CHANNEL_6_OFFSET pio1_offset
// SMIDs
#define CHANNEL_1_SMID 0
#define CHANNEL_2_SMID 1
#define CHANNEL_3_SMID 2
#define CHANNEL_4_SMID 3
#define CHANNEL_5_SMID 0
#define CHANNEL_6_SMID 1
#define IR_PIO pio1
#define IR_TX_GPIO 16
//Send a keepalive even if nothing is changing
//40 bytes in status with 3 bytes as header, 1 in footer (not stored)
#define STATUS_LEN 35
#define KEEPALIVE_INTERVAL 50
#define ZPAD_COMMAND_HEADER 67
#define SLIDER_COMMAND_HEADER 68
#define IR_COMMAND_HEADER 66
#define ELAN_VOLUP 4
#define ELAN_VOLDOWN 36
// Global to keep track of same status
static int identicalStatusRecieved = 0;
static int sliderTargetVol[] = {-1,-1,-1,-1,-1,-1};
static struct statusStruct curStatus;
static int ir_tx_sm = -1;
// Constants for status data header and footers
const char header[] = {0xE0, 0xC0, 0x00, 0x81};
const char data_footer = 0xEA;
const char err_footer = 0xEF;
const int zoneToChannel[] = {5, 1, 4, 2, 6, 3}; // Zone to channel (depends on how plugs are ordered)
//Unpacked status from serial
struct statusStruct {
int volume[6];
int input[6];
bool mute[6];
};
// Send error message with header/footer
void sendError(const char *errString) {
for (int i=0; i < sizeof(header); i++) {
putchar_raw(header[i]);
}
puts_raw(errString);
putchar_raw(err_footer);
fflush(stdout);
}
//Unpack the data1
struct statusStruct unPackData(unsigned char receivedChars[]) {
struct statusStruct status;
for (int i=0; i < 6; i++) {
status.volume[i] = 48 - receivedChars[i*6+2] & 0b00111111; //Volume is sent as a 6-bit attenuation value in LSBs
status.mute[i] = (receivedChars[i*6] & 0b00010000) >> 4; // Mute is just a bit
status.input[i] = receivedChars[i*6] & 0b00000111; // Status is 3 LSBs
}
return status;
}
//Compair Status structures
bool isStatusDiff(struct statusStruct data1, struct statusStruct data2) {
bool is_diff = false;
for (int i=0; i < 6; i++) {
is_diff = is_diff || (data1.volume[i] != data2.volume[i]);
is_diff = is_diff || (data1.mute[i] != data2.mute[i]);
is_diff = is_diff || (data1.input[i] != data2.input[i]);
}
return true;
}
//Send commands to PIO state machines
void sentZPadCommand(int channel, int command) {
if (command < 0 || command > 63) {
sendError("Invalid Command");
return;
}
command = command << 21; //Shift command to upper bits with 6 zeroa as header
switch (channel) {
case 1:
pio_sm_put_blocking(CHANNEL_1_PIO, CHANNEL_1_SMID, command);
break;
case 2:
pio_sm_put_blocking(CHANNEL_2_PIO, CHANNEL_2_SMID, command);
break;
case 3:
pio_sm_put_blocking(CHANNEL_3_PIO, CHANNEL_3_SMID, command);
break;
case 4:
pio_sm_put_blocking(CHANNEL_4_PIO, CHANNEL_4_SMID, command);
break;
case 5:
pio_sm_put_blocking(CHANNEL_5_PIO, CHANNEL_5_SMID, command);
break;
case 6:
pio_sm_put_blocking(CHANNEL_6_PIO, CHANNEL_6_SMID, command);
break;
default:
sendError("Invalid Channel");
}
}
//Handle slider updates
void handleSliderUpdates () {
for (int i; i < 6; i++) {
if (sliderTargetVol[i] > 0) {
if (sliderTargetVol[i] > curStatus.volume[i]) {
sentZPadCommand(i, ELAN_VOLUP);
sentZPadCommand(i, ELAN_VOLUP);
}
else if (sliderTargetVol[i] < curStatus.volume[i]) {
sentZPadCommand(i, ELAN_VOLDOWN);
sentZPadCommand(i, ELAN_VOLDOWN);
}
else {
sliderTargetVol[i] = -1; //We're done
}
}
}
}
//Send a status with header/footer
void sendData(struct statusStruct status) {
for (int i=0; i < sizeof(header); i++) {
putchar_raw(header[i]); // Send the header
}
for (int i=0; i < 6; i++) {
putchar_raw(status.volume[i]);
putchar_raw(status.mute[i]);
putchar_raw(status.input[i]);
}
putchar_raw(data_footer); //footer to indicate this is a status
fflush(stdout);
identicalStatusRecieved = 0; //Reset counter when status is sent
gpio_put(LED_PIN, !gpio_get(LED_PIN)); // Toggle LED on update sent
}
//Handle data
void handleRXData(unsigned char receivedChars[]) {
struct statusStruct newStatus;
newStatus = unPackData(receivedChars);
if (isStatusDiff(newStatus,curStatus) || identicalStatusRecieved > KEEPALIVE_INTERVAL) {
sendData(newStatus);
curStatus = newStatus;
handleSliderUpdates(); //Do we care if this happends on timeouts?
}
}
// Hardware UART RX interrupt handler
void on_uart_rx() {
char cur_rx;
//Statics to keep track between interrupts
static int charsRXed = 0;
static bool identical = true; // Keep track if pattern is identical
static unsigned char receivedChars[STATUS_LEN]; // an array to store the received data
static unsigned char lastBuffer[STATUS_LEN]; // an array to store the last received data
while (uart_is_readable(uart0)) {
cur_rx = uart_getc(uart0); // Get a char
if (charsRXed < 4 && cur_rx == header[charsRXed]) { //Header
charsRXed++;
}
else if (charsRXed > 3 && charsRXed < 39) { //Data payload
receivedChars[charsRXed - 4] = cur_rx;
charsRXed++;
if (receivedChars[charsRXed - 4] != lastBuffer[charsRXed - 4]) {
lastBuffer[charsRXed - 4] = receivedChars[charsRXed - 4]; //Copy
identical = false; //We are not identical
}
}
else if (charsRXed == 39 && cur_rx == data_footer) { //End of data payload
if (identical && identicalStatusRecieved <= KEEPALIVE_INTERVAL) {
identicalStatusRecieved++; //Counter for keepalive
}
else {
handleRXData(receivedChars); //Send the new status
}
charsRXed = 0; //Reset chars recieved counter
identical = true; //Reset identical flag
}
else { //We did not match expected data pattern
sendError("Malformatted Serial Status Message");
charsRXed = 0; //Reset counter
identical = true; //Reset identical
}
}
}
//Initialise everything
void initAll() {
//Initialise Standard I/O to USB
stdio_usb_init();
// initialise GPIO (Green LED connected to pin 25)
gpio_init(LED_PIN);
gpio_set_dir(LED_PIN, GPIO_OUT);
//PIO Init Section
int pio0_offset = pio_add_program(pio0, &sendzpadcommand_program);
int pio1_offset = pio_add_program(pio1, &sendzpadcommand_program);
sendzpadcommand_program_init(CHANNEL_1_PIO, CHANNEL_1_SMID, CHANNEL_1_OFFSET, CHANNEL_1_PIN);
sendzpadcommand_program_init(CHANNEL_2_PIO, CHANNEL_2_SMID, CHANNEL_2_OFFSET, CHANNEL_2_PIN);
sendzpadcommand_program_init(CHANNEL_3_PIO, CHANNEL_3_SMID, CHANNEL_3_OFFSET, CHANNEL_3_PIN);
sendzpadcommand_program_init(CHANNEL_4_PIO, CHANNEL_4_SMID, CHANNEL_4_OFFSET, CHANNEL_4_PIN);
sendzpadcommand_program_init(CHANNEL_5_PIO, CHANNEL_5_SMID, CHANNEL_5_OFFSET, CHANNEL_5_PIN);
sendzpadcommand_program_init(CHANNEL_6_PIO, CHANNEL_6_SMID, CHANNEL_6_OFFSET, CHANNEL_6_PIN);
// configure and enable the state machines
int ir_tx_sm = nec_tx_init(IR_PIO, IR_TX_GPIO); // uses two state machines, 16 instructions and one IRQ
if (ir_tx_sm == -1) {
sendError("could not configure IR PIO\n");
}
// Set up our UART with a basic baud rate.
uart_init(uart0, 2400);
// Set the TX and RX pins by using the function select on the GPIO
gpio_set_function(UART_TX_PIN, GPIO_FUNC_UART);
gpio_set_function(UART_RX_PIN, GPIO_FUNC_UART);
// The call will return the actual baud rate selected
int __unused actual = uart_set_baudrate(uart0, BAUD_RATE);
// Set UART flow control CTS/RTS, we don't want these, so turn them off
uart_set_hw_flow(uart0, false, false);
// Set our data format
uart_set_format(uart0, DATA_BITS, STOP_BITS, PARITY);
// Turn on FIFOs
uart_set_fifo_enabled(uart0, true);
uart_set_translate_crlf (uart0, false);
// And set up and enable the interrupt handlers
irq_set_exclusive_handler(UART0_IRQ, on_uart_rx);
irq_set_enabled(UART0_IRQ, true);
// Now enable the UART to send interrupts - RX only
uart_set_irq_enables(uart0, true, false);
}
//Handle a request to send a straight ZPAD command
void handleZPADCommand() {
int channel, command;
if ((channel = getchar_timeout_us(10000)) == PICO_ERROR_TIMEOUT) {
sendError("Channel Timeout");
return;
}
if ((command = getchar_timeout_us(10000)) == PICO_ERROR_TIMEOUT) {
sendError("Command Timeout");
return;
}
sentZPadCommand(channel, command);
if (command == ELAN_VOLUP || command == ELAN_VOLDOWN) {
sentZPadCommand(channel, command); // Volume commands should be doubled
}
}
//Handle a slider request
void handleSliderReq() {
int zone, volume, channel;
char errstr[50];
if ((zone = getchar_timeout_us(10000)) == PICO_ERROR_TIMEOUT) {
sendError("Zone Timeout");
return;
}
if ((volume = getchar_timeout_us(10000)) == PICO_ERROR_TIMEOUT) {
sendError("Volume Timeout");
return;
}
channel = zoneToChannel[zone];
sliderTargetVol[channel] = volume; //set the target
if (volume < curStatus.volume[channel]) {
sentZPadCommand(channel, ELAN_VOLUP);
sentZPadCommand(channel, ELAN_VOLUP);
}
else {
sentZPadCommand(channel, ELAN_VOLDOWN);
sentZPadCommand(channel, ELAN_VOLDOWN);
}
sprintf(errstr,"Got a Slider Request, Channel: %d, Vol: %d", channel, volume);
sendError(errstr);
}
//Handle an IR request
void handleIRReq() {
uint8_t tx_address, tx_data;
if (ir_tx_sm == -1) {
sendError("IR Not Setup\n");
return;
}
if ((tx_address = getchar_timeout_us(10000)) == PICO_ERROR_TIMEOUT) {
sendError("TX Address Timeout");
return;
}
if ((tx_data = getchar_timeout_us(10000)) == PICO_ERROR_TIMEOUT) {
sendError("TX Data Timeout");
return;
}
uint32_t tx_frame = nec_encode_frame(tx_address, tx_data);
pio_sm_put_blocking(IR_PIO, ir_tx_sm, tx_frame);
}
//Main: init, then infinite loop to handle commands
int main() {
int in;
initAll();
//Main Loop
while(1) {
in = getchar_timeout_us(10000000); // Get the command from USB serial
switch (in) {
case PICO_ERROR_TIMEOUT:
identicalStatusRecieved = KEEPALIVE_INTERVAL; //Prime us for rapid start
break;
case ZPAD_COMMAND_HEADER:
handleZPADCommand();
break;
case SLIDER_COMMAND_HEADER:
handleSliderReq();
break;
case IR_COMMAND_HEADER:
handleIRReq();
default:
sendError("Invalid Command");
}
}
}