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
|
|
|
/* -------------------------------------------------- */
|
|
|
|
/* some interesting macros */
|
|
|
|
#define prt(a) Serial.print(a)
|
|
|
|
#define prtln(a) Serial.println(a)
|
|
|
|
|
|
|
|
/* -------------------------------------------------- */
|
|
|
|
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");
|
|
|
|
prtln("w\twrite config");
|
2019-05-14 02:49:21 +11:00
|
|
|
prtln("T\ttest relay");
|
|
|
|
prtln("0/1\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;
|
|
|
|
|
|
|
|
default: prtln("GNI ?");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
/* --------------------------------------------------------------- */
|
|
|
|
|
|
|
|
void test_relais(short nb) {
|
|
|
|
short foo;
|
|
|
|
prtln("test du relais frigo");
|
|
|
|
|
|
|
|
for (foo=0; foo<nb; foo++) {
|
|
|
|
digitalWrite(RELAIS_FRIGO, HIGH);
|
|
|
|
delay(parametres.delai);
|
|
|
|
|
|
|
|
digitalWrite(RELAIS_FRIGO, LOW);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
prtln("...");
|
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-04-29 20:40:22 +11:00
|
|
|
hexdump((unsigned char *)line, ret);
|
|
|
|
#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;
|
|
|
|
case 'T': test_relais(5); break;
|
2019-05-01 01:49:21 +11:00
|
|
|
|
2019-05-14 02:49:21 +11:00
|
|
|
case '+': controle_frigo(1); break;
|
|
|
|
case '-': controle_frigo(0); break;
|
|
|
|
|
2019-05-01 01:49:21 +11:00
|
|
|
default: prtln("gni ?"); break;
|
2019-04-29 20:40:22 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
} while (!flag_exit);
|
|
|
|
|
|
|
|
}
|
|
|
|
/* --------------------------------------------------------------- */
|