{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-}
module System.Hardware.Arduino.SamplePrograms.Counter where
import Control.Monad.Trans (liftIO)
import System.Hardware.Arduino
counter :: IO ()
counter :: IO ()
counter = Bool -> FilePath -> Arduino () -> IO ()
withArduino Bool
False FilePath
"/dev/cu.usbmodemFD131" (Arduino () -> IO ()) -> Arduino () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
Pin -> PinMode -> Arduino ()
setPinMode Pin
led PinMode
OUTPUT
Pin -> PinMode -> Arduino ()
setPinMode Pin
bUp PinMode
INPUT
Pin -> PinMode -> Arduino ()
setPinMode Pin
bDown PinMode
INPUT
Int -> Arduino ()
forall {t} {b}. (Show t, Eq t, Num t) => t -> Arduino b
update (Int
0::Int)
where bUp :: Pin
bUp = Word8 -> Pin
digital Word8
4
bDown :: Pin
bDown = Word8 -> Pin
digital Word8
2
led :: Pin
led = Word8 -> Pin
digital Word8
13
update :: t -> Arduino b
update t
curVal = do
IO () -> Arduino ()
forall a. IO a -> Arduino a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> Arduino ()) -> IO () -> Arduino ()
forall a b. (a -> b) -> a -> b
$ t -> IO ()
forall a. Show a => a -> IO ()
print t
curVal
Pin -> Bool -> Arduino ()
digitalWrite Pin
led (t
curVal t -> t -> Bool
forall a. Eq a => a -> a -> Bool
== t
0)
~[Bool
up, Bool
down] <- [Pin] -> Arduino [Bool]
waitAnyHigh [Pin
bUp, Pin
bDown]
let newVal :: t
newVal = case (Bool
up, Bool
down) of
(Bool
True, Bool
True) -> t
curVal
(Bool
True, Bool
False) -> t
curValt -> t -> t
forall a. Num a => a -> a -> a
+t
1
(Bool
False, Bool
True) -> t
curValt -> t -> t
forall a. Num a => a -> a -> a
-t
1
(Bool
False, Bool
False) -> t
curVal
t -> Arduino b
update t
newVal