2019-04-29 20:40:22 +11:00
|
|
|
/*
|
|
|
|
* Phytotron
|
|
|
|
* configuration par la console
|
|
|
|
*/
|
|
|
|
/* -------------------------------------------------- */
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2019-05-13 23:26:05 +11:00
|
|
|
#define DEBUG 0
|
2019-04-29 20:40:22 +11:00
|
|
|
/* -------------------------------------------------- */
|
2019-05-17 03:17:22 +11:00
|
|
|
short validate_config(Global *param)
|
|
|
|
{
|
|
|
|
short delta;
|
|
|
|
|
|
|
|
if (param->magic != 0xfde9) {
|
|
|
|
prtln("bad magic");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (param->delai < 100) {
|
2019-05-20 23:49:31 +11:00
|
|
|
prt(param->delai); prtln("delay too short");
|
2019-05-17 03:17:22 +11:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
delta = param->temp_maxi - param->temp_mini;
|
|
|
|
if (delta < 20) {
|
|
|
|
prt(delta); prtln(" bad delta");
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2019-04-29 20:40:22 +11:00
|
|
|
/* -------------------------------------------------- */
|
|
|
|
char waitkey(char echo)
|
|
|
|
{
|
|
|
|
char key;
|
|
|
|
while (!Serial.available());
|
|
|
|
key = Serial.read();
|
|
|
|
if (echo) Serial.write(key);
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
/* -------------------------------------------------- */
|
|
|
|
short readline(char *where, short sz)
|
|
|
|
{
|
|
|
|
char key;
|
|
|
|
short count = 0;
|
|
|
|
for(;;) {
|
|
|
|
/* check for end of buffer */
|
|
|
|
if (count==sz) {
|
|
|
|
where[count] = '\0';
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
key = waitkey(1);
|
|
|
|
#if DEBUG > 1
|
|
|
|
prtln((int)key);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if ((0x08==key) && count) {
|
|
|
|
/* we have got a backspace */
|
|
|
|
count--;
|
|
|
|
where[count] = '\0';
|
|
|
|
#if DEBUG
|
|
|
|
prt("< "); prtln(count);
|
|
|
|
#endif
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ('\r' == key) {
|
|
|
|
prt('\n');
|
|
|
|
where[count] = '\0';
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
where[count++] = key;
|
|
|
|
}
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
/* --------------------------------------------------------------- */
|
|
|
|
void hexdump(unsigned char *ptr, short nbre)
|
|
|
|
{
|
|
|
|
short foo;
|
|
|
|
for (foo=0; foo<nbre; foo++) {
|
|
|
|
Serial.print(ptr[foo], HEX);
|
|
|
|
Serial.print(' ');
|
|
|
|
}
|
|
|
|
prtln("");
|
|
|
|
}
|
|
|
|
/* --------------------------------------------------------------- */
|
|
|
|
static void clihelp()
|
|
|
|
{
|
2019-04-30 02:46:14 +11:00
|
|
|
prtln("x\texit cli");
|
2019-05-13 23:26:05 +11:00
|
|
|
prtln("I\tinit storage"); /* dangerous !!! */
|
|
|
|
prtln("s d N\tset delay");
|
|
|
|
prtln("s l N\tset lo temp");
|
|
|
|
prtln("s h N\tset hi temp");
|
2019-05-01 01:49:21 +11:00
|
|
|
prtln("d\tdisplay config");
|
|
|
|
prtln("r\tread config");
|
2019-05-17 03:17:22 +11:00
|
|
|
prtln("v\tvalidate config");
|
2019-05-01 01:49:21 +11:00
|
|
|
prtln("w\twrite config");
|
2019-05-14 02:49:21 +11:00
|
|
|
prtln("T\ttest relay");
|
2019-05-15 00:46:49 +11:00
|
|
|
prtln("+/-\tcontrole frigo");
|
2019-05-13 23:26:05 +11:00
|
|
|
prtln("h\thexdump config");
|
2019-04-29 20:40:22 +11:00
|
|
|
}
|
|
|
|
/* --------------------------------------------------------------- */
|
2019-05-14 02:49:21 +11:00
|
|
|
void setvalue(char *cmdline, Global *param)
|
2019-05-13 23:26:05 +11:00
|
|
|
{
|
2019-05-14 02:49:21 +11:00
|
|
|
char key, *sptr, *vptr;
|
|
|
|
short value;
|
2019-05-13 23:26:05 +11:00
|
|
|
|
2019-05-14 02:49:21 +11:00
|
|
|
// hexdump((unsigned char *)cmdline, 10);
|
|
|
|
key = *(sptr = strtok(NULL, " "));
|
|
|
|
// hexdump((unsigned char *)cmdline, 10);
|
|
|
|
vptr = strtok(NULL, " ");
|
|
|
|
value = atoi(vptr);
|
|
|
|
|
|
|
|
prt(key); prt(' '); prtln(value);
|
|
|
|
|
|
|
|
switch(key) {
|
|
|
|
case 'd': param->delai = value; break;
|
|
|
|
case 'h': param->temp_maxi = value; break;
|
|
|
|
case 'l': param->temp_mini = value; break;
|
|
|
|
|
2019-05-21 22:52:21 +11:00
|
|
|
default: prtln("GNI ?"); break;
|
2019-05-14 02:49:21 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
/* --------------------------------------------------------------- */
|
2019-05-20 23:49:31 +11:00
|
|
|
/*
|
|
|
|
* use this function with caution, it can burn your system !
|
|
|
|
*/
|
2019-05-14 02:49:21 +11:00
|
|
|
void test_relais(short nb) {
|
|
|
|
short foo;
|
|
|
|
prtln("test du relais frigo");
|
|
|
|
|
|
|
|
for (foo=0; foo<nb; foo++) {
|
2019-05-20 23:49:31 +11:00
|
|
|
controle_frigo(1);
|
2019-05-14 02:49:21 +11:00
|
|
|
delay(parametres.delai);
|
|
|
|
|
2019-05-20 23:49:31 +11:00
|
|
|
controle_frigo(1);
|
2019-05-14 02:49:21 +11:00
|
|
|
delay(parametres.delai);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* --------------------------------------------------------------- */
|
|
|
|
void controle_frigo(char on_off)
|
|
|
|
{
|
|
|
|
if (on_off) {
|
|
|
|
prtln("M frigo ON");
|
|
|
|
digitalWrite(RELAIS_FRIGO, HIGH);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
prtln("M frigo OFF");
|
|
|
|
digitalWrite(RELAIS_FRIGO, LOW);
|
|
|
|
}
|
2019-05-16 02:22:44 +11:00
|
|
|
}
|
|
|
|
/* --------------------------------------------------------------- */
|
|
|
|
void controle_ventilo(char on_off)
|
|
|
|
{
|
|
|
|
if (on_off) {
|
|
|
|
prtln("M ventilo ON");
|
|
|
|
digitalWrite(RELAIS_VENTILO, HIGH);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
prtln("M ventilo OFF");
|
|
|
|
digitalWrite(RELAIS_VENTILO, LOW);
|
|
|
|
}
|
2019-05-13 23:26:05 +11:00
|
|
|
}
|
|
|
|
/* --------------------------------------------------------------- */
|
|
|
|
|
2019-04-29 20:40:22 +11:00
|
|
|
void phytocli(void)
|
|
|
|
{
|
|
|
|
char flag_exit = 0;
|
|
|
|
char ret, key;
|
|
|
|
|
|
|
|
#define TLINE 40
|
|
|
|
char line[TLINE+1], *sptr;
|
|
|
|
static char separators[] = " \t";
|
|
|
|
|
|
|
|
prtln("entering cli, '?' to help, 'x' to quit.");
|
|
|
|
|
|
|
|
do {
|
2019-05-14 02:49:21 +11:00
|
|
|
prt("$ ");
|
2019-04-29 20:40:22 +11:00
|
|
|
ret = readline(line,TLINE);
|
2019-05-01 01:49:21 +11:00
|
|
|
#if DEBUG > 1
|
2019-05-15 00:46:49 +11:00
|
|
|
prt("readline -> "); hexdump((unsigned char *)line, ret);
|
2019-04-29 20:40:22 +11:00
|
|
|
#endif
|
|
|
|
key = *(sptr = strtok(line, " "));
|
|
|
|
|
|
|
|
switch(key) {
|
2019-05-01 01:49:21 +11:00
|
|
|
case 'x': flag_exit=1; break;
|
|
|
|
case '?': clihelp(); break;
|
2019-05-13 23:26:05 +11:00
|
|
|
case 'I': init_storage(¶metres); break;
|
2019-05-01 01:49:21 +11:00
|
|
|
case 'd': display_config(¶metres); break;
|
|
|
|
case 'r': read_config(0, ¶metres); break;
|
|
|
|
case 'w': write_config(0, ¶metres); break;
|
2019-05-13 23:26:05 +11:00
|
|
|
case 'h': hexdump((unsigned char *)¶metres,
|
2019-05-01 01:49:21 +11:00
|
|
|
sizeof(Global)); break;
|
2019-05-14 02:49:21 +11:00
|
|
|
case 's': setvalue(line, ¶metres); break;
|
2019-05-20 23:49:31 +11:00
|
|
|
|
2019-05-17 03:17:22 +11:00
|
|
|
case 'v': validate_config(¶metres); break;
|
2019-05-20 23:49:31 +11:00
|
|
|
|
2019-05-14 02:49:21 +11:00
|
|
|
case '+': controle_frigo(1); break;
|
|
|
|
case '-': controle_frigo(0); break;
|
2019-05-20 23:49:31 +11:00
|
|
|
case '1': controle_ventilo(1); break;
|
|
|
|
case '0': controle_ventilo(0); break;
|
|
|
|
case 'T': test_relais(5); break;
|
|
|
|
|
2019-05-01 01:49:21 +11:00
|
|
|
default: prtln("gni ?"); break;
|
2019-04-29 20:40:22 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
} while (!flag_exit);
|
|
|
|
|
|
|
|
}
|
|
|
|
/* --------------------------------------------------------------- */
|