hArduino 0.6 → 0.7
raw patch · 6 files changed
+138/−23 lines, 6 files
Files
- CHANGES.md +7/−2
- System/Hardware/Arduino.hs +3/−13
- System/Hardware/Arduino/Comm.hs +0/−1
- System/Hardware/Arduino/Parts/LCD.hs +1/−1
- System/Hardware/Arduino/SamplePrograms/NumGuess.hs +122/−0
- hArduino.cabal +5/−6
CHANGES.md view
@@ -1,13 +1,18 @@ * Hackage: (http://hackage.haskell.org/package/hArduino) * GitHub: (http://leventerkok.github.com/hArduino) -* Latest Hackage released version: 0.6+* Latest Hackage released version: 0.7 +### Version 0.7, 2013-11-09+ * Export LCD type, for ease of programming+ * Added the number guessing game using the OSEpp shield.+ Thanks to David Palmer for lending me his shield to play with!+ ### Version 0.6, 2013-03-08 * Make hArduino Windows friendly by removing dependence on the unix package. Thanks to Andriy Drozdyuk for pointing- out the Windows compilation issue. (Tested on Windows XP.)+ out the Windows compilation issue. (Tested on Windows 7.) ### Version 0.5, 2013-03-07
System/Hardware/Arduino.hs view
@@ -6,20 +6,10 @@ -- Maintainer : erkokl@gmail.com -- Stability : experimental ----- (The hArduino library is hosted at <http://leventerkok.github.com/hArduino/>.--- Comments, bug reports, and patches are always welcome.)------ hArduino: Control Arduino from Haskell, using the Firmata protocol.------ The hArduino library allows construction of Haskell programs that control--- Arduino boards that are running the (freely available) Firmata program. Note--- that hArduino does /not/ allow you to run arbitrary Haskell code on the--- Arduino! It simply allows you to control a board from Haskell, where you--- can exchange information with the board, send/receive commands from other--- peripherals connected, etc.+-- hArduino allows Haskell programs to control Arduino boards (<http://www.arduino.cc>)+-- and peripherals, using the Firmata protocol (<http://firmata.org>). ----- See <http://www.youtube.com/watch?v=PPa3im44t2g> for a short video (4m29s)--- of the blink example.+-- For details, see: <http://leventerkok.github.com/hArduino>. ------------------------------------------------------------------------------- module System.Hardware.Arduino ( -- * Running the controller
System/Hardware/Arduino/Comm.hs view
@@ -9,7 +9,6 @@ -- Basic serial communication routines ------------------------------------------------------------------------------- -{-# LANGUAGE CPP #-} {-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE ScopedTypeVariables #-} module System.Hardware.Arduino.Comm where
System/Hardware/Arduino/Parts/LCD.hs view
@@ -18,7 +18,7 @@ {-# LANGUAGE NamedFieldPuns #-} module System.Hardware.Arduino.Parts.LCD( -- * LCD types and registration- LCDController(..), lcdRegister+ LCD, LCDController(..), lcdRegister -- * Writing text on the LCD , lcdClear, lcdWrite -- * Moving the cursor
+ System/Hardware/Arduino/SamplePrograms/NumGuess.hs view
@@ -0,0 +1,122 @@+-----------------------------------------------------------------------------+-- |+-- Module : System.Hardware.Arduino.SamplePrograms.NumGuess+-- Copyright : (c) Levent Erkok+-- License : BSD3+-- Maintainer : erkokl@gmail.com+-- Stability : experimental+--+-- Simple number guessing game on the OSEPP Keyboard shield.+--+-- /Thanks to David Palmer for lending me his OSEPP shield to play with!/+-------------------------------------------------------------------------------++module System.Hardware.Arduino.SamplePrograms.NumGuess where++import System.Hardware.Arduino+import System.Hardware.Arduino.Parts.LCD++-- | The OSepp LCD Shield is a 16x2 LCD using a Hitachi Controller+-- Furthermore, it has backlight, and 5 buttons. The hook-up is+-- quite straightforward, using our existing Hitachi44780 controller+-- as an example. More information on this shield can be found at:+--+-- <http://osepp.com/products/shield-arduino-compatible/16x2-lcd-display-keypad-shield/>+osepp :: LCDController+osepp = Hitachi44780 { lcdRS = digital 8+ , lcdEN = digital 9+ , lcdD4 = digital 4+ , lcdD5 = digital 5+ , lcdD6 = digital 6+ , lcdD7 = digital 7+ , lcdRows = 2+ , lcdCols = 16+ , dotMode5x10 = False+ }++-- | There are 5 keys on the OSepp shield.+data Key = KeyRight+ | KeyLeft+ | KeyUp+ | KeyDown+ | KeySelect++-- | Initialize the shield. This is essentially simply registering the+-- lcd with the HArduino library. In addition, we return two values to+-- the user:+--+-- * A function to control the back-light+--+-- * A function to read (if any) key-pressed+initOSepp :: Arduino (LCD, Bool -> Arduino (), Arduino (Maybe Key))+initOSepp = do lcd <- lcdRegister osepp+ let button = analog 0+ light = digital 10+ setPinMode button ANALOG+ setPinMode light OUTPUT+ -- Analog values obtained from OSEPP site, seems reliable+ let threshHolds = [ (KeyRight, 30)+ , (KeyUp, 150)+ , (KeyDown, 360)+ , (KeyLeft, 535)+ , (KeySelect, 760)+ ]+ backLight = digitalWrite light+ readButton = do val <- analogRead button+ let walk [] = Nothing+ walk ((k, t):keys)+ | val < t = Just k+ | True = walk keys+ return $ walk threshHolds+ return (lcd, backLight, readButton)++-- | Number guessing game, as a simple LCD demo. User thinks of a number+-- between @0@ and @1000@, and the Arduino guesses it.+numGuess :: LCD -> (Bool -> Arduino ()) -> Arduino (Maybe Key) -> Arduino ()+numGuess lcd light readKey = game+ where home = lcdHome lcd+ write = lcdWrite lcd+ clear = lcdClear lcd+ go = lcdSetCursor lcd+ at (r, c) s = go (c, r) >> write s+ getKey = do mbK <- readKey+ case mbK of+ Nothing -> getKey+ Just k -> do delay 500 -- stabilize by waiting 0.5s+ return k+ game = do clear+ home+ light True+ at (0, 4) "HArduino!"+ at (1, 0) "# Guessing game"+ delay 2000+ guess 1 0 1000+ newGame = getKey >> game+ guess :: Int -> Int -> Int -> Arduino ()+ guess rnd l h+ | h == l = do clear+ at (0, 0) $ "It must be: " ++ show h+ at (1, 0) $ "Guess no: " ++ show rnd+ newGame+ | h < l = do clear+ at (0, 0) "You lied!"+ newGame+ | True = do clear+ let g = (l+h) `div` 2+ at (0, 0) $ "(" ++ show rnd ++ ") Is it " ++ show g ++ "?"+ k <- getKey+ case k of+ KeyUp -> guess (rnd+1) (g+1) h+ KeyDown -> guess (rnd+1) l (g-1)+ KeySelect -> do at (1, 0) $ "Got it in " ++ show rnd ++ "!"+ newGame+ _ -> do at (1, 0) "Use up/down/select only.."+ delay 1000+ guess rnd l h++-- | Entry to the classing number guessing game. Simply initialize the+-- shield and call our game function.+guessGame :: IO ()+guessGame = withArduino False "/dev/cu.usbmodemfd131" $ do+ (lcd, light, readButton) <- initOSepp+ numGuess lcd light readButton
hArduino.cabal view
@@ -1,13 +1,11 @@ Name: hArduino-Version: 0.6+Version: 0.7 Category: Hardware Synopsis: Control your Arduino board from Haskell.-Description: hArduino library allows Haskell programs to control Arduino boards and- peripherals, using the Firmata protocol (<http://firmata.org>).- .- A short (4m29s) video of the blinking example: <http://www.youtube.com/watch?v=PPa3im44t2g>+Description: hArduino allows Haskell programs to control Arduino boards (<http://www.arduino.cc>)+ and peripherals, using the Firmata protocol (<http://firmata.org>). .- Comments, bug-reports, and patches are welcome.+ For details, see: <http://leventerkok.github.com/hArduino>. Copyright: Levent Erkok, 2013 License: BSD3 License-file: LICENSE@@ -40,6 +38,7 @@ , System.Hardware.Arduino.SamplePrograms.Counter , System.Hardware.Arduino.SamplePrograms.Distance , System.Hardware.Arduino.SamplePrograms.LCD+ , System.Hardware.Arduino.SamplePrograms.NumGuess , System.Hardware.Arduino.SamplePrograms.Pulse , System.Hardware.Arduino.SamplePrograms.SevenSegment , System.Hardware.Arduino.SamplePrograms.Servo