mirror of
https://github.com/stianeikeland/go-rpio.git
synced 2025-01-23 02:31:05 +01:00
Merge pull request #41 from CodeLingoBot/rewrite
Fix function comments based on best practices from Effective Go
This commit is contained in:
commit
a36b96d0b1
52
rpio.go
52
rpio.go
|
@ -159,32 +159,32 @@ var (
|
||||||
intrMem8 []uint8
|
intrMem8 []uint8
|
||||||
)
|
)
|
||||||
|
|
||||||
// Set pin as Input
|
// Input: Set pin as Input
|
||||||
func (pin Pin) Input() {
|
func (pin Pin) Input() {
|
||||||
PinMode(pin, Input)
|
PinMode(pin, Input)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set pin as Output
|
// Output: Set pin as Output
|
||||||
func (pin Pin) Output() {
|
func (pin Pin) Output() {
|
||||||
PinMode(pin, Output)
|
PinMode(pin, Output)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set pin as Clock
|
// Clock: Set pin as Clock
|
||||||
func (pin Pin) Clock() {
|
func (pin Pin) Clock() {
|
||||||
PinMode(pin, Clock)
|
PinMode(pin, Clock)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set pin as Pwm
|
// Pwm: Set pin as Pwm
|
||||||
func (pin Pin) Pwm() {
|
func (pin Pin) Pwm() {
|
||||||
PinMode(pin, Pwm)
|
PinMode(pin, Pwm)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set pin High
|
// High: Set pin High
|
||||||
func (pin Pin) High() {
|
func (pin Pin) High() {
|
||||||
WritePin(pin, High)
|
WritePin(pin, High)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set pin Low
|
// Low: Set pin Low
|
||||||
func (pin Pin) Low() {
|
func (pin Pin) Low() {
|
||||||
WritePin(pin, Low)
|
WritePin(pin, Low)
|
||||||
}
|
}
|
||||||
|
@ -194,22 +194,22 @@ func (pin Pin) Toggle() {
|
||||||
TogglePin(pin)
|
TogglePin(pin)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set frequency of Clock or Pwm pin (see doc of SetFreq)
|
// Freq: Set frequency of Clock or Pwm pin (see doc of SetFreq)
|
||||||
func (pin Pin) Freq(freq int) {
|
func (pin Pin) Freq(freq int) {
|
||||||
SetFreq(pin, freq)
|
SetFreq(pin, freq)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set duty cycle for Pwm pin (see doc of SetDutyCycle)
|
// DutyCycle: Set duty cycle for Pwm pin (see doc of SetDutyCycle)
|
||||||
func (pin Pin) DutyCycle(dutyLen, cycleLen uint32) {
|
func (pin Pin) DutyCycle(dutyLen, cycleLen uint32) {
|
||||||
SetDutyCycle(pin, dutyLen, cycleLen)
|
SetDutyCycle(pin, dutyLen, cycleLen)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set pin Mode
|
// Mode: Set pin Mode
|
||||||
func (pin Pin) Mode(mode Mode) {
|
func (pin Pin) Mode(mode Mode) {
|
||||||
PinMode(pin, mode)
|
PinMode(pin, mode)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set pin state (high/low)
|
// Write: Set pin state (high/low)
|
||||||
func (pin Pin) Write(state State) {
|
func (pin Pin) Write(state State) {
|
||||||
WritePin(pin, state)
|
WritePin(pin, state)
|
||||||
}
|
}
|
||||||
|
@ -219,32 +219,32 @@ func (pin Pin) Read() State {
|
||||||
return ReadPin(pin)
|
return ReadPin(pin)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set a given pull up/down mode
|
// Pull: Set a given pull up/down mode
|
||||||
func (pin Pin) Pull(pull Pull) {
|
func (pin Pin) Pull(pull Pull) {
|
||||||
PullMode(pin, pull)
|
PullMode(pin, pull)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pull up pin
|
// PullUp: Pull up pin
|
||||||
func (pin Pin) PullUp() {
|
func (pin Pin) PullUp() {
|
||||||
PullMode(pin, PullUp)
|
PullMode(pin, PullUp)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pull down pin
|
// PullDown: Pull down pin
|
||||||
func (pin Pin) PullDown() {
|
func (pin Pin) PullDown() {
|
||||||
PullMode(pin, PullDown)
|
PullMode(pin, PullDown)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Disable pullup/down on pin
|
// PullOff: Disable pullup/down on pin
|
||||||
func (pin Pin) PullOff() {
|
func (pin Pin) PullOff() {
|
||||||
PullMode(pin, PullOff)
|
PullMode(pin, PullOff)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Enable edge event detection on pin
|
// Detect: Enable edge event detection on pin
|
||||||
func (pin Pin) Detect(edge Edge) {
|
func (pin Pin) Detect(edge Edge) {
|
||||||
DetectEdge(pin, edge)
|
DetectEdge(pin, edge)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check edge event on pin
|
// EdgeDetected checks edge event on pin
|
||||||
func (pin Pin) EdgeDetected() bool {
|
func (pin Pin) EdgeDetected() bool {
|
||||||
return EdgeDetected(pin)
|
return EdgeDetected(pin)
|
||||||
}
|
}
|
||||||
|
@ -334,7 +334,7 @@ func WritePin(pin Pin, state State) {
|
||||||
memlock.Unlock() // not deferring saves ~600ns
|
memlock.Unlock() // not deferring saves ~600ns
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read the state of a pin
|
// ReadPin reads the state of a pin
|
||||||
func ReadPin(pin Pin) State {
|
func ReadPin(pin Pin) State {
|
||||||
// Input level register offset (13 / 14 depending on bank)
|
// Input level register offset (13 / 14 depending on bank)
|
||||||
levelReg := uint8(pin)/32 + 13
|
levelReg := uint8(pin)/32 + 13
|
||||||
|
@ -346,7 +346,7 @@ func ReadPin(pin Pin) State {
|
||||||
return Low
|
return Low
|
||||||
}
|
}
|
||||||
|
|
||||||
// Toggle a pin state (high -> low -> high)
|
// TogglePin: Toggle a pin state (high -> low -> high)
|
||||||
func TogglePin(pin Pin) {
|
func TogglePin(pin Pin) {
|
||||||
p := uint8(pin)
|
p := uint8(pin)
|
||||||
|
|
||||||
|
@ -366,7 +366,7 @@ func TogglePin(pin Pin) {
|
||||||
memlock.Unlock()
|
memlock.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Enable edge event detection on pin.
|
// DetectEdge: Enable edge event detection on pin.
|
||||||
//
|
//
|
||||||
// Combine with pin.EdgeDetected() to check whether event occured.
|
// Combine with pin.EdgeDetected() to check whether event occured.
|
||||||
//
|
//
|
||||||
|
@ -411,7 +411,7 @@ func DetectEdge(pin Pin, edge Edge) {
|
||||||
gpioMem[edsReg] = bit // to clear outdated detection
|
gpioMem[edsReg] = bit // to clear outdated detection
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check whether edge event occured since last call
|
// EdgeDetected checks whether edge event occured since last call
|
||||||
// or since detection was enabled
|
// or since detection was enabled
|
||||||
//
|
//
|
||||||
// There is no way (yet) to handle interruption caused by edge event, you have to use polling.
|
// There is no way (yet) to handle interruption caused by edge event, you have to use polling.
|
||||||
|
@ -457,7 +457,7 @@ func PullMode(pin Pin, pull Pull) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set clock speed for given pin in Clock or Pwm mode
|
// SetFreq: Set clock speed for given pin in Clock or Pwm mode
|
||||||
//
|
//
|
||||||
// Param freq should be in range 4688Hz - 19.2MHz to prevent unexpected behavior,
|
// Param freq should be in range 4688Hz - 19.2MHz to prevent unexpected behavior,
|
||||||
// however output frequency of Pwm pins can be further adjusted with SetDutyCycle.
|
// however output frequency of Pwm pins can be further adjusted with SetDutyCycle.
|
||||||
|
@ -531,7 +531,7 @@ func SetFreq(pin Pin, freq int) {
|
||||||
// NOTE without root permission this changes will simply do nothing successfully
|
// NOTE without root permission this changes will simply do nothing successfully
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set cycle length (range) and duty length (data) for Pwm pin in M/S mode
|
// SetDutyCycle: Set cycle length (range) and duty length (data) for Pwm pin in M/S mode
|
||||||
//
|
//
|
||||||
// |<- duty ->|
|
// |<- duty ->|
|
||||||
// __________
|
// __________
|
||||||
|
@ -585,21 +585,21 @@ func SetDutyCycle(pin Pin, dutyLen, cycleLen uint32) {
|
||||||
// NOTE without root permission this changes will simply do nothing successfully
|
// NOTE without root permission this changes will simply do nothing successfully
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stop pwm for both channels
|
// StopPwm: Stop pwm for both channels
|
||||||
func StopPwm() {
|
func StopPwm() {
|
||||||
const pwmCtlReg = 0
|
const pwmCtlReg = 0
|
||||||
const pwen = 1
|
const pwen = 1
|
||||||
pwmMem[pwmCtlReg] &^= pwen<<8 | pwen
|
pwmMem[pwmCtlReg] &^= pwen<<8 | pwen
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start pwm for both channels
|
// StartPwm starts pwm for both channels
|
||||||
func StartPwm() {
|
func StartPwm() {
|
||||||
const pwmCtlReg = 0
|
const pwmCtlReg = 0
|
||||||
const pwen = 1
|
const pwen = 1
|
||||||
pwmMem[pwmCtlReg] |= pwen<<8 | pwen
|
pwmMem[pwmCtlReg] |= pwen<<8 | pwen
|
||||||
}
|
}
|
||||||
|
|
||||||
// Enables given IRQs (by setting bit to 1 at intended position).
|
// EnableIRQs: Enables given IRQs (by setting bit to 1 at intended position).
|
||||||
// See 'ARM peripherals interrupts table' in pheripherals datasheet.
|
// See 'ARM peripherals interrupts table' in pheripherals datasheet.
|
||||||
// WARNING: you can corrupt your system, only use this if you know what you are doing.
|
// WARNING: you can corrupt your system, only use this if you know what you are doing.
|
||||||
func EnableIRQs(irqs uint64) {
|
func EnableIRQs(irqs uint64) {
|
||||||
|
@ -609,7 +609,7 @@ func EnableIRQs(irqs uint64) {
|
||||||
intrMem[irqEnable2] = uint32(irqs >> 32) // IRQ 32..63
|
intrMem[irqEnable2] = uint32(irqs >> 32) // IRQ 32..63
|
||||||
}
|
}
|
||||||
|
|
||||||
// Disables given IRQs (by setting bit to 1 at intended position).
|
// DisableIRQs: Disables given IRQs (by setting bit to 1 at intended position).
|
||||||
// See 'ARM peripherals interrupts table' in pheripherals datasheet.
|
// See 'ARM peripherals interrupts table' in pheripherals datasheet.
|
||||||
// WARNING: you can corrupt your system, only use this if you know what you are doing.
|
// WARNING: you can corrupt your system, only use this if you know what you are doing.
|
||||||
func DisableIRQs(irqs uint64) {
|
func DisableIRQs(irqs uint64) {
|
||||||
|
|
14
spi.go
14
spi.go
|
@ -24,7 +24,7 @@ var (
|
||||||
SpiMapError = errors.New("SPI registers not mapped correctly - are you root?")
|
SpiMapError = errors.New("SPI registers not mapped correctly - are you root?")
|
||||||
)
|
)
|
||||||
|
|
||||||
// Sets all pins of given SPI device to SPI mode
|
// SpiBegin: Sets all pins of given SPI device to SPI mode
|
||||||
// dev\pin | CE0 | CE1 | CE2 | SCLK | MOSI | MISO |
|
// dev\pin | CE0 | CE1 | CE2 | SCLK | MOSI | MISO |
|
||||||
// Spi0 | 7 | 8 | - | 9 | 10 | 11 |
|
// Spi0 | 7 | 8 | - | 9 | 10 | 11 |
|
||||||
// Spi1 | 16 | 17 | 18 | 19 | 20 | 21 |
|
// Spi1 | 16 | 17 | 18 | 19 | 20 | 21 |
|
||||||
|
@ -49,7 +49,7 @@ func SpiBegin(dev SpiDev) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sets SPI pins of given device to default (Input) mode. See SpiBegin.
|
// SpiEnd: Sets SPI pins of given device to default (Input) mode. See SpiBegin.
|
||||||
func SpiEnd(dev SpiDev) {
|
func SpiEnd(dev SpiDev) {
|
||||||
var pins = getSpiPins(dev)
|
var pins = getSpiPins(dev)
|
||||||
for _, pin := range pins {
|
for _, pin := range pins {
|
||||||
|
@ -57,7 +57,7 @@ func SpiEnd(dev SpiDev) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set (maximal) speed [Hz] of SPI clock.
|
// SpiSpeed: Set (maximal) speed [Hz] of SPI clock.
|
||||||
// Param speed may be as big as 125MHz in theory, but
|
// Param speed may be as big as 125MHz in theory, but
|
||||||
// only values up to 31.25MHz are considered relayable.
|
// only values up to 31.25MHz are considered relayable.
|
||||||
func SpiSpeed(speed int) {
|
func SpiSpeed(speed int) {
|
||||||
|
@ -66,7 +66,7 @@ func SpiSpeed(speed int) {
|
||||||
setSpiDiv(cdiv)
|
setSpiDiv(cdiv)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Select chip, one of 0, 1, 2
|
// SpiChipSelect: Select chip, one of 0, 1, 2
|
||||||
// for selecting slave on CE0, CE1, or CE2 pin
|
// for selecting slave on CE0, CE1, or CE2 pin
|
||||||
func SpiChipSelect(chip uint8) {
|
func SpiChipSelect(chip uint8) {
|
||||||
const csMask = 3 // chip select has 2 bits
|
const csMask = 3 // chip select has 2 bits
|
||||||
|
@ -76,7 +76,7 @@ func SpiChipSelect(chip uint8) {
|
||||||
spiMem[csReg] = spiMem[csReg]&^csMask | cs
|
spiMem[csReg] = spiMem[csReg]&^csMask | cs
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sets polarity (0/1) of active chip select
|
// SpiChipSelectPolarity: Sets polarity (0/1) of active chip select
|
||||||
// default active=0
|
// default active=0
|
||||||
func SpiChipSelectPolarity(chip uint8, polarity uint8) {
|
func SpiChipSelectPolarity(chip uint8, polarity uint8) {
|
||||||
if chip > 2 {
|
if chip > 2 {
|
||||||
|
@ -91,7 +91,7 @@ func SpiChipSelectPolarity(chip uint8, polarity uint8) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set polarity (0/1) and phase (0/1) of spi clock
|
// SpiMode: Set polarity (0/1) and phase (0/1) of spi clock
|
||||||
// default polarity=0; phase=0
|
// default polarity=0; phase=0
|
||||||
func SpiMode(polarity uint8, phase uint8) {
|
func SpiMode(polarity uint8, phase uint8) {
|
||||||
const cpol = 1 << 3
|
const cpol = 1 << 3
|
||||||
|
@ -127,7 +127,7 @@ func SpiReceive(n int) []byte {
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
// Transmit all bytes in data to slave
|
// SpiExchange: Transmit all bytes in data to slave
|
||||||
// and simultaneously receives bytes from slave to data.
|
// and simultaneously receives bytes from slave to data.
|
||||||
//
|
//
|
||||||
// If you want to only send or only receive, use SpiTransmit/SpiReceive
|
// If you want to only send or only receive, use SpiTransmit/SpiReceive
|
||||||
|
|
Loading…
Reference in New Issue
Block a user