add arduino uno compatability
authorMart Lubbers <mart@martlubbers.net>
Wed, 3 May 2017 10:11:08 +0000 (12:11 +0200)
committerMart Lubbers <mart@martlubbers.net>
Wed, 3 May 2017 10:12:39 +0000 (12:12 +0200)
22 files changed:
client/arduino/arduino.ino [new file with mode: 0644]
client/arduino/interface.cpp [new file with mode: 0644]
client/arduino/interface.h [new symlink]
client/arduino/interpret.c [new symlink]
client/arduino/interpret.h [new symlink]
client/arduino/mTaskSymbols.h [new symlink]
client/arduino/mem.c [new symlink]
client/arduino/mem.h [new symlink]
client/arduino/sds.c [new symlink]
client/arduino/sds.h [new symlink]
client/arduino/spec.c [new symlink]
client/arduino/spec.h [new symlink]
client/arduino/task.c [new symlink]
client/arduino/task.h [new symlink]
client/client.c
client/interface.h
client/interpret.c
client/interpret.h
client/mem.c
client/mem.h
client/sds.c
client/task.c

diff --git a/client/arduino/arduino.ino b/client/arduino/arduino.ino
new file mode 100644 (file)
index 0000000..305f51d
--- /dev/null
@@ -0,0 +1,135 @@
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+
+#ifdef LINUX
+#include <stdio.h>
+#endif
+
+#include "interpret.h"
+#include "mTaskSymbols.h"
+#include "sds.h"
+#include "spec.h"
+#include "task.h"
+#include "interface.h"
+
+#define MSG_GET_TASK 't'
+#define MSG_DEL_TASK 'd'
+#define MSG_SDS_SPEC 's'
+#define MSG_SDS_DEL 'a'
+#define MSG_SDS_UPD 'u'
+#define MSG_SPEC 'c'
+
+#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
+        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);
+        //Write acknowledgement
+        write_byte('d');
+        write16(c);
+        write_byte('\n');
+        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);
+    }
+  } else {
+    //         debug("No input");
+  }
+}
+
+unsigned long loopmillis = 0;
+void loop(void)
+{
+#if defined ARDUINO_ESP8266_NODEMCU || defined ARDUINO_AVR_UNO
+  if (getmillis() - loopmillis < LOOPDELAY) {
+    return;
+  }
+  loopmillis = getmillis();
+#endif
+  read_message();
+
+  //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 (t->interval == 0) {
+        task_delete(t->taskid);
+      }
+      t->lastrun = cyclestart;
+    }
+  }
+}
+
+#ifdef STM
+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;
+#endif
+
+  //Initialize device independant functionality
+  real_setup();
+
+#if !defined(ARDUINO_ESP8266_NODEMCU) && !defined(ARDUINO_AVR_UNO)
+  while (true) {
+    //Check for newtasks
+    loop();
+    msdelay(LOOPDELAY);
+  }
+
+  return 0;
+#endif
+}
diff --git a/client/arduino/interface.cpp b/client/arduino/interface.cpp
new file mode 100644 (file)
index 0000000..c48f7c3
--- /dev/null
@@ -0,0 +1,62 @@
+#include <Arduino.h>
+#include <stdbool.h>
+#include <stdint.h>
+
+extern "C" unsigned long getmillis(void);
+extern "C" void pdie(char *, ...);
+extern "C" void die(char *, ...);
+extern "C" void debug(char *, ...);
+extern "C" void real_setup(void);
+extern "C" void write_byte(uint8_t);
+extern "C" uint8_t read_byte(void);
+extern "C" bool input_available(void);
+extern "C" void msdelay(unsigned long);
+
+unsigned long getmillis(void)
+{
+       return millis();
+}
+
+void msdelay(unsigned long ms)
+{
+       delay(ms);
+}
+
+bool input_available(void)
+{
+       return Serial.available();
+}
+
+uint8_t read_byte(void)
+{
+       return Serial.read();
+}
+
+void write_byte(uint8_t b)
+{
+       Serial.write(b);
+}
+
+void real_setup(void)
+{
+       Serial.begin(9600);
+}
+
+void debug(char *fmt, ...)
+{
+       Serial.println(fmt);
+}
+
+void die(char *fmt, ...)
+{
+       Serial.println(fmt);
+       while(1){
+               msdelay(1000);
+               Serial.print("die");
+       }
+}
+
+void pdie(char *s)
+{
+       die(s);
+}
diff --git a/client/arduino/interface.h b/client/arduino/interface.h
new file mode 120000 (symlink)
index 0000000..b854400
--- /dev/null
@@ -0,0 +1 @@
+../interface.h
\ No newline at end of file
diff --git a/client/arduino/interpret.c b/client/arduino/interpret.c
new file mode 120000 (symlink)
index 0000000..213f9b8
--- /dev/null
@@ -0,0 +1 @@
+../interpret.c
\ No newline at end of file
diff --git a/client/arduino/interpret.h b/client/arduino/interpret.h
new file mode 120000 (symlink)
index 0000000..a3858d4
--- /dev/null
@@ -0,0 +1 @@
+../interpret.h
\ No newline at end of file
diff --git a/client/arduino/mTaskSymbols.h b/client/arduino/mTaskSymbols.h
new file mode 120000 (symlink)
index 0000000..482ac01
--- /dev/null
@@ -0,0 +1 @@
+../mTaskSymbols.h
\ No newline at end of file
diff --git a/client/arduino/mem.c b/client/arduino/mem.c
new file mode 120000 (symlink)
index 0000000..d01a49b
--- /dev/null
@@ -0,0 +1 @@
+../mem.c
\ No newline at end of file
diff --git a/client/arduino/mem.h b/client/arduino/mem.h
new file mode 120000 (symlink)
index 0000000..2e72483
--- /dev/null
@@ -0,0 +1 @@
+../mem.h
\ No newline at end of file
diff --git a/client/arduino/sds.c b/client/arduino/sds.c
new file mode 120000 (symlink)
index 0000000..9d106bb
--- /dev/null
@@ -0,0 +1 @@
+../sds.c
\ No newline at end of file
diff --git a/client/arduino/sds.h b/client/arduino/sds.h
new file mode 120000 (symlink)
index 0000000..f6b82b5
--- /dev/null
@@ -0,0 +1 @@
+../sds.h
\ No newline at end of file
diff --git a/client/arduino/spec.c b/client/arduino/spec.c
new file mode 120000 (symlink)
index 0000000..3a0ed1c
--- /dev/null
@@ -0,0 +1 @@
+../spec.c
\ No newline at end of file
diff --git a/client/arduino/spec.h b/client/arduino/spec.h
new file mode 120000 (symlink)
index 0000000..bbf3789
--- /dev/null
@@ -0,0 +1 @@
+../spec.h
\ No newline at end of file
diff --git a/client/arduino/task.c b/client/arduino/task.c
new file mode 120000 (symlink)
index 0000000..e4f6627
--- /dev/null
@@ -0,0 +1 @@
+../task.c
\ No newline at end of file
diff --git a/client/arduino/task.h b/client/arduino/task.h
new file mode 120000 (symlink)
index 0000000..d8ffa8e
--- /dev/null
@@ -0,0 +1 @@
+../task.h
\ No newline at end of file
index b501e0f..305f51d 100644 (file)
 
 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
-                       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);
-                       //Write acknowledgement
-                       write_byte('d');
-                       write16(c);
-                       write_byte('\n');
-                       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);
-               }
-       } else {
-//             debug("No input");
-       }
+  //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);
+        //Write acknowledgement
+        write_byte('d');
+        write16(c);
+        write_byte('\n');
+        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);
+    }
+  } else {
+    //         debug("No input");
+  }
 }
 
 unsigned long loopmillis = 0;
 void loop(void)
 {
-#ifdef ARDUINO_ESP8266_NODEMCU
-       if(getmillis()-loopmillis < LOOPDELAY){
-               return;
-       }
-       loopmillis = getmillis();
+#if defined ARDUINO_ESP8266_NODEMCU || defined ARDUINO_AVR_UNO
+  if (getmillis() - loopmillis < LOOPDELAY) {
+    return;
+  }
+  loopmillis = getmillis();
 #endif
-       read_message();
+  read_message();
 
-       //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);
+  //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(t->interval == 0){
-                               task_delete(t->taskid);
-                       }
-                       t->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 device independant functionality
-       real_setup();
+  //Initialize device independant functionality
+  real_setup();
 
-#ifndef ARDUINO_ESP8266_NODEMCU
-       while(true){
-               //Check for newtasks
-               loop();
-               msdelay(LOOPDELAY);
-       }
+#if !defined(ARDUINO_ESP8266_NODEMCU) && !defined(ARDUINO_AVR_UNO)
+  while (true) {
+    //Check for newtasks
+    loop();
+    msdelay(LOOPDELAY);
+  }
 
-       return 0;
+  return 0;
 #endif
 }
index 9e16292..266c13d 100644 (file)
@@ -10,8 +10,8 @@ extern "C" {
 #include <stdarg.h>
 
 #ifdef LINUX
-#define MAXTASKS 5
-#define MAXSDSS 100
+#define STACKSIZE 1024
+#define MEMSIZE 1024
 #define HAVELED 1
 #define HAVEAIO 1
 #define HAVEDIO 1
@@ -20,26 +20,32 @@ extern int gargc;
 extern char **gargv;
 
 #elif defined STM
-#define MAXTASKS 5
-#define MAXSDSS 100
+#define STACKSIZE 1024
+#define MEMSIZE 1024
 #define HAVELED 1
 #define HAVEAIO 1
 #define HAVEDIO 1
 
 #elif defined ARDUINO_ESP8266_NODEMCU
-#define MAXTASKS 5
-#define MAXSDSS 100
+#define STACKSIZE 1024
+#define MEMSIZE 1024
 #define HAVELED 0
 #define HAVEAIO 0
 #define HAVEDIO 0
 
+#elif defined ARDUINO_AVR_UNO
+#define STACKSIZE 64
+#define MEMSIZE 256
+#define HAVELED 0
+#define HAVEAIO 0
+#define HAVEDIO 0
 #else
 //Add you device here
 #endif
 
 #define read16() 256*(uint8_t)read_byte() + (uint8_t)read_byte()
 #define from16(a, b) 256*a+b
-#define write16(i) { write_byte((uint8_t)i/256); write_byte((uint8_t)i%256); }
+#define write16(i) { write_byte((uint8_t)(i/256)); write_byte((uint8_t)(i%256)); }
 
 /* Communication */
 bool input_available(void);
index 6f2f20d..92e58d1 100644 (file)
 
 #define f16(p) program[pc]*265+program[pc+1]
 
+uint8_t stack[STACKSIZE] = {0};
+
 void run_task(struct task *t)
 {
        uint8_t *program = t->bc;
        int plen = t->tasklength;
        int pc = 0;
        int sp = 0;
-       char stack[STACKSIZE] = {0};
        debug("Running task with length: %d", plen);
        while(pc < plen){
                switch(program[pc++]){
index 726ebf6..bb93bec 100644 (file)
@@ -1,8 +1,6 @@
 #ifndef INTEPRET_H
 #define INTEPRET_H
 
-#define STACKSIZE 1024
-
 #ifdef __cplusplus
 extern "C" {
 #endif
index f4cdf2c..9b72b8a 100644 (file)
@@ -1,6 +1,7 @@
 #include <stdlib.h>
 #include <stdint.h>
 
+#include "interface.h"
 #include "mem.h"
 
 uint8_t mem[MEMSIZE] = {0};
index c1adc94..1a1d39c 100644 (file)
@@ -7,8 +7,6 @@ extern "C" {
 #include <stdint.h>
 #include <stdbool.h>
 
-#define MEMSIZE 1024
-
 uint8_t *mem_top;
 uint8_t *mem_bottom;
 uint8_t *mem_task;
index 56f5895..2570017 100644 (file)
@@ -1,15 +1,11 @@
 #include <stdlib.h>
 #include <string.h>
-#include <stdio.h>
 
 #include "interface.h"
 #include "sds.h"
 #include "mem.h"
 
-extern uint8_t *mem_top;
-extern uint8_t *mem_bottom;
-extern uint8_t *mem_task;
-extern uint8_t *mem_sds;
+extern uint8_t *mem_top, *mem_bottom, *mem_task, *mem_sds;
 
 struct sds *sds_head(void)
 {
index e36600d..050784c 100644 (file)
@@ -6,10 +6,7 @@
 #include "interface.h"
 #include "mem.h"
 
-extern uint8_t *mem_top;
-extern uint8_t *mem_bottom;
-extern uint8_t *mem_task;
-extern uint8_t *mem_sds;
+extern uint8_t *mem_top, *mem_bottom, *mem_task, *mem_sds;
 
 uint8_t taskid = 0;