first version of ncurses waterfall

This commit is contained in:
tth 2019-01-10 14:07:56 +01:00
parent 6fe2c54cfb
commit ba9b0d993f
5 changed files with 98 additions and 33 deletions

View File

@ -1,21 +1,23 @@
# --------------- ***
COPT = -Wall -g -DTRACE=0
# --------------- *** # --------------- ***
ecran.o: ecran.c Makefile ecran.h ecran.o: ecran.c Makefile ecran.h
gcc -Wall -c $< gcc $(COPT) -c $<
7segments.o: 7segments.c Makefile 7segments.o: 7segments.c Makefile
gcc -Wall -c $< gcc $(COPT) -c $<
waterfall.o: waterfall.c Makefile waterfall.o: waterfall.c Makefile
gcc -Wall -c $< gcc $(COPT) -c $<
# --------------- *** # --------------- ***
OBJ = ecran.o waterfall.o 7segments.o OBJ = ecran.o waterfall.o 7segments.o
t: t.c Makefile $(OBJ) ecran.h t: t.c Makefile $(OBJ) ecran.h
gcc -Wall $< $(OBJ) -lncurses -o $@ gcc $(COPT) $< $(OBJ) -lncurses -o $@
# --------------- *** # --------------- ***

View File

@ -1,6 +1,6 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <strings.h> #include <string.h>
#include <getopt.h> #include <getopt.h>
#include <ncurses.h> #include <ncurses.h>
@ -21,16 +21,21 @@ standend();
/* refresh(); */ /* refresh(); */
} }
/* ---------------------------------------------------------------- */ /* ---------------------------------------------------------------- */
int fond_ecran(void) int fond_ecran(char *title)
{ {
char *tp;
#if TRACE #if TRACE
int foo; int foo;
char buffer[200]; char buffer[200];
#endif #endif
tp = " DD2 MONITORING by tTh 2019 ";
if (NULL != title) tp = title;
barre_inverse('+', 0); barre_inverse('+', 0);
standout(); standout();
mvaddstr(0, 2, " DD2 MONITORING by tTh 2019 "); mvaddstr(0, 2, tp);
#if TRACE #if TRACE
sprintf(buffer, " ecran %dx%d ", COLS, LINES); sprintf(buffer, " ecran %dx%d ", COLS, LINES);
foo = strlen(buffer); foo = strlen(buffer);
@ -39,12 +44,6 @@ mvaddstr(0, COLS-2-foo, buffer);
standend(); standend();
refresh(); refresh();
#if TRACE
fprintf(stderr, "HAUT %3d\n", HAUT);
fprintf(stderr, "HAUT_1 %3d BARRE_1 %3d\n", HAUT_1, BARRE_1);
fprintf(stderr, "HAUT_2 %3d BARRE_2 %3d\n", HAUT_2, BARRE_2);
#endif
return 0; return 0;
} }
/* ---------------------------------------------------------------- */ /* ---------------------------------------------------------------- */

View File

@ -1,6 +1,8 @@
int fond_ecran(void); int fond_ecran(char *titre);
WINDOW * open_waterfall(char *title, int flags); WINDOW * open_waterfall(char *title, int flags);
int plot_waterfall(WINDOW *wf, int flags, float values[4]);
int close_waterfall(WINDOW *wf, int notused);

View File

@ -12,20 +12,36 @@ int verbosity;
/* ---------------------------------------------------------------- */ /* ---------------------------------------------------------------- */
void demo(int nbl, int k) void demo(int nbl, int k)
{ {
int loop; int loop, foo;
char line[100]; char line[100];
WINDOW *water; WINDOW *water;
static float rvals[4];
water = open_waterfall("premier essai", 0); water = open_waterfall("premier essai", 0);
for (loop=0; loop<nbl; loop++) { for (loop=0; loop<nbl; loop++) {
sprintf(line, "%04X %04X", loop, rand()&0xffff); sprintf(line, "%04X %04X", loop, rand()&0xffff);
mvaddstr(5, 5, line); mvwaddstr(stdscr, LINES-1, 1, line);
wrefresh(stdscr);
sleep(1); for (foo=0; foo<4; foo++) {
refresh(); if (rand()%100<42) {
rvals[foo] += 3.8*(foo + 1);
} }
if (rvals[foo] > 1023.0) {
rvals[foo] = (float)(rand() % 15);
}
}
plot_waterfall(water, 1, rvals);
if (rand()%10 < 1) sleep(1);
}
close_waterfall(water, 0);
} }
/* ---------------------------------------------------------------- */ /* ---------------------------------------------------------------- */
static void finish(int signal) static void finish(int signal)
@ -59,9 +75,9 @@ nonl(); cbreak(); noecho();
keypad(stdscr, TRUE); /* acces aux touches 'curseur' */ keypad(stdscr, TRUE); /* acces aux touches 'curseur' */
fond_ecran(); fond_ecran(" Demonstrator ");
demo(1024, 0); demo(9000, 0);
/* /*
* plop, on a fini, restaurer la console * plop, on a fini, restaurer la console

View File

@ -22,27 +22,73 @@ WINDOW *open_waterfall(char *title, int flags)
WINDOW *win; WINDOW *win;
int l, c, w, h; int l, c, w, h;
l = 2; c = 1; l = 1; c = 1;
w = COLS - 2; h = LINES - 3; w = COLS - 2; h = LINES - 3;
win = newwin(h, w, l, c); win = newwin(h, w, l, c);
scrollok(win, 1);
waddstr(win, title); waddch(win, '\n');
wrefresh(win);
return win; return win;
} }
/* ---------------------------------------------------------------- */ /* ---------------------------------------------------------------- */
int plot_waterfall(WINDOW *wf, float values[4]) int plot_waterfall(WINDOW *wf, int mode, float values[4])
{ {
#define TL 1000 #define TL 1000
int foo; int foo, idx;
char tag, ligne[TL+1]; char tag, ligne[TL+1];
float coef_w;
static long iter;
for(foo=0; foo<TL; foo++) *(ligne+foo) = '-'; if (0 == (iter%10)) {
memset(ligne, '-', TL);
}
else {
memset(ligne, ' ', TL);
}
for (foo=0; foo<500; foo+=10) {
ligne[foo] = '|';
}
ligne[COLS-4] = '\0';
iter++;
coef_w = (float)(COLS-2) / 1024.0;
#if TRACE
sprintf(ligne, "coef_w = %f\n", coef_w);
waddstr(wf, ligne); waddch(wf, '\n');
#endif
switch (mode) {
case 0: default:
sprintf(ligne, "%11.3f %11.3f %11.3f %11.3f",
values[0], values[1], values[2], values[3]);
#if TRACE
fprintf(stderr, "%s [%s]\n", __func__, ligne);
#endif
break;
case 1:
for(foo=0; foo<4; foo++) { for(foo=0; foo<4; foo++) {
tag = "ATOX"[foo]; tag = "ATOX"[foo];
idx = (int)(values[foo]*coef_w);
ligne[idx] = tag;
}
ligne[COLS-4] = '\0';
break;
} }
/* poke the text in the curses window */
// scroll(wf);
waddstr(wf, ligne); waddch(wf, '\n');
wrefresh(wf);
return -1; return -1;
} }
/* ---------------------------------------------------------------- */ /* ---------------------------------------------------------------- */