MBot 0.1.1.0 → 0.1.2.0
raw patch · 3 files changed
+41/−18 lines, 3 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
+ MBot: playTone :: Int -> Int -> Command
Files
- ChangeLog.md +4/−0
- MBot.cabal +1/−1
- src/MBot.hs +36/−17
ChangeLog.md view
@@ -3,3 +3,7 @@ ## 0.1.0.0 -- YYYY-mm-dd * First version. Released on an unsuspecting world.++# 0.1.2.0 ++* Rien Maertens contributed a function to play sound on the Mbot !
MBot.cabal view
@@ -10,7 +10,7 @@ -- PVP summary: +-+------- breaking API changes -- | | +----- non-breaking API additions -- | | | +--- code changes with no API change-version: 0.1.1.0+version: 0.1.2.0 -- A short (one-line) description of the package. synopsis: Haskell interface for controlling the mBot educational robot
src/MBot.hs view
@@ -18,6 +18,9 @@ -- or just want to ask me questions -- please contact me at -- Christophe.Scholliers@UGent.be+--+-- PlayTone is contributed by Rien Maertens+-- ------------------------------------------------ -- Compiling this library on mac can be done as -- follows:@@ -29,7 +32,7 @@ = Programming the mBot With this library it is possible to control the mBot robot from within Haskell over 2.4ghz wireless.-The mBot itself needs to contain the standard firmware otherwise the library will not behave as expected. +The mBot itself needs to contain the standard firmware otherwise the library will not behave as expected. There is support for steering the motors and leds and for reading the linesensor and the ultrasonic sensor. An small example program is shown below, for more information about the individual functions take a look at the api documentation below. @@ -46,7 +49,7 @@ putStrLn "Look at all the pretty colors !" -- Turn on led 2 of the mBot and set the RGB value to (100,0,0) sendCommand d $ setRGB 2 100 0 0- -- close the connection with the mBot + -- close the connection with the mBot closeMBot d @ -}@@ -64,8 +67,9 @@ stop, setRGB, setMotor,+ playTone, Line(LEFTB, RIGHTB, BOTHB, BOTHW),- Command() ) where + Command() ) where import Control.Monad.Trans import Control.Concurrent@@ -116,11 +120,11 @@ {-| The line sensor consists of two sensors which are able to detect either a black or a white surface.-Therefore there are four different states to represent the state of the line sensor +Therefore there are four different states to represent the state of the line sensor -}-data Line = LEFTB -- ^ Left sensor reads black right sensor reads white - | RIGHTB -- ^ Right sensor reads black left sensor reads white +data Line = LEFTB -- ^ Left sensor reads black right sensor reads white+ | RIGHTB -- ^ Right sensor reads black left sensor reads white | BOTHB -- ^ Both the left and right sensor observe a black surface | BOTHW -- ^ Both the left and right sensor observe a white surface deriving(Show,Eq)@@ -284,9 +288,9 @@ -------------------------------------------------------------------------------------------- {-|- Opens a connection with the mBot + Opens a connection with the mBot -}-openMBot :: (IO Device) -- ^ gives back the connection with the mBot +openMBot :: (IO Device) -- ^ gives back the connection with the mBot openMBot = withHIDAPI $ do HID.init d <- HID.open dongleID deviceID Nothing@@ -315,8 +319,8 @@ clearBuffer device act return () -{-| - Create an mBot command to turn on the led on a particular rgb value +{-|+ Create an mBot command to turn on the led on a particular rgb value -} setRGB index red green blue = MBotCommand idx RUN RGBLED rgbp [2,index,red,green,blue] @@ -327,11 +331,11 @@ {-|- Read out the status of the ultrasonic line follower + Read out the status of the ultrasonic line follower -} readUltraSonic d = ultra <$> readSensor d getUltrasonicSensor ultraIdx {-|- Read out the status of line follower sensor + Read out the status of line follower sensor -} readLineFollower d = convertToReading <$> readSensor d getLineFollower lineIdx @@ -349,7 +353,7 @@ Start both motors so that the robot moves backwards -} goBackwards d = do sendCommand d $ setMotor rightMotor (complement speed) (complement stops)- sendCommand d $ setMotor leftMotor speed stops + sendCommand d $ setMotor leftMotor speed stops @@ -357,16 +361,31 @@ Start the motors let the mBot turn left -} goLeft d = do sendCommand d $ setMotor leftMotor stops stops- sendCommand d $ setMotor rightMotor speed stops + sendCommand d $ setMotor rightMotor speed stops {-|- Start the motors so that the robots turns right + Start the motors so that the robots turns right -} goRight d = do sendCommand d $ setMotor rightMotor stops stops- sendCommand d $ setMotor leftMotor (complement speed) (complement stops) + sendCommand d $ setMotor leftMotor (complement speed) (complement stops) -{-| +{-| Stop both motors -} stop d = do sendCommand d $ setMotor rightMotor stops stops sendCommand d $ setMotor leftMotor stops stops+++{-|+Contributed by: Rien Maertens+Create an mBot command to sound a tone with the buzzer.+-}+playTone freq time = let (highFreq, lowFreq) = shortToBytes freq+ (highTime, lowTime) = shortToBytes time+ -- There is no port value. Instead, the least+ -- signifcant byte of the frequency-value is stored+ -- there. Something something little-endian?+ in MBotCommand idx RUN TONE+ lowFreq [highFreq, lowTime, highTime]+ where -- Split a short into a tuple of two bytes+ shortToBytes i = (shiftR i 8, i)