Specifically, I hooked a switch to a GPIO pin and read its state. Then I used that to control a LED hooked to another GPIO pin. To keep is simple, I used the same LED as in the last post, adding a switch.
For the input line, I wanted an internal pullup resister, so I needed a few more constants. Here’s all the options for the
(define pin-mode-in 0) (define pin-mode-out 1) (define otype-pushpull 0) (define otype-opendrain 1) (define ospd-low 0) (define ospd-medium 1) (define ospd-fast 2) (define ospd-highspeed 3) (define pupd-none 0) (define pupd-pullup 1) (define pupd-pulldown 2)
I didn’t need any utility functions as the logic is simple. So here’s the main function:
(define (light-switch) (let ((port giod) (led-pin 10) (switch-pin 9)) (config-pin port led-pin pin-mode-out) (config-pin port switch-pin pin-mode-in 0 0 pupd-pullup) (let loop () ((if (pin-set? port switch-pin) pin-clear pin-set) port led-pin) (loop))))
The main loop continuously reads the switch and turns the LED on or off based on whether the switch is pressed (indicated by a low signal and thus a false value).
Code is available on Bitbucket.