From fc6b9e3317d9349a03b545dc764abb0f283f1ccc Mon Sep 17 00:00:00 2001 From: Geoffrey Frogeye Date: Mon, 12 Feb 2018 19:23:24 +0100 Subject: [PATCH] =?UTF-8?q?Communication=20A=E2=86=94C=20:=20Base=20c?= =?UTF-8?q?=C3=B4t=C3=A9=20Chef?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- chef/Makefile | 2 +- chef/src/ACsignals.h | 1 + chef/src/premier.c | 14 ++++++++--- chef/src/serial.c | 58 ++++++++++++++++++++++++++++++++++++++++++++ chef/src/serial.h | 19 +++++++++++++++ 5 files changed, 90 insertions(+), 4 deletions(-) create mode 120000 chef/src/ACsignals.h create mode 100644 chef/src/serial.c create mode 100644 chef/src/serial.h diff --git a/chef/Makefile b/chef/Makefile index bc71177..2042105 100644 --- a/chef/Makefile +++ b/chef/Makefile @@ -37,7 +37,7 @@ endif default: bin/testpin bin/premier # Binaires (dont il faut spécifier les objets explicitement) -bin/premier: obj/common.o +bin/premier: obj/common.o obj/serial.o bin/testPin: obj/testPin.o $(CC) $(LDFLAGS) $^ -lwiringPi -o $@ diff --git a/chef/src/ACsignals.h b/chef/src/ACsignals.h new file mode 120000 index 0000000..3156691 --- /dev/null +++ b/chef/src/ACsignals.h @@ -0,0 +1 @@ +../../arduino/ACsignals.h \ No newline at end of file diff --git a/chef/src/premier.c b/chef/src/premier.c index 785155c..e7d0f03 100644 --- a/chef/src/premier.c +++ b/chef/src/premier.c @@ -1,7 +1,15 @@ #include "premier.h" -int main() { - printf("Hello world!\n"); - // avancer(10); +#include "serial.h" + +int main() +{ + unsigned char g; + configureCA(); + sendByteCA('c'); + while ((g = readByteCA())) { + printf("%c\n", g); + } + deconfigureCA(); return 0; } diff --git a/chef/src/serial.c b/chef/src/serial.c new file mode 100644 index 0000000..2c75b46 --- /dev/null +++ b/chef/src/serial.c @@ -0,0 +1,58 @@ +#include "serial.h" +#include // printf... +#include // stuff +#include // read(), write()... +#include // O_* +#include // bzero + +#include "common.h" + +void configureCA() +{ + // Connection au port série + printf("Connexion à %s... ", ARDUINO_PORTNAME); + arduino = open(ARDUINO_PORTNAME, O_RDWR | O_NOCTTY | O_NDELAY); + if (arduino < 0) { + printf("Échec !\n"); + exit(1); + } + + // Configuration du port série + struct termios cfg; + bzero(&cfg, sizeof(cfg)); + cfg.c_cflag = CLOCAL | CREAD | CA_BAUDRATE | CS8; + cfg.c_iflag = 0; + cfg.c_oflag = 0; + cfg.c_lflag = 0; /* set input mode (non-canonical, no echo,...) */ + cfg.c_cc[VTIME] = 0; /* inter-character timer unused */ + cfg.c_cc[VMIN] = 1; /* blocking read until 1 char received */ + if (tcsetattr(arduino, TCSANOW, &cfg) < 0) { + perror("serialConfig.tcsetattr"); + exit(1); + } + + sleep(1); + + printf("OK!\n"); +} + +void deconfigureCA() { + close(arduino); +} + +void sendByteCA(unsigned char data) { + write(arduino, &data, sizeof(data)); +} + +void sendCA(unsigned char code, void* data, size_t size) +{ + sendByteCA(code); + write(arduino, data, size); +} + +unsigned char readByteCA() { + unsigned char c; + while (read(arduino, &c, sizeof(c)) < 1) { + } + return c; +} diff --git a/chef/src/serial.h b/chef/src/serial.h new file mode 100644 index 0000000..a021964 --- /dev/null +++ b/chef/src/serial.h @@ -0,0 +1,19 @@ +#ifndef __SERIAL_H_ +#define __SERIAL_H_ + +#include +#include // baudrates + +#include "ACsignals.h" + +#define ARDUINO_PORTNAME "/dev/ttyACM0" +#define CA_BAUDRATE B9600 + +int arduino; + +void configureCA(); +void deconfigureCA(); +void sendByteCA(unsigned char data); +unsigned char readByteCA(); + +#endif