Commit d69c8d6b0880d6cad4bcc88cb56d8b35eb330bca
0 parents
première séance : sections 1,2,3,4
Showing
6 changed files
with
312 additions
and
0 deletions
Show diff stats
1 | +++ a/section_3/arduino.c | |
... | ... | @@ -0,0 +1,159 @@ |
1 | +/* | |
2 | + * Very basic emulation of an Arduino UNO with some buttons and some LEDs | |
3 | + */ | |
4 | + | |
5 | +//// | |
6 | +// Include files | |
7 | +//// | |
8 | +#include <stdio.h> | |
9 | +#include <stdlib.h> | |
10 | +#include <unistd.h> | |
11 | +#include <string.h> | |
12 | +#include <sys/poll.h> | |
13 | +#include <sys/time.h> | |
14 | +#include <stdbool.h> | |
15 | + | |
16 | +#include "virtual_serial.h" | |
17 | +#include "arduino.h" | |
18 | + | |
19 | +//// | |
20 | +// Constants | |
21 | +//// | |
22 | +#define SERIAL_SPEED 9600 | |
23 | +#define SLEEP_DELAY 100 | |
24 | + | |
25 | +#define MAX_NAME 1024 | |
26 | +#define MAX_LINE 1024 | |
27 | +#define MAX_SERIAL 1024 | |
28 | + | |
29 | +//// | |
30 | +// Global variables | |
31 | +//// | |
32 | + | |
33 | +//// | |
34 | +// Utility functions | |
35 | +//// | |
36 | + | |
37 | +static unsigned char ledstates[LEDS_NB]; | |
38 | +void led(int num,unsigned char state){ | |
39 | +if(num>=0 && num<LEDS_NB) ledstates[num]=state; | |
40 | +int i; | |
41 | +printf(" LEDs: "); | |
42 | +for(i=0;i<LEDS_NB;i++) printf("[%c] ",ledstates[i]?'X':' '); | |
43 | +printf("\r"); | |
44 | +fflush(stdout); | |
45 | +} | |
46 | + | |
47 | +static unsigned char outbuf[MAX_SERIAL]; | |
48 | +int outsize=0; | |
49 | +void serialSend(unsigned char *data,int size){ | |
50 | +if(size+outsize<MAX_SERIAL){ | |
51 | + memcpy(outbuf+outsize,data,size); | |
52 | + } | |
53 | +} | |
54 | + | |
55 | +static unsigned char butstates[BUTTONS_NB]; | |
56 | +static void _button(int num,unsigned char state){ | |
57 | +if(num>=0 && num<BUTTONS_NB) butstates[num]=state; | |
58 | +int i; | |
59 | +printf("Buttons: "); | |
60 | +for(i=0;i<BUTTONS_NB;i++) printf("[%c] ",(butstates[i]?'A':'a')+i); | |
61 | +printf("\n"); | |
62 | +} | |
63 | + | |
64 | +unsigned char button(int num){ | |
65 | +if(num>=0 && num<BUTTONS_NB) return butstates[num]; | |
66 | +return 255; | |
67 | +} | |
68 | + | |
69 | +long millis(void){ | |
70 | +struct timeval tv; | |
71 | +gettimeofday(&tv,NULL); | |
72 | +return tv.tv_sec*1000+tv.tv_usec/1000; | |
73 | +} | |
74 | + | |
75 | +//// | |
76 | +// Events function | |
77 | +//// | |
78 | + | |
79 | +static void handleEvents(int sd){ | |
80 | +struct pollfd fds[2]; | |
81 | +char line[MAX_LINE]; | |
82 | +unsigned char packet[MAX_SERIAL]; | |
83 | +int ret; | |
84 | +// my variables | |
85 | +int i; | |
86 | +// | |
87 | +fds[0].fd=STDIN_FILENO; | |
88 | +fds[0].events=POLLIN; | |
89 | +fds[1].fd=sd; | |
90 | +fds[1].events=POLLIN; | |
91 | + | |
92 | +while(1){ | |
93 | + | |
94 | + if (butstates[0] == 1) | |
95 | + { | |
96 | + | |
97 | + i = 0; | |
98 | + while (i <= BUTTONS_NB && butstates[i]) | |
99 | + i += 1; | |
100 | + if(i <= BUTTONS_NB) loop(); | |
101 | + } | |
102 | + ret=poll(fds,2,SLEEP_DELAY); | |
103 | + if(ret==-1){ perror("poll"); exit(EXIT_FAILURE); } | |
104 | + if(fds[0].revents&POLLIN){ | |
105 | + if(fgets(line,MAX_LINE,stdin)==NULL){ perror("fgets"); exit(EXIT_FAILURE); } | |
106 | +#ifdef DEBUG | |
107 | + printf("keyboard: %s",line); | |
108 | +#endif | |
109 | + int i; | |
110 | + for(i=0;i<strlen(line);i++){ | |
111 | + if(line[i]>='a' && line[i]<='d') _button(line[i]-'a',0); | |
112 | + if(line[i]>='A' && line[i]<='D') _button(line[i]-'A',1); | |
113 | + } | |
114 | + } | |
115 | + if(fds[1].revents&POLLIN){ | |
116 | + int nb; | |
117 | + if((nb=read(sd,packet,MAX_SERIAL))<=0) break; | |
118 | +#ifdef DEBUG | |
119 | + printf("serial in: "); | |
120 | +#endif | |
121 | + int i; | |
122 | + for(i=0;i<nb;i++){ | |
123 | +#ifdef DEBUG | |
124 | + printf("%02x ",packet[i]); | |
125 | +#endif | |
126 | + serialInHandler(packet[i]); | |
127 | + } | |
128 | +#ifdef DEBUG | |
129 | + printf("\n"); | |
130 | +#endif | |
131 | + } | |
132 | + if(outsize>0){ | |
133 | + if(write(sd,outbuf,outsize)!=outsize) break; | |
134 | +#ifdef DEBUG | |
135 | + printf("serial out: "); | |
136 | + int i; | |
137 | + for(i=0;i<outsize;i++){ | |
138 | + printf("%02x ",outbuf[i]); | |
139 | + } | |
140 | + printf("\n"); | |
141 | +#endif | |
142 | + outsize=0; | |
143 | + } | |
144 | + } | |
145 | +} | |
146 | + | |
147 | +//// | |
148 | +// Main function | |
149 | +//// | |
150 | + | |
151 | +int main(void){ | |
152 | +char name[MAX_NAME]; | |
153 | +int sd=virtualSerialOpen(name,SERIAL_SPEED); | |
154 | +printf("Slave device is '%s'\n",name); | |
155 | +setup(); | |
156 | +handleEvents(sd); | |
157 | +virtualSerialClose(sd); | |
158 | +exit(0); | |
159 | +} | ... | ... |
1 | +++ a/section_3/arduino.h | |
... | ... | @@ -0,0 +1,26 @@ |
1 | +/* | |
2 | + * Constants and prototype for Arduino simulator | |
3 | + */ | |
4 | + | |
5 | +//// | |
6 | +// Constants | |
7 | +//// | |
8 | +#define BUTTONS_NB 4 | |
9 | +#define LEDS_NB 6 | |
10 | + | |
11 | +//// | |
12 | +// Prototypes of defined fonctions | |
13 | +//// | |
14 | + | |
15 | +void led(int num,unsigned char state); | |
16 | +unsigned char button(int num); | |
17 | +void serialSend(unsigned char *data,int size); | |
18 | +long millis(void); | |
19 | + | |
20 | +//// | |
21 | +// Prototypes of users handlers | |
22 | +//// | |
23 | + | |
24 | +void setup(void); | |
25 | +void loop(void); | |
26 | +void serialInHandler(unsigned char byte); | ... | ... |
1 | +++ a/section_3/main.c | |
... | ... | @@ -0,0 +1,37 @@ |
1 | +/* | |
2 | + * Arduino program example | |
3 | + */ | |
4 | + | |
5 | +//// | |
6 | +// Include files | |
7 | +//// | |
8 | +#include "arduino.h" | |
9 | +#include <stdio.h> | |
10 | +//// | |
11 | +// Global variables | |
12 | +// my variables | |
13 | +int i; | |
14 | +long now; | |
15 | +// | |
16 | +//// | |
17 | + | |
18 | +//// | |
19 | +// Handler functions | |
20 | +/// | |
21 | + | |
22 | +void setup(void){ | |
23 | + i = 0; | |
24 | +} | |
25 | + | |
26 | +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; | |
32 | + | |
33 | +} | |
34 | + | |
35 | + | |
36 | +void serialInHandler(unsigned char byte){ | |
37 | +} | ... | ... |
1 | +++ a/section_3/virtual_serial.c | |
... | ... | @@ -0,0 +1,69 @@ |
1 | +/* | |
2 | + * Virtual Serial library | |
3 | + */ | |
4 | + | |
5 | +//// | |
6 | +// Include files | |
7 | +//// | |
8 | +#include <stdio.h> | |
9 | +#include <stdlib.h> | |
10 | +#include <unistd.h> | |
11 | +#include <termios.h> | |
12 | +#include <strings.h> | |
13 | +#include <sys/types.h> | |
14 | +#include <sys/ioctl.h> | |
15 | +#include <sys/file.h> | |
16 | +#include <linux/serial.h> | |
17 | +#include <pty.h> | |
18 | + | |
19 | +#include "virtual_serial.h" | |
20 | + | |
21 | +//// | |
22 | +// Functions | |
23 | +//// | |
24 | + | |
25 | +// | |
26 | +// Serial structure filling | |
27 | +// | |
28 | +static struct termios serialConfig(int speed){ | |
29 | +int fspeed; | |
30 | +switch(speed){ | |
31 | + case 1200: fspeed=B1200; break; | |
32 | + case 2400: fspeed=B2400; break; | |
33 | + case 4800: fspeed=B4800; break; | |
34 | + case 9600: fspeed=B9600; break; | |
35 | + case 19200: fspeed=B19200; break; | |
36 | + case 38400: fspeed=B38400; break; | |
37 | + case 57600: fspeed=B57600; break; | |
38 | + case 115200: fspeed=B115200; break; | |
39 | + default: fprintf(stderr,"Unknown serial speed %d.\n",speed); exit(-1); | |
40 | + } | |
41 | +struct termios new; | |
42 | +bzero(&new,sizeof(new)); | |
43 | +new.c_cflag=CLOCAL|CREAD|fspeed|CS8; | |
44 | +new.c_iflag=0; | |
45 | +new.c_oflag=0; | |
46 | +new.c_lflag=0; /* set input mode (non-canonical, no echo,...) */ | |
47 | +new.c_cc[VTIME]=0; /* inter-character timer unused */ | |
48 | +new.c_cc[VMIN]=1; /* blocking read until 1 char received */ | |
49 | +return new; | |
50 | +} | |
51 | + | |
52 | +// | |
53 | +// Virtual serial creation | |
54 | +// | |
55 | + | |
56 | +int virtualSerialOpen(char *name,int speed){ | |
57 | +int master; | |
58 | +int slave; | |
59 | +struct termios serial=serialConfig(speed); | |
60 | +openpty(&master,&slave,name,&serial,NULL); | |
61 | +return master; | |
62 | +} | |
63 | + | |
64 | +// | |
65 | +// Serial port termination | |
66 | +// | |
67 | +void virtualSerialClose(int fd){ | |
68 | +close(fd); | |
69 | +} | ... | ... |