[Haskell-beginners] GPIO/I2C/PWM and Haskell

Chaddaï Fouché chaddai.fouche at gmail.com
Wed Jan 28 20:39:23 UTC 2015


On Tue, Jan 27, 2015 at 5:02 PM, Michael Jones <mike at proclivis.com> wrote:

> Michal,
>
> I am doing similar things (not arial) on a MinnowBoardMax. Did not want to
> deal with ARM and Haskell. But if you make that work, I think it would be
> worth publishing how you did it. I struggled to build a cross compiler for
> ARM and gave up.
>
> As for MinnowBoardMax, I am running Ubuntu with a 3.18.1 kernel and a
> solid state drive. A little heavy on the weight when you consider the
> batteries required to run a 5W system.
>
> I have libraries for I2C, UART, and GPIO, but not published.
>
> Here is an example of how I deal with ALERTB
>
> module SMBusAlert (
>   alert
> ) where
>
> import System.IO
>
> alert :: IO Bool
> alert = do
>         writeFile "/sys/class/gpio/export" "340"
>         writeFile "/sys/class/gpio/gpio340/direction" "in"
>         s <- readFile "/sys/class/gpio/gpio340/value"
>         s `seq` writeFile "/sys/class/gpio/unexport" "340"
>         if s!!0 == '0' then return True else return False
>
>
While I do not understand much about what you're doing, I would suggest
instead :

alert :: IO Bool
alert = do
    writeGpio "export" "340"
    writeGpio "gpio340/direction" "in"
    c <- withFile "/sys/class/gpio/gpio340/value" ReadMode getChar
    writeGpio "unexport" "340"
    return (c == '0')
  where
    writeGpio p = writeFile ("/sys/class/gpio/" ++ p)

and maybe writeGpio (or a general gpio transformer) should be part of an
utility library since those IO to related files/devices seems to form a
large part of your code.
(Also the "if blabla then return True else return False" == "return blabla"
is a common mistake)

-- 
Jedaï
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20150128/5b551622/attachment.html>


More information about the Beginners mailing list