Commit 2dcf20ecf2e7bf3a6f6006a41186ef60946a81c7

Authored by HAMEL
1 parent 43960e63

first program withour button control

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