clean:
- $(RM) -r $(BINARIES) Clean\ System\ Files
+ $(RM) -r $(BINARIES) Clean\ System\ Files miTask-data
#ifdef STM32F767xx
#include "stm32f7xx_hal.h"
-#include "gpio.h"
#include "usart.h"
+#include "gpio.h"
+
+#define SET_LED_RED GPIOB->BSRR = GPIO_PIN_14
+#define RESET_LED_RED GPIOB->BSRR = GPIO_PIN_14 << 16
+
+#define SET_LED_BLUE GPIOB->BSRR = GPIO_PIN_7
+#define RESET_LED_BLUE GPIOB->BSRR = GPIO_PIN_7 << 16
+
+#define SET_LED_GREEN GPIOB->BSRR = GPIO_PIN_0
+#define RESET_LED_GREEN GPIOB->BSRR = GPIO_PIN_0 << 16
+
#else
#include <stdio.h>
#include <netdb.h>
#include "interface.h"
-#define SET_LED_RED GPIOB->BSRR = GPIO_PIN_14
-#define RESET_LED_RED GPIOB->BSRR = GPIO_PIN_14 << 16
-
-#define SET_LED_BLUE GPIOB->BSRR = GPIO_PIN_7
-#define RESET_LED_BLUE GPIOB->BSRR = GPIO_PIN_7 << 16
-
-#define SET_LED_GREEN GPIOB->BSRR = GPIO_PIN_0
-#define RESET_LED_GREEN GPIOB->BSRR = GPIO_PIN_0 << 16
-
//Globals
#ifdef STM32F767xx
volatile char uartf = 0;
bool input_available(){
#ifdef STM32F767xx
+ if(HAL_UART_Receive(&huart3, &bt, 1, 1) == HAL_TIMEOUT)
+ return false;
return true;
#else
struct timeval tv;
FD_SET(fd, &fds);
if (select(fd+1, &fds, NULL, NULL, &tv) == -1)
pdie("select");
- return FD_ISSET(fd, &fds);
+ if(!FD_ISSET(fd, &fds))
+ return false;
+ read(fd, &bt, 1);
+ return true;
#endif
}
{
#ifdef STM32F767xx
HAL_UART_Receive(&huart3, &bt, 1, 1000);
- return 0;
+ return bt;
#else
read(fd, &bt, 1);
return bt;
{
#ifdef STM32F767xx
#else
- debug("dwrite %d: %d\n", i, b);
+ debug("dwrite %d: %d", i, b);
#endif
+ (void) i;
+ (void) b;
}
bool read_dpin(uint8_t i)
#ifdef STM32F767xx
return false;
#else
- debug("dread %d\n", i);
+ debug("dread %d", i);
return false;
#endif
+ (void) i;
}
void write_apin(uint8_t i, uint8_t a)
SET_LED_GREEN;
}
#else
- debug("awrite %d: %d\n", i, a);
+ debug("awrite %d: %d", i, a);
#endif
+ (void) a;
+ (void) i;
}
uint8_t read_apin(uint8_t i)
#ifdef STM32F767xx
return 0;
#else
- debug("aread %d\n", i);
+ debug("aread %d", i);
return 0;
#endif
+ (void) i;
}
void delay(long ms)
int port = 8123, opti = 1;
//Register signal handler
if(signal(SIGINT, killHandler) == SIG_ERR){
- die("Couldn't register signal handler...\n");
+ die("Couldn't register signal handler...");
}
if(signal(SIGTERM, killHandler) == SIG_ERR){
- die("Couldn't register signal handler...\n");
+ die("Couldn't register signal handler...");
}
//Command line arguments
while(opti < gargc){
} else if(strcmp(gargv[opti], "-p") == 0 && opti+1<gargc){
port = atoi(gargv[++opti]);
if(port < 1)
- die("Port numbers are > 1\n");
+ die("Port numbers are > 1");
} else {
usage(stderr, gargv[0]);
exit(EXIT_FAILURE);
extern int gargc;
extern char **gargv;
#endif
+extern uint8_t bt;
uint8_t read_byte();
void write_byte(uint8_t b);
#define read16() 256*read_byte() + read_byte()
#ifdef STM32F767xx
#define debug(s, ...) {\
- sprintf(buf, s, ##__VA_ARGS__);\
+ sprintf(buf, "m" s "\r\n", ##__VA_ARGS__);\
HAL_UART_Transmit(&huart3, (uint8_t*)buf, strlen(buf), 1000);\
}
#define pdie(s) ;
#else
#ifdef DEBUG
-#define debug(s, ...) printf(s, ##__VA_ARGS__);
+#define debug(s, ...) printf(s "\n", ##__VA_ARGS__);
#else
#define debug(s, ...) ;
#endif
#define pdie(s) {perror(s); exit(1);}
-#define die(s, ...) {fprintf(stderr, s, ##__VA_ARGS__); exit(1);}
+#define die(s, ...) {fprintf(stderr, s "\n", ##__VA_ARGS__); exit(1);}
#endif
#endif
#define trace(op, ...) ;
#else
#define trace(op, ...) ;
-//printf("pc: %d, sp: %d, op: " op, pc, sp, ##__VA_ARGS__);
+//printf("pc: %d, sp: %d, op: " op "\n", pc, sp, ##__VA_ARGS__);
#endif
void run_task(struct task *t)
int pc = 0;
int sp = 0;
char stack[STACKSIZE] = {0};
- debug("Running task with length: %d\n", plen);
+ debug("Running task with length: %d", plen);
while(pc != plen){
- debug("program: %d\n", program[pc]);
- debug("stack: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
+ debug("program: %d", program[pc]);
+ debug("stack: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
stack[0], stack[1], stack[2], stack[3], stack[4],
stack[5], stack[6], stack[7], stack[8], stack[9]);
switch(program[pc++]){
- case BCNOP: trace("nop\n");
+ case BCNOP: trace("nop");
break;
- case BCLAB: trace("label: 0x%02x!!!!!!\n", program[pc]);
+ case BCLAB: trace("label: 0x%02x!!!!!!", program[pc]);
pc++;
break;
- case BCPUSH: trace("push %d\n", program[pc]*265+program[pc+1]);
+ case BCPUSH: trace("push %d", program[pc]*265+program[pc+1]);
stack[sp++] = program[pc]*265 + program[pc+1];
pc+=2;
break;
- case BCPOP: trace("pop\n");
+ case BCPOP: trace("pop");
sp--;
break;
- case BCSDSSTORE: trace("sds store: %d\n", program[pc]);
+ case BCSDSSTORE: trace("sds store: %d", program[pc]);
sds_store(program[pc++], stack[--sp]);
break;
- case BCSDSFETCH: trace("sds fetch: %d\n", program[pc]);
+ case BCSDSFETCH: trace("sds fetch: %d", program[pc]);
stack[sp++] = sds_fetch(program[pc++]);
break;
- case BCSDSPUBLISH: trace("sds publish %d\n", program[pc]);
+ case BCSDSPUBLISH: trace("sds publish %d", program[pc]);
sds_publish(program[pc++]);
break;
- case BCNOT: trace("not\n");
+ case BCNOT: trace("not");
stack[sp] = stack[sp] > 0 ? 0 : 1;
break;
- case BCADD: trace("add\n");
+ case BCADD: trace("add");
stack[sp-2] = stack[sp-1] + stack[sp-2];
sp -= 1;
break;
- case BCSUB: trace("sub\n");
+ case BCSUB: trace("sub");
stack[sp-2] = stack[sp-1] - stack[sp-2];
sp -= 1;
break;
- case BCMUL: trace("mul\n");
+ case BCMUL: trace("mul");
stack[sp-2] = stack[sp-1] * stack[sp-2];
sp -= 1;
break;
- case BCDIV: trace("div\n");
+ case BCDIV: trace("div");
stack[sp-2] = stack[sp-1] / stack[sp-2];
sp -= 1;
break;
- case BCAND: trace("and\n");
+ case BCAND: trace("and");
stack[sp-2] = stack[sp-1] && stack[sp-2];
sp -= 1;
break;
- case BCOR: trace("or\n");
+ case BCOR: trace("or");
stack[sp-2] = stack[sp-1] || stack[sp-2];
sp -= 1;
break;
- case BCEQ: trace("eq\n");
+ case BCEQ: trace("eq");
stack[sp-2] = stack[sp-1] == stack[sp-2];
sp -= 1;
break;
- case BCNEQ: trace("neq\n");
+ case BCNEQ: trace("neq");
stack[sp-2] = stack[sp-1] != stack[sp-2];
sp -= 1;
break;
- case BCLES: trace("les\n");
+ case BCLES: trace("les");
stack[sp-2] = stack[sp-1] < stack[sp-2];
sp -= 1;
break;
- case BCGRE: trace("gre\n");
+ case BCGRE: trace("gre");
stack[sp-2] = stack[sp-1] > stack[sp-2];
sp -= 1;
break;
- case BCLEQ: trace("leq\n");
+ case BCLEQ: trace("leq");
stack[sp-2] = stack[sp-1] <= stack[sp-2];
sp -= 1;
break;
- case BCGEQ: trace("geq\n");
+ case BCGEQ: trace("geq");
stack[sp-2] = stack[sp-1] >= stack[sp-2];
sp -= 1;
break;
- case BCJMP: trace("jmp to %d\n", program[pc]);
+ case BCJMP: trace("jmp to %d", program[pc]);
pc = program[pc]-1;
break;
- case BCJMPT: trace("jmpt to %d\n", program[pc]);
+ case BCJMPT: trace("jmpt to %d", program[pc]);
pc = stack[--sp] ? program[pc]-1 : pc+1;
break;
- case BCJMPF: trace("jmpf to %d\n", program[pc]);
+ case BCJMPF: trace("jmpf to %d", program[pc]);
pc = stack[--sp] ? pc+1 : program[pc]-1;
break;
- case BCSERIALAVAIL: trace("SerialAvailable()\n");
+ case BCSERIALAVAIL: trace("SerialAvailable()");
break;
- case BCSERIALPRINT: trace("SerialPrint()\n");
+ case BCSERIALPRINT: trace("SerialPrint()");
break;
- case BCSERIALPRINTLN: trace("SerialPrintln()\n");
+ case BCSERIALPRINTLN: trace("SerialPrintln()");
break;
- case BCSERIALREAD: trace("SerialRead()\n");
+ case BCSERIALREAD: trace("SerialRead()");
break;
- case BCSERIALPARSEINT: trace("SerialParseInt()\n");
+ case BCSERIALPARSEINT: trace("SerialParseInt()");
break;
- case BCANALOGREAD: trace("AnalogRead(%d)\n", program[pc]);
+ case BCANALOGREAD: trace("AnalogRead(%d)", program[pc]);
stack[sp++] = read_apin(program[pc++]);
break;
- case BCANALOGWRITE: trace("AnalogWrite(%d)\n", program[pc]);
+ case BCANALOGWRITE: trace("AnalogWrite(%d)", program[pc]);
write_apin(program[pc++], stack[sp-1]);
sp--;
break;
- case BCDIGITALREAD: trace("DigitalRead(%d)\n", program[pc]);
+ case BCDIGITALREAD: trace("DigitalRead(%d)", program[pc]);
stack[sp++] = read_dpin(program[pc++]);
break;
- case BCDIGITALWRITE: trace("DigitalWrite(%d)\n", program[pc]);
+ case BCDIGITALWRITE: trace("DigitalWrite(%d)", program[pc]);
write_dpin(program[pc++], stack[sp-1]);
sp--;
break;
default:
- trace("unrecognized\n");
- die("Unrecognized command: %d\n", program[pc-1]);
+ trace("unrecognized");
+ die("Unrecognized command: %d", program[pc-1]);
}
}
- debug("Task terminated\n");
+ debug("Task terminated");
}
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();
- break;
- case MSG_SDS_UPD:
- debug("Receiving an sds\n");
- //TODO do something with the return value
- sds_update();
- break;
- case MSG_DEL_TASK:
- debug("Receiving a delete task request\n");
- task_delete();
- break;
- case MSG_GET_TASK:
- debug("Receiving a task\n");
- c = task_register();
- break;
- case '\n':
- break;
- default:
- debug("Unknown message: %X\n", c);
+ if(input_available()){
+ //debug("Receiving input: %c\n", c);
+ switch(bt){
+ case MSG_SDS_SPEC:
+ debug("Receiving an sds");
+ sds_register();
+ break;
+ case MSG_SDS_UPD:
+ debug("Receiving an sds");
+ //TODO do something with the return value
+ sds_update();
+ 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 '\0':
+ break;
+ case '\n':
+ break;
+ default:
+ debug("Unknown message: %X", bt);
+ }
}
}
long cyclestart;
struct task *curtask;
- if(input_available())
- read_message();
+ read_message();
//Run tasks
cyclestart = millis();
// debug("Task %d not scheduled\n", ct);
continue;
}
- debug("Current task to run: %d\n", ct);
+ debug("Current task to run: %d", ct);
run_task(curtask);
curtask->lastrun = cyclestart;
write_byte('\n');
#ifdef STM32F767xx
char s[128] = "";
int main1(void){
+ uint8_t bt;
+ HAL_UART_Receive(&huart3, &bt, 1, 100000);
#else
int main(int argc, char *argv[]){
gargc = argc;
gargv = argv;
#endif
- debug("booting up\r\n");
+ debug("booting up");
//Initialize systems
setup();
while(true){
//Check for new tasks
- debug("loop\r\n");
+// debug("loop\r\n");
loop();
delay(100);
}
break;
if(cs == MAXSDSS)
- die("Trying to add too much sdss...\n");
+ die("Trying to add too much sdss...");
memset(&sdss[cs], 0, sizeof(struct sds));
//Read identifier
//Read value
sdss[cs].value = read16();
- debug("\nReceived sds %d: %d\n", sdss[cs].id, sdss[cs].value);
+ debug("Received sds %d: %d", sdss[cs].id, sdss[cs].value);
sdss[cs].used = true;
}
if(sdss[cs].id == id){
//Read value
sdss[cs].value = read16();
- debug("\nReceived sds update %d: %d\n",
+ debug("Received sds update %d: %d",
sdss[cs].id, sdss[cs].value);
- printf("\nReceived sds update %d: %d\n",
+ printf("Received sds update %d: %d",
sdss[cs].id, sdss[cs].value);
return true;
}
uint8_t cs;
for(cs = 0; cs<MAXSDSS; cs++){
if(sdss[cs].used && sdss[cs].id == id){
- debug("Publish %d=%d\n", sdss[cs].id, sdss[cs].value);
+ debug("Publish %d=%d", sdss[cs].id, sdss[cs].value);
write_byte('u');
write_byte(0);
write_byte(sdss[cs].id);
return;
}
}
- debug("SDS identifier unknown: %d\n", id);
+ debug("SDS identifier unknown: %d", id);
}
int sds_fetch(int id)
for(cs = 0; cs<MAXSDSS; cs++)
if(sdss[cs].used && sdss[cs].id == id)
return sdss[cs].value;
- debug("SDS identifier unknown: %d\n", id);
+ debug("SDS identifier unknown: %d", id);
return 0;
}
return;
}
}
- debug("SDS identifier unknown: %d\n", id);
+ debug("SDS identifier unknown: %d", id);
}
if(!tasks[ct].used)
break;
if(ct == MAXTASKS)
- die("Trying to add too much tasks...\n");
+ die("Trying to add too much tasks...");
memset(&tasks[ct], 0, sizeof(struct task));
//Read interval
tasks[ct].tlen = read16();
if(tasks[ct].tlen > MAXTASKSIZE)
- die("Task is too long: %d\n", tasks[ct].tlen);
+ die("Task is too long: %d", tasks[ct].tlen);
//Read task bytecode
for(unsigned int i = 0; i<tasks[ct].tlen; i++){
tasks[ct].bc[i] = read_byte();
- debug("t[][%i]: 0x%02x %d\n", i,
+ debug("t[][%i]: 0x%02x %d", i,
tasks[ct].bc[i], tasks[ct].bc[i]);
}
//Return the task number for later removal
- debug("Received a task of length %d\n", tasks[ct].tlen);
+ debug("Received a task of length %d", tasks[ct].tlen);
tasks[ct].used = true;
tasks[ct].lastrun = 0L;
+
return ct;
}
| size x == 0 = MTEmpty
= case x.[0] of
'\0' = MTEmpty
+ '\n' = MTEmpty
'm' = MTMessage x
'u' = MTPub (from16bit (x % (1,3))) (x % (3,5))
_ = abort ("Didn't understand message: " +++ join " " [toString (toInt c)\\c<-: x] +++ "\n")
mTaskTask :: (Shared ([MTaskMSGRecv],Bool,[MTaskMSGSend],Bool)) -> Task ()
mTaskTask ch =
-// deviceSelectorNetwork >>= \(p,h)->syncNetworkChannel h p "\n" decode encode` ch ||-
- deviceSelectorSerial >>= \(s,set)->syncSerialChannel s set decode` encode` ch ||-
+ deviceSelectorNetwork >>= \(p,h)->syncNetworkChannel h p "\n" decode encode ch ||-
+// deviceSelectorSerial >>= \(s,set)->syncSerialChannel s set decode encode ch ||-
sendMsg msgs ch ||-
(
(
) >>* [OnAction ActionFinish (always shutDown)]
)
where
- encode` m
- | not (trace_tn (toString (toJSON m))) = undef
- = encode m
-
- decode` m
- | not (trace_tn (toString (toJSON m))) = undef
- = decode m
-
messageShare :: Shared [String]
messageShare = sharedStore "mTaskMessagesRecv" []
(Ok _, iworld) = (DestroyedResult, iworld)
serialDeviceBackgroundTask :: (Shared ([m],Bool,[n],Bool)) (String -> m) (n -> String) !*IWorld -> *IWorld
-serialDeviceBackgroundTask rw en de iworld
+serialDeviceBackgroundTask rw de en iworld
= case read rw iworld of
- (Error e, iworld) = iworld
+ (Error e, iworld) = abort "share couldn't be read"
(Ok (r,rs,s,ss), iworld)
# (Just (TTYd tty)) = iworld.resources
- # tty = writet (map de s) tty
- = case TTYavailable tty of
- (False, tty) = {iworld & resources=Just (TTYd tty)}
+ # tty = writet (map en s) tty
+ # (ml, tty) = case TTYavailable tty of
+ (False, tty) = ([], tty)
(_, tty)
- # (l, tty) = appFst en (TTYreadline tty)
- # iworld = {iworld & resources=Just (TTYd tty)}
- = snd (write (r ++ [l],rs,[],ss) rw iworld)
+ # (l, tty) = TTYreadline tty
+ = ([de l], tty)
+ # iworld = {iworld & resources=Just (TTYd tty)}
+ = case write (r++ml,rs,[],ss) rw iworld of
+ (Error e, iworld) = abort "share couldn't be written"
+ (Ok _, iworld) = case notify rw iworld of
+ (Error e, iworld) = abort "share couldn't be notified"
+ (Ok _, iworld) = iworld
where
writet :: [String] !*TTY -> *TTY
writet [] t = t
- writet [x:xs] t = writet xs (TTYwrite t x)
+ writet [x:xs] t = writet xs (TTYwrite x t)
syncNetworkChannel :: String Int String (String -> m) (n -> String) (Shared ([m],Bool,[n],Bool)) -> Task () | iTask m & iTask n