forked from laamaa/m8c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
129 lines (104 loc) · 2.75 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
#include <SDL2/SDL.h>
#include <SDL2/SDL_timer.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "serial.h"
#include "command.h"
#include "input.h"
#include "render.h"
#include "slip.h"
#include "write.h"
uint8_t run = 1;
// Handles CTRL+C / SIGINT
void intHandler(int dummy) { run = 0; }
int main(int argc, char *argv[]) {
// maximum amount of bytes to read from the serial in one read()
const int serial_read_size = 1024;
// allocate memory for serial buffer
uint8_t serial_buf[serial_read_size];
static uint8_t slip_buffer[1024]; // SLIP command buffer
// settings for the slip packet handler
static const slip_descriptor_s slip_descriptor = {
.buf = slip_buffer,
.buf_size = sizeof(slip_buffer),
.recv_message = process_command, // the function where complete slip
// packets are processed further
};
static slip_handler_s slip;
signal(SIGINT, intHandler);
slip_init(&slip, &slip_descriptor);
// open device
char *portname;
if (argc > 1) {
portname = argv[1];
} else {
portname = "/dev/ttyACM0";
}
int port = init_serial(portname);
if (port == -1)
return -1;
if (enable_and_reset_display(port) == -1)
run = 0;
if (initialize_sdl() == -1)
run = 0;
// initialize joystick etc.
initialize_input();
uint8_t prev_input = 0;
// main loop
while (run) {
// read data from serial port
size_t bytes_read = read(port, &serial_buf, sizeof(serial_buf));
if (bytes_read == -1) {
fprintf(stderr, "Error %d reading serial: %s\n", errno, strerror(errno));
run = 0;
}
if (bytes_read > 0) {
for (int i = 0; i < bytes_read; i++) {
uint8_t rx = serial_buf[i];
// process the incoming bytes into commands and draw them
int n = slip_read_byte(&slip, rx);
if (n != SLIP_NO_ERROR) {
fprintf(stderr, "SLIP error %d\n", n);
}
}
}
input_msg_s input = get_input_msg();
switch (input.type) {
case normal:
if (input.value != prev_input) {
prev_input = input.value;
send_msg_controller(port, input.value);
}
break;
case keyjazz:
if (input.value != prev_input) {
prev_input = input.value;
if (input.value != 0) {
send_msg_keyjazz(port, input.value, 64);
} else {
send_msg_keyjazz(port, 0, 0);
}
}
break;
case special:
switch (input.value) {
case msg_quit:
run = 0;
break;
}
break;
}
render_screen();
usleep(10);
}
// exit, clean up
fprintf(stderr, "\nShutting down\n");
close_input();
close_renderer();
disconnect(port);
close(port);
return 0;
}