bidirectionnal communication, first milestone reached
This commit is contained in:
75
essai.c
75
essai.c
@@ -5,6 +5,7 @@
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
#include <curses.h>
|
||||
#include <time.h>
|
||||
@@ -17,7 +18,7 @@
|
||||
int verbosity;
|
||||
|
||||
/* --------------------------------------------------------------- */
|
||||
int affiche_valeurs(int sfd, int nbloops)
|
||||
int traite_les_messages(int sfd, int nbloops)
|
||||
{
|
||||
int idx, foo, key;
|
||||
char ligne[200];
|
||||
@@ -27,56 +28,75 @@ FILE *fp;
|
||||
time_t temps,
|
||||
old_temps = (time_t)0L;
|
||||
|
||||
|
||||
/* XXX XXX XXX XXX XXX XXX XXX XXX XXX XXX */
|
||||
fp = fopen("serial/foo.dat", "a");
|
||||
if (NULL==fp) {
|
||||
fprintf(stderr, "***** error fopen datafile *****\n");
|
||||
fprintf(stderr, "*** error fopen datafile ***\n");
|
||||
return -1;
|
||||
}
|
||||
/* XXX XXX XXX XXX XXX XXX XXX XXX XXX XXX */
|
||||
|
||||
for (idx=0; idx<nbloops; idx++) {
|
||||
|
||||
if (kbhit()) {
|
||||
message("!!! KEY !!!");
|
||||
sleep(2);
|
||||
static char valid_k[] = "+-";
|
||||
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);
|
||||
|
||||
if (*ligne == 'M') {
|
||||
message(ligne);
|
||||
}
|
||||
|
||||
#if DEBUG_LEVEL
|
||||
if (foo) fprintf(stderr, "get values -> %d\n", foo);
|
||||
#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 (foo) fprintf(stderr, "parse I val -> %d\n", foo);
|
||||
if (foo) fprintf(stderr, "parse I val -> %d\n", foo);
|
||||
#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++) {
|
||||
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 loging all that temps values */
|
||||
|
||||
/* here we are trying to log all that temps values */
|
||||
if (NULL!=fp) {
|
||||
temps = time(NULL);
|
||||
if ((temps-old_temps) > 50) {
|
||||
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);
|
||||
old_temps = temps;
|
||||
}
|
||||
break; /* case 'T' */
|
||||
}
|
||||
|
||||
fflush(stderr); /* really WTF? here */
|
||||
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
@@ -102,7 +122,7 @@ exit(0);
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int opt, foo;
|
||||
int opt;
|
||||
int serial_in;
|
||||
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);
|
||||
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);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
sleep(1);
|
||||
|
||||
initscr();
|
||||
nonl(); cbreak(); noecho();
|
||||
keypad(stdscr, TRUE); /* acces aux touches 'curseur' */
|
||||
fond_ecran(" Demonstrator ");
|
||||
|
||||
affiche_valeurs(serial_in, 50000000);
|
||||
traite_les_messages(serial_in, 50000000);
|
||||
|
||||
/*
|
||||
* plop, on a fini, il faut restaurer la console
|
||||
|
||||
Reference in New Issue
Block a user