bidirectionnal communication, first milestone reached

This commit is contained in:
phyto 2019-05-14 15:46:49 +02:00
parent 565c70b54f
commit 6d597e59e8
5 changed files with 69 additions and 42 deletions

View File

@ -6,16 +6,21 @@ CC = gcc
CCOPT = -Wall -g CCOPT = -Wall -g
CLIB = core/libdd2m-core.a viz/libdd2m-viz.a CLIB = core/libdd2m-core.a viz/libdd2m-viz.a
all: essai fake-values all: essai
# --------------------------------------------- # -------------------------------------------------------
O = serial/serial.o serial/funcs.o OSERIAL = serial/serial.o serial/funcs.o
essai: essai.c Makefile $(CLIB) essai.o: essai.c Makefile
$(CC) ${CCOPT} $< $(CLIB) ${O} -lncurses -o $@ $(CC) ${CCOPT} $< -c
essai: essai.o Makefile $(CLIB)
$(CC) ${CCOPT} $< $(CLIB) ${OSERIAL} -lncurses -o $@
fake-values: fake-values.c Makefile $(CLIB) fake-values: fake-values.c Makefile $(CLIB)
$(CC) ${CCOPT} $< $(CLIB) -o $@ $(CC) ${CCOPT} $< $(CLIB) -o $@
# --------------------------------------------- # -------------------------------------------------------

75
essai.c
View File

@ -5,6 +5,7 @@
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include <ctype.h>
#include <string.h> #include <string.h>
#include <curses.h> #include <curses.h>
#include <time.h> #include <time.h>
@ -17,7 +18,7 @@
int verbosity; int verbosity;
/* --------------------------------------------------------------- */ /* --------------------------------------------------------------- */
int affiche_valeurs(int sfd, int nbloops) int traite_les_messages(int sfd, int nbloops)
{ {
int idx, foo, key; int idx, foo, key;
char ligne[200]; char ligne[200];
@ -27,56 +28,75 @@ FILE *fp;
time_t temps, time_t temps,
old_temps = (time_t)0L; old_temps = (time_t)0L;
/* XXX XXX XXX XXX XXX XXX XXX XXX XXX XXX */
fp = fopen("serial/foo.dat", "a"); fp = fopen("serial/foo.dat", "a");
if (NULL==fp) { if (NULL==fp) {
fprintf(stderr, "***** error fopen datafile *****\n"); fprintf(stderr, "*** error fopen datafile ***\n");
return -1; return -1;
} }
/* XXX XXX XXX XXX XXX XXX XXX XXX XXX XXX */
for (idx=0; idx<nbloops; idx++) { for (idx=0; idx<nbloops; idx++) {
if (kbhit()) { if (kbhit()) {
message("!!! KEY !!!"); static char valid_k[] = "+-";
sleep(2); key = getch();
/* if it is a valid key, send it to the Arduino */
if (NULL!=strchr(valid_k, key)) {
foo = putbyte(sfd, key);
fprintf(stderr, "put byte %02X -> %d\n",
key, foo);
}
else {
sprintf(ligne, "Key 0x%02X invalid ", key);
fprintf(stderr, "%s\n", ligne);
aff_message(ligne); sleep(1); aff_message("");
}
} }
/* here we are waiting for a frame from the
Arduino. In the future, we may have an
finely tunned event-synth system here */
foo = getline_to(sfd, ligne, 100, 0); foo = getline_to(sfd, ligne, 100, 0);
if (*ligne == 'M') {
message(ligne);
}
#if DEBUG_LEVEL #if DEBUG_LEVEL
if (foo) fprintf(stderr, "get values -> %d\n", foo); if (foo) fprintf(stderr, "get values -> %d\n", foo);
#endif #endif
foo = parse4_Ivalues(ligne, 'T', Idatas); switch (*ligne) {
case 'M':
case 'L':
aff_message(ligne);
break;
case 'T':
foo = parse4_Ivalues(ligne, 'T', Idatas);
#if DEBUG_LEVEL #if DEBUG_LEVEL
if (foo) fprintf(stderr, "parse I val -> %d\n", foo); if (foo) fprintf(stderr, "parse I val -> %d\n", foo);
#endif #endif
values2temperature(Idatas, Fdatas);
values2temperature(Idatas, Fdatas); for (foo=0; foo<3; foo++) {
sprintf(ligne, "%4d", Idatas[foo]);
minidigit_affstr(stdscr, 12+(12*foo), 8, ligne);
aff7segs_float(stdscr, 8+(12*foo), 55, Fdatas[foo]);
}
for (foo=0; foo<3; foo++) { /* here we are loging all that temps values */
sprintf(ligne, "%4d", Idatas[foo]);
minidigit_affstr(stdscr, 12+(12*foo), 8, ligne);
aff7segs_float(stdscr, 8+(12*foo), 55, Fdatas[foo]);
}
/* here we are trying to log all that temps values */
if (NULL!=fp) {
temps = time(NULL); temps = time(NULL);
if ((temps-old_temps) > 50) { if ((temps-old_temps) > 50) {
fprintf(fp, "%ld %f %f %f %f\n", temps, fprintf(fp, "%ld %f %f %f %f\n", temps,
Fdatas[0], Fdatas[1], Fdatas[2], Fdatas[3]); Fdatas[0], Fdatas[1],
Fdatas[2], Fdatas[3]);
fflush(fp); fflush(fp);
old_temps = temps; old_temps = temps;
} }
break; /* case 'T' */
} }
fflush(stderr); /* really WTF? here */
} }
fclose(fp); fclose(fp);
@ -102,7 +122,7 @@ exit(0);
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int opt, foo; int opt;
int serial_in; int serial_in;
char *device = "/dev/ttyACM0"; char *device = "/dev/ttyACM0";
@ -115,21 +135,24 @@ while ((opt = getopt(argc, argv, "d:hv")) != -1) {
} }
} }
printf("\n**** %s **** compiled the %s at %s ***\n",
argv[0], __DATE__, __TIME__);
serial_in = prepare_UART(device, 9600); serial_in = prepare_UART(device, 9600);
if (serial_in < 0) { if (serial_in < 0) {
fprintf(stderr, "%s : open device : error %d on %s\n", fprintf(stderr, "\n%s : open device : error %d on %s\n",
argv[0], serial_in, device); argv[0], serial_in, device);
exit(1); exit(1);
} }
sleep(1);
initscr(); initscr();
nonl(); cbreak(); noecho(); nonl(); cbreak(); noecho();
keypad(stdscr, TRUE); /* acces aux touches 'curseur' */ keypad(stdscr, TRUE); /* acces aux touches 'curseur' */
fond_ecran(" Demonstrator "); fond_ecran(" Demonstrator ");
affiche_valeurs(serial_in, 50000000); traite_les_messages(serial_in, 50000000);
/* /*
* plop, on a fini, il faut restaurer la console * plop, on a fini, il faut restaurer la console

View File

@ -10,8 +10,6 @@
#define DEBUG 0 #define DEBUG 0
/* -------------------------------------------------- */ /* -------------------------------------------------- */
/* some interesting macros */ /* some interesting macros */
#define prt(a) Serial.print(a)
#define prtln(a) Serial.println(a)
/* -------------------------------------------------- */ /* -------------------------------------------------- */
char waitkey(char echo) char waitkey(char echo)
@ -78,7 +76,7 @@ static void clihelp()
prtln("r\tread config"); prtln("r\tread config");
prtln("w\twrite config"); prtln("w\twrite config");
prtln("T\ttest relay"); prtln("T\ttest relay");
prtln("0/1\tcontrole frigo"); prtln("+/-\tcontrole frigo");
prtln("h\thexdump config"); prtln("h\thexdump config");
} }
/* --------------------------------------------------------------- */ /* --------------------------------------------------------------- */
@ -148,7 +146,7 @@ do {
prt("$ "); prt("$ ");
ret = readline(line,TLINE); ret = readline(line,TLINE);
#if DEBUG > 1 #if DEBUG > 1
hexdump((unsigned char *)line, ret); prt("readline -> "); hexdump((unsigned char *)line, ret);
#endif #endif
key = *(sptr = strtok(line, " ")); key = *(sptr = strtok(line, " "));

View File

@ -10,6 +10,9 @@
#define RELAIS_FRIGO 42 #define RELAIS_FRIGO 42
#define RELAIS_VENTILO 40 #define RELAIS_VENTILO 40
#define prt(a) Serial.print(a)
#define prtln(a) Serial.println(a)
typedef struct { typedef struct {
unsigned short magic; unsigned short magic;
@ -73,9 +76,7 @@ void updatevalues(short *ptr)
delay(50); delay(50);
} }
} }
for (foo=0; foo<NBVAL; foo++) { ptr[foo] /= NB_PASSE; } for (foo=0; foo<NBVAL; foo++) { ptr[foo] /= NB_PASSE; }
digitalWrite(LED_BUILTIN, LOW); digitalWrite(LED_BUILTIN, LOW);
} }
/* -------------------------------------------------- */ /* -------------------------------------------------- */
@ -125,13 +126,13 @@ void loop() {
controle_frigo(0); controle_frigo(0);
break; break;
default: default:
Serial.print("M bad control code"); prtln("M bad control code");
break; break;
} }
} }
delay(DELAI); delay(DELAI);
/* ETERNEL LOOP HERE */ /* ETERNAL LOOP END HERE */
} }
/* -------------------------------------------------- */ /* -------------------------------------------------- */

View File

@ -27,7 +27,7 @@ prtln(">>> read config");
magic = 0; magic = 0;
EEPROM.get(0, magic); EEPROM.get(0, magic);
prt("magic is "); prtln(magic); prt("M magic is "); prtln(magic);
if (0xfde9 != magic) return -1; if (0xfde9 != magic) return -1;
EEPROM.get(0, *where); EEPROM.get(0, *where);
return -2; return -2;
@ -61,7 +61,7 @@ prtln("");
prt("Delay : "); prtln(what->delai); prt("Delay : "); prtln(what->delai);
prt("Temp mini : "); prtln(what->temp_mini); prt("Temp mini : "); prtln(what->temp_mini);
prt("Temp maxi : "); prtln(what->temp_maxi); prt("Temp maxi : "); prtln(what->temp_maxi);
prt("Control : "); prtln(what->control);
return -1; return -1;
} }
/* --------------------------------------------------------------- */ /* --------------------------------------------------------------- */