import qualified Data.Map as DM
import Text.Encodings.Base64
-encode :: MTaskMessage -> String
+encode :: MTaskMSGSend -> String
encode (MTSds i v) = "s" +++ to16bit i +++ v +++ "\n"
encode (MTTask to data) = "t" +++ to16bit to +++ to16bit (size data) +++ data +++ "\n"
-encode (MTPub i v) = "u" +++ to16bit i +++ v +++ "\n"
encode (MTUpd i v) = "u" +++ to16bit i +++ v +++ "\n"
-encode MTEmpty = ""
-decode :: String -> MTaskMessage
+decode :: String -> MTaskMSGRecv
decode x
| size x == 0 = MTEmpty
= case x.[0] of
'\0' = MTEmpty
- 'u' = MTUpd (from16bit (x % (1,3))) (x % (3,5))
+ 'm' = MTMessage x
+ 'u' = MTPub (from16bit (x % (1,3))) (x % (3,5))
_ = abort ("Didn't understand message: " +++ join " " [toString (toInt c)\\c<-: x] +++ "\n")
safePrint :== toString o toJSON
-derive gPrint MTaskMessage
-instance toString MTaskMessage where
+instance toString MTaskMSGSend 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
+
+instance toString MTaskMSGRecv where
+ toString (MTPub i v) = "Publish id: " +++ toString i
+ +++ " value " +++ safePrint v
toString MTEmpty = "Empty message"
bclength :: BC -> Int
digitalRead p = retrn [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 e 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
-instance If ByteCode x y Expr where If b t e = BCIfStmt b t e
+//instance If ByteCode Stmt Stmt Stmt where If b t e = BCIfStmt b t e
+//instance If ByteCode e 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
+instance If ByteCode x y Stmt where If b t e = BCIfStmt b t e
instance IF ByteCode where
IF b t e = BCIfStmt b t e
(?) b t = BCIfStmt b t $ retrn []
// where
// bc :: ByteCode Int Expr
// bc = (lit 36 +. lit 42) +. lit 44
-toMessages :: Int (String, BCState) -> [MTaskMessage]
-toMessages interval (bytes, {sdss}) = [MTSds i (toString b)\\(i,b)<-sdss] ++ [MTTask interval bytes]
+toMessages :: Int (String, BCState) -> ([MTaskMSGSend], BCState)
+toMessages interval (bytes, st=:{sdss}) = ([MTSds i (toString b)\\(i,b)<-sdss] ++ [MTTask interval bytes], st)
+
+toSDSUpdate :: Int Int -> [MTaskMSGSend]
+toSDSUpdate i v = [MTUpd i (to16bit v)]
Start = toMessages 500 $ toRealByteCode (unMain bc)
//Start = fst $ toReadableByteCode $ unMain bc