summaryrefslogtreecommitdiff
path: root/assignment-13/uFPL.icl
diff options
context:
space:
mode:
Diffstat (limited to 'assignment-13/uFPL.icl')
-rw-r--r--assignment-13/uFPL.icl79
1 files changed, 14 insertions, 65 deletions
diff --git a/assignment-13/uFPL.icl b/assignment-13/uFPL.icl
index 607eabd..f3b382b 100644
--- a/assignment-13/uFPL.icl
+++ b/assignment-13/uFPL.icl
@@ -22,9 +22,9 @@ import Data.Map
import Data.Tuple
from Text import class Text(concat), instance Text String
-import uFPL.Arduino
import uFPL.Bootstrap
import uFPL.C
+import uFPL.Examples
import uFPL.Sim
import uFPL.Util
@@ -86,6 +86,7 @@ where
allShares` (t >>> rs) = foldr1 append [allShares` t:map allShares` rs]
allShares` (SetCursor (c,r)) = append (allShares` c) (allShares` r)
allShares` (Print v) = allShares` v
+ allShares` (PrintS _) = NoShares
instance allShares NamedRule
where
@@ -104,6 +105,7 @@ where
allTriggers (t >>> rs) = [t:allTriggers rs]
allTriggers (SetCursor _) = []
allTriggers (Print _) = []
+ allTriggers (PrintS _) = []
instance allTriggers NamedRule
where
@@ -184,6 +186,7 @@ where
gen (t >>> rs) = CBIf (gen t) (gen rs) [] Nothing
gen (SetCursor (c,r)) = CBExpr (CEApp "lcd.setCursor" [gen c, gen r])
gen (Print e) = CBExpr (CEApp "lcd.print" [gen e])
+ gen (PrintS s) = CBExpr (CEApp "lcd.print" [CEString s])
instance gen [r] CBody | gen r CBody
where
@@ -193,16 +196,14 @@ where
instance gen NamedRule CFun
where
gen (name :=: rs) =
- { params = []
- , body = gen rs
+ { body = gen rs
, type = CTVoid
, name = "t" +++ name
}
fun_setup :: [NamedRule] -> CFun
fun_setup rs =
- { params = []
- , type = CTVoid
+ { type = CTVoid
, name = "setup"
, body =
CBExpr (CEApp "lcd.begin" [CEInt 16, CEInt 2]) `seq`
@@ -214,17 +215,15 @@ fun_setup rs =
fun_loop :: [NamedRule] -> CFun
fun_loop rs =
- { params = []
- , type = CTVoid
+ { type = CTVoid
, name = "loop"
, body =
foldr (`seq`) (CBExpr $ CEApp "system" []) [CBExpr $ CEApp ("t" +++ r) [] \\ r :=: _ <- rs]
}
-fun_system :: [NamedRule] -> CFun
-fun_system rs =
- { params = []
- , type = CTVoid
+fun_system :: CFun
+fun_system =
+ { type = CTVoid
, name = "system"
, body =
CBAssign "int val" (CEApp "analogRead" [CEGlobal "A0"]) `seq`
@@ -243,12 +242,12 @@ where
gen r = combinePrograms zero
{ bootstrap = ""
, globals = sharesMap gen (allShares r)
- , funs = [gen r, fun_setup [r], fun_loop [r], fun_system [r]]
+ , funs = [gen r, fun_setup [r], fun_loop [r], fun_system]
}
instance gen [NamedRule] CProg
where
- gen rs = combinePrograms {zero & funs=[fun_setup rs, fun_loop rs, fun_system rs]} $
+ gen rs = combinePrograms {zero & funs=[fun_setup rs, fun_loop rs, fun_system]} $
foldr (combinePrograms o gen) zero rs
instance toString Display
@@ -322,60 +321,10 @@ where
run (t >>> rs) = \st -> evalTrigger t st >>= \(b,st) -> if b (run rs) Just st
run (SetCursor (c,r)) = \st -> eval c st >>= \c -> eval r st >>= \r -> Just {State | st & display.cursor=(c,r)}
run (Print e) = \st -> eval e st >>= \e -> Just {State | st & display=display (toString e) st.State.display}
+ run (PrintS s) = \st -> Just {State | st & display=display s st.State.display}
instance run NamedRule
where
run (_ :=: r) = run r
-Start w = simulate example_countdown w
-
-example_score :: (String, [NamedRule])
-example_score = ("score",
- "a" :=: pressed b0 >>> [scorea <# scorea +. lit 1]
- ||| "b" :=: pressed b1 >>> [scoreb <# scoreb +. lit 1]
- ||| "r" :=: pressed b2 >>> [scorea <# lit 0, scoreb <# lit 0]
- ||| "print" :=: (Change scorea ?| Change scoreb) >>> (
- SetCursor (lit 0, lit 0) :.
- Print scorea :.
- Print (lit '-') :.
- Print scoreb
- ))
-where
- scorea = rwInt "scorea" 0
- scoreb = rwInt "scoreb" 0
-
-example_countdown :: (String, [NamedRule])
-example_countdown = ("countdown",
- "time" :=:
- (Change millis >>> [When (millis -. DELAY >. counter) (
- counter <# counter +. DELAY :.
- seconds <# running ? (seconds -. lit 1, seconds)
- )]) :.
- seconds ?= lit -1 >>> (
- minutes <# minutes -. lit 1 :.
- seconds <# lit 59
- ) :.
- minutes ?= lit -1 >>> (
- running <# false :.
- minutes <# lit 0 :.
- seconds <# lit 0
- ) :.
- Change seconds >>> (
- SetCursor (lit 0, lit 0) :.
- Print minutes :.
- Print (lit ':') :.
- Print seconds
- )
- ||| "setsec" :=: pressed b0 >>> [seconds <# seconds +. lit 1]
- ||| "setmin" :=: pressed b1 >>> [minutes <# minutes +. lit 1]
- ||| "on_off" :=: pressed b2 >>> [running <# running ? (false, true)]
- ||| "reset" :=: pressed b3 >>> (
- seconds <# lit 0 :.
- minutes <# lit 0
- ))
-where
- running = rwBool "running" False
- minutes = rwInt "minutes" 2
- seconds = rwInt "seconds" 0
- counter = rwULong "counter" 0 // If set to 0, this will overflow on first iteration
- DELAY = lit 1000
+Start w = simulate example_score w