Methods
callback(userGpio, edge, cb)
Calls a user supplied function (a callback) whenever the specified GPIO edge is detected.
The user supplied callback receives three parameters, the GPIO, the level, and the tick.
If a user callback is not specified a default tally callback is
provided which simply counts edges. The count may be retrieved
by calling the tally function. The count may be reset to zero
by calling the reset_tally function.
The callback may be cancelled by calling the cancel function.
A GPIO may have multiple callbacks (although I can't think of
a reason to do so).
Parameters:
| Name | Type | Description |
|---|---|---|
userGpio |
number | The number of the gpio to address (0-31). |
edge |
number | Indicate the edge to detect. |
cb |
callback | Callback to be run. |
callback(userGpio, edge, func)
Calls a user supplied function (a callback) whenever the
specified GPIO edge is detected.
If a user callback is not specified a default tally callback is
provided which simply counts edges. The count may be retrieved
by calling the tally function. The count may be reset to zero
by calling the reset_tally function.
The callback may be cancelled by calling the cancel function.
A GPIO may have multiple callbacks (although I can't think of
a reason to do so).
Parameters:
| Name | Type | Description |
|---|---|---|
userGpio |
number | Port 0-31. |
edge |
number | Must be EITHER_EDGE, RISING_EDGE (default), or FALLING_EDGE. |
func |
callback | User supplied callback function. The user supplied callback receives three parameters, the GPIO, the level, and the tick. |
close()
Half-closes the connection. We might still get some data from the server.
get_mode(gpio, callback)
Returns the GPIO mode.
Parameters:
| Name | Type | Description |
|---|---|---|
gpio |
number | Port 0-53. |
callback |
callback | Function to be run when the data has been received. |
get_PWM_dutycycle(userGpio, cb)
Returns the PWM dutycycle being used on the GPIO.
For normal PWM the dutycycle will be out of the defined range
for the GPIO (see [*get_PWM_range*]).
If a hardware clock is active on the GPIO the reported
dutycycle will be 500000 (500k) out of 1000000 (1M).
If hardware PWM is active on the GPIO the reported dutycycle
will be out of a 1000000 (1M).
Parameters:
| Name | Type | Description |
|---|---|---|
userGpio |
number | The number of the gpio to address (0-31). |
cb |
callback | Function that the value will be passed back to in form function(err, data). |
get_PWM_frequency(userGpio, cb)
Returns the frequency of PWM being used on the GPIO.
Parameters:
| Name | Type | Description |
|---|---|---|
userGpio |
number | The number of the gpio to address (0-31). |
cb |
callback | Function that the value will be passed back to in form function(err, data). |
get_PWM_range(userGpio, cb)
Returns the range of PWM values being used on the GPIO.
If a hardware clock or hardware PWM is active on the GPIO the reported range will be 1000000 (1M).
Parameters:
| Name | Type | Description |
|---|---|---|
userGpio |
number | The number of the gpio to address (0-31). |
cb |
callback | Function that the value will be passed back to in form function(err, data). |
get_PWM_real_range(userGpio, cb)
Returns the real (underlying) range of PWM values being used on the GPIO.
If a hardware clock is active on the GPIO the reported real range will be 1000000 (1M).
If hardware PWM is active on the GPIO the reported real range
will be approximately 250M divided by the set PWM frequency.
Parameters:
| Name | Type | Description |
|---|---|---|
userGpio |
number | The number of the gpio to address (0-31). |
cb |
callback | Function that the value will be passed back to in form function(err, data). |
getHardwareRevision(cb)
Returns the Pi's hardware revision number.
The hardware revision is the last few characters on the Revision line of /proc/cpuinfo.
The revision number can be used to determine the assignment of GPIO to pins (see [*gpio*]).
There are at least three types of board.
* Type 1 boards have hardware revision numbers of 2 and 3.
* Type 2 boards have hardware revision numbers of 4, 5, 6, and 15.
* Type 3 boards have hardware revision numbers of 16 or greater.
* If the hardware revision can not be found or is not a valid hexadecimal number the function returns 0.
Parameters:
| Name | Type | Description |
|---|---|---|
cb |
callback | Callback that will receive the result in form of function (err, data). |
pi(hostopt, portopt, cbopt)
Connects to the pigpio daemon.
This method has to be called before it's possible to do anything with
the GPIO pins.
The callback will be called when the connection has been established or
if an error occurs.
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
host |
string |
<optional> |
The host to connect to. |
port |
number |
<optional> |
The port on the host to connect to. |
cb |
Object |
<optional> |
Callback function. |
read(userGpio, cb)
Returns the GPIO level.
Parameters:
| Name | Type | Description |
|---|---|---|
userGpio |
number | The number of the gpio to address. 0-31. |
cb |
callback | The function to be called with the result. |
set_glitch_filter(gpio, steady)
Sets a glitch filter on a GPIO.
Level changes on the GPIO are not reported unless the level
has been stable for at least [*steady*] microseconds. The
level is then reported. Level changes of less than [*steady*]
microseconds are ignored.
Parameters:
| Name | Type | Description |
|---|---|---|
gpio |
number | Port 0-31. |
steady |
number | Number of setmicroseconds after detection before a change is confirmed. |
set_mode(gpio, mode)
Sets the GPIO mode.
Parameters:
| Name | Type | Description |
|---|---|---|
gpio |
number | Port 0-53. |
mode |
string | Must be either INPUT, OUTPUT, ALT0, ALT1, ALT2, ALT3, ALT4 or ALT5. |
set_pull_up_down(gpio, pud)
Sets or clears the internal GPIO pull-up/down resistor.
Parameters:
| Name | Type | Description |
|---|---|---|
gpio |
number | Port 0-53. |
pud |
string | Must be either PUD_UP, PUD_DOWN, PUD_OFF. |
set_PWM_dutycycle(userGpio, dutycycle)
Starts (non-zero dutycycle) or stops (0) PWM pulses on the gpio.
Example
pi.set_PWM_dutycycle(4, 0) # PWM off
pi.set_PWM_dutycycle(4, 64) # PWM 1/4 on
pi.set_PWM_dutycycle(4, 128) # PWM 1/2 on
pi.set_PWM_dutycycle(4, 192) # PWM 3/4 on
pi.set_PWM_dutycycle(4, 255) # PWM full on
Parameters:
| Name | Type | Description |
|---|---|---|
userGpio |
number | The number of the gpio to address (0-31). |
dutycycle |
number | The pwm dutycycle to use: 0 (off), 255 (full on). |
set_PWM_frequency(userGpio, frequency)
Sets the frequency (in Hz) of the PWM to be used on the GPIO.
If PWM is currently active on the GPIO it will be switched
off and then back on at the new frequency.
Each GPIO can be independently set to one of 18 different PWM frequencies.
The selectable frequencies depend upon the sample rate which
may be 1, 2, 4, 5, 8, or 10 microseconds (default 5).
The sample rate is set when the pigpio daemon is started.
The frequencies for each sample rate are:
hertz
1: 40000 20000 10000 8000 5000 4000 2500 2000 1600
1250 1000 800 500 400 250 200 100 50
2: 20000 10000 5000 4000 2500 2000 1250 1000 800
625 500 400 250 200 125 100 50 25
4: 10000 5000 2500 2000 1250 1000 625 500 400
313 250 200 125 100 63 50 25 13
sample
rate
(us) 5: 8000 4000 2000 1600 1000 800 500 400 320
250 200 160 100 80 50 40 20 10
8: 5000 2500 1250 1000 625 500 313 250 200
156 125 100 63 50 31 25 13 6
10: 4000 2000 1000 800 500 400 250 200 160
125 100 80 50 40 25 20 10 5.
Parameters:
| Name | Type | Description |
|---|---|---|
userGpio |
number | The number of the gpio to address (0-31). |
frequency |
number | Frequency >=0 Hz. |
set_PWM_range(userGpio, range)
Sets the range of PWM values to be used on the GPIO.
Example
pi.set_PWM_range(9, 100) // now 25 1/4, 50 1/2, 75 3/4 on
pi.set_PWM_range(9, 500) // now 125 1/4, 250 1/2, 375 3/4 on
pi.set_PWM_range(9, 3000) // now 750 1/4, 1500 1/2, 2250 3/4 on
Parameters:
| Name | Type | Description |
|---|---|---|
userGpio |
number | The number of the gpio to address (0-31). |
range |
number | A number in the range 25-40000. |
setServoPulsewidth(userGpio, pulseWidth)
Starts (500-2500) or stops (0) servo pulses on the given gpio pin.
The selected pulsewidth will continue to be transmitted until
changed by a subsequent call to set_servo_pulsewidth.
The pulsewidths supported by servos varies and should probably
be determined by experiment. A value of 1500 should always be
safe and represents the mid-point of rotation.
You can DAMAGE a servo if you command it to move beyond its
limits.
Example
gpio.setServoPulsewidth(17, 0) # off
gpio.setServoPulsewidth(17, 1000) # safe anti-clockwise
gpio.setServoPulsewidth(17, 1500) # centre
gpio.setServoPulsewidth(17, 2000) # safe clockwise
Parameters:
| Name | Type | Description |
|---|---|---|
userGpio |
number | The number of the gpio to address. 0-31. |
pulseWidth |
number | The servo pulsewidth to generate 0 (off), 500 (most anti-clockwise) - 2500 (most clockwise). |
write(userGpio, level, cb)
Returns the GPIO level.
Parameters:
| Name | Type | Description |
|---|---|---|
userGpio |
number | The number of the gpio to address. 0-31. |
level |
number | The output level 0 or 1. |
cb |
callback | The function to be called with the result. |