hArduino 0.8 → 0.9
raw patch · 4 files changed
+87/−4 lines, 4 files
Files
- CHANGES.md +5/−1
- COPYRIGHT +1/−1
- System/Hardware/Arduino/SamplePrograms/Morse.hs +78/−0
- hArduino.cabal +3/−2
CHANGES.md view
@@ -1,7 +1,11 @@ * Hackage: (http://hackage.haskell.org/package/hArduino) * GitHub: (http://leventerkok.github.com/hArduino) -* Latest Hackage released version: 0.8+* Latest Hackage released version: 0.9++### Version 0.9, 2014-02-09+ * Added example program for Morse encoding.+ Original idea by Antoine Dumont. Thanks! ### Version 0.8, 2013-12-15 * Add support for Piezo speakers
COPYRIGHT view
@@ -1,4 +1,4 @@-Copyright (c) 2013, Levent Erkok (erkokl@gmail.com)+Copyright (c) 2013-2014, Levent Erkok (erkokl@gmail.com) All rights reserved. The usbArduino program is distributed with the BSD3 license. See the LICENSE file
+ System/Hardware/Arduino/SamplePrograms/Morse.hs view
@@ -0,0 +1,78 @@+-------------------------------------------------------------------------------+-- |+-- Module : System.Hardware.Arduino.SamplePrograms.Morse+-- Copyright : (c) Antoine R. Dumont, Levent Erkok+-- License : BSD3+-- Maintainer : erkokl@gmail.com+-- Stability : experimental+--+-- Morse code blinker. Original by Antoine R. Dumont, modified to simplify+-- and fit into the existing examples structure.+-------------------------------------------------------------------------------+module System.Hardware.Arduino.SamplePrograms.Morse where++import Control.Monad (forever)+import Control.Monad.Trans (liftIO)+import Data.Char (toUpper)+import Data.List (intercalate)+import Data.Maybe (fromMaybe)++import System.Hardware.Arduino++-- | A dit or a dah is all we need for Morse:+-- A @dit@ is a dot; and a @dah@ is a dash in the Morsian world.+-- We use 'LBreak' and 'WBreak' to indicate a letter and a word break+-- so we can insert some delay between letters and words as we+-- transmit.+data Morse = Dit | Dah | LBreak | WBreak+ deriving Show++-- | Morse code dictionary+dict :: [(Char, [Morse])]+dict = map encode m+ where encode (k, s) = (k, map (\c -> if c == '.' then Dit else Dah) s)+ m = [ ('A', ".-" ), ('B', "-..." ), ('C', "-.-." ), ('D', "-.." ), ('E', "." )+ , ('F', "..-." ), ('G', "--." ), ('H', "...." ), ('I', ".." ), ('J', ".---" )+ , ('K', "-.-" ), ('L', ".-.." ), ('M', "--" ), ('N', "-." ), ('O', "---" )+ , ('P', ".--." ), ('Q', "--.-" ), ('R', ".-." ), ('S', "..." ), ('T', "-" )+ , ('U', "..-" ), ('V', "...-" ), ('W', ".--" ), ('X', "-..-" ), ('Y', "-.--" )+ , ('Z', "--.." ), ('0', "-----"), ('1', ".----"), ('2', "..---"), ('3', "...--")+ , ('4', "....-"), ('5', "....."), ('6', "-...."), ('7', "--..."), ('8', "---..")+ , ('9', "----."), ('+', ".-.-."), ('/', "-..-."), ('=', "-...-")+ ]++-- | Given a sentence, decode it. We simply drop any letters that we+-- do not have a mapping for.+decode :: String -> [Morse]+decode = intercalate [WBreak] . map (intercalate [LBreak] . map cvt) . words+ where cvt c = fromMaybe [] $ toUpper c `lookup` dict++-- | Given a morsified sentence, compute the delay times. A 'Left' value means+-- turn the led on that long, a 'Right' value means turn it off that long.+morsify :: [Morse] -> [Either Int Int]+morsify = map t+ where unit = 300+ t Dit = Left $ 1 * unit+ t Dah = Left $ 3 * unit+ t LBreak = Right $ 3 * unit+ t WBreak = Right $ 7 * unit++-- | Finally, turn a full sentence into a sequence of blink on/off codes+transmit :: Pin -> String -> Arduino ()+transmit p = sequence_ . concatMap code . morsify . decode+ where code (Left i) = [digitalWrite p True, delay i, digitalWrite p False, delay i]+ code (Right i) = [digitalWrite p False, delay i]++-- | A simple demo driver. To run this example, you only need the Arduino connected to your+-- computer, no other hardware is needed. We use the internal led on pin 13. Of course,+-- you can attach a led to pin 13 as well, for artistic effect.+--+-- <<http://github.com/LeventErkok/hArduino/raw/master/System/Hardware/Arduino/SamplePrograms/Schematics/Blink.png>>+morseDemo :: IO ()+morseDemo = withArduino False "/dev/cu.usbmodemfd131" $ do+ setPinMode led OUTPUT+ forever send+ where led = digital 13+ send = do liftIO $ putStr "Message? "+ m <- liftIO getLine+ transmit led m
hArduino.cabal view
@@ -1,12 +1,12 @@ Name: hArduino-Version: 0.8+Version: 0.9 Category: Hardware Synopsis: Control your Arduino board from Haskell. Description: hArduino allows Haskell programs to control Arduino boards (<http://www.arduino.cc>) and peripherals, using the Firmata protocol (<http://firmata.org>). . For details, see: <http://leventerkok.github.com/hArduino>.-Copyright: Levent Erkok, 2013+Copyright: Levent Erkok, 2013-2014 License: BSD3 License-file: LICENSE Stability: Experimental@@ -39,6 +39,7 @@ , System.Hardware.Arduino.SamplePrograms.Counter , System.Hardware.Arduino.SamplePrograms.Distance , System.Hardware.Arduino.SamplePrograms.JingleBells+ , System.Hardware.Arduino.SamplePrograms.Morse , System.Hardware.Arduino.SamplePrograms.LCD , System.Hardware.Arduino.SamplePrograms.NumGuess , System.Hardware.Arduino.SamplePrograms.Pulse