Commit 00cd350be94508548260f0d25eb8cf4f1dd3a12c

Authored by Calvin Bulla
1 parent 42b8a8cc
Exists in master and in 1 other branch dev

Debugging Serial Communication

app/src/heisenwave.h 0 → 100644
... ... @@ -0,0 +1,22 @@
  1 +#ifndef COMMANDS_H
  2 +#define COMMANDS_H
  3 +
  4 +#define HW_INO_VERSION 1
  5 +
  6 +// COMMANDS
  7 +
  8 +#define HW_ACK 1
  9 +#define HW_NACK 2
  10 +#define HW_HELO 3
  11 +#define HW_SMPL_RATE 4
  12 +#define HW_GET_DATA 5
  13 +#define HW_DTIME_SET 6
  14 +#define HW_DTIME_GET 7
  15 +#define HW_SLEEP 8
  16 +
  17 +// COMMANDS SIZE
  18 +
  19 +#define HW_HELO_SIZE 16
  20 +#define HW_DATE_SIZE 4
  21 +
  22 +#endif
... ...
app/src/main.c
1 1 #include <stdio.h>
2 2 #include <stdlib.h>
3 3 #include <unistd.h>
  4 +#include <string.h>
  5 +#include <errno.h>
  6 +#include <time.h>
  7 +#include <stdint.h>
4 8  
5 9 #include <GL/glew.h>
6 10 #include <GLFW/glfw3.h>
... ... @@ -10,58 +14,121 @@
10 14 #include "serial.h"
11 15 #include "timer.h"
12 16 #include "graphics.h"
  17 +#include "heisenwave.h"
13 18  
14   -char *bye = "Bye";
15   -char *hello = "Hello";
16   -int printer;
  19 +struct ino ino;
  20 +int ino_avail;
17 21  
18   -int print_sth(double dt, void *args)
  22 +int recv_ack(double dt, void *args)
19 23 {
20   - printf("%s\n",args);
  24 + debug("Waiting for response...");
  25 + if( serial_available(&ino) >= 1 ) {
  26 + char c;
  27 + serial_read(&ino, 1, &c);
  28 + if( c == HW_ACK ) {
  29 + log_info("%s => ACK",(char *)args);
  30 + } else {
  31 + log_warn("%s => NO ACK",(char *)args);
  32 + }
  33 + free(args);
  34 + return 0;
  35 + }
21 36 return 1;
22 37 }
23 38  
24   -int count_down(double dt, void *args)
  39 +int send_time(double dt, void *args)
  40 +{
  41 + char timestamp[4];
  42 + char com = HW_DTIME_SET;
  43 + serial_write(&ino, 1, &com);
  44 + uint32_t now = (uint32_t)time(0);
  45 + log_info("Sending time %d",(int)now);
  46 + serial_write(&ino, 4, (char*)&now);
  47 + char *msg = strdup("Time set");
  48 + timer_submit(100, recv_ack, msg);
  49 + return 0;
  50 +}
  51 +
  52 +int recv_hello(double dt, void *args)
  53 +{
  54 + char response[4];
  55 + log_info("Waiting for response...");
  56 + if( serial_available(&ino) >= 4 ) {
  57 + serial_read(&ino, 4, response);
  58 + if( response[0] != HW_ACK ) {
  59 + log_warn("No Ack");
  60 + return 0;
  61 + }
  62 + if( response[1] != 'H' || response[2] != 'W' ) {
  63 + log_warn("No Heisenwave");
  64 + return 0;
  65 + }
  66 + log_info("%s connected", ino.port);
  67 + log_info("Version %d", response[3]);
  68 +
  69 + timer_submit(100, send_time, NULL);
  70 +
  71 + return 0;
  72 + } else return 1;
  73 +}
  74 +
  75 +int send_hello(double dt, void *args)
25 76 {
26   - int i = *(int *)args - 1;
27   - if( i == 0 ) {
28   - timer_submit(1000,print_sth,bye);
29   - timer_cancel(printer);
  77 + char buf[16];
  78 + char com = HW_HELO;
  79 + serial_name(&ino, buf);
  80 + serial_flush(&ino);
  81 + log_info("HELO => %s", ino.port);
  82 + if( serial_write(&ino, 1, &com) != 0 ) {
  83 + log_warn("Unable to write");
  84 + }
  85 + if( serial_write(&ino, 16, buf) != 0 ) {
  86 + log_warn("Unable to write");
  87 + }
  88 + if( recv_hello(0, NULL) != 0 ) {
  89 + timer_submit(300, recv_hello, NULL);
30 90 }
31   - *(int *)args = i;
32   - return i;
  91 + return 0;
33 92 }
34 93  
35   -int main(int argc, const char *argv[])
  94 +void arduino_init()
36 95 {
37   - int i;
  96 + ino_avail = serial_discover(1, &ino);
  97 + if( ino_avail ) {
  98 + log_info("Arduino @ %s", ino.port);
  99 + if( serial_connect(&ino) != 0 ) {
  100 + log_warn("Unable to connect to %s", ino.port);
  101 + log_err("%s",strerror(errno));
  102 + return;
  103 + }
  104 + timer_submit(3000, send_hello, NULL);
  105 + } else {
  106 + log_warn("No Arduino connected");
  107 + }
  108 +}
38 109  
  110 +void arduino_free()
  111 +{
  112 + if( ino_avail ) {
  113 + serial_free(&ino);
  114 + }
  115 +}
  116 +
  117 +int main(int argc, const char *argv[])
  118 +{
39 119 timer_init();
40 120 graphics_init();
41   -
42   - struct ino inos[4];
43   - int disc = serial_discover(4,inos);
44   - log_info("%d Arduino discovered", disc);
45   - for( i = 0; i < disc; ++i ) {
46   - log_info("Arduino @ %s",inos[i].port);
47   - }
  121 + arduino_init();
48 122  
49 123 int running = 1;
50   -
51   - int count = 5;
52 124 timer_submit(16,graphics_update,&running);
53   - printer = timer_submit(1000,print_sth,hello);
54   - timer_submit(1000,count_down,&count);
55 125  
56 126 while( running ) {
57 127 usleep(1);
58 128 timer_update();
59 129 }
60 130  
61   - for( i = 0; i < disc; ++i ) {
62   - serial_free(&inos[i]);
63   - }
64   -
65   - timer_free();
  131 + arduino_free();
66 132 graphics_free();
  133 + timer_free();
67 134 }
... ...
app/src/serial.c
1 1 #include "serial.h"
2 2  
3   -#define SERIAL_DIR "/dev"
  3 +#define SERIAL_DIR "/dev/"
4 4  
5 5 #ifdef OS_LINUX
6 6 #define PATTERN_COUNT 2
... ... @@ -60,7 +60,11 @@ int serial_discover(int max, struct ino *inos)
60 60 }
61 61 char *port = entry->d_name;
62 62 if( matches_any_pattern(port) ) {
63   - serial_init(&inos[i++], port);
  63 + char *newport = malloc(strlen(port) + strlen(SERIAL_DIR) + 1);
  64 + memcpy(newport, SERIAL_DIR, strlen(SERIAL_DIR));
  65 + strcat(newport, port);
  66 + serial_init(&inos[i++], newport);
  67 + free(newport);
64 68 }
65 69 }
66 70 closedir(dir);
... ... @@ -88,12 +92,12 @@ int serial_free(struct ino *ino)
88 92  
89 93 int serial_connect(struct ino *ino)
90 94 {
91   - check(ino->port,"No port for Arduino");
92   - check(!(ino->status & INO_OPEN),"Arduino already connected");
  95 + ensure(ino->port,"No port for Arduino");
  96 + ensure(!(ino->status & INO_OPEN),"Arduino already connected");
93 97  
94 98 struct termios toptions;
95 99 int fd;
96   -
  100 +
97 101 fd = open(ino->port, O_RDWR | O_NONBLOCK);
98 102  
99 103 if( fd == -1 ) {
... ... @@ -116,7 +120,7 @@ int serial_connect(struct ino *ino)
116 120 case 38400: brate = B38400; break;
117 121 case 57600: brate = B57600; break;
118 122 case 115200: brate = B115200; break;
119   - default: check(0,"Invalid Baud Rate %d",BAUD_RATE);
  123 + default: ensure(0,"Invalid Baud Rate %d",BAUD_RATE);
120 124 }
121 125 cfsetispeed(&toptions, brate);
122 126 cfsetospeed(&toptions, brate);
... ... @@ -161,7 +165,7 @@ int serial_connect(struct ino *ino)
161 165  
162 166 int serial_disconnect(struct ino *ino)
163 167 {
164   - check(ino->status & INO_OPEN, "Arduino already disconnected");
  168 + ensure(ino->status & INO_OPEN, "Arduino already disconnected");
165 169 ino->status &= ~INO_OPEN;
166 170 return close(ino->fd);
167 171 }
... ... @@ -169,7 +173,7 @@ int serial_disconnect(struct ino *ino)
169 173  
170 174 int serial_write(struct ino *ino, int argc, char *argv)
171 175 {
172   - check(ino->status & INO_OPEN,"Arduino not connected");
  176 + ensure(ino->status & INO_OPEN,"Arduino not connected");
173 177 int n = write(ino->fd,argv,argc);
174 178 if( n != argc ) {
175 179 return -1;
... ... @@ -179,7 +183,7 @@ int serial_write(struct ino *ino, int argc, char *argv)
179 183  
180 184 int serial_available(struct ino *ino)
181 185 {
182   - check(ino->status & INO_OPEN,"Arduino not connected");
  186 + ensure(ino->status & INO_OPEN,"Arduino not connected");
183 187  
184 188 if( ino->read == INO_SIZE ) return INO_SIZE;
185 189 char *buf = &(ino->buffer[ino->read]);
... ... @@ -194,8 +198,8 @@ int serial_available(struct ino *ino)
194 198  
195 199 int serial_read(struct ino *ino, int argc, char *argv)
196 200 {
197   - check(ino->status & INO_OPEN, "Arduino not connected");
198   - check(argc <= INO_SIZE, "Arduino Buffer too small");
  201 + ensure(ino->status & INO_OPEN, "Arduino not connected");
  202 + ensure(argc <= INO_SIZE, "Arduino Buffer too small");
199 203  
200 204 if( ino->read < argc ) return -1;
201 205  
... ... @@ -207,7 +211,17 @@ int serial_read(struct ino *ino, int argc, char *argv)
207 211  
208 212 int serial_flush(struct ino *ino)
209 213 {
210   - check(ino->status & INO_OPEN, "Arduino not connected");
  214 + ensure(ino->status & INO_OPEN, "Arduino not connected");
211 215 //sleep(2); //required to make flush work, for some reason
212 216 return tcflush(ino->fd, TCIOFLUSH);
213 217 }
  218 +
  219 +void serial_name(struct ino *ino, char *buf)
  220 +{
  221 + int i;
  222 + memset(buf, 0, 16);
  223 + for( i = 0; i < 16; ++i ) {
  224 + if( ino->port[i] == 0 ) break;
  225 + buf[i] = ino->port[i];
  226 + }
  227 +}
... ...
app/src/serial.h
... ... @@ -7,7 +7,7 @@
7 7  
8 8 #include <stdint.h>
9 9  
10   -#define BAUD_RATE 9600
  10 +#define BAUD_RATE 115200
11 11  
12 12 #define INO_SIZE 256
13 13  
... ... @@ -30,5 +30,6 @@ int serial_write(struct ino *ino, int argc, char *argv);
30 30 int serial_available(struct ino *ino);
31 31 int serial_read(struct ino *ino, int argc, char *argv);
32 32 int serial_flush(struct ino *ino);
  33 +void serial_name(struct ino *ino, char *buf);
33 34  
34 35 #endif
... ...
app/src/util.h
... ... @@ -11,9 +11,11 @@
11 11 #ifdef NDEBUG
12 12 #define debug(M, ...)
13 13 #define check(A, M, ...)
  14 +#define ensure(A, M, ...)
14 15 #else
15 16 #define debug(M,...) fprintf(stderr, "[\x1B[36mDEBUG\x1B[0m] " M "\n",##__VA_ARGS__)
16   -#define check(A, M, ...) if(!(A)) { log_err(M, ##__VA_ARGS__); exit(EXIT_FAILURE); }
  17 +#define check(A, M, ...) if(!(A)) { log_warn(M, ##__VA_ARGS__); }
  18 +#define ensure(A, M, ...) if(!(A)) { log_err(M, ##__VA_ARGS__); exit(EXIT_FAILURE); }
17 19 #endif
18 20  
19 21 #define log_err(M,...) fprintf(stderr, "[\x1B[31mERROR\x1B[0m] " M "\n",##__VA_ARGS__)
... ...