add sending and encoding options
[mTask.git] / mTaskInterpret.icl
index 0d4de17..760281c 100644 (file)
@@ -21,6 +21,33 @@ from Text import class Text(concat,join,toUpperCase), instance Text String
 
 import Text.Encodings.Base64
 
+encode :: MTaskMessage -> String
+encode (MTSds i v) = "s" +++ to16bit i +++ v
+encode (MTTask to data) = "t" +++ to16bit to +++ toString (size data) +++ data
+encode (MTPub i v) = "u" +++ to16bit i +++ v
+encode (MTUpd i v) = "u" +++ to16bit i +++ v
+
+decode :: String -> MTaskMessage
+decode x = case x.[0] of
+       'u' = MTUpd (toInt x.[1]) (x % (2,4))
+       _ = abort ("Didn't understand message: " +++ x)
+
+safePrint :: String -> String
+safePrint s = join " " [saf c\\c<-:s]
+       where
+               saf c = "\x" +++ toString (toInt c)
+
+derive gPrint MTaskMessage
+instance toString MTaskMessage where
+       toString (MTSds i v) = "Sds id: " +++ toString i
+               +++ " value " +++ safePrint v
+       toString (MTTask to data) = "Task timeout: " +++ toString to
+               +++ " data " +++ safePrint data
+       toString (MTPub i v) = "Publish id: " +++ toString i
+               +++ " value " +++ safePrint v
+       toString (MTUpd i v) = "Update id: " +++ toString i
+               +++ " value " +++ safePrint v
+
 toByteVal :: BC -> [Char]
 toByteVal b
 # bt = toChar $ consIndex{|*|} b + 1
@@ -184,18 +211,12 @@ toReadableByteCode x
 //     where
 //             bc :: ByteCode Int Expr
 //             bc = (lit 36 +. lit 42) +. lit 44
-getSDSBytes :: BCState -> String
-getSDSBytes {sdss} = concat $ map sd sdss
-       where sd (i, v) = "s" +++ toString (toChar i) +++ toString v +++ "\n"
-
-getTaskBytes :: Int String -> String
-getTaskBytes i b = "t" +++ to16bit i +++ to16bit (size b) +++ b 
-//
-Start = getSDSBytes (snd bc`) +++ getTaskBytes 400 (fst bc`)
+toMessages :: Int (String, BCState) -> [MTaskMessage]
+toMessages interval (bytes, {sdss}) = [MTSds i (toString b)\\(i,b)<-sdss] ++ [MTTask interval bytes]
+
+Start = toMessages 500 $ toRealByteCode (unMain bc)
 //Start = fst $ toReadableByteCode $ unMain bc
        where
-               bc` = toRealByteCode (unMain bc)
-//             bc :: Main (ByteCode Int Stmt)
                bc = sds \x=5 In 
                        sds \y=4 In
                        {main = If (y ==. lit 0) (pub x) (x =. x *. y :. y =. y -. lit 1)}
@@ -205,15 +226,6 @@ pub x = fmp makePub x
 
 to16bit :: Int -> String
 to16bit i = toString (toChar (i/265)) +++ toString (toChar (i rem 265))
-//
-////Run test programma en pretty print
-////Start :: String
-////Start = "t" +++ to16bit (size b) +++ b
-//Start :: Main (ByteCode Int Expr)
-//Start = bc
-//     where
-//             bc = sds \x=43 In {main = If (x ==. lit 42) (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)
+
+from16bit :: String -> Int
+from16bit s = toInt s.[0] * 265 + toInt s.[1]