pigpio

package module
v0.0.0-...-63a2a85 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 26, 2022 License: MIT Imports: 7 Imported by: 0

README

Go PiGPIO Socket Interface

Implementation of the PiGPIO socket to communicate with the PiGPIO daemon. Can be used to communicate over network or localhost.

import "github.com/BxNiom/go-pigpiod"

For documentation have a look at doc/pigpio.md or Go Package Details

Features
  • Communication via sockets allows remote control pigpiod over network
  • Almost complete implementation of python pigpio (see features to implement)
  • No need of any C-Headers for compilation

Progress
Title Progress Description Status
🟢 connection socket create a socket and connect to daemon Finished
🟢 pin mode set pin mode Finished
🟢 read/write read/write pin value Finished
🟢 PWM software PWM Finished
🟢 hardware hardware PWM and clock Finished
🟠 wave add/create/modify waves Finished
Features todo:
* Wave chains helper
🟠 i2c communication with I2C devices Finished
Features todo:
* Batch helper
🟢 spi communication with SPI devices Finished
🟢 filter noise and glitch filters Finished
🟢 serial Finished
🟡 callbacks
🟠 scripts Finished
Features todo:
* Script helper
error codes implement pigpiod error codes
🟡 documentation

⚪ Todo | 🟡 Work in progress | 🟠 Features todo | 🟢 Done

Documentation

Index

Constants

View Source
const (
	CmdModeSet            int32 = 0
	CmdModeGet            int32 = 1
	CmdPullUpDown         int32 = 2
	CmdRead               int32 = 3
	CmdWrite              int32 = 4
	CmdPwm                int32 = 5
	CmdPwmRangeSet        int32 = 6
	CmdPwmFrequencySet    int32 = 7
	CmdServoPulseWidthSet int32 = 8
	CmdBank1Read          int32 = 10
	CmdBank2Read          int32 = 11
	CmdBank1Clear         int32 = 12
	CmdBank2Clear         int32 = 13
	CmdBank1Set           int32 = 14
	CmdBank2Set           int32 = 15
	CmdGetTick            int32 = 16
	CmdHWVersion          int32 = 17
	CmdPwmRangeGet        int32 = 22
	CmdPwmFrequencyGet    int32 = 23
	CmdPwmRealRangeGet    int32 = 24
	CmdGetVersion         int32 = 26
	CmdWaveClear          int32 = 27
	CmdWaveAddGeneric     int32 = 28
	CmdWaveAddSerial      int32 = 29
	CmdWaveTxBusy         int32 = 32
	CmdWaveTxHalt         int32 = 33
	CmdWaveMicros         int32 = 34
	CmdWavePulse          int32 = 35
	CmdWaveCBS            int32 = 36
	CmdTrigger            int32 = 37
	CmdScript             int32 = 38
	CmdScriptDelete       int32 = 39
	CmdScriptRun          int32 = 40
	CmdScriptStop         int32 = 41
	CmdScriptStatus       int32 = 45
	CmdWaveCreate         int32 = 49
	CmdWaveDelete         int32 = 50
	CmdWaveTxOnce         int32 = 51
	CmdWaveTxRepeat       int32 = 52
	CmdWaveNew            int32 = 53
	CmdI2COpen            int32 = 54
	CmdI2CClose           int32 = 55
	CmdI2CReadDevice      int32 = 56
	CmdI2CWriteDevice     int32 = 57
	CmdI2CWriteQuick      int32 = 58
	CmdI2CReadByte        int32 = 59
	CmdI2CWriteByte       int32 = 60
	CmdI2CReadByteData    int32 = 61
	CmdI2CWriteByteData   int32 = 62
	CmdI2CReadWordData    int32 = 63
	CmdI2CWriteWordData   int32 = 64
	CmdI2CReadBlockData   int32 = 65
	CmdI2CWriteBlockData  int32 = 66
	CmdI2CProcessCall     int32 = 69
	CmdSPIOpen            int32 = 71
	CmdSPIClose           int32 = 72
	CmdSPIRead            int32 = 73
	CmdSPIWrite           int32 = 74
	CmdSPIXfer            int32 = 75
	CmdSerialOpen         int32 = 76
	CmdSerialClose        int32 = 77
	CmdSerialReadByte     int32 = 78
	CmdSerialWriteByte    int32 = 79
	CmdSerialRead         int32 = 80
	CmdSerialWrite        int32 = 81
	CmdSerialAvailable    int32 = 82
	CmdPwmGetDutyCycle    int32 = 83
	CmdServoPulseWidthGet int32 = 84
	CmdHWClock            int32 = 85
	CmdHWPWM              int32 = 86
	CmdI2CBatch           int32 = 92
	CmdWaveChain          int32 = 93
	CmdFilterGlitch       int32 = 97
	CmdFilterNoise        int32 = 98
	CmdWaveTxUsingMode    int32 = 100
	CmdWaveTxAt           int32 = 101
	CmdScriptUpdate       int32 = 117
	CmdWaveCreateAndPad   int32 = 118
)

Commands

View Source
const (
	PudOff  int32 = 0
	PudDown int32 = 1
	PudUp   int32 = 2
)

Pull-Up-Down

View Source
const (
	LevelOff   int32 = 0
	LevelOn    int32 = 1
	LevelClear int32 = 0
	LevelSet   int32 = 1
	LevelLow   int32 = 0
	LevelHigh  int32 = 1
)

Levels

View Source
const (
	ModeInput  int32 = 0
	ModeOutput int32 = 1
	ModeAlt0   int32 = 4
	ModeAlt1   int32 = 5
	ModeAlt2   int32 = 6
	ModeAlt3   int32 = 7
	ModeAlt4   int32 = 3
	ModeAlt5   int32 = 2
)

Modes

View Source
const (
	WaveModeOneShot     int32 = 0
	WaveModeRepeat      int32 = 1
	WaveModeOneShotSync int32 = 2
	WaveModeRepeatSync  int32 = 3
)

Wave tx modes

View Source
const (
	WaveResNotFound int32 = 9998
	WaveResNoTx     int32 = 9999
)

Wave special results

View Source
const (
	I2CFlagWrite      int16 = 0x0000 /* write data */
	I2CFlagRead       int16 = 0x0001 /* read data */
	I2CFlagTen        int16 = 0x0010 /* ten bit chip address */
	I2CFlagReceiveLen int16 = 0x0400 /* length will be first received byte */
	I2CFlagNoReadAck  int16 = 0x0800 /* if I2C_FUNC_PROTOCOL_MANGLING */
	I2CFlagIgnoreNak  int16 = 0x1000 /* if I2C_FUNC_PROTOCOL_MANGLING */
	I2CFlagRevDirAddr int16 = 0x2000 /* if I2C_FUNC_PROTOCOL_MANGLING */
	I2CFlagNoStart    int16 = 0x4000 /* if I2C_FUNC_PROTOCOL_MANGLING */
)

I2C flags

View Source
const (
	I2CCmdEnd         int32 = 0
	I2CCmdEsc         int32 = 1
	I2CCmdStart       int32 = 2
	I2CCmdCombinedOn  int32 = 2
	I2CCmdStop        int32 = 3
	I2CCmdCombinedOff int32 = 3
	I2CCmdAddress     int32 = 4
	I2CCmdFlags       int32 = 5
	I2CCmdRead        int32 = 6
	I2CCmdWrite       int32 = 7
)

I2C commands

View Source
const (
	BSCDr       int32 = 0
	BSCRsr      int32 = 1
	BSCSlv      int32 = 2
	BSCCr       int32 = 3
	BSCFr       int32 = 4
	BSCIfLs     int32 = 5
	BSCImsc     int32 = 6
	BSCRis      int32 = 7
	BSCMis      int32 = 8
	BSCIcr      int32 = 9
	BSCDmacr    int32 = 10
	BSCTdr      int32 = 11
	BSCGpuStat  int32 = 12
	BSCHCtrl    int32 = 13
	BSCDebugI2C int32 = 14
	BSCDebugSpi int32 = 15
)

BSC registers

Variables

This section is empty.

Functions

This section is empty.

Types

type Callback

type Callback struct {
	// contains filtered or unexported fields
}

type CallbackFunc

type CallbackFunc func(int32, int32, int32)

type PiError

type PiError struct {
	State    int32
	ErrMsg   string
	InnerErr error
}

func (*PiError) Error

func (e *PiError) Error() string

type PiGPIO

type PiGPIO struct {
	Host string
	Port int
	// contains filtered or unexported fields
}

func Initialize

func Initialize(host string, port int) (pi PiGPIO, err error)

Initialize

Initialize a new connection to pigpiod

host := host string, e.g. 'localhost' or IP-Address port := the port pigpiod ist listen to

func (*PiGPIO) ClearBank1

func (pi *PiGPIO) ClearBank1(bits int32) error

ClearBank1

Clears GPIO 0-31 if the corresponding bit in bits is set.

bits := a 32 bit mask with 1 set if the corresponding GPIO is to be cleared.

func (*PiGPIO) ClearBank2

func (pi *PiGPIO) ClearBank2(bits int32) error

ClearBank2

Clears GPIO 32-53 if the corresponding bit (0-21) in bits is set.

bits := a 32 bit mask with 1 set if the corresponding GPIO is to be cleared.

func (*PiGPIO) Close

func (pi *PiGPIO) Close() error

Close

Close socket connection

func (*PiGPIO) FilterGlitch

func (pi *PiGPIO) FilterGlitch(pin int32, steady int32) error

FilterGlitch

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.

pin := 0-31 steady := 0-300000

Returns 0 if OK, otherwise PI_BAD_USER_GPIO, or PI_BAD_FILTER.

This filter affects the GPIO samples returned to callbacks set up with [*callback*] and [*wait_for_edge*].

It does not affect levels read by [*read*], [*read_bank_1*], or [*read_bank_2*].

Each (stable) edge will be timestamped [*steady*] microseconds after it was first detected.

func (*PiGPIO) FilterNoise

func (pi *PiGPIO) FilterNoise(pin int32, steady int32, active int32) error

FilterNoise

Sets a noise filter on a GPIO.

Level changes on the GPIO are ignored until a level which has been stable for [*steady*] microseconds is detected. Level changes on the GPIO are then reported for [*active*] microseconds after which the process repeats.

pin := 0-31 steady := 0-300000 active := 0-1000000

Returns 0 if OK, otherwise PI_BAD_USER_GPIO, or PI_BAD_FILTER.

This filter affects the GPIO samples returned to callbacks set up with [*callback*] and [*wait_for_edge*].

It does not affect levels read by [*read*], [*read_bank_1*], or [*read_bank_2*].

Level changes before and after the active period may be reported. Your software must be designed to cope with such reports.

func (*PiGPIO) GetCurrentTick

func (pi *PiGPIO) GetCurrentTick() (tick int32, err error)

GetCurrentTick

Returns the current system tick.

Tick is the number of microseconds since system boot. As an unsigned 32 bit quantity tick wraps around approximately every 71.6 minutes.

func (*PiGPIO) GetHardwareRevision

func (pi *PiGPIO) GetHardwareRevision() (rev int32, err error)

GetHardwareRevision

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.

func (*PiGPIO) GetMode

func (pi *PiGPIO) GetMode(pin int32) (res int32, err error)

GetMode

Returns the GPIO mode

pin := 0 - 53

func (*PiGPIO) GetPWMDutyCycle

func (pi *PiGPIO) GetPWMDutyCycle(pin int32) (res int32, err error)

GetPWMDutyCycle

Returns the PWM duty cycle being used on the GPIO.

pin := 0 - 31

func (*PiGPIO) GetPWMFrequency

func (pi *PiGPIO) GetPWMFrequency(pin int32) (res int32, err error)

GetPWMFrequency

Returns the frequency of PWM being used on the GPIO.

pin := 0 - 31

Returns the frequency (in Hz) used for the GPIO.

func (*PiGPIO) GetPWMRange

func (pi *PiGPIO) GetPWMRange(pin int32) (res int32, err error)

GetPWMRange

Returns the range of PWM values being used on the GPIO.

pin := 0 - 31

If a hardware clock or hardware PWM is active on the GPIO the reported range will be 1000000 (1M).

func (*PiGPIO) GetPWMRealRange

func (pi *PiGPIO) GetPWMRealRange(pin int32) (res int32, err error)

GetPWMRealRange

Returns the real (underlying) range of PWM values being used on the GPIO.

pin := 0 - 31

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.

func (*PiGPIO) GetServoPulseWidth

func (pi *PiGPIO) GetServoPulseWidth(pin int32) (res int32, err error)

GetServoPulseWidth

Returns the servo pulsewidth being used on the GPIO.

pin := 0-31.

func (*PiGPIO) GetWaveCbs

func (pi *PiGPIO) GetWaveCbs() (length int32, err error)

GetWaveCbs

Returns the length in DMA control blocks of the current waveform

func (*PiGPIO) GetWaveMaxCbs

func (pi *PiGPIO) GetWaveMaxCbs() (length int32, err error)

GetWaveMaxCbs

Returns the maximum possible size of a waveform in DMA control blocks

func (*PiGPIO) GetWaveMaxMicros

func (pi *PiGPIO) GetWaveMaxMicros() (micros int32, err error)

GetWaveMaxMicros

Returns the maximum possible size of a waveform in microseconds

func (*PiGPIO) GetWaveMaxPulses

func (pi *PiGPIO) GetWaveMaxPulses() (length int32, err error)

GetWaveMaxPulses

Returns the maximum possible size of a waveform in pulses

func (*PiGPIO) GetWaveMicros

func (pi *PiGPIO) GetWaveMicros() (micros int32, err error)

GetWaveMicros

Returns the length in microseconds of current waveform

func (*PiGPIO) GetWavePulses

func (pi *PiGPIO) GetWavePulses() (length int32, err error)

GetWavePulses

Returns the length in pulses of the current waveform

func (*PiGPIO) HardwareClock

func (pi *PiGPIO) HardwareClock(pin int32, frequency int32) error

HardwareClock

Starts a hardware clock on a GPIO at the specified frequency. Frequencies above 30MHz are unlikely to work.

pin := see description frequency := 0 (off) or 4689-250M (13184-375M for the BCM2711)

Returns 0 if OK, otherwise PI_NOT_PERMITTED, PI_BAD_GPIO, PI_NOT_HCLK_GPIO, PI_BAD_HCLK_FREQ,or PI_BAD_HCLK_PASS.

The same clock is available on multiple GPIO. The latest frequency setting will be used by all GPIO which share a clock.

The GPIO must be one of the following:

4 clock 0 All models 5 clock 1 All models but A and B (reserved for system use) 6 clock 2 All models but A and B 20 clock 0 All models but A and B 21 clock 1 All models but A and Rev.2 B (reserved for system use)

32 clock 0 Compute module only 34 clock 0 Compute module only 42 clock 1 Compute module only (reserved for system use) 43 clock 2 Compute module only 44 clock 1 Compute module only (reserved for system use)

Access to clock 1 is protected by a password as its use will likely crash the Pi. The password is given by or'ing 0x5A000000 with the GPIO number.

func (*PiGPIO) HardwarePWM

func (pi *PiGPIO) HardwarePWM(pin int32, pwmFrequency int32, pwmDutyCycle int32) error

HardwarePWM

Starts hardware PWM on a GPIO at the specified frequency and dutycycle. Frequencies above 30MHz are unlikely to work.

NOTE: Any waveform started by [*wave_send_once*], [*wave_send_repeat*], or [*wave_chain*] will be cancelled.

This function is only valid if the pigpio main clock is PCM. The main clock defaults to PCM but may be overridden when the pigpio daemon is started (option -t).

gpio:= see descripton pwmFrequency := 0 (off) or 1-125M (1-187.5M for the BCM2711). pwmDutyCycle := 0 (off) to 1000000 (1M)(fully on).

Returns 0 if OK, otherwise PI_NOT_PERMITTED, PI_BAD_GPIO, PI_NOT_HPWM_GPIO, PI_BAD_HPWM_DUTY, PI_BAD_HPWM_FREQ.

The same PWM channel is available on multiple GPIO. The latest frequency and dutycycle setting will be used by all GPIO which share a PWM channel.

The GPIO must be one of the following:

12 PWM channel 0 All models but A and B 13 PWM channel 1 All models but A and B 18 PWM channel 0 All models 19 PWM channel 1 All models but A and B

40 PWM channel 0 Compute module only 41 PWM channel 1 Compute module only 45 PWM channel 1 Compute module only 52 PWM channel 0 Compute module only 53 PWM channel 1 Compute module only

The actual number of steps beween off and fully on is the integral part of 250M/PWMfreq (375M/PWMfreq for the BCM2711).

The actual frequency set is 250M/steps (375M/steps for the BCM2711).

There will only be a million steps for a PWMfreq of 250 (375 for the BCM2711). Lower frequencies will have more steps and higher frequencies will have fewer steps. PWMduty is automatically scaled to take this into account.

func (*PiGPIO) I2CBatch

func (pi *PiGPIO) I2CBatch(handle int32, batchData []byte) (read int32, receivedData []byte, err error)

I2CBatch

This function executes a sequence of I2C operations. The operations to be performed are specified by the contents of data which contains the concatenated command codes and associated data.

handle := >=0 (as returned by a prior call to [*i2c_open*]). data := the concatenated I2C commands, see below

The returned value is a tuple of the number of bytes read and a bytearray containing the bytes. If there was an error the number of bytes read will be less than zero (and will contain the error code).

The following command codes are supported:

| Name | Cmd & Data | Meaning | | ------- | ---------- | --------------------------------- | | End | 0 | No more commands | | Escape | 1 | Next P is two bytes | | On | 2 | Switch combined flag on | | Off | 3 | Switch combined flag off | | Address | 4 P | Set I2C address to P | | Flags | 5 lsb msb | Set I2C flags to lsb + (msb << 8) | | Read | 6 P | Read P bytes of data | | Write | 7 P ... | Write P bytes of data |

The address, read, and write commands take a parameter P. Normally P is one byte (0-255). If the command is preceded by the Escape command then P is two bytes (0-65535, least significant byte first).

The address defaults to that associated with the handle. The flags default to 0. The address and flags maintain their previous value until updated.

Any read I2C data is concatenated in the returned bytearray.

func (*PiGPIO) I2CClose

func (pi *PiGPIO) I2CClose(handle int32) error

I2CClose

Closes the I2C device associated with handle.

handle := >=0 (as returned by a prior call to I2COpen).

func (*PiGPIO) I2COpen

func (pi *PiGPIO) I2COpen(bus int32, address int32, flags int32) (handle int32, err error)

I2COpen

Returns a handle (>=0) for the device at the I2C bus address.

bus := >=0. address := 0-0x7F. flags := 0, no flags are currently defined.

Physically buses 0 and 1 are available on the Pi. Higher numbered buses will be available if a kernel supported bus multiplexor is being used.

The GPIO used are given in the following table.

| | SDA | SCL | | ----- | --- | --- | | I2C 0 | 0 | 1 | | I2C 1 | 2 | 3 |

For the SMBus commands the low level transactions are shown at the end of the function description. The following abbreviations are used:

S (1 bit) : Start bit P (1 bit) : Stop bit Rd/Wr (1 bit) : Read/Write bit. Rd equals 1, Wr equals 0. A, NA (1 bit) : Accept and not accept bit. Addr (7 bits): I2C 7 bit address. reg (8 bits): Command byte, which often selects a register. Data (8 bits): A data byte. Count (8 bits): A byte defining the length of a block operation.

[..]: Data sent by the device.

func (*PiGPIO) I2CProcessCall

func (pi *PiGPIO) I2CProcessCall(handle int32, reg int32, wordValue int16) (data int32, err error)

I2CProcessCall

Writes 16 bits of data to the specified register of the device associated with handle and reads 16 bits of data in return.

handle := >=0 (as returned by a prior call to I2COpen). reg := >=0, the device register. wordValue := 0-65535, the value to write.

SMBus 2.0 5.5.6 - Process call.

func (*PiGPIO) I2CReadBlockData

func (pi *PiGPIO) I2CReadBlockData(handle int32, reg int32, count int32) (data []byte, err error)

I2CReadBlockData

Reads count bytes from the specified register of the device associated with handle . The count may be 1-32.

handle := >=0 (as returned by a prior call to I2COpen). reg := >=0, the device register. count := >0, the number of bytes to read.

func (*PiGPIO) I2CReadByte

func (pi *PiGPIO) I2CReadByte(handle int32) (value int32, err error)

I2CReadByte

Reads a single byte from the device associated with handle.

handle := >=0 (as returned by a prior call to I2COpen).

SMBus 2.0 5.5.3 - Receive byte.

func (*PiGPIO) I2CReadByteData

func (pi *PiGPIO) I2CReadByteData(handle int32, reg int32) (data int32, err error)

I2CReadByteData

Reads a single byte from the specified register of the device associated with handle.

handle := >= 0 (as returned by a prior call to I2COpen). reg := >= 0, the device register.

SMBus 2.0 5.5.5 - Read byte.

func (*PiGPIO) I2CReadDevice

func (pi *PiGPIO) I2CReadDevice(handle int32, count int32) (data []byte, err error)

I2CReadDevice

Returns count bytes read from the raw device associated with handle.

handle := >=0 (as returned by a prior call to I2COpen). count := >0, the number of bytes to read.

func (*PiGPIO) I2CReadWordData

func (pi *PiGPIO) I2CReadWordData(handle int32, reg int32) (data int32, err error)

I2CReadWordData

Reads a single 16 bit word from the specified register of the device associated with handle.

handle:= >= 0 (as returned by a prior call to I2COpen). reg:= >= 0, the device register.

SMBus 2.0 5.5.5 - Read word.

func (*PiGPIO) I2CWriteBlockData

func (pi *PiGPIO) I2CWriteBlockData(handle int32, reg int32, data []byte) error

I2CWriteBlockData

Writes up to 32 bytes to the specified register of the device associated with handle.

handle := >=0 (as returned by a prior call to I2COpen). reg := >=0, the device register. data := the bytes to write.

SMBus 2.0 5.5.7 - Block write.

func (*PiGPIO) I2CWriteByte

func (pi *PiGPIO) I2CWriteByte(handle int32, byteValue byte) error

I2CWriteByte

Sends a single byte to the device associated with handle.

handle := >=0 (as returned by a prior call to I2COpen). byte_val := 0-255, the value to write.

SMBus 2.0 5.5.2 - Send byte.

func (*PiGPIO) I2CWriteByteData

func (pi *PiGPIO) I2CWriteByteData(handle int32, reg int32, byteValue byte) error

I2CWriteByteData

Writes a single byte to the specified register of the device associated with handle.

handle := >=0 (as returned by a prior call to I2COpen). reg := >=0, the device register. byte_val := 0-255, the value to write.

SMBus 2.0 5.5.4 - Write byte.

func (*PiGPIO) I2CWriteDevice

func (pi *PiGPIO) I2CWriteDevice(handle int32, data []byte) error

I2CWriteDevice

Writes the data bytes to the raw device associated with handle.

handle := >=0 (as returned by a prior call to I2COpen). data := the bytes to write.

func (*PiGPIO) I2CWriteQuick

func (pi *PiGPIO) I2CWriteQuick(handle int32, bit bool) error

I2CWriteQuick

Sends a single bit to the device associated with handle.

handle := >=0 (as returned by a prior call to I2COpen). bit := 0 (false) or 1 (true), the value to write.

SMBus 2.0 5.5.1 - Quick command.

func (*PiGPIO) I2CWriteWordData

func (pi *PiGPIO) I2CWriteWordData(handle int32, reg int32, wordValue int16) error

I2CWriteWordData

Writes a single 16 bit word to the specified register of the device associated with handle.

handle:= >= 0 (as returned by a prior call to I2COpen). reg := >= 0, the device register. wordValue:= 0-65535, the value to write.

SMBus 2.0 5.5.4 - Write word.

func (*PiGPIO) Read

func (pi *PiGPIO) Read(pin int32) (level int32, err error)

Read

Returns the GPIO level.

pin := 0 - 53

func (*PiGPIO) ReadBank1

func (pi *PiGPIO) ReadBank1() (res int32, err error)

ReadBank1

Returns the levels of the bank 1 GPIO (GPIO 0 - 31).

The returned 32 bit integer has a bit set if the corresponding GPIO is high. GPIO n has bit value (1<<n).

func (*PiGPIO) ReadBank2

func (pi *PiGPIO) ReadBank2() (res int32, err error)

ReadBank2

Returns the levels of the bank 2 GPIO (GPIO 32 - 53).

The returned 32 bit integer has a bit set if the corresponding GPIO is high. GPIO n has bit value (1<<n-32).

func (*PiGPIO) SPIClose

func (pi *PiGPIO) SPIClose(handle int32) error

SPIClose

Closes the SPI device associated with handle.

handle := >=0 (as returned by a prior call to [*spi_open*]).

func (*PiGPIO) SPIOpen

func (pi *PiGPIO) SPIOpen(channel int32, baud int32, spiFlags int32) (handle int32, err error)

SPIOpen

Returns a handle for the SPI device on the channel. Data will be transferred at baud bits per second. The flags may be used to modify the default behaviour of 4-wire operation, mode 0, active low chip select.

The Pi has two SPI peripherals: main and auxiliary.

The main SPI has two chip selects (channels), the auxiliary has three.

The auxiliary SPI is available on all models but the A and B.

The GPIO used are given in the following table.

| | MISO | MOSI | SCLK | CE0 | CE1 | CE2 | | -------- | ---- | ---- | ---- | --- | --- | --- | | Main SPI | 9 | 10 | 11 | 8 | 7 | - | | Aux SPI | 19 | 20 | 21 | 18 | 17 | 16 |

spi_channel := 0-1 (0-2 for the auxiliary SPI). baud := 32K-125M (values above 30M are unlikely to work). spi_flags := see below.

spiFlags consists of the least significant 22 bits.

mm defines the SPI mode.

WARNING: modes 1 and 3 do not appear to work on the auxiliary SPI.

| Mode | POL | PHA | | ---- | --- | --- | | 0 | 0 | 0 | | 1 | 0 | 1 | | 2 | 1 | 0 | | 3 | 1 | 1 |

px is 0 if CEx is active low (default) and 1 for active high.

ux is 0 if the CEx GPIO is reserved for SPI (default) and 1 otherwise.

A is 0 for the main SPI, 1 for the auxiliary SPI.

W is 0 if the device is not 3-wire, 1 if the device is 3-wire. Main SPI only.

nnnn defines the number of bytes (0-15) to write before switching the MOSI line to MISO to read data. This field is ignored if W is not set. Main SPI only.

T is 1 if the least significant bit is transmitted on MOSI first, the default (0) shifts the most significant bit out first. Auxiliary SPI only.

R is 1 if the least significant bit is received on MISO first, the default (0) receives the most significant bit first. Auxiliary SPI only.

bbbbbb defines the word size in bits (0-32). The default (0) sets 8 bits per word. Auxiliary SPI only.

The [*spi_read*], [*spi_write*], and [*spi_xfer*] functions transfer data packed into 1, 2, or 4 bytes according to the word size in bits.

For bits 1-8 there will be one byte per character. For bits 9-16 there will be two bytes per character. For bits 17-32 there will be four bytes per character.

Multi-byte transfers are made in least significant byte first order.

E.g. to transfer 32 11-bit words data should contain 64 bytes.

E.g. to transfer the 14 bit value 0x1ABC send the bytes 0xBC followed by 0x1A.

The other bits in flags should be set to zero.

func (*PiGPIO) SPIRead

func (pi *PiGPIO) SPIRead(handle int32, count int32) (data []byte, err error)

SPIRead

Reads count bytes from the SPI device associated with handle.

handle := >=0 (as returned by a prior call to [*spi_open*]). count := >0, the number of bytes to read.

The returned value is a tuple of the number of bytes read and a bytearray containing the bytes. If there was an error the number of bytes read will be less than zero (and will contain the error code).

func (*PiGPIO) SPIWrite

func (pi *PiGPIO) SPIWrite(handle int32, data []byte) error

SPIWrite

Writes the data bytes to the SPI device associated with handle.

handle := >=0 (as returned by a prior call to [*spi_open*]). data := the bytes to write.

func (*PiGPIO) SPIXfer

func (pi *PiGPIO) SPIXfer(handle int32, data []byte) (read int32, receivedData []byte, err error)

SPIXfer

Writes the data bytes to the SPI device associated with handle, returning the data bytes read from the device.

handle := >=0 (as returned by a prior call to [*spi_open*]). data := the bytes to write.

The returned value is a tuple of the number of bytes read and a bytearray containing the bytes. If there was an error the number of bytes read will be less than zero (and will contain the error code).

func (*PiGPIO) ScriptDelete

func (pi *PiGPIO) ScriptDelete(handle int32) error

ScriptDelete

Deletes a stored script.

handle := id of stored script.

func (*PiGPIO) ScriptGetStatus

func (pi *PiGPIO) ScriptGetStatus(handle int32) (status ScriptStatus, err error)

ScriptGetStatus

Returns the run status of a stored script as well as the current values of parameters 0 to 9.

handle := id of stored script.

The run status may be

PI_SCRIPT_INITING PI_SCRIPT_HALTED PI_SCRIPT_RUNNING PI_SCRIPT_WAITING PI_SCRIPT_FAILED

The return value is a tuple of run status and a list of the 10 parameters. On error the run status will be negative and the parameter list will be empty.

func (*PiGPIO) ScriptRun

func (pi *PiGPIO) ScriptRun(handle int32, parameters ...int32) error

ScriptRun

Runs a stored script.

handle := id of stored script. parameters := up to 10 parameters required by the script.

func (*PiGPIO) ScriptStop

func (pi *PiGPIO) ScriptStop(handle int32) error

ScriptStop

Stops a running script.

handle := id of stored script.

func (*PiGPIO) ScriptStore

func (pi *PiGPIO) ScriptStore(script string) (handle int32, err error)

ScriptStore

Store a script for later execution.

See http://abyz.me.uk/rpi/pigpio/pigs.html#Scripts for details.

script := the script text as a series of bytes.

func (*PiGPIO) ScriptUpdate

func (pi *PiGPIO) ScriptUpdate(handle int32, parameters ...int32) error

ScriptUpdate

Sets the parameters of a script. The script may or may not be running. The first parameters of the script are overwritten with the new values.

handle := id of stored script. parameters := up to 10 parameters required by the script.

func (*PiGPIO) SerialClose

func (pi *PiGPIO) SerialClose(handle int32) error

SerialClose

Closes the serial device associated with handle.

handle := >=0 (as returned by a prior call to SerialOpen).

func (*PiGPIO) SerialDataAvailable

func (pi *PiGPIO) SerialDataAvailable(handle int32) (availableBytes int32, err error)

SerialDataAvailable

Returns the number of bytes available to be read from the device associated with handle.

handle:= >=0 (as returned by a prior call to [*serial_open*]).

func (*PiGPIO) SerialOpen

func (pi *PiGPIO) SerialOpen(tty string, baud int32, serialFlags int32) (handle int32, err error)

SerialOpen

Returns a handle for the serial tty device opened at baud bits per second. The device name must start with /dev/tty or /dev/serial.

tty := the serial device to open. baud := baud rate in bits per second, see below. serialFlags := 0, no flags are currently defined.

Normally you would only use the [*serial_**] functions if you are or will be connecting to the Pi over a network. If you will always run on the local Pi use the standard serial module instead.

The baud rate must be one of 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, 9600, 19200, 38400, 57600, 115200, or 230400.

func (*PiGPIO) SerialRead

func (pi *PiGPIO) SerialRead(handle int32, count int32) (data []byte, err error)

SerialRead

Reads up to count bytes from the device associated with handle.

handle:= >=0 (as returned by a prior call to [*serial_open*]). count:= >0, the number of bytes to read.

The returned value is a tuple of the number of bytes read and a bytearray containing the bytes. If there was an error the number of bytes read will be less than zero (and will contain the error code).

If no data is ready a bytes read of zero is returned.

func (*PiGPIO) SerialReadByte

func (pi *PiGPIO) SerialReadByte(handle int32) (b int32, err error)

SerialReadByte

Returns a single byte from the device associated with handle.

handle := >=0 (as returned by a prior call to SerialOpen).

If no data is ready a negative error code will be returned.

func (*PiGPIO) SerialWrite

func (pi *PiGPIO) SerialWrite(handle int32, data []byte) error

SerialWrite

Writes the data bytes to the device associated with handle.

handle := >=0 (as returned by a prior call to [*serial_open*]). data := the bytes to write.

func (*PiGPIO) SerialWriteByte

func (pi *PiGPIO) SerialWriteByte(handle int32, byteValue byte) error

SerialWriteByte

Writes a single byte to the device associated with handle.

handle := >=0 (as returned by a prior call to SerialOpen). byteValue := 0-255, the value to write.

func (*PiGPIO) SetMode

func (pi *PiGPIO) SetMode(pin int32, mode int32) (res int32, err error)

SetMode

Sets the GPIO mode

pin := 0 - 53 mode := ModeInput, ModeOutput, ModeAlt0, ModeAlt1, ModeAlt2, ModeAlt3, ModeAlt4, ModeAlt5

func (*PiGPIO) SetPWMDutyCycle

func (pi *PiGPIO) SetPWMDutyCycle(pin int32, dutyCycle int32) (res int32, err error)

SetPWMDutyCycle

Starts (non-zero duty cycle) or stops (0) PWM pulses on the GPIO.

pin := 0 - 31 dutyCycle := 0-range (range defaults to 255).

func (*PiGPIO) SetPWMFrequency

func (pi *PiGPIO) SetPWMFrequency(pin int32, frequency int32) error

SetPWMFrequency

Sets the frequency (in Hz) of the PWM to be used on the GPIO.

pin := 0 - 31 frequency := >=0 Hz

Returns the numerically closest frequency if OK, otherwise ErrBadUserGpio or ErrNotPermitted

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:

sr | 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
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

sr = sample rate (us)

func (*PiGPIO) SetPWMRange

func (pi *PiGPIO) SetPWMRange(pin int32, pwmRange int32) error

SetPWMRange

Sets the range of PWM values to be used on the GPIO.

pin := 0 - 31 pwmRange := 25 - 40000

func (*PiGPIO) SetPullUpDown

func (pi *PiGPIO) SetPullUpDown(pin int32, pud int32) error

SetPullUpDown

Sets or clears the internal GPIO pull-up/down resistor.

pin := 0 - 53 pud := PudUp, PudDown, PudOff

func (*PiGPIO) SetServoPulseWidth

func (pi *PiGPIO) SetServoPulseWidth(pin int32, pulseWidth int32) error

SetServoPulseWidth

Starts (500-2500) or stops (0) servo pulses on the GPIO.

pin := 0 - 31 pulsewidth := 0 (off), 500 (most anti-clockwise) - 2500 (most clockwise).

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.

func (*PiGPIO) Trigger

func (pi *PiGPIO) Trigger(pin int32, pulseLen int32, level int32) error

Trigger

Send a trigger pulse to a GPIO. The GPIO is set to level for pulse_len microseconds and then reset to not level.

pin := 0-31 pulseLen := 1-100 level := 0-1

func (*PiGPIO) Version

func (pi *PiGPIO) Version() (ver int32, err error)

Version

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.

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, ver == 0

func (*PiGPIO) WaveAddGeneric

func (pi *PiGPIO) WaveAddGeneric(pulses []Pulse) (totalPulses int32, err error)

WaveAddGeneric

Adds a list of pulses to the current waveform.

pulses := list of pulses to add to the waveform.

Returns the new total number of pulses in the current waveform.

The pulses are interleaved in time order within the existing waveform (if any).

Merging allows the waveform to be built in parts, that is the settings for GPIO#1 can be added, and then GPIO#2 etc.

If the added waveform is intended to start after or within the existing waveform then the first pulse should consist solely of a delay.

func (*PiGPIO) WaveAddNew

func (pi *PiGPIO) WaveAddNew() error

WaveAddNew

Starts a new empty waveform.

You would not normally need to call this function as it is automatically called after a waveform is created with the WaveCreate function.

func (*PiGPIO) WaveAddSerial

func (pi *PiGPIO) WaveAddSerial(pin int32, baud int32, data []byte, offset int32,
	dataBits int32, stopBits int32) (totalPulses int32, err error)

WaveAddSerial

Adds a waveform representing serial data to the existing waveform (if any). The serial data starts [*offset*] microseconds from the start of the waveform.

user_gpio:= GPIO to transmit data. You must set the GPIO mode to output. baud:= 50-1000000 bits per second. data:= the bytes to write. offset:= number of microseconds from the start of the waveform, default 0. dataBits:= number of data bits, default 8. stopBits:= number of stop half bits, default 2.

Returns the new total number of pulses in the current waveform.

The serial data is formatted as one start bit, dataBits and stopBits/2 stop bits.

It is legal to add serial data streams with different baud rates to the same waveform.

The bytes required for each character depend upon [*bb_bits*].

For dataBits 1-8 there will be one byte per character. For dataBits 9-16 there will be two bytes per character. For dataBits 17-32 there will be four bytes per character.

func (*PiGPIO) WaveChain

func (pi *PiGPIO) WaveChain(data []byte) error

WaveChain

This function transmits a chain of waveforms.

NOTE: Any hardware PWM started by [*hardware_PWM*] will be cancelled.

The waves to be transmitted are specified by the contents of data which contains an ordered list of WaveIds and optional command codes and related data.

Returns 0 if OK, otherwise PI_CHAIN_NESTING, PI_CHAIN_LOOP_CNT, PI_BAD_CHAIN_LOOP, PI_BAD_CHAIN_CMD, PI_CHAIN_COUNTER, PI_BAD_CHAIN_DELAY, PI_CHAIN_TOO_BIG, or PI_BAD_WAVE_ID.

Each wave is transmitted in the order specified. A wave may occur multiple times per chain.

A blocks of waves may be transmitted multiple times by using the loop commands. The block is bracketed by loop start and end commands. Loops may be nested.

Delays between waves may be added with the delay command.

The following command codes are supported:

| Name | Cmd & Data | Meaning | | ------------ | ---------- | ------- | | Loop Start | 255 0 | Identify start of a wave block | Loop Repeat | 255 1 x y | loop x + y*256 times | Delay | 255 2 x y | delay x + y*256 microseconds | Loop Forever | 255 3 | loop forever

If present Loop Forever must be the last entry in the chain.

The code is currently dimensioned to support a chain with roughly 600 entries and 20 loop counters.

func (*PiGPIO) WaveClear

func (pi *PiGPIO) WaveClear() error

WaveClear

Clears all waveforms and any data added by calls to the WaveAdd functions.

func (*PiGPIO) WaveCreate

func (pi *PiGPIO) WaveCreate() (waveId int32, err error)

WaveCreate

Creates a waveform from the data provided by the prior calls to the WaveAdd functions.

Returns a wave id (>=0) if OK, otherwise PI_EMPTY_WAVEFORM, PI_TOO_MANY_CBS, PI_TOO_MANY_OOL, or PI_NO_WAVEFORM_ID.

The data provided by the [*wave_add_**] functions is consumed by this function.

As many waveforms may be created as there is space available. The wave id is passed to [*wave_send_**] to specify the waveform to transmit.

Normal usage would be

Step 1. WaveClear to clear all waveforms and added data.

Step 2. WaveAdd calls to supply the waveform data.

Step 3. WaveCreate to create the waveform and get a unique id

Repeat steps 2 and 3 as needed.

Step 4. WaveSend with the id of the waveform to transmit.

A waveform comprises one or more pulses.

A pulse specifies

1) the GPIO to be switched on at the start of the pulse. 2) the GPIO to be switched off at the start of the pulse. 3) the delay in microseconds before the next pulse.

Any or all the fields can be zero. It doesn't make any sense to set all the fields to zero (the pulse will be ignored).

When a waveform is started each pulse is executed in order with the specified delay between the pulse and the next.

func (*PiGPIO) WaveCreateAndPad

func (pi *PiGPIO) WaveCreateAndPad(percent int32) (waveId int32, err error)

WaveCreateAndPad

This function creates a waveform like WaveCreate but pads the consumed resources. Where percent gives the percentage of the resources to use (in terms of the theoretical maximum, not the current amount free). This allows the reuse of deleted waves while a transmission is active.

Upon success a wave id greater than or equal to 0 is returned, otherwise PI_EMPTY_WAVEFORM, PI_TOO_MANY_CBS, PI_TOO_MANY_OOL, or PI_NO_WAVEFORM_ID.

percent: 0-100, size of waveform as percentage of maximum available.

The data provided by the WaveAdd functions are consumed by this function.

As many waveforms may be created as there is space available. The wave id is passed to WaveSend to specify the waveform to transmit.

A usage would be the creation of two waves where one is filled while the other is being transmitted. Each wave is assigned 50% of the resources. This buffer structure allows the transmission of infinite wave sequences.

Normal usage:

Step 1. WaveClear to clear all waveforms and added data.

Step 2. WaveAdd calls to supply the waveform data.

Step 3. WaveCreateAndPad to create a waveform of uniform size.

Step 4. WaveSend with the id of the waveform to transmit.

Repeat steps 2-4 as needed.

Step 5. Any wave id can now be deleted and another wave of the same size can be created in its place.

func (*PiGPIO) WaveDelete

func (pi *PiGPIO) WaveDelete(waveId int32) error

WaveDelete

This function deletes the waveform with id wave_id.

wave_id := >=0 (as returned by a prior call to WaveCreate).

Wave ids are allocated in order, 0, 1, 2, etc.

The wave is flagged for deletion. The resources used by the wave will only be reused when either of the following apply.

  • all waves with higher numbered wave ids have been deleted or have been flagged for deletion.

  • a new wave is created which uses exactly the same resources as the current wave (see the C source for gpioWaveCreate for details).

func (*PiGPIO) WaveSendOnce

func (pi *PiGPIO) WaveSendOnce(waveId int32) (dmaBlocks int32, err error)

WaveSendOnce

Transmits the waveform with id wave_id. The waveform is sent once.

NOTE: Any hardware PWM started by HardwarePwm will be cancelled.

wave_id := >=0 (as returned by a prior call to WaveCreate).

Returns the number of DMA control blocks used in the waveform.

func (*PiGPIO) WaveSendRepeat

func (pi *PiGPIO) WaveSendRepeat(waveId int32) (dmaBlocks int32, err error)

WaveSendRepeat

Transmits the waveform with id wave_id. The waveform repeats until wave_tx_stop is called or another call to WaveSend* is made.

NOTE: Any hardware PWM started by HardwarePwm will be cancelled.

wave_id := >=0 (as returned by a prior call to WaveCreate).

Returns the number of DMA control blocks used in the waveform.

func (*PiGPIO) WaveSendUsingMode

func (pi *PiGPIO) WaveSendUsingMode(waveId int32, mode int32) (dmaBlocks int32, err error)

WaveSendUsingMode

Transmits the waveform with id wave_id using mode mode.

wave_id := >=0 (as returned by a prior call to WaveCreate). mode := WaveModeOneShot, WaveModeRepeat, WaveModeOneShotSync, WaveModeRepeatSync

WaveModeOneShot: same as WaveSendOnce.

WaveModeRepeat: same as WaveSendRepeat.

WaveModeOneShotSync: same as WaveSendOnce but tries to sync with the previous waveform.

WaveModeRepeatSync: same as WaveSendRepeat but tries to sync with the previous waveform.

WARNING: bad things may happen if you delete the previous waveform before it has been synced to the new waveform.

NOTE: Any hardware PWM started by HardwarePwm will be cancelled.

Returns the number of DMA control blocks used in the waveform.

func (*PiGPIO) WaveTxAt

func (pi *PiGPIO) WaveTxAt() (waveFormId int32, err error)

WaveTxAt

Returns the id of the waveform currently being transmitted using [*wave_send**]. Chained waves are not supported.

Returns the waveform id or one of the following special values:

WAVE_NOT_FOUND (9998) - transmitted wave not found. NO_TX_WAVE (9999) - no wave being transmitted.

func (*PiGPIO) WaveTxBusy

func (pi *PiGPIO) WaveTxBusy() (isBusy bool, err error)

WaveTxBusy

Returns true if a waveform is currently being transmitted, otherwise false.

func (*PiGPIO) WaveTxStop

func (pi *PiGPIO) WaveTxStop() error

WaveTxStop

Stops the transmission of the current waveform.

This function is intended to stop a waveform started with WaveSendRepeat.

func (*PiGPIO) Write

func (pi *PiGPIO) Write(pin int32, level int32) error

Write

Sets the GPIO level.

pin := 0 - 53 level := 0, 1

If PWM or servo pulses are active on the GPIO they are switched off.

func (*PiGPIO) WriteBank1

func (pi *PiGPIO) WriteBank1(bits int32) error

WriteBank1

Sets GPIO 0-31 if the corresponding bit in bits is set.

bits := a 32 bit mask with 1 set if the corresponding GPIO is to be set.

func (*PiGPIO) WriteBank2

func (pi *PiGPIO) WriteBank2(bits int32) error

WriteBank2

Sets GPIO 32-53 if the corresponding bit (0-21) in bits is set.

bits := a 32 bit mask with 1 set if the corresponding GPIO is to be set.

type Pulse

type Pulse struct {
	GpioOn  int32
	GpioOff int32
	Delay   int32
}

Pulse information

func NewPulse

func NewPulse(gpioOn int32, gpioOff int32, delay int32) Pulse

NewPulse

Initialises a pulse.

gpio_on := the GPIO to switch on at the start of the pulse. gpio_off := the GPIO to switch off at the start of the pulse. delay := the delay in microseconds before the next pulse.

type ScriptStatus

type ScriptStatus struct {
	Status     int32
	Parameters []int32
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL