From 34e31d383f1c2315dee254e82c8a8ec482f210c3 Mon Sep 17 00:00:00 2001 From: Mart Lubbers Date: Tue, 10 Jan 2017 18:18:01 +0100 Subject: [PATCH] transfer to cpp arduino --- Makefile | 2 +- int/.gitignore | 1 + int/Makefile | 11 +- int/Makefile.arduino | 6 + int/{interpret.c => interpret.cpp} | 10 +- int/interpret.h | 2 +- int/{main.c => main.cpp} | 219 ++++++++++++++++++----------- int/main.h | 8 ++ int/misc.h | 18 ++- int/{sds.c => sds.cpp} | 33 ++--- int/sds.h | 6 +- int/{task.c => task.cpp} | 24 ++-- int/task.h | 4 +- 13 files changed, 215 insertions(+), 129 deletions(-) create mode 100644 int/Makefile.arduino rename int/{interpret.c => interpret.cpp} (94%) rename int/{main.c => main.cpp} (62%) create mode 100644 int/main.h rename int/{sds.c => sds.cpp} (78%) rename int/{task.c => task.cpp} (74%) diff --git a/Makefile b/Makefile index 4510a04..915f396 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CLEAN_HOME?=/opt/clean CLM:=clm -override CLMFLAGS+=-dynamics -h 200M -nt +override CLMFLAGS+=-dynamics -h 200M -nt -l -no-pie CLMLIBS:=\ -I $(CLEAN_HOME)/lib/iTasks-SDK/Patches/Dynamics\ -I $(CLEAN_HOME)/lib/iTasks-SDK/Patches/Generics\ diff --git a/int/.gitignore b/int/.gitignore index 87e54c2..11d6560 100644 --- a/int/.gitignore +++ b/int/.gitignore @@ -1,2 +1,3 @@ main *.o +build-uno diff --git a/int/Makefile b/int/Makefile index fb845f0..b311ea5 100644 --- a/int/Makefile +++ b/int/Makefile @@ -1,11 +1,14 @@ -CFLAGS:=-g -Wall -Wextra -Werror -std=c99 +CPPFLAGS:=-g -Wall -Wextra -Werror -DDEBUG PROG:=main -OBJS:=interpret.o sds.o task.o +OBJS:=interpret.o sds.o task.o main.o all: mTaskSymbols.h $(PROG) -$(PROG): $(PROG).c $(OBJS) misc.h - $(LINK.c) $(LDLIBS) $^ $(OUTPUT_OPTION) +%.o: %.cpp + g++ $(CPPFLAGS) -c $< -o $@ + +$(PROG): $(OBJS) misc.h + g++ $(LDFLAGS) -o $@ $(OBJS) mTaskSymbols.h: CLMFLAGS=-nr make -BC .. mTaskInterpret diff --git a/int/Makefile.arduino b/int/Makefile.arduino new file mode 100644 index 0000000..be29f33 --- /dev/null +++ b/int/Makefile.arduino @@ -0,0 +1,6 @@ +BOARD_TAG = uno +ARDUINO_LIBS = LiquidCrystal +CPPFLAGS += -std=gnu11 + +include /usr/share/arduino/Arduino.mk + diff --git a/int/interpret.c b/int/interpret.cpp similarity index 94% rename from int/interpret.c rename to int/interpret.cpp index bf437ae..4238f44 100644 --- a/int/interpret.c +++ b/int/interpret.cpp @@ -7,7 +7,13 @@ #include "task.h" #include "sds.h" -void run_task(struct task *t, int fd) +#ifdef ARDUINO +#define trace(op, ...) ; +#else +#define trace(op, ...) printf("pc: %d, sp: %d, op: " op, pc, sp, ##__VA_ARGS__); +#endif + +void run_task(struct task *t) { uint8_t *program = t->bc; int plen = t->tlen; @@ -41,7 +47,7 @@ void run_task(struct task *t, int fd) stack[sp++] = sds_fetch(program[pc++]); break; case BCSDSPUBLISH: trace("sds publish %d\n", program[pc]); - sds_publish(program[pc++], fd); + sds_publish(program[pc++]); break; case BCNOT: trace("not\n"); stack[sp] = stack[sp] > 0 ? 0 : 1; diff --git a/int/interpret.h b/int/interpret.h index 98a257b..d2effe7 100644 --- a/int/interpret.h +++ b/int/interpret.h @@ -8,6 +8,6 @@ #include "task.h" -void run_task(struct task *task, int fd); +void run_task(struct task *task); #endif diff --git a/int/main.c b/int/main.cpp similarity index 62% rename from int/main.c rename to int/main.cpp index 02852ef..491cc3b 100644 --- a/int/main.c +++ b/int/main.cpp @@ -1,16 +1,22 @@ -#define _BSD_SOURCE +#define _DEFAULT_SOURCE +#include +#include +#include + +#ifdef ARDUINO +#include +#include +#else +#include #include #include #include -#include -#include #include -#include -#include #include -#include #include +#include #include +#endif #include "interpret.h" #include "mTaskSymbols.h" @@ -25,18 +31,28 @@ #define MSG_SDS_SPEC 's' #define MSG_SDS_UPD 'u' +//Globals +#ifndef ARDUINO struct timeval tv1; int sock_fd = -1; int fd = -1; -int port = 8123; +int *argc; +char **argv; +char bt; +#endif -void killHandler(int i) -{ - printf("%i caught, Bye...\n", i); - exit(1); +#ifndef ARDUINO +long millis() { + if (gettimeofday(&tv1, NULL) == -1) + pdie("gettimeofday"); + return tv1.tv_sec*1000 + tv1.tv_usec/1000; } +#endif -bool input_available(int fd){ +bool input_available(){ +#ifdef ARDUINO + return Serial.available(); +#else struct timeval tv; fd_set fds; tv.tv_sec = 0; @@ -46,38 +62,67 @@ bool input_available(int fd){ if (select(fd+1, &fds, NULL, NULL, &tv) == -1) pdie("select"); return FD_ISSET(fd, &fds); +#endif } -long millis() { - if (gettimeofday(&tv1, NULL) == -1) - pdie("gettimeofday"); - return tv1.tv_sec*1000 + tv1.tv_usec/1000; +uint8_t read_byte() +{ +#ifdef ARDUINO + return Serial.read(); +#else + read(fd, &bt, 1); + return bt; +#endif } -void read_message(int fd_in, int fd_out) +void write_byte(uint8_t b) { - uint8_t c; - //Find next task +#ifdef ARDUINO + Serial.write(b); +#else + write(fd, &b, 1); +#endif +} + +void sleep(int ms) +{ +#ifdef ARDUINO + delay(ms); +#else + usleep(ms*1000); +#endif +} + +#ifndef ARDUINO +void killHandler(int i) +{ + printf("%i caught, Bye...\n", i); + exit(1); +} +#endif - read(fd_in, &c, 1); +void read_message() +{ + //Find next task + uint8_t c = read_byte(); debug("Receiving input: %c\n", c); switch(c){ case MSG_SDS_SPEC: debug("Receiving an sds\n"); - sds_register(fd_in); + sds_register(); break; case MSG_SDS_UPD: debug("Receiving an sds\n"); //TODO do something with the return value - sds_update(fd_in); + sds_update(); break; case MSG_DEL_TASK: debug("Receiving a delete task request\n"); - task_delete(fd); + task_delete(); break; case MSG_GET_TASK: debug("Receiving a task\n"); - c = task_register(fd_in); + c = task_register(); // write(fd_out, &c, 1); // write(fd_out, break; @@ -89,29 +134,10 @@ void read_message(int fd_in, int fd_out) default: debug("Unknown message: %X\n", c); } - (void) fd_out; } void open_filedescriptors() { - struct sockaddr_in sa; - - memset(&sa, 0, sizeof(sa)); - sa.sin_family = AF_INET; - sa.sin_addr.s_addr = INADDR_ANY; - sa.sin_port = htons(port); - - if((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) - pdie("socket"); - if(bind(sock_fd, (struct sockaddr*)&sa, sizeof(sa)) == -1) - pdie("bind"); - if(listen(sock_fd, 10) == -1) - pdie("listen"); - - printf("Listening on %d\n", port); - fflush(stdout); - if((fd = accept(sock_fd, (struct sockaddr*)NULL, NULL)) == -1) - pdie("accept"); } void usage(FILE *o, char *arg0){ @@ -123,10 +149,12 @@ void usage(FILE *o, char *arg0){ , arg0); } -int main(int argc, char *argv[]) +void setup() { - int ct; - +#ifdef ARDUINO + Serial.begin(9600); +#else + int port = 8123, opti = 1; //Register signal handler if(signal(SIGINT, killHandler) == SIG_ERR){ die("Couldn't register signal handler...\n"); @@ -134,14 +162,12 @@ int main(int argc, char *argv[]) if(signal(SIGTERM, killHandler) == SIG_ERR){ die("Couldn't register signal handler...\n"); } - //Command line arguments - int opti = 1; - while(opti < argc){ - if(strcmp(argv[opti], "-h") == 0){ + while(opti < *argc){ + if(strcmp((*argv)+opti, "-h") == 0){ usage(stdout, argv[0]); exit(EXIT_SUCCESS); - } else if(strcmp(argv[opti], "-p") == 0 && opti+1 1\n"); @@ -152,43 +178,76 @@ int main(int argc, char *argv[]) opti++; } + //Open file descriptors + struct sockaddr_in sa; + + memset(&sa, 0, sizeof(sa)); + sa.sin_family = AF_INET; + sa.sin_addr.s_addr = INADDR_ANY; + sa.sin_port = htons(port); + + if((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) + pdie("socket"); + if(bind(sock_fd, (struct sockaddr*)&sa, sizeof(sa)) == -1) + pdie("bind"); + if(listen(sock_fd, 10) == -1) + pdie("listen"); + + printf("Listening on %d\n", port); + fflush(stdout); + if((fd = accept(sock_fd, (struct sockaddr*)NULL, NULL)) == -1) + pdie("accept"); +#endif + //Initialize systems sds_init(); task_init(); +} - //Open communication - open_filedescriptors(); - write(fd, "\n", 1); - +void loop() +{ + int ct; long cyclestart; struct task *curtask; - while(true){ - //Check for new tasks - if(input_available(fd)) - read_message(fd, fd); - //Run tasks - cyclestart = millis(); - for(ct = 0; ctlastrun < curtask->interval){ -// debug("Task %d not scheduled\n", ct); - continue; - } + if(input_available()) + read_message(); + //Run tasks + cyclestart = millis(); + for(ct = 0; ctlastrun < curtask->interval){ +// debug("Task %d not scheduled\n", ct); + continue; + } #ifdef DEBUG - printf("Current task to run: %d\n", ct); - getchar(); + printf("Current task to run: %d\n", ct); + getchar(); #endif - run_task(curtask, fd); - } - debug("Waiting for 500ms\n"); - usleep(500000); - debug("done waiting\n"); - write(fd, "\n", 1); + run_task(curtask); + } + debug("Waiting for 500ms\n"); + sleep(500); + debug("done waiting\n"); + write_byte('\n'); +} + +int main(int ac, char *av[]) +{ +#ifndef ARDUINO + argc = ∾ + argv = av; +#endif + setup(); + + write_byte('\n'); + + while(true){ + //Check for new tasks } return 0; } diff --git a/int/main.h b/int/main.h new file mode 100644 index 0000000..02c9b26 --- /dev/null +++ b/int/main.h @@ -0,0 +1,8 @@ +#ifndef MAIN_H +#define MAIN_H + +#include + +uint8_t read_byte(); +void write_byte(uint8_t b); +#endif diff --git a/int/misc.h b/int/misc.h index 0c6bec9..3d5236e 100644 --- a/int/misc.h +++ b/int/misc.h @@ -1,24 +1,22 @@ #ifndef MISC_H #define MISC_H +#include "main.h" -#define DEBUG +#define read16() 256*read_byte() + read_byte() +#ifdef ARDUINO +#define debug(s, ...) ; +#define pdie(s) ; +#define die(s, ...) ; +#else #ifdef DEBUG #define debug(s, ...) printf(s, ##__VA_ARGS__); -#define trace(op, ...) printf("pc: %d, sp: %d, op: " op, pc, sp, ##__VA_ARGS__); #else #define debug(s, ...) ; -#define trace(pc-1, sp, op) ; #endif #define pdie(s) {perror(s); exit(1);} #define die(s, ...) {fprintf(stderr, s, ##__VA_ARGS__); exit(1);} - -#define read16(fd, c, i) {\ - read(fd, &c, 1); \ - i = 256*c; \ - read(fd, &c, 1); \ - i += c; \ - } +#endif #endif diff --git a/int/sds.c b/int/sds.cpp similarity index 78% rename from int/sds.c rename to int/sds.cpp index 19ebadb..6ac2d19 100644 --- a/int/sds.c +++ b/int/sds.cpp @@ -1,9 +1,12 @@ -#include #include #include + +#ifndef ARDUINO #include +#include +#endif -#include "mTaskSymbols.h" +#include "main.h" #include "interpret.h" #include "misc.h" #include "sds.h" @@ -16,7 +19,7 @@ void sds_init() memset(&sdss, 0, sizeof(struct sds)*MAXSDSS); } -void sds_register(int fd) +void sds_register() { uint8_t cs; for(cs = 0; cs #include #include + +#ifdef ARDUINO +#include +#include +#else #include +#include +#endif #include "misc.h" #include "task.h" struct task tasks[MAXTASKS]; -uint8_t c; void task_init() { memset(&tasks, 0, sizeof(struct task)*MAXTASKS); } -int task_register(int fd) +int task_register() { uint8_t ct; @@ -26,15 +31,15 @@ int task_register(int fd) memset(&tasks[ct], 0, sizeof(struct task)); //Read interval - read16(fd, c, tasks[ct].interval); + tasks[ct].interval = read16(); //Read tasklength - read16(fd, c, tasks[ct].tlen); + tasks[ct].tlen = read16(); if(tasks[ct].tlen > MAXTASKSIZE) die("Task is too long: %d\n", tasks[ct].tlen); //Read task bytecode - for(int i = 0; i