CLEAN_HOME?=/opt/clean
CLM:=clm
-override CLMFLAGS+=-dynamics -l -no-pie -h 200M -t -nt -lat
+override CLMFLAGS+=-dynamics -l -no-pie -h 200M -nt
CLMLIBS:=\
-I $(CLEAN_HOME)/lib/iTasks-SDK/Patches/Dynamics\
-I $(CLEAN_HOME)/lib/iTasks-SDK/Patches/Generics\
from Data.Monoid import class Semigroup, class Monoid
import mTask
+:: MTaskMessage
+ = MTSds Int String
+ | MTTask Int String
+ | MTPub Int String
+ | MTUpd Int String
+
+instance toString MTaskMessage
+safePrint :: String -> String
+encode :: MTaskMessage -> String
+decode :: String -> MTaskMessage
+
:: BC
= BCNop
| BCLab Int
instance seq ByteCode
instance serial ByteCode
-getSDSBytes :: BCState -> String
-getTaskBytes :: Int String -> String
+toMessages :: Int (String, BCState) -> [MTaskMessage]
toByteVal :: BC -> [Char]
toReadableByteCode :: (ByteCode a b) -> (String, BCState)
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
// 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)}
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]
import iTasks
import mTask
+derive class iTask MTaskMessage
+
Start :: *World -> *World
Start world = startEngine (withShared ([], False, [], False) mTaskTask) world
//Start world = startEngine mTaskTask world
-mTaskTask :: (Shared ([String],Bool,[String],Bool)) -> Task ()
+mTaskTask :: (Shared ([MTaskMessage],Bool,[MTaskMessage],Bool)) -> Task ()
mTaskTask ch =
- syncNetworkChannel "localhost" 8124 "\n" id id ch ||-
- viewSharedInformation "channels" [ViewWith lens] ch ||-
- sendString (makemTask 500 bc) ch @! ()
+ syncNetworkChannel "localhost" 8124 "\n" decode encode ch ||-
+ (
+ sendMsg (hd msgs) ch >>= \_->
+ sendMsg (hd (tl msgs)) ch >>= \_->
+ viewSharedInformation "channels" [ViewWith lens] ch @! ()
+ )
where
- lens :: ([String],Bool,[String],Bool) -> String
- lens (r,_,s,_) = "channels"
+ lens :: ([MTaskMessage],Bool,[MTaskMessage],Bool) -> ([String], [String])
+ lens (r,_,s,_) = (map toString r, map toString s)
+
+ msgs = toMessages 500 (toRealByteCode (unMain bc))
bc :: Main (ByteCode Int Expr)
bc = sds \x=0 In {main = x =. x +. lit 1}
= "t" +++ toString (toChar (to / 265))
+++ toString (toChar (to rem 265)) +++ toString bc +++ "\n"
-sendString :: String (Shared ([String],Bool,[String],Bool)) -> Task ()
-sendString m ch = upd (\(r,rs,s,ss)->(r,rs,s ++ [m],ss)) ch @! ()
+sendMsg :: MTaskMessage (Shared ([MTaskMessage],Bool,[MTaskMessage],Bool)) -> Task ()
+sendMsg m ch
+| not (trace_tn ("\'" +++ toString m +++ "\'")) = undef
+= upd (\(r,rs,s,ss)->(r,rs,s ++ [m],ss)) ch @! ()
syncNetworkChannel :: String Int String (String -> m) (m -> String) (Shared ([m],Bool,[m],Bool)) -> Task () | iTask m
syncNetworkChannel server port msgSeparator decodeFun encodeFun channel