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 3.8KB

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