void read_message(int fd_in, int fd_out)
{
//Find next task
- uint8_t c;
+ uint8_t c, ct;
uint16_t tasklen;
- uint8_t ct;
for(ct = 0; ct<MAXTASKS; ct++)
if(!tasks_used[ct])
debug("Receiving input for task %d\n", ct);
read(fd_in, &c, 1);
- if((char) c == 's') {
+ if(c == 's') {
debug("Receiving an sds\n");
- } else if((char) c == 't'){
+ } else if(c == 't'){
read(fd_in, &c, 1);
tasklen = 256*c;
read(fd_in, &c, 1);
debug("Received a task of length %d\n", tasklen);
tasks_used[ct] = true;
} else {
- die("Unknown message: %c?\n", c);
+ debug("Unknown message: %X?\n", c);
}
}
char stack[1024] = {0};
while(pc != plen){
switch(program[pc++]){
- case BCNOP:;
+ case BCNOP:
break;
case BCPUSH:
stack[sp++] = program[pc++];
printf("SerialParseInt()\n");
break;
case BCANALOGREAD:
- printf("AnalogRead()\n");
+ printf("AnalogRead(%d)\n", program[pc++]);
break;
case BCANALOGWRITE:
- printf("AnalogWrite()\n");
+ printf("AnalogWrite(%d)\n", program[pc++]);
+ break;
+ case BCDIGITALREAD:
+ printf("DigitalRead(%d)\n", program[pc++]);
+ break;
+ case BCDIGITALWRITE:
+ printf("DigitalWrite(%d)\n", program[pc++]);
+ break;
default:
- die("Unrecognized command: %X\n", program[--pc]);
+ die("Unrecognized command: %d\n", program[--pc]);
}
}
}
//Check for new tasks
if(input_available(fd_in)){
read_message(fd_in, fd_out);
- exit(1);
}
//Run tasks
for(ct = 0; ct<MAXTASKS; ct++){
debug("Going to execute task %d\n", ct);
run_task(tasks[ct]);
}
+// exit(1);
usleep(10);
}
from Data.Func import $
from Text import class Text(concat,join,toUpperCase), instance Text String
-toByteVal :: BC -> String
-toByteVal a = toString $ toChar $ consIndex{|*|} a
-
-derive gPrint BC, AnalogPin
-derive consIndex BC
-derive consName BC
+toByteVal :: BC -> [Char]
+toByteVal b
+# bt = toByteCode b
+= [bt:case b of
+ (BCPush i) = [toChar i]
+ (BCAnalogRead i) = [toChar i]
+ (BCAnalogRead i) = [toChar i]
+ (BCAnalogWrite i) = [toChar i]
+ (BCDigitalRead i) = [toChar i]
+ (BCDigitalWrite i) = [toChar i]
+ (BCJmp i) = [toChar i]
+ (BCJmpT i) = [toChar i]
+ (BCJmpF i) = [toChar i]
+ _ = []]
+ where
+ toByteCode b = toChar $ consIndex{|*|} b + 1
+
+instance toChar Pin where
+ toChar (Digital p) = toChar $ consIndex{|*|} p + 1
+ toChar (Analog p) = toChar $ consIndex{|*|} p + 1
+
+derive gPrint BC, AnalogPin, Pin, DigitalPin
+derive consIndex BC, Pin
+derive consName BC, Pin
toReadableByteVal :: BC -> String
toReadableByteVal a = printToString a
instance arith ByteCode where
- lit x = BC [BCPush $ toCode x]
+ lit x = BC [BCPush $ 1]
(+.) x y = x <++> y <+-> [BCAdd]
(-.) x y = x <++> y <+-> [BCSub]
(*.) x y = x <++> y <+-> [BCMul]
(>=.) x y = x <++> y <+-> [BCGeq]
instance analogIO ByteCode where
- analogRead p = BC [BCAnalogRead $ toCode p]
- analogWrite p b = b <+-> [BCAnalogWrite $ toCode p]
+ analogRead p = BC [BCAnalogRead $ pin p]
+ analogWrite p b = b <+-> [BCAnalogWrite $ pin p]
instance digitalIO ByteCode where
- digitalRead p = BC [BCDigitalRead $ toCode p]
- digitalWrite p b = b <+-> [BCDigitalWrite $ toCode p]
+ digitalRead p = BC [BCDigitalRead $ pin p]
+ digitalWrite p b = b <+-> [BCDigitalWrite $ pin p]
instance If ByteCode Stmt Stmt Stmt where If b t e = BCIfStmt b t e
instance If ByteCode Stmt e Stmt where If b t e = BCIfStmt b t e
zero = {a=()}
toRealByteCode :: (ByteCode a Expr) -> String
-toRealByteCode (BC x) = concat $ map toByteVal x
+toRealByteCode (BC x) = concat $ map (toString o toByteVal) x
//Start :: ByteCode Int Expr
//Start = (lit 36 +. lit 42) +. lit 84
+to16bit :: Int -> String
+to16bit i = toString (toChar (i/265)) +++ toString (toChar (i rem 265))
+
//Run test programma en pretty print
-Start = toRealByteCode $ If (lit True) (analogRead A1) (analogRead A0)
+Start :: String
+Start = "t" +++ to16bit (size b) +++ b
+//Start :: ByteCode Int Expr
+//Start = bc
+ where
+ bc = If (lit True) (analogRead A1) (analogRead A0)
+ b = toRealByteCode bc
//Start :: ByteCode Int Expr
//Start = If (lit True) (analogRead A1) (analogRead A0)
//Start = If ((lit 36) ==. (lit 42)) (noOp) (noOp)