X-Git-Url: https://git.martlubbers.net/?a=blobdiff_plain;f=client%2Fclient.c;h=da74940d07aaa7457dfbc172af80da9511a350f4;hb=HEAD;hp=8fa596fb64cb75e595f7c49ef4a548dcbce002fe;hpb=2e196d4e484c3945f7e6bd1c680a2021613bece7;p=mTask.git diff --git a/client/client.c b/client/client.c index 8fa596f..da74940 100644 --- a/client/client.c +++ b/client/client.c @@ -12,6 +12,7 @@ #include "sds.h" #include "spec.h" #include "task.h" +#include "mem.h" #include "interface.h" #define MSG_GET_TASK 't' @@ -20,106 +21,118 @@ #define MSG_SDS_DEL 'a' #define MSG_SDS_UPD 'u' #define MSG_SPEC 'c' +#define MSG_SHUTDOWN 'h' + +#define LOOPDELAY 100 void read_message(void) { - //Find next task - if(input_available()){ - uint8_t c = read_byte(); - debug("Receiving input: %c %02x\n", c, c); - switch(c){ - case MSG_SDS_SPEC: - debug("Receiving an sds"); - sds_register(); - break; - case MSG_SDS_UPD: - debug("Receiving an sds update"); - //TODO do something with the return value - sds_update(); - break; - case MSG_SDS_DEL: - debug("Receiving a delete SDS request"); - sds_delete(); - break; - case MSG_DEL_TASK: - debug("Receiving a delete task request"); - task_delete(); - break; - case MSG_GET_TASK: - debug("Receiving a task"); - task_register(); - break; - case MSG_SPEC: - debug("Receiving a spec request"); - spec_send(); - break; - case '\0': - debug("Server closed connection"); - break; - case '\n': - break; - default: - debug("Unknown message: %X", c); - } - } + //Find next task + if (input_available()) { + uint8_t c = read_byte(); + debug("Receiving input: %c %02x\n", c, c); + switch (c) { + case MSG_SDS_SPEC: + debug("Receiving an sds"); + sds_register(); + break; + case MSG_SDS_UPD: + debug("Receiving an sds update"); + //TODO do something with the return value + c = read16(); + sds_update(c); + break; + case MSG_SDS_DEL: + debug("Receiving a delete SDS request"); + c = read16(); + sds_delete(c); + write_byte('a'); + write16(c); + write_byte('\n'); + break; + case MSG_DEL_TASK: + debug("Receiving a delete task request"); + c = read16(); + task_delete(c); + break; + case MSG_GET_TASK: + debug("Receiving a task"); + task_register(); + break; + case MSG_SPEC: + debug("Receiving a spec request"); + spec_send(); + break; + case MSG_SHUTDOWN: + debug("Shutdown received"); + mem_reset(); + reset(); + break; + case '\0': + debug("Server closed connection"); + break; + case '\n': + break; + default: + debug("Unknown message: %X", c); + } + } else { + // debug("No input"); + } } +unsigned long loopmillis = 0; void loop(void) { - int ct; - long cyclestart; - struct task *curtask; - - read_message(); +#if defined ARDUINO_ESP8266_NODEMCU || defined ARDUINO_AVR_UNO + if (getmillis() - loopmillis < LOOPDELAY) { + return; + } + loopmillis = getmillis(); +#endif + read_message(); - //Run tasks - cyclestart = getmillis(); - for(ct = 0; ctlastrun > curtask->interval){ - debug("Running interval task: %d", ct); - run_task(curtask); + //Run tasks + unsigned long cyclestart = getmillis(); + for (struct task *t = task_head(); t != NULL; t = task_next(t)) { + //interrupt task + if (is_interrupt_task(t) && had_interrupt(t)) { + debug("Interrupt task %d not implemented", t->taskid); + run_task(t); + //Interval task, and interval passed + } else if (cyclestart - t->lastrun > t->interval) { + debug("Running interval task: %d", t->taskid); + run_task(t); - //Oneshot task, thus disable - if(curtask->interval == 0){ - curtask->used = false; - } - curtask->lastrun = cyclestart; - } - } + //Oneshot task, thus disable + if (t->interval == 0) { + task_delete(t->taskid); + } + t->lastrun = cyclestart; + } + } } #ifdef STM -int main(void){ -#elif defined ARDUINO_ESP8266_NODEMCU -void setup(){ +int main(void) { +#elif defined ARDUINO_ESP8266_NODEMCU || defined ARDUINO_AVR_UNO +void setup() { #elif defined LINUX -int main(int argc, char *argv[]){ - gargc = argc; - gargv = argv; +int main(int argc, char *argv[]) { + gargc = argc; + gargv = argv; #endif - //Initialize systems - setup(); - sds_init(); - task_init(); - debug("sending device spec"); - while(true){ - //Check for newtasks - loop(); - msdelay(100); - } + //Initialize device independant functionality + real_setup(); + +#if !defined(ARDUINO_ESP8266_NODEMCU) && !defined(ARDUINO_AVR_UNO) + while (true) { + //Check for newtasks + loop(); + msdelay(LOOPDELAY); + } -#ifndef ARDUINO_ESP8266_NODEMCU - return 0; + return 0; #endif }