Commit e2aafd68254acc0d483cd9fe34893a7be6982114

Authored by HAMEL
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,10 +79,11 @@ return tv.tv_sec*1000+tv.tv_usec/1000;
79 static void handleEvents(int sd){ 79 static void handleEvents(int sd){
80 struct pollfd fds[2]; 80 struct pollfd fds[2];
81 char line[MAX_LINE]; 81 char line[MAX_LINE];
82 -unsigned char packet[MAX_SERIAL]; 82 +
83 int ret; 83 int ret;
84 // my variables 84 // my variables
85 -int i; 85 +unsigned char packet[MAX_SERIAL];
  86 +
86 // 87 //
87 fds[0].fd=STDIN_FILENO; 88 fds[0].fd=STDIN_FILENO;
88 fds[0].events=POLLIN; 89 fds[0].events=POLLIN;
@@ -91,13 +92,9 @@ fds[1].events=POLLIN; @@ -91,13 +92,9 @@ fds[1].events=POLLIN;
91 92
92 while(1){ 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 ret=poll(fds,2,SLEEP_DELAY); 99 ret=poll(fds,2,SLEEP_DELAY);
103 if(ret==-1){ perror("poll"); exit(EXIT_FAILURE); } 100 if(ret==-1){ perror("poll"); exit(EXIT_FAILURE); }
@@ -119,16 +116,20 @@ while(1){ @@ -119,16 +116,20 @@ while(1){
119 printf("serial in: "); 116 printf("serial in: ");
120 #endif 117 #endif
121 int i; 118 int i;
  119 +
122 for(i=0;i<nb;i++){ 120 for(i=0;i<nb;i++){
123 #ifdef DEBUG 121 #ifdef DEBUG
124 - printf("%02x ",packet[i]); 122 + printf("%02x ",packet[i]);
125 #endif 123 #endif
126 - serialInHandler(packet[i]); 124 + serialInHandler(packet[i]);
  125 +
127 } 126 }
  127 +
128 #ifdef DEBUG 128 #ifdef DEBUG
129 printf("\n"); 129 printf("\n");
130 #endif 130 #endif
131 } 131 }
  132 +
132 if(outsize>0){ 133 if(outsize>0){
133 if(write(sd,outbuf,outsize)!=outsize) break; 134 if(write(sd,outbuf,outsize)!=outsize) break;
134 #ifdef DEBUG 135 #ifdef DEBUG
@@ -5,13 +5,19 @@ @@ -5,13 +5,19 @@
5 //// 5 ////
6 // Include files 6 // Include files
7 //// 7 ////
8 -#include "arduino.h"  
9 #include <stdio.h> 8 #include <stdio.h>
  9 +#include <stdbool.h>
  10 +#include "arduino.h"
  11 +#define MAX_SERIAL 1024
10 //// 12 ////
11 // Global variables 13 // Global variables
12 // my variables 14 // my variables
13 int i; 15 int i;
14 long now; 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,17 +27,44 @@ long now;
21 27
22 void setup(void){ 28 void setup(void){
23 i = 0; 29 i = 0;
  30 + j = 0;
  31 + decod = false;
  32 + nb_0C = 0;
24 } 33 }
25 34
26 void loop(void){ 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 void serialInHandler(unsigned char byte){ 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 }