source: project/wiki/eggref/4/raspberry-pi-gpio @ 33083

Last change on this file since 33083 was 33083, checked in by Caolan McMahon, 4 years ago

API updates for 1.1.0

File size: 4.5 KB
Line 
1[[tags: egg]]
2== RaspberryPI GPIO library
3
4This egg provides an interface to the RaspberryPi GPIO pins for CHICKEN Scheme.
5
6[[toc:]]
7
8== Installation / Dependencies
9
10You'll need to install [[http://wiringpi.com|Wiring Pi]] before using this
11library. The [[http://wiringpi.com/reference|Wiring Pi documentation]] may
12help you understand some of these functions in more detail.
13
14== Example
15
16<enscript highlight="scheme">
17(use raspberry-pi-gpio)
18
19(setup-gpio)
20
21;; digital read / write
22
23(pin-mode 1 'output)
24
25(digital-write 1 HIGH)
26(digital-write 1 LOW)
27
28(printf "Pin 1 is: ~S~n" (digital-read 1))
29
30;; using interrupts to respond to events
31
32(set-edge 7 'falling)
33(set-edge 8 'both)
34
35(define (loop)
36  (receive (pin value intsec intnsec) (receive-gpio-event)
37    (printf "Interrupt on pin ~S, interrupt time ~S.~S, value: ~S, value now: ~S ~n"
38            pin intsec intnsec value (digital-read pin))
39    (loop)))
40
41(loop)
42</enscript>
43
44== API
45
46One of the setup functions must be called at the start of your program, or
47your program will fail to work correctly.
48
49<procedure>(setup-gpio)</procedure>
50
51Use the Broadcom GPIO pin numbers when referencing pins.
52
53<procedure>(setup-virtual)</procedure>
54
55Use the wiringPi pin numbering scheme. This is a simplified numbering
56scheme which provides a mapping from virtual pin numbers 0 through 16 to
57the real underlying Broadcom GPIO pin numbers. See the [Wiring Pi
58documentation](http://wiringpi.com/reference/setup/) for more info.
59
60<procedure>(setup-physical)</procedure>
61
62Use the physical pin numbers *on the P1 connector only*.
63
64<procedure>(setup-system)</procedure>
65
66Use the /sys/class/gpio interface rather than accessing the hardware
67directly. This can be called as a non-root user provided the GPIO pins have
68been exported before-hand using the gpio program.
69
70<procedure>(board-rev)</procedure>
71
72Returns the board revision (1 or 2).
73
74<procedure>(pin-mode pin mode)</procedure>
75
76Sets the pin to the given mode. Mode is one of the following symbols:
77`input`, `output`, `pwm-output`, `gpio-clock`.
78
79<procedure>(pull-up-dn-control pin pud)</procedure>
80
81This sets the pull-up or pull-down resistor mode on the given pin, which
82should be set as an input. Pud should be one of the following symbols:
83`off`, `up`, `down`.
84
85<procedure>(pwm-write pin value)</procedure>
86
87Writes the value to the PWM register for the given pin. The Raspberry Pi
88has one on-board PWM pin, pin 1 (BMC\_GPIO 18, Phys 12) and the range is
890-1024. Other PWM devices may have other PWM ranges.
90
91This function is not able to control the Pi’s on-board PWM when in Sys
92mode.
93
94<procedure>(digital-write pin value)</procedure>
95
96Writes the value HIGH or LOW (1 or 0) to the given pin which must have been
97previously set as an output.
98
99<procedure>(digital-write/time pin value)</procedure>
100
101Writes the value HIGH or LOW (1 or 0) to the given pin which must have been previously set as an output.
102
103Returns two values, seconds and nanoseconds since process- or machine startup when the pin value was changed.
104
105<procedure>(digital-read pin)</procedure>
106
107This function returns the value read at the given pin. It will be HIGH or
108LOW (1 or 0) depending on the logic level at the pin.
109
110<procedure>(analog-write pin value)</procedure>
111
112This writes the given value to the supplied analog pin.
113
114<procedure>(analog-read pin)</procedure>
115
116This returns the value read on the supplied analog input pin.
117
118<procedure>(set-edge pin edge)</procedure>
119
120Sets up an interrupt handler for the pin and given edge using wiringPiISR.
121Use this to hook up events for use with `recieve-gpio-event`.
122
123Type should be one of the following symbols: `falling`, `rising`, `both`,
124`setup`. If `setup` is used then no initialisation of the pin will happen –
125it’s assumed that you have already setup the pin elsewhere (e.g. with the
126gpio program).
127
128<procedure>(receive-gpio-event)</procedure>
129
130Blocks until a GPIO interrupt occurs on a pin that had set-edge called on it.
131
132Returns four values the pin number that caused the interrupt, the value read from the pin in the interrupt routine, the number of seconds and nanoseconds representing the time when the interrupt routine was executed.
133
134<procedure>(current-time-raw)</procedure>
135
136Returns two values the number of seconds and nanosecons since process- or machine startup.
137
138Uses Linux' clock_gettime to read CLOCK_MONOTONIC_RAW, i.e. the result value is not effected by adjustments from NTP etc. and therefore suited to measure physical time deltas.
139
140<constant>HIGH</constant>
141
142The HIGH value (1) exported by wiringPi.
143
144<constant>LOW</constant>
145
146The LOW value (0) exported by wiringPi.
Note: See TracBrowser for help on using the repository browser.