-mTaskTask :: Int (Shared ([MTaskMessage],Bool,[MTaskMessage],Bool)) -> Task ()
-mTaskTask port ch =
- syncNetworkChannel "localhost" port "\n" decode encode ch ||-
- (
- sendMsg msgs ch >>= \_->
- viewSharedInformation "channels" [ViewWith lens] ch @! ()
- ) >>* [OnAction ActionFinish (always shutDown)]
+bc :: Main (ByteCode () Stmt)
+bc = sds \x=1 In sds \pinnetje=1 In {main =
+ IF (digitalRead D3) (
+ x =. x +. lit 1 :.
+ pub x
+ ) (
+ noOp
+ ) :.
+ IF (pinnetje ==. lit 1) (
+ ledOn LED1
+ ) (
+ IF (pinnetje ==. lit 2) (
+ ledOn LED2
+ ) (
+ ledOn LED3
+ )
+ )}
+
+bc2 :: UserLED -> Main (ByteCode () Stmt)
+bc2 d = {main = ledOn d}
+
+bc3 :: UserLED -> Main (ByteCode () Stmt)
+bc3 d = {main = ledOff d}
+
+
+withDevice :: ((Shared ([MTaskMSGRecv],[MTaskMSGSend],Bool)) -> Task a) -> Task a | iTask a
+withDevice t = withShared ([], [], False) \ch->
+ enterInformation "Type" []
+ >>= \ty->case ty of
+ TCP = (enterInformation "Host" [] -&&- enterInformation "Port" [])
+ >>= \(port,host)->t ch -|| syncNetworkChannel host port ch
+ Serial = accWorld getDevices
+ >>= \dl->(enterChoice "Device" [] dl -&&- updateInformation "Settings" [] zero)
+ >>= \(dev,set)->t ch -|| syncSerialChannel dev set ch
+ where
+ getDevices :: !*World -> *(![String], !*World)
+ getDevices w = case readDirectory "/dev" w of
+ (Error (errcode, errmsg), w) = abort errmsg
+ (Ok entries, w) = (map ((+++) "/dev/") (filter isTTY entries), w)
+ where
+ isTTY s = not (isEmpty (filter (flip startsWith s) prefixes))
+ prefixes = ["ttyS", "ttyACM", "ttyUSB", "tty.usbserial"]
+
+mTaskTask :: Task ()
+mTaskTask = let (msgs, sdsShares) = makeMsgs 1000 bc in
+ withDevice \ch->
+ sendMsg msgs ch
+ ||- processMessages ch messageShare sdsShares
+ ||- forever (enterChoice "Choose led to enable" [] [LED1, LED2, LED3]
+ >>= \p->sendMsg (fst (makeMsgs 0 (bc2 p))) ch)
+ ||- forever (enterChoice "Choose led to disable" [] [LED1, LED2, LED3]
+ >>= \p->sendMsg (fst (makeMsgs 0 (bc3 p))) ch)
+ ||- viewSharedInformation "channels" [ViewWith lens] ch
+ ||- viewSharedInformation "messages" [] messageShare
+ ||- viewSh sdsShares ch
+ >>* [OnAction ActionFinish (always shutDown)]