diff --git a/section_3/Makefile b/section_3/Makefile new file mode 100644 index 0000000..f843fa4 --- /dev/null +++ b/section_3/Makefile @@ -0,0 +1,12 @@ +SOURCES = $(wildcard *.c) +OBJETS = $(SOURCES:.c=.o) +EXECUTABLE = arduino +LDLIBS += -lutil +CFLAGS += -Wall + +all: $(EXECUTABLE) + +$(EXECUTABLE): $(OBJETS) + +clean: + rm -rf $(EXECUTABLE) *.o diff --git a/section_3/arduino.c b/section_3/arduino.c new file mode 100644 index 0000000..e68b61e --- /dev/null +++ b/section_3/arduino.c @@ -0,0 +1,159 @@ +/* + * Very basic emulation of an Arduino UNO with some buttons and some LEDs + */ + +//// +// Include files +//// +#include +#include +#include +#include +#include +#include +#include + +#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=0 && num=0 && num='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;i0){ + if(write(sd,outbuf,outsize)!=outsize) break; +#ifdef DEBUG + printf("serial out: "); + int i; + for(i=0;i +//// +// Global variables +// my variables +int i; +long now; +// +//// + +//// +// Handler functions +/// + +void setup(void){ + i = 0; +} + +void loop(void){ + led((i-1 + LEDS_NB) % LEDS_NB, 0); + led(i % LEDS_NB, 1); + now = millis(); + while(millis() != now+300); + i = (i+1) % LEDS_NB; + +} + + +void serialInHandler(unsigned char byte){ +} diff --git a/section_3/virtual_serial.c b/section_3/virtual_serial.c new file mode 100644 index 0000000..aa69b5a --- /dev/null +++ b/section_3/virtual_serial.c @@ -0,0 +1,69 @@ +/* + * Virtual Serial library + */ + +//// +// Include files +//// +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "virtual_serial.h" + +//// +// Functions +//// + +// +// Serial structure filling +// +static struct termios serialConfig(int speed){ +int fspeed; +switch(speed){ + case 1200: fspeed=B1200; break; + case 2400: fspeed=B2400; break; + case 4800: fspeed=B4800; break; + case 9600: fspeed=B9600; break; + case 19200: fspeed=B19200; break; + case 38400: fspeed=B38400; break; + case 57600: fspeed=B57600; break; + case 115200: fspeed=B115200; break; + default: fprintf(stderr,"Unknown serial speed %d.\n",speed); exit(-1); + } +struct termios new; +bzero(&new,sizeof(new)); +new.c_cflag=CLOCAL|CREAD|fspeed|CS8; +new.c_iflag=0; +new.c_oflag=0; +new.c_lflag=0; /* set input mode (non-canonical, no echo,...) */ +new.c_cc[VTIME]=0; /* inter-character timer unused */ +new.c_cc[VMIN]=1; /* blocking read until 1 char received */ +return new; +} + +// +// Virtual serial creation +// + +int virtualSerialOpen(char *name,int speed){ +int master; +int slave; +struct termios serial=serialConfig(speed); +openpty(&master,&slave,name,&serial,NULL); +return master; +} + +// +// Serial port termination +// +void virtualSerialClose(int fd){ +close(fd); +} diff --git a/section_3/virtual_serial.h b/section_3/virtual_serial.h new file mode 100644 index 0000000..20c91e6 --- /dev/null +++ b/section_3/virtual_serial.h @@ -0,0 +1,9 @@ +/* + * Public definitions for virtual serial library + */ + +//// +// Public prototypes +//// +int virtualSerialOpen(char *name,int speed); +void virtualSerialClose(int fd); -- libgit2 0.21.2