add back device specific capabilities
[mTask.git] / mTaskInterpret.icl
index e603f82..bd5572a 100644 (file)
@@ -41,26 +41,25 @@ import Text.Encodings.Base64
 import Tasks.Examples
 
 encode :: MTaskMSGSend -> String
-encode (MTTask to data) = "t" +++ tob +++ to16bit (size data) +++ data +++ "\n"
-       where 
-               tob = case to of
-                       OneShot = to16bit 0
-                       OnInterval i = to16bit i
-                       OnInterrupt _ = abort "Interrupts not implemented yet"
+encode (MTTask to data) = "t" +++ toByteCode to +++ to16bit (size data) +++ data +++ "\n"
 encode (MTTaskDel i) = "d" +++ to16bit i +++ "\n"
 encode (MTSds i v) = "s" +++ to16bit i +++ v +++ "\n"
 encode (MTUpd i v) = "u" +++ to16bit i +++ v +++ "\n"
+encode (MTSpec) = "c\n"
 
+import StdDebug
 decode :: String -> MTaskMSGRecv
 decode x
+| not (trace_tn ("decoding: " +++ toString (toJSON x))) = undef
 | size x == 0 = MTEmpty
 = case x.[0] of
-       't' = MTTaskAck (from16bit (x % (1,3)))
-       'd' = MTTaskDelAck (from16bit (x % (1,3)))
+       't' = MTTaskAck $ fromByteCode x
+       'd' = MTTaskDelAck $ fromByteCode x
        'm' = MTMessage x
-       's' = MTSDSAck (from16bit (x % (1,3)))
-       'a' = MTSDSDelAck (from16bit (x % (1,3)))
-       'p' = MTPub (from16bit (x % (1,3))) (x % (3,size x))
+       's' = MTSDSAck $ fromByteCode x
+       'a' = MTSDSDelAck $ fromByteCode x
+       'p' = MTPub (fromByteCode x) $ fromByteCode $ x % (3,size x)
+       'c'     = MTDevSpec $ fromByteCode (x % (1, size x))
        '\0' = MTEmpty
        '\n' = MTEmpty
        _ = MTMessage x//abort ("Didn't understand message: " +++ join " " [toString (toInt c)\\c<-: x] +++ "\n")
@@ -88,6 +87,7 @@ instance toString MTaskMSGRecv where
        toString (MTSDSDelAck i) = "SDS deleted with id: " +++ toString i
        toString (MTPub i v) = "Publish id: " +++ toString i
                +++ " value " +++ safePrint v
+       toString (MTDevSpec mt) = "Specification: " +++ printToString mt
        toString (MTMessage m) = m
        toString MTEmpty = "Empty message"
 
@@ -139,7 +139,7 @@ instance fromByteCode UserLED where fromByteCode s = conses{|*|} !! toInt s.[1]
 instance fromByteCode BCValue where fromByteCode s = parseBCValue s.[0] s
 
 instance toByteCode MTaskInterval where
-       toByteCode OneShot = toByteCode 0
+       toByteCode OneShot = toByteCode (OnInterval 0)
        //Intervals have the first bit 0 and the rest is a 15 bit unsigned int
        toByteCode (OnInterval i) = {toChar $ i/256 bitand 127, toChar $ i rem 256}
        //Intervals have the first bit 1 and the rest is a 15 bit unsigned int
@@ -152,8 +152,17 @@ instance fromByteCode MTaskInterval
                        0 = OneShot
                        i = OnInterval i
                = OnInterrupt $ fromByteCode s bitand 127
-
-derive gPrint Long, UserLED, Button, AnalogPin, DigitalPin, PinMode, Pin, BC
+instance fromByteCode MTaskDeviceSpec where
+       fromByteCode s = let c = toInt s.[0] in
+               {MTaskDeviceSpec
+               |haveLed=(c bitand 1) > 0
+               ,haveAio=(c bitand 2) > 0
+               ,haveDio=(c bitand 4) > 0
+               ,maxTask=from16bit $ s % (1,3)
+               ,maxSDS=from16bit $ s % (3,5)
+               }
+
+derive gPrint Long, UserLED, Button, AnalogPin, DigitalPin, PinMode, Pin, BC, MTaskDeviceSpec
 derive class gCons BC
 
 consIndex{|BCValue|} _ = 0
@@ -182,7 +191,7 @@ gDefault{|BCValue|} = BCValue 0
 gEq{|BCValue|} (BCValue e) (BCValue f) = toByteCode e == toByteCode f
 
 derive class gCons Long, UserLED, Button, AnalogPin, DigitalPin, PinMode, Pin
-derive class iTask UserLED, Long, Pin, Button, AnalogPin, DigitalPin, PinMode
+derive class iTask UserLED, Long, Pin, Button, AnalogPin, DigitalPin, PinMode, MTaskDeviceSpec
 
 op2 :: (ByteCode a p1) (ByteCode a p2) BC -> ByteCode b Expr
 op2 (BC x) (BC y) bc = BC $ x >>| y >>| tell [bc]