+from Text import class Text(subString,lpad,concat,toUpperCase), instance Text String
+import qualified Text
+import Text.JSON
+
+import Control.Monad.RWST
+import Control.Monad.Identity
+import Control.Monad
+import Control.Applicative
+import Data.Functor
+import Data.Either
+
+import Data.Array
+import qualified Data.Map as DM
+import qualified Data.List as DL
+import Text.Encodings.Base64
+
+import Tasks.Examples
+
+instance == BCValue where (==) a b = toByteCode a == toByteCode b
+
+encode :: MTaskMSGSend -> String
+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 +++ toByteCode v +++ "\n"
+encode (MTUpd i v) = "u" +++ to16bit i +++ toByteCode v +++ "\n"
+encode (MTSpec) = "c\n"
+encode (MTShutdown) = "h\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 (fromByteCode x) (fromByteCode (x % (2, size x)))
+ 'd' = MTTaskDelAck $ fromByteCode x
+ 'm' = MTMessage 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")
+
+safePrint :== toString o toJSON
+
+instance toString MTaskInterval where
+ toString OneShot = "One shot"
+ toString (OnInterrupt i) = "Interrupt: " +++ toString i
+ toString (OnInterval i) = "Every " +++ toString i +++ "ms"
+
+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 (MTTaskDel i) = "Task delete request: " +++ toString i
+ toString (MTUpd i v) = "Update id: " +++ toString i
+ +++ " value " +++ safePrint v
+ toString (MTSpec) = "Spec request"
+ toString (MTShutdown) = "Shutdown request"
+
+instance toString MTaskMSGRecv where
+ toString (MTTaskAck i mem) = "Task added with id: " +++ toString i
+ +++ " free memory: " +++ toString mem
+ toString (MTTaskDelAck i) = "Task deleted with id: " +++ toString i
+ toString (MTSDSAck i) = "SDS added with id: " +++ toString i
+ 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"
+
+toByteVal :: BC -> String
+toByteVal b = {toChar $ consIndex{|*|} b} +++
+ case b of
+ (BCPush (BCValue i)) = toByteCode i
+ (BCLab i) = {toChar i}
+ (BCSdsStore i) = to16bit i.sdsi
+ (BCSdsFetch i) = to16bit i.sdsi
+ (BCSdsPublish i) = to16bit i.sdsi
+ (BCAnalogRead i) = {toChar $ consIndex{|*|} i}
+ (BCAnalogWrite i) = {toChar $ consIndex{|*|} i}
+ (BCDigitalRead i) = {toChar $ consIndex{|*|} i}
+ (BCDigitalWrite i) = {toChar $ consIndex{|*|} i}
+ (BCJmp i) = {toChar i}
+ (BCJmpT i) = {toChar i}
+ (BCJmpF i) = {toChar i}
+ _ = ""
+
+parseBCValue :: Char String -> BCValue
+parseBCValue c s = case c of
+ 'b' = BCValue $ castfbc True s
+ 'i' = BCValue $ castfbc 0 s
+ 'l' = BCValue $ castfbc (L 0) s
+ 'c' = BCValue $ castfbc ('0') s
+ 'B' = BCValue $ castfbc (NoButton) s
+ 'L' = BCValue $ castfbc (LED1) s
+
+castfbc :: a -> (String -> a) | mTaskType a
+castfbc _ = fromByteCode
+
+instance toByteCode Bool where toByteCode b = {'b',if b '\x01' '\0'}
+instance toByteCode Int where toByteCode n = {'i',toChar $ n/256,toChar $ n rem 256}
+instance toByteCode Long where toByteCode (L n) = {'l',toChar $ n/256,toChar $ n rem 256}
+instance toByteCode Char where toByteCode c = {'c',c}
+instance toByteCode String where toByteCode s = abort $ "Undef on toBytecode String" +++ s
+instance toByteCode Button where toByteCode s = {'B',toChar $ consIndex{|*|} s}
+instance toByteCode UserLED where toByteCode s = {'L',toChar $ consIndex{|*|} s}
+instance toByteCode BCValue where toByteCode (BCValue v) = toByteCode v
+
+instance fromByteCode Bool where fromByteCode s = s.[1] == '\x01'
+instance fromByteCode Int where fromByteCode s = (toInt s.[1])*256 + toInt s.[2]
+instance fromByteCode Long where fromByteCode s = L $ fromByteCode s
+instance fromByteCode Char where fromByteCode s = s.[1]
+instance fromByteCode String where fromByteCode s = abort $ "Undef on fromBytecode String" +++ s
+instance fromByteCode Button where fromByteCode s = conses{|*|} !! toInt s.[1]
+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 (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
+ toByteCode (OnInterrupt i) = {toChar $ i/256 bitor 127, toChar $ i rem 256}
+instance fromByteCode MTaskInterval