| |
1
| +++ a/section_3/arduino.c |
| @@ -0,0 +1,159 @@ |
| @@ -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
| +} |