Commit e2aafd68254acc0d483cd9fe34893a7be6982114
1 parent
d69c8d6b
slip protocol
Showing
2 changed files
with
50 additions
and
16 deletions
Show diff stats
section_3/arduino.c
... | ... | @@ -79,10 +79,11 @@ return tv.tv_sec*1000+tv.tv_usec/1000; |
79 | 79 | static void handleEvents(int sd){ |
80 | 80 | struct pollfd fds[2]; |
81 | 81 | char line[MAX_LINE]; |
82 | -unsigned char packet[MAX_SERIAL]; | |
82 | + | |
83 | 83 | int ret; |
84 | 84 | // my variables |
85 | -int i; | |
85 | +unsigned char packet[MAX_SERIAL]; | |
86 | + | |
86 | 87 | // |
87 | 88 | fds[0].fd=STDIN_FILENO; |
88 | 89 | fds[0].events=POLLIN; |
... | ... | @@ -91,13 +92,9 @@ fds[1].events=POLLIN; |
91 | 92 | |
92 | 93 | while(1){ |
93 | 94 | |
94 | - if (butstates[0] == 1) | |
95 | + if (butstates[0] == 1 && butstates[1] == 0 && butstates[2] == 0 && butstates[3] == 0) | |
95 | 96 | { |
96 | - | |
97 | - i = 0; | |
98 | - while (i <= BUTTONS_NB && butstates[i]) | |
99 | - i += 1; | |
100 | - if(i <= BUTTONS_NB) loop(); | |
97 | + loop(); | |
101 | 98 | } |
102 | 99 | ret=poll(fds,2,SLEEP_DELAY); |
103 | 100 | if(ret==-1){ perror("poll"); exit(EXIT_FAILURE); } |
... | ... | @@ -119,16 +116,20 @@ while(1){ |
119 | 116 | printf("serial in: "); |
120 | 117 | #endif |
121 | 118 | int i; |
119 | + | |
122 | 120 | for(i=0;i<nb;i++){ |
123 | 121 | #ifdef DEBUG |
124 | - printf("%02x ",packet[i]); | |
122 | + printf("%02x ",packet[i]); | |
125 | 123 | #endif |
126 | - serialInHandler(packet[i]); | |
124 | + serialInHandler(packet[i]); | |
125 | + | |
127 | 126 | } |
127 | + | |
128 | 128 | #ifdef DEBUG |
129 | 129 | printf("\n"); |
130 | 130 | #endif |
131 | 131 | } |
132 | + | |
132 | 133 | if(outsize>0){ |
133 | 134 | if(write(sd,outbuf,outsize)!=outsize) break; |
134 | 135 | #ifdef DEBUG | ... | ... |
section_3/main.c
... | ... | @@ -5,13 +5,19 @@ |
5 | 5 | //// |
6 | 6 | // Include files |
7 | 7 | //// |
8 | -#include "arduino.h" | |
9 | 8 | #include <stdio.h> |
9 | +#include <stdbool.h> | |
10 | +#include "arduino.h" | |
11 | +#define MAX_SERIAL 1024 | |
10 | 12 | //// |
11 | 13 | // Global variables |
12 | 14 | // my variables |
13 | 15 | int i; |
14 | 16 | long now; |
17 | +bool decod; | |
18 | +unsigned char decoded_packet[MAX_SERIAL]; | |
19 | +int j; | |
20 | +int nb_0C; | |
15 | 21 | // |
16 | 22 | //// |
17 | 23 | |
... | ... | @@ -21,17 +27,44 @@ long now; |
21 | 27 | |
22 | 28 | void setup(void){ |
23 | 29 | i = 0; |
30 | + j = 0; | |
31 | + decod = false; | |
32 | + nb_0C = 0; | |
24 | 33 | } |
25 | 34 | |
26 | 35 | void loop(void){ |
27 | - led((i-1 + LEDS_NB) % LEDS_NB, 0); | |
28 | - led(i % LEDS_NB, 1); | |
29 | - now = millis(); | |
30 | - while(millis() != now+300); | |
31 | - i = (i+1) % LEDS_NB; | |
36 | + if(millis() > now+300){ | |
37 | + led((i-1 + LEDS_NB) % LEDS_NB, 0); | |
38 | + led(i % LEDS_NB, 1); | |
39 | + i = (i+1) % LEDS_NB; | |
40 | + } | |
32 | 41 | |
33 | 42 | } |
34 | 43 | |
35 | 44 | |
36 | 45 | void serialInHandler(unsigned char byte){ |
46 | + if(byte == '\xdb') { | |
47 | + decod = true; | |
48 | + } | |
49 | + else if(byte == '\x0c') nb_0C += 1; | |
50 | + else if(decod){ | |
51 | + if(byte == '\xdc') | |
52 | + decoded_packet[j] = '\xc0'; | |
53 | + else if(byte== '\xdd') | |
54 | + decoded_packet[j] = '\xdb'; | |
55 | + j += 1; | |
56 | + decod = false; | |
57 | + }else{ | |
58 | + | |
59 | + decoded_packet[j] = byte; | |
60 | + j += 1; | |
61 | + } | |
62 | + | |
63 | + if(nb_0C == 2){ | |
64 | + printf("le packet décodé est de taille %d:\n", j); | |
65 | + nb_0C = 0; | |
66 | + } | |
67 | + /*for(int i = 0; i < j; i++) | |
68 | + printf("%02x ", decoded_packet[i]); | |
69 | + printf("\n");*/ | |
37 | 70 | } | ... | ... |