I wrote haskell code to switch a pin on a raspberry pi depending on the interrupt that I get from another pin on a raspberry pi. I just don't know how to switch the pin state without knowing the previous switch state. The program itself is very simple.
import Control.Concurrent import Data.IORef import HasberryPi main = do wiringPiSetup pinMode 0 output pinMode 7 input pullUpDnControl 7 pull_down wiringPiISR 7 edge_both onoff threadDelay (15*(10^6)) onoff s = do a <- readIORef s -- This is wrong digitalWrite 0 (if b then pinhigh else pinlow) -- This is wrong
So basically what happens here, pin 7 is registered as an interrupt. An interrupt is triggered when pin 7 goes from high to low or from low to high. And whenever an interrupt is triggered, it calls the onoff function, which switches the state of pin 0.
The main function is correct. Its onoff function is a problem. The desired behavior of the onoff function is to make pin 0 high when the pin is low, and to switch low contact when it is high. But for this I need to save the previous pin state in the previous onoff call.
I tried the state monad. But the problem is that the state monad passes the state around based on the initial value of the state. But in subsequent onoff calls, onoff seems impossible to change the initial state value. I was thinking about IORef and that is no different. It seems that he is doing what the state is doing, but only inside the IO.
I clearly see that I really lack the ability to save state in a global variable. And I am glad that I am not able to do this, because I know that there is some other idiomatic way to achieve the same goal.
Any help in the right direction is greatly appreciated.
Greetings and greetings.
source share