-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
333 lines (276 loc) · 9.86 KB
/
main.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
/*
Hans(2): OSC UDP + serial MIDI -> II over I2C for ER-301 and TXo.
Version 2 runs on RPI PICO W and is programmed in C. This is the first draft.
Personal project, work in progress.
Version: 0.2
Recent changes: Now using PIO for serial MIDI IN.
Author: M.BUDDE
*/
#include <stdio.h>
#include "pico/stdlib.h"
#include "pico/cyw43_arch.h"
#include <string.h>
#include "pico/binary_info.h"
#include "hardware/i2c.h"
#include "hardware/pio.h"
#include "hardware/dma.h"
#include "hardware/uart.h"
#include "hardware/irq.h"
#include "ii.h"
#include <stdarg.h>
#include "dhcpserver.h"
#include "dnsserver.h"
#include <lwip/udp.h>
#include "lwip/pbuf.h"
#include "lwip/sockets.h"
#include "midi_func.h"
#include "helpers.h"
#include "pio_midi_uart_lib.h"
/* Debug */
#define DEBUG 1
/* I2c pins on Pico */
#define I2C_SDA_PIN 12
#define I2C_SCL_PIN 13
#define I2C_PORT i2c0
// Define the UDP port to listen on
#define UDP_PORT 9301
/* UART (console) */
#define UART_ID uart1
#define BAUD_RATE 115200
#define UART_TX_PIN 8
#define UART_RX_PIN 9
/* MIDI */
#define MIDI_TX_PIN 0
#define MIDI_RX_PIN 1
// Define MIDI message types
#define MIDI_NOTE_ON 0x90
#define MIDI_NOTE_OFF 0x80
#define MIDI_CONTROL_CHANGE 0xB0
// MIDI message buffer and index
#define MIDI_MESSAGE_BUFFER_SIZE 8
uint8_t midi_message_buffer[MIDI_MESSAGE_BUFFER_SIZE];
uint8_t midi_message_index = 0;
static void *midi_uart_instance;
// Define the maximum length of incoming OSC messages
#define MAX_OSC_LEN 256
Module_info* retrieve_module(char* slice_module_name) {
if(strcmp(slice_module_name, "er301") == 0) {
return &er301_info;
} else if(strcmp(slice_module_name, "txo") == 0) {
return &txo_info;
}
return NULL;
}
II_command* retrieve_cmd(Module_info* module_info, char* module_command) {
int i;
for(i = 0; i < module_info->number_of_cmd; i++) {
if(strcmp(module_info->cmd_set[i].name, module_command) == 0) {
return &module_info->cmd_set[i];
}
}
return NULL;
}
int retrieve_module_address(Module_info *module_info, int module_port) {
if (module_port < 0 || module_port >= module_info->max_ports) {
printy("Invalid module port: %d\n", module_port);
return 0;
}
if (module_info == &er301_info) {
int address = module_info->addresses[module_port / 100];
return address;
}
else if (module_info == &txo_info) {
int address = module_info->addresses[module_port / 8];
return address;
}
else {
// add support for more modules here
return 0;
}
}
void send_i2c_message(Module_info* module_info, II_command* ii_cmd, int module_port, long int cmd_value) {
int address = retrieve_module_address(module_info, module_port);
// Define the I2C message buffer and buffer length variables
uint8_t i2c_buf[6];
size_t buf_len;
module_port--;
if (ii_cmd->arg_count > 1) {
// The command requires a value
if (cmd_value < 0 || cmd_value > 32767) {
printy("Invalid command value: %ld\n", cmd_value);
return;
}
// Construct the I2C message buffer with command value
i2c_buf[0] = ii_cmd->command_number;
i2c_buf[1] = module_port;
i2c_buf[2] = (cmd_value >> 8) & 0xFF; // high byte
i2c_buf[3] = cmd_value & 0xFF; // low byte
buf_len = 4;
} else {
// The command does not require a value
i2c_buf[0] = ii_cmd->command_number;
i2c_buf[1] = module_port;
buf_len = 2;
}
// Send the I2C message
i2c_write_blocking(I2C_PORT, address, i2c_buf, buf_len, false);
}
void parseData(char packet[]) {
const char *delim = "/";
char packet_copy[256];
char buffer[256];
strncpy(packet_copy, packet, sizeof(packet_copy));
Module_info *module_info = NULL;
II_command *ii_cmd = NULL;
int module_port = -1;
char *module_name = strtok(packet_copy, delim);
if (module_name == NULL) {
printy("Invalid module name: %s\n", packet);
return;
}
module_info = retrieve_module(module_name);
if (module_info == NULL) {
printy("Module not found: %s\n", module_name);
return;
}
char *cmd_name = strtok(NULL, delim);
if (cmd_name == NULL) {
printy("Missing command name\n");
return;
}
ii_cmd = retrieve_cmd(module_info, cmd_name);
if (ii_cmd == NULL) {
printy("Command not found: %s\n", cmd_name);
return;
}
char *module_port_str = strtok(NULL, delim);
if (module_port_str == NULL) {
printy("Missing module port\n");
return;
}
module_port = atoi(module_port_str);
if (module_port == 0 && module_port_str[0] != '0') {
printy("Invalid module port: %s\n", module_port_str);
return;
}
if (module_port >= module_info->max_ports) {
printy("Module port out of range: %d\n", module_port);
return;
}
// Check if the command requires a value
bool has_value = (ii_cmd->arg_count > 1);
// Call the new function with the necessary parameters
if (has_value) {
// Extract the command value from the packet
char *cmd_value_str = strtok(NULL, delim);
if (cmd_value_str == NULL) {
printy("Missing command value\n");
return;
}
char *endptr;
long int cmd_value = strtol(cmd_value_str, &endptr, 10);
if (endptr == cmd_value_str || *endptr != '\0' || cmd_value < 0 || cmd_value > 32767) {
printy("Invalid command value: %s\n", cmd_value_str);
return;
}
send_i2c_message(module_info, ii_cmd, module_port, cmd_value);
} else {
send_i2c_message(module_info, ii_cmd, module_port, 0);
}
}
// UDP server callback
static void udp_server_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port) {
// Handle the received data here
parseData((char*)p->payload);
// Free the received buffer
pbuf_free(p);
}
// Function to print the MIDI message buffer using uart_puts
void printMidiMessageBuffer() {
char buffer[32]; // Buffer to hold the formatted hexadecimal string
uint8_t message_length = 0;
// Calculate the MIDI message length based on the status byte
uint8_t message_type = midi_message_buffer[0] & 0xF0;
if (message_type == MIDI_NOTE_ON || message_type == MIDI_NOTE_OFF || message_type == MIDI_CONTROL_CHANGE) {
message_length = 3; // These messages have 3 bytes in total
}
// Print the bytes of the MIDI message buffer as hexadecimal
for (uint8_t i = 0; i < message_length; i++) {
sprintf(buffer, "%02X ", midi_message_buffer[i]);
uart_puts(UART_ID, buffer);
}
// Print a new line to separate messages
uart_puts(UART_ID, "\r\n");
}
int main() {
/* Initialize i2c */
i2c_init(i2c_default, 100 * 1000);
gpio_set_function(I2C_SDA_PIN, GPIO_FUNC_I2C);
gpio_set_function(I2C_SCL_PIN, GPIO_FUNC_I2C);
gpio_pull_up(I2C_SDA_PIN);
gpio_pull_up(I2C_SCL_PIN);
/* Initialize UART (console on UART 1)*/
uart_init(UART_ID, BAUD_RATE);
gpio_set_function(UART_TX_PIN, GPIO_FUNC_UART);
gpio_set_function(UART_RX_PIN, GPIO_FUNC_UART);
midi_uart_instance = pio_midi_uart_create(MIDI_TX_PIN, MIDI_RX_PIN);
if (midi_uart_instance == NULL) {
printy("Error creating MIDI UART instance.\n");
return 1;
}
stdio_init_all();
cyw43_arch_init();
const char *ap_name = "hans";
const char *password = "password";
cyw43_arch_enable_ap_mode(ap_name, password, CYW43_AUTH_WPA2_AES_PSK);
ip4_addr_t gw, mask;
IP4_ADDR(&gw, 192, 168, 4, 1);
IP4_ADDR(&mask, 255, 255, 255, 0);
// Start the dhcp server
dhcp_server_t dhcp_server;
dhcp_server_init(&dhcp_server, &gw, &mask);
// Create a new UDP PCB (Protocol Control Block)
struct udp_pcb *pcb = udp_new();
// Bind the UDP PCB to the server port
udp_bind(pcb, IP_ADDR_ANY, UDP_PORT);
// Set the receive callback function for the UDP PCB
udp_recv(pcb, udp_server_recv, NULL);
printy("\rstarting hans OSC\n");
// Buffer to hold received MIDI data
uint8_t received_data[16];
while (1) {
// Poll the MIDI UART receive buffer
uint8_t nread = pio_midi_uart_poll_rx_buffer(midi_uart_instance, received_data, sizeof(received_data));
if (nread > 0) {
// Process each MIDI message in the received buffer
for (uint8_t i = 0; i < nread; i++) {
uint8_t data_byte = received_data[i];
// Check if it's the start of a new MIDI message
if (data_byte >= 0x80 && data_byte <= 0xEF) {
// Start of a new MIDI message, reset the buffer index
midi_message_index = 0;
midi_message_buffer[midi_message_index++] = data_byte;
} else if (midi_message_index > 0) {
// Continue collecting bytes of the current MIDI message
midi_message_buffer[midi_message_index++] = data_byte;
// Check if we have a complete MIDI message
uint8_t message_type = midi_message_buffer[0] & 0xF0;
uint8_t message_length = 0;
if (message_type == MIDI_NOTE_ON || message_type == MIDI_NOTE_OFF || message_type == MIDI_CONTROL_CHANGE) {
message_length = 3; // These messages have 3 bytes in total
}
if (midi_message_index == message_length) {
// We have a complete MIDI message, pass it to the parser
parse_midi_command(midi_message_buffer);
// Reset the buffer index for the next message
midi_message_index = 0;
}
}
}
}
sleep_us(10);
}
udp_remove(pcb);
cyw43_arch_deinit();
return 0;
}