Blame view

section_3/arduino.c 2.9 KB
d69c8d6b   HAMEL   première séance :...
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
  /*
   * 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 <stdbool.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];
e2aafd68   HAMEL   slip protocol
82
  
d69c8d6b   HAMEL   première séance :...
83
84
  int ret;
  // my variables 
e2aafd68   HAMEL   slip protocol
85
86
  unsigned char packet[MAX_SERIAL];
  
d69c8d6b   HAMEL   première séance :...
87
88
89
90
91
92
93
94
  //
  fds[0].fd=STDIN_FILENO;
  fds[0].events=POLLIN;
  fds[1].fd=sd;
  fds[1].events=POLLIN;
  
  while(1){
    
e2aafd68   HAMEL   slip protocol
95
    if (butstates[0] == 1 && butstates[1] == 0 && butstates[2] == 0 && butstates[3] == 0)
d69c8d6b   HAMEL   première séance :...
96
    {
e2aafd68   HAMEL   slip protocol
97
  	loop();
d69c8d6b   HAMEL   première séance :...
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
    }
    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;
e2aafd68   HAMEL   slip protocol
119
  
d69c8d6b   HAMEL   première séance :...
120
121
      for(i=0;i<nb;i++){
  #ifdef DEBUG
e2aafd68   HAMEL   slip protocol
122
  	printf("%02x ",packet[i]);
d69c8d6b   HAMEL   première séance :...
123
  #endif
e2aafd68   HAMEL   slip protocol
124
125
  	serialInHandler(packet[i]);
        	
d69c8d6b   HAMEL   première séance :...
126
        }
e2aafd68   HAMEL   slip protocol
127
  
d69c8d6b   HAMEL   première séance :...
128
129
130
131
  #ifdef DEBUG
      printf("\n");
  #endif
      }
e2aafd68   HAMEL   slip protocol
132
  
d69c8d6b   HAMEL   première séance :...
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
    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);
  }