arduino.c
2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* Very basic emulation of an Arduino UNO with some buttons and some LEDs
*/
////
// Include files
////
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/poll.h>
#include <sys/time.h>
#include "virtual_serial.h"
#include "arduino.h"
////
// Constants
////
#define SERIAL_SPEED 9600
#define SLEEP_DELAY 100
#define MAX_NAME 1024
#define MAX_LINE 1024
#define MAX_SERIAL 1024
////
// Global variables
////
////
// Utility functions
////
static unsigned char ledstates[LEDS_NB];
void led(int num,unsigned char state){
if(num>=0 && num<LEDS_NB) ledstates[num]=state;
int i;
printf(" LEDs: ");
for(i=0;i<LEDS_NB;i++) printf("[%c] ",ledstates[i]?'X':' ');
printf("\r");
fflush(stdout);
}
static unsigned char outbuf[MAX_SERIAL];
int outsize=0;
void serialSend(unsigned char *data,int size){
if(size+outsize<MAX_SERIAL){
memcpy(outbuf+outsize,data,size);
}
}
static unsigned char butstates[BUTTONS_NB];
static void _button(int num,unsigned char state){
if(num>=0 && num<BUTTONS_NB) butstates[num]=state;
int i;
printf("Buttons: ");
for(i=0;i<BUTTONS_NB;i++) printf("[%c] ",(butstates[i]?'A':'a')+i);
printf("\n");
}
unsigned char button(int num){
if(num>=0 && num<BUTTONS_NB) return butstates[num];
return 255;
}
long millis(void){
struct timeval tv;
gettimeofday(&tv,NULL);
return tv.tv_sec*1000+tv.tv_usec/1000;
}
////
// Events function
////
static void handleEvents(int sd){
struct pollfd fds[2];
char line[MAX_LINE];
unsigned char packet[MAX_SERIAL];
int ret;
// my variables
//
fds[0].fd=STDIN_FILENO;
fds[0].events=POLLIN;
fds[1].fd=sd;
fds[1].events=POLLIN;
while(1){
loop();
ret=poll(fds,2,SLEEP_DELAY);
if(ret==-1){ perror("poll"); exit(EXIT_FAILURE); }
if(fds[0].revents&POLLIN){
if(fgets(line,MAX_LINE,stdin)==NULL){ perror("fgets"); exit(EXIT_FAILURE); }
#ifdef DEBUG
printf("keyboard: %s",line);
#endif
int i;
for(i=0;i<strlen(line);i++){
if(line[i]>='a' && line[i]<='d') _button(line[i]-'a',0);
if(line[i]>='A' && line[i]<='D') _button(line[i]-'A',1);
}
}
if(fds[1].revents&POLLIN){
int nb;
if((nb=read(sd,packet,MAX_SERIAL))<=0) break;
#ifdef DEBUG
printf("serial in: ");
#endif
int i;
for(i=0;i<nb;i++){
#ifdef DEBUG
printf("%02x ",packet[i]);
#endif
serialInHandler(packet[i]);
}
#ifdef DEBUG
printf("\n");
#endif
}
if(outsize>0){
if(write(sd,outbuf,outsize)!=outsize) break;
#ifdef DEBUG
printf("serial out: ");
int i;
for(i=0;i<outsize;i++){
printf("%02x ",outbuf[i]);
}
printf("\n");
#endif
outsize=0;
}
}
}
////
// Main function
////
int main(void){
char name[MAX_NAME];
int sd=virtualSerialOpen(name,SERIAL_SPEED);
printf("Slave device is '%s'\n",name);
setup();
handleEvents(sd);
virtualSerialClose(sd);
exit(0);
}