2018-12-08 23:02:24 +11:00
|
|
|
/*
|
|
|
|
* essai.c
|
|
|
|
*/
|
|
|
|
|
2019-02-07 03:22:32 +11:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
2019-05-15 00:46:49 +11:00
|
|
|
#include <ctype.h>
|
2019-02-07 03:22:32 +11:00
|
|
|
#include <string.h>
|
2019-05-16 21:28:47 +11:00
|
|
|
#include <ncurses.h>
|
2019-02-07 03:22:32 +11:00
|
|
|
#include <time.h>
|
2018-12-08 23:02:24 +11:00
|
|
|
|
2019-02-07 03:22:32 +11:00
|
|
|
#include "core/utils.h"
|
|
|
|
#include "core/sysmetrics.h"
|
2019-03-28 22:04:40 +11:00
|
|
|
#include "serial/serial.h"
|
2019-02-07 03:22:32 +11:00
|
|
|
#include "viz/curses/ecran.h"
|
2018-12-08 23:02:24 +11:00
|
|
|
|
2019-01-18 04:37:30 +11:00
|
|
|
|
2019-05-21 03:20:18 +11:00
|
|
|
int run_the_terminal(int fd_serial, char *title, WINDOW *win); /* XXX */
|
|
|
|
|
|
|
|
int verbosity;
|
2019-05-16 21:28:47 +11:00
|
|
|
|
2019-01-18 04:37:30 +11:00
|
|
|
/* --------------------------------------------------------------- */
|
2019-05-25 01:59:29 +11:00
|
|
|
int traite_les_messages(int sfd, int nbloops, int notused)
|
2019-01-31 05:14:40 +11:00
|
|
|
{
|
2019-05-25 01:59:29 +11:00
|
|
|
int foo, key;
|
|
|
|
int loop_forever;
|
2019-04-04 01:25:38 +11:00
|
|
|
char ligne[200];
|
|
|
|
int Idatas[4];
|
|
|
|
float Fdatas[4];
|
2019-04-04 04:53:15 +11:00
|
|
|
FILE *fp;
|
2019-04-11 02:13:54 +11:00
|
|
|
time_t temps,
|
|
|
|
old_temps = (time_t)0L;
|
2019-04-04 04:53:15 +11:00
|
|
|
|
|
|
|
fp = fopen("serial/foo.dat", "a");
|
|
|
|
if (NULL==fp) {
|
2019-05-15 00:46:49 +11:00
|
|
|
fprintf(stderr, "*** error fopen datafile ***\n");
|
2019-04-04 04:53:15 +11:00
|
|
|
return -1;
|
|
|
|
}
|
2019-01-31 05:14:40 +11:00
|
|
|
|
2019-05-25 01:59:29 +11:00
|
|
|
if (0==nbloops) {
|
|
|
|
fprintf(stderr, "looping forever...\n");
|
|
|
|
loop_forever = 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fprintf(stderr, "looping %d rounds\n", nbloops);
|
|
|
|
loop_forever = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
2019-01-31 05:14:40 +11:00
|
|
|
|
2019-05-14 02:48:58 +11:00
|
|
|
if (kbhit()) {
|
2019-05-21 03:20:18 +11:00
|
|
|
static char valid_k[] = "+-01";
|
2019-05-15 00:46:49 +11:00
|
|
|
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);
|
|
|
|
}
|
2019-05-21 03:20:18 +11:00
|
|
|
else if (0x14==key) { /* request for terminal */
|
|
|
|
|
|
|
|
foo = run_the_terminal(sfd, " terminal ", 0);
|
2019-05-23 00:48:21 +11:00
|
|
|
// putbyte(sfd, 'x'); putbyte(sfd, '\r');
|
2019-05-21 03:20:18 +11:00
|
|
|
sprintf(ligne, "retour terminal = %d", foo);
|
|
|
|
aff_message(ligne); sleep(1);
|
|
|
|
}
|
2019-05-15 00:46:49 +11:00
|
|
|
else {
|
|
|
|
sprintf(ligne, "Key 0x%02X invalid ", key);
|
|
|
|
fprintf(stderr, "%s\n", ligne);
|
|
|
|
aff_message(ligne); sleep(1); aff_message("");
|
|
|
|
}
|
2019-05-14 02:48:58 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-15 00:46:49 +11:00
|
|
|
/* 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);
|
2019-03-28 22:04:40 +11:00
|
|
|
#if DEBUG_LEVEL
|
|
|
|
if (foo) fprintf(stderr, "get values -> %d\n", foo);
|
|
|
|
#endif
|
2019-01-31 05:14:40 +11:00
|
|
|
|
2019-05-15 00:46:49 +11:00
|
|
|
switch (*ligne) {
|
|
|
|
|
|
|
|
case 'M':
|
|
|
|
case 'L':
|
|
|
|
aff_message(ligne);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'T':
|
|
|
|
foo = parse4_Ivalues(ligne, 'T', Idatas);
|
2019-03-28 22:04:40 +11:00
|
|
|
#if DEBUG_LEVEL
|
2019-05-15 00:46:49 +11:00
|
|
|
if (foo) fprintf(stderr, "parse I val -> %d\n", foo);
|
2019-03-28 22:04:40 +11:00
|
|
|
#endif
|
2019-05-15 00:46:49 +11:00
|
|
|
values2temperature(Idatas, Fdatas);
|
2019-03-28 22:04:40 +11:00
|
|
|
|
2019-05-15 00:46:49 +11:00
|
|
|
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]);
|
|
|
|
}
|
2019-03-28 22:04:40 +11:00
|
|
|
|
2019-05-15 00:46:49 +11:00
|
|
|
/* here we are loging all that temps values */
|
2019-04-04 04:53:15 +11:00
|
|
|
|
|
|
|
temps = time(NULL);
|
2019-04-14 20:33:52 +11:00
|
|
|
if ((temps-old_temps) > 50) {
|
2019-04-11 02:13:54 +11:00
|
|
|
fprintf(fp, "%ld %f %f %f %f\n", temps,
|
2019-05-15 00:46:49 +11:00
|
|
|
Fdatas[0], Fdatas[1],
|
|
|
|
Fdatas[2], Fdatas[3]);
|
2019-04-11 02:13:54 +11:00
|
|
|
fflush(fp);
|
|
|
|
old_temps = temps;
|
|
|
|
}
|
2019-05-15 00:46:49 +11:00
|
|
|
break; /* case 'T' */
|
2019-04-04 04:53:15 +11:00
|
|
|
}
|
|
|
|
|
2019-05-15 00:46:49 +11:00
|
|
|
fflush(stderr); /* really WTF? here */
|
|
|
|
|
2019-05-25 01:59:29 +11:00
|
|
|
if ( loop_forever ) nbloops = 1;
|
|
|
|
|
|
|
|
} while (0 != nbloops);
|
2019-04-11 02:13:54 +11:00
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
|
2019-01-31 05:14:40 +11:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* --------------------------------------------------------------- */
|
|
|
|
static void finish(int signal)
|
|
|
|
{
|
2019-02-02 13:52:30 +11:00
|
|
|
endwin();
|
|
|
|
fprintf(stderr, "end of pid %d\n", getpid());
|
|
|
|
exit(0);
|
2019-01-31 05:14:40 +11:00
|
|
|
}
|
2018-12-08 23:02:24 +11:00
|
|
|
/* --------------------------------------------------------------- */
|
2019-03-28 22:04:40 +11:00
|
|
|
void help(int k)
|
|
|
|
{
|
|
|
|
puts("options : ");
|
|
|
|
puts("\t-d\tserial device to read.");
|
2019-05-16 21:28:47 +11:00
|
|
|
puts("\t-K\tset the K parameter.");
|
2019-05-25 01:59:29 +11:00
|
|
|
puts("\t-n NN\tnumber of loops, 0 is infinity");
|
2019-03-28 22:04:40 +11:00
|
|
|
puts("\t-v\tincrease verbosity.");
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
/* --------------------------------------------------------------- */
|
2018-12-08 23:02:24 +11:00
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2019-05-15 00:46:49 +11:00
|
|
|
int opt;
|
2019-03-28 22:04:40 +11:00
|
|
|
int serial_in;
|
|
|
|
char *device = "/dev/ttyACM0";
|
2019-05-16 21:28:47 +11:00
|
|
|
int K = 0;
|
2019-05-16 02:22:44 +11:00
|
|
|
char ligne[100];
|
2019-05-25 01:59:29 +11:00
|
|
|
int nbreloops;
|
|
|
|
|
|
|
|
printf("\n**** %s **** compiled the %s at %s ***\n",
|
|
|
|
argv[0], __DATE__, __TIME__);
|
|
|
|
|
2018-12-08 23:02:24 +11:00
|
|
|
|
2019-05-25 01:59:29 +11:00
|
|
|
while ((opt = getopt(argc, argv, "d:hn:v")) != -1) {
|
2018-12-08 23:02:24 +11:00
|
|
|
switch (opt) {
|
2019-03-28 22:04:40 +11:00
|
|
|
case 'd': device = optarg; break;
|
|
|
|
case 'h': help(0); break;
|
2019-05-16 21:28:47 +11:00
|
|
|
case 'K': K = atoi(optarg); break;
|
2019-05-25 01:59:29 +11:00
|
|
|
case 'n': nbreloops=atoi(optarg); break;
|
2018-12-08 23:02:24 +11:00
|
|
|
case 'v': verbosity++; break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-28 22:04:40 +11:00
|
|
|
serial_in = prepare_UART(device, 9600);
|
|
|
|
if (serial_in < 0) {
|
2019-05-15 00:46:49 +11:00
|
|
|
fprintf(stderr, "\n%s : open device : error %d on %s\n",
|
2019-03-28 22:04:40 +11:00
|
|
|
argv[0], serial_in, device);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2019-05-15 00:46:49 +11:00
|
|
|
sleep(1);
|
2019-03-28 22:04:40 +11:00
|
|
|
|
2019-01-31 05:14:40 +11:00
|
|
|
initscr();
|
|
|
|
nonl(); cbreak(); noecho();
|
|
|
|
keypad(stdscr, TRUE); /* acces aux touches 'curseur' */
|
2019-05-21 06:31:28 +11:00
|
|
|
sprintf(ligne, " Demonstrator pid:%d %s ", getpid(), device);
|
2019-05-16 02:22:44 +11:00
|
|
|
fond_ecran(ligne);
|
2019-01-31 05:14:40 +11:00
|
|
|
|
2019-05-25 01:59:29 +11:00
|
|
|
traite_les_messages(serial_in, nbreloops, K);
|
2019-01-31 05:14:40 +11:00
|
|
|
|
|
|
|
/*
|
|
|
|
* plop, on a fini, il faut restaurer la console
|
|
|
|
*/
|
|
|
|
finish(0);
|
|
|
|
|
2018-12-08 23:02:24 +11:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------- */
|