module System.Hardware.Arduino.SamplePrograms.SevenSegment where
import Control.Monad (forever)
import Control.Monad.Trans (liftIO)
import Data.Bits (testBit)
import Data.Word (Word8)
import System.IO (hSetBuffering, stdin, BufferMode(NoBuffering))
import System.Hardware.Arduino
import System.Hardware.Arduino.Parts.ShiftRegisters
import System.Hardware.Arduino.Parts.SevenSegmentCodes
sr :: SR_74HC595
sr :: SR_74HC595
sr = SR_74HC595 { serial :: Pin
serial = Word8 -> Pin
digital Word8
8
, nEnable :: Pin
nEnable = Word8 -> Pin
digital Word8
9
, rClock :: Pin
rClock = Word8 -> Pin
digital Word8
10
, sClock :: Pin
sClock = Word8 -> Pin
digital Word8
11
, nClear :: Pin
nClear = Word8 -> Pin
digital Word8
12
, mbBits :: Maybe [Pin]
mbBits = Maybe [Pin]
forall a. Maybe a
Nothing
}
sevenSegment :: IO ()
sevenSegment :: IO ()
sevenSegment = Bool -> FilePath -> Arduino () -> IO ()
withArduino Bool
False FilePath
"/dev/cu.usbmodemFD131" (Arduino () -> IO ()) -> Arduino () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
SR_74HC595 -> Arduino ()
forall a. ShiftRegister a => a -> Arduino ()
initialize SR_74HC595
sr
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
$ do Handle -> BufferMode -> IO ()
hSetBuffering Handle
stdin BufferMode
NoBuffering
FilePath -> IO ()
putStrLn FilePath
"Seven-Segment-Display demo."
FilePath -> IO ()
putStrLn FilePath
"For each key-press, we will try to display it as a 7-segment character."
FilePath -> IO ()
putStrLn FilePath
"If there is no good mapping (which is common), we'll just display a dot."
FilePath -> IO ()
putStrLn FilePath
""
FilePath -> IO ()
putStrLn FilePath
"Press-keys to be shown on the display, Ctrl-C to quit.."
Arduino () -> Arduino ()
forall (f :: * -> *) a b. Applicative f => f a -> f b
forever Arduino ()
repl
where pushWord :: a -> Arduino ()
pushWord a
w = do (Bool -> Arduino ()) -> [Bool] -> Arduino ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (SR_74HC595 -> Bool -> Arduino ()
forall a. ShiftRegister a => a -> Bool -> Arduino ()
push SR_74HC595
sr) [a
w a -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
`testBit` Int
i | Int
i <- [Int
0..Int
7]]
SR_74HC595 -> Arduino ()
forall a. ShiftRegister a => a -> Arduino ()
store SR_74HC595
sr
repl :: Arduino ()
repl = do c <- IO Char -> Arduino Char
forall a. IO a -> Arduino a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO Char
getChar
case char2SS c of
Just Word8
w -> Word8 -> Arduino ()
forall {a}. Bits a => a -> Arduino ()
pushWord Word8
w
Maybe Word8
Nothing -> Word8 -> Arduino ()
forall {a}. Bits a => a -> Arduino ()
pushWord (Word8
0x01::Word8)