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.

cli.ino 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Phytotron
  3. * configuration par la console
  4. */
  5. /* -------------------------------------------------- */
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #define DEBUG 0
  9. /* -------------------------------------------------- */
  10. short validate_config(Global *param)
  11. {
  12. short delta;
  13. if (param->magic != 0xfde9) {
  14. prtln("bad magic");
  15. return 1;
  16. }
  17. if (param->delai < 100) {
  18. prt(param->delai); prtln("delay too short");
  19. return 2;
  20. }
  21. delta = param->temp_maxi - param->temp_mini;
  22. if (delta < 20) {
  23. prt(delta); prtln(" bad delta");
  24. return 3;
  25. }
  26. return 0;
  27. }
  28. /* -------------------------------------------------- */
  29. char waitkey(char echo)
  30. {
  31. char key;
  32. while (!Serial.available());
  33. key = Serial.read();
  34. if (echo) Serial.write(key);
  35. return key;
  36. }
  37. /* -------------------------------------------------- */
  38. short readline(char *where, short sz)
  39. {
  40. char key;
  41. short count = 0;
  42. for(;;) {
  43. /* check for end of buffer */
  44. if (count==sz) {
  45. where[count] = '\0';
  46. return -1;
  47. }
  48. key = waitkey(1);
  49. #if DEBUG > 1
  50. prtln((int)key);
  51. #endif
  52. if ((0x08==key) && count) {
  53. /* we have got a backspace */
  54. count--;
  55. where[count] = '\0';
  56. #if DEBUG
  57. prt("< "); prtln(count);
  58. #endif
  59. continue;
  60. }
  61. if ('\r' == key) {
  62. prt('\n');
  63. where[count] = '\0';
  64. return count;
  65. }
  66. where[count++] = key;
  67. }
  68. /* NOTREACHED */
  69. }
  70. /* --------------------------------------------------------------- */
  71. void hexdump(unsigned char *ptr, short nbre)
  72. {
  73. short foo;
  74. for (foo=0; foo<nbre; foo++) {
  75. Serial.print(ptr[foo], HEX);
  76. Serial.print(' ');
  77. }
  78. prtln("");
  79. }
  80. /* --------------------------------------------------------------- */
  81. static void clihelp()
  82. {
  83. prtln("x\texit cli");
  84. prtln("I\tinit storage"); /* dangerous !!! */
  85. prtln("s d N\tset delay");
  86. prtln("s l N\tset lo temp");
  87. prtln("s h N\tset hi temp");
  88. prtln("d\tdisplay config");
  89. prtln("r\tread config");
  90. prtln("v\tvalidate config");
  91. prtln("w\twrite config");
  92. prtln("T\ttest relay");
  93. prtln("+/-\tcontrole frigo");
  94. prtln("h\thexdump config");
  95. }
  96. /* --------------------------------------------------------------- */
  97. void setvalue(char *cmdline, Global *param)
  98. {
  99. char key, *sptr, *vptr;
  100. short value;
  101. // hexdump((unsigned char *)cmdline, 10);
  102. key = *(sptr = strtok(NULL, " "));
  103. // hexdump((unsigned char *)cmdline, 10);
  104. vptr = strtok(NULL, " ");
  105. value = atoi(vptr);
  106. prt(key); prt(' '); prtln(value);
  107. switch(key) {
  108. case 'd': param->delai = value; break;
  109. case 'h': param->temp_maxi = value; break;
  110. case 'l': param->temp_mini = value; break;
  111. default: prtln("GNI ?");
  112. }
  113. }
  114. /* --------------------------------------------------------------- */
  115. /*
  116. * use this function with caution, it can burn your system !
  117. */
  118. void test_relais(short nb) {
  119. short foo;
  120. prtln("test du relais frigo");
  121. for (foo=0; foo<nb; foo++) {
  122. controle_frigo(1);
  123. delay(parametres.delai);
  124. controle_frigo(1);
  125. delay(parametres.delai);
  126. }
  127. }
  128. /* --------------------------------------------------------------- */
  129. void controle_frigo(char on_off)
  130. {
  131. if (on_off) {
  132. prtln("M frigo ON");
  133. digitalWrite(RELAIS_FRIGO, HIGH);
  134. }
  135. else {
  136. prtln("M frigo OFF");
  137. digitalWrite(RELAIS_FRIGO, LOW);
  138. }
  139. }
  140. /* --------------------------------------------------------------- */
  141. void controle_ventilo(char on_off)
  142. {
  143. if (on_off) {
  144. prtln("M ventilo ON");
  145. digitalWrite(RELAIS_VENTILO, HIGH);
  146. }
  147. else {
  148. prtln("M ventilo OFF");
  149. digitalWrite(RELAIS_VENTILO, LOW);
  150. }
  151. }
  152. /* --------------------------------------------------------------- */
  153. void phytocli(void)
  154. {
  155. char flag_exit = 0;
  156. char ret, key;
  157. #define TLINE 40
  158. char line[TLINE+1], *sptr;
  159. static char separators[] = " \t";
  160. prtln("entering cli, '?' to help, 'x' to quit.");
  161. do {
  162. prt("$ ");
  163. ret = readline(line,TLINE);
  164. #if DEBUG > 1
  165. prt("readline -> "); hexdump((unsigned char *)line, ret);
  166. #endif
  167. key = *(sptr = strtok(line, " "));
  168. switch(key) {
  169. case 'x': flag_exit=1; break;
  170. case '?': clihelp(); break;
  171. case 'I': init_storage(&parametres); break;
  172. case 'd': display_config(&parametres); break;
  173. case 'r': read_config(0, &parametres); break;
  174. case 'w': write_config(0, &parametres); break;
  175. case 'h': hexdump((unsigned char *)&parametres,
  176. sizeof(Global)); break;
  177. case 's': setvalue(line, &parametres); break;
  178. case 'v': validate_config(&parametres); break;
  179. case '+': controle_frigo(1); break;
  180. case '-': controle_frigo(0); break;
  181. case '1': controle_ventilo(1); break;
  182. case '0': controle_ventilo(0); break;
  183. case 'T': test_relais(5); break;
  184. default: prtln("gni ?"); break;
  185. }
  186. } while (!flag_exit);
  187. }
  188. /* --------------------------------------------------------------- */