Monitoring du Phytotron
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

essai.c 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * essai.c
  3. */
  4. #include <stdio.h>
  5. #include <unistd.h>
  6. #include <stdlib.h>
  7. #include <ctype.h>
  8. #include <string.h>
  9. #include <ncurses.h>
  10. #include <time.h>
  11. #include "core/utils.h"
  12. #include "core/sysmetrics.h"
  13. #include "serial/serial.h"
  14. #include "viz/curses/ecran.h"
  15. int run_the_terminal(int fd_serial, char *title, WINDOW *win); /* XXX */
  16. int verbosity;
  17. /* --------------------------------------------------------------- */
  18. int traite_les_messages(int sfd, int nbloops, int notused)
  19. {
  20. int foo, key;
  21. int loop_forever;
  22. char ligne[200];
  23. int Idatas[4];
  24. float Fdatas[4];
  25. FILE *fp;
  26. time_t temps,
  27. old_temps = (time_t)0L;
  28. fp = fopen("serial/foo.dat", "a");
  29. if (NULL==fp) {
  30. fprintf(stderr, "*** error fopen datafile ***\n");
  31. return -1;
  32. }
  33. if (0==nbloops) {
  34. fprintf(stderr, "looping forever...\n");
  35. loop_forever = 1;
  36. }
  37. else {
  38. fprintf(stderr, "looping %d rounds\n", nbloops);
  39. loop_forever = 0;
  40. }
  41. do {
  42. if (kbhit()) {
  43. static char valid_k[] = "+-01";
  44. key = getch();
  45. /* if it is a valid key, send it to the Arduino */
  46. if (NULL!=strchr(valid_k, key)) {
  47. foo = putbyte(sfd, key);
  48. fprintf(stderr, "put byte %02X -> %d\n",
  49. key, foo);
  50. }
  51. else if (0x14==key) { /* request for terminal */
  52. foo = run_the_terminal(sfd, " terminal ", 0);
  53. // putbyte(sfd, 'x'); putbyte(sfd, '\r');
  54. sprintf(ligne, "retour terminal = %d", foo);
  55. aff_message(ligne); sleep(1);
  56. }
  57. else {
  58. sprintf(ligne, "Key 0x%02X invalid ", key);
  59. fprintf(stderr, "%s\n", ligne);
  60. aff_message(ligne); sleep(1); aff_message("");
  61. }
  62. }
  63. /* here we are waiting for a frame from the
  64. Arduino. In the future, we may have an
  65. finely tunned event-synth system here */
  66. foo = getline_to(sfd, ligne, 100, 0);
  67. #if DEBUG_LEVEL
  68. if (foo) fprintf(stderr, "get values -> %d\n", foo);
  69. #endif
  70. switch (*ligne) {
  71. case 'M':
  72. case 'L':
  73. aff_message(ligne);
  74. break;
  75. case 'T':
  76. foo = parse4_Ivalues(ligne, 'T', Idatas);
  77. #if DEBUG_LEVEL
  78. if (foo) fprintf(stderr, "parse I val -> %d\n", foo);
  79. #endif
  80. values2temperature(Idatas, Fdatas);
  81. for (foo=0; foo<3; foo++) {
  82. sprintf(ligne, "%4d", Idatas[foo]);
  83. minidigit_affstr(stdscr, 12+(12*foo), 8, ligne);
  84. aff7segs_float(stdscr, 8+(12*foo), 55, Fdatas[foo]);
  85. }
  86. /* here we are loging all that temps values */
  87. temps = time(NULL);
  88. if ((temps-old_temps) > 50) {
  89. fprintf(fp, "%ld %f %f %f %f\n", temps,
  90. Fdatas[0], Fdatas[1],
  91. Fdatas[2], Fdatas[3]);
  92. fflush(fp);
  93. old_temps = temps;
  94. }
  95. break; /* case 'T' */
  96. }
  97. fflush(stderr); /* really WTF? here */
  98. if ( loop_forever ) nbloops = 1;
  99. } while (0 != nbloops);
  100. fclose(fp);
  101. return 0;
  102. }
  103. /* --------------------------------------------------------------- */
  104. static void finish(void)
  105. {
  106. endwin();
  107. fprintf(stderr, "end of pid %d\n", getpid());
  108. exit(0);
  109. }
  110. /* --------------------------------------------------------------- */
  111. void help(int k)
  112. {
  113. puts("options : ");
  114. puts("\t-d\tserial device to read.");
  115. puts("\t-K\tset the K parameter.");
  116. puts("\t-n NN\tnumber of loops, 0 is infinity");
  117. puts("\t-v\tincrease verbosity.");
  118. exit(0);
  119. }
  120. /* --------------------------------------------------------------- */
  121. int main(int argc, char *argv[])
  122. {
  123. int opt;
  124. int serial_in;
  125. char *device = "/dev/ttyACM0";
  126. int K = 0;
  127. char ligne[100];
  128. int nbreloops;
  129. printf("\n**** %s **** compiled the %s at %s ***\n",
  130. argv[0], __DATE__, __TIME__);
  131. while ((opt = getopt(argc, argv, "d:hn:v")) != -1) {
  132. switch (opt) {
  133. case 'd': device = optarg; break;
  134. case 'h': help(0); break;
  135. case 'K': K = atoi(optarg); break;
  136. case 'n': nbreloops=atoi(optarg); break;
  137. case 'v': verbosity++; break;
  138. default: break;
  139. }
  140. }
  141. serial_in = prepare_UART(device, 9600);
  142. if (serial_in < 0) {
  143. fprintf(stderr, "\n%s : open device : error %d on %s\n",
  144. argv[0], serial_in, device);
  145. exit(1);
  146. }
  147. sleep(1);
  148. initscr();
  149. nonl(); cbreak(); noecho();
  150. keypad(stdscr, TRUE); /* acces aux touches 'curseur' */
  151. atexit(finish);
  152. sprintf(ligne, " Demonstrator pid:%d %s ", getpid(), device);
  153. fond_ecran(ligne);
  154. traite_les_messages(serial_in, nbreloops, K);
  155. /*
  156. * plop, on a fini, il faut restaurer la console
  157. */
  158. finish();
  159. return 0;
  160. }
  161. /* --------------------------------------------------------------- */