As part of the R&D work on port! datatype in port-type branch, we have implemented a GPIO driver for Raspberry Pi boards, as a gpio:// scheme (no third-party library needed). This work helped define the low-level API for ports written in purely Red/System or a mix of Red and Red/System.
The Raspberry Pi is a very popular board with millions of units sold, so this is a market where Red could be potentially helpful to developers. We could run Red on such boards for years but did not have proper GPIO support, so this is now fixed!
The current features supported by the GPIO port are:
joypad: context [ mapping: [ 16 down 20 up 17 left 21 right ]row: [(id) state: #[false] direction: (direction)] table: collect [ foreach [id direction] mapping [keep compose row] ] pins: extract table length? row port: none acquire: has [pin][ port: open gpio:// foreach pin pins [ insert port [ set-mode pin in pull-down pin ] ] insert port [ set-mode 18 out set-mode 4 out set 4 on ] ] pressed?: function [][ foreach pin pins [ entry: find table pin old: entry/state insert port [get pin] entry/state: make logic! port/data ;-- detect 0-to-1 transitions only, to avoid auto-firing if all [not old entry/state][return entry/direction] ] none ] show-win: does [insert port [set 18 on]] release: does [ insert port [ set 4 off set 18 off ] close port ] ]
Set the working mode for a given pin:set-mode <pin> <mode> <pin> : pin number (integer!) <mode>: in, out, pwm
Write a value on a pin:
set <pin> <value> <pin> : pin number (integer!) <value>: true, false, on, off, yes, no, 0, 1
Read a value from a pin (the returned value is in port/data):
get <pin> <pin>: pin number (integer!)
Manage pull-up/down resistors:
pull-off <pin> ;– disable any pull previously set pull-down <pin> ;– activate pull-down on the given pin pull-up <pin> ;– activate pull-up on the given pin
Write a PWM value on a pin:
set-pwm <pin> <value>
<pin> : pin number (integer!) <value>: an integer between 0 and 1024, or a percentage for duty cycle.
Fade in/out values on a PWM pin:
fade <pin> from <start> to <end> <delay>
<pin> : pin number (integer!) <start>: starting value (0-1024) <end> : ending value (0-1024) <delay>: duration of the whole fading (time!)
Wait for a given duration:
pause <delay>
<delay>: integer => pause in miliseconds, float => pause in seconds.