mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-07-26 02:28:41 +00:00
feat: add support for seesaw encoders
Adds the necessary function addresses for reading and writing encoders on a seesaw. Also provides two helper functions to make this easier.
This commit is contained in:
committed by
deadprogram
parent
a31ba26a6c
commit
857ab80ae6
@@ -0,0 +1,35 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"machine"
|
||||
"time"
|
||||
|
||||
"tinygo.org/x/drivers/seesaw"
|
||||
)
|
||||
|
||||
// example reading the position of a rotary encoder (4991) powered by a seesaw
|
||||
// https://learn.adafruit.com/adafruit-i2c-qt-rotary-encoder/arduino
|
||||
func main() {
|
||||
// This assumes you are using an Adafruit QT Py RP2040 for its Stemma QT connector
|
||||
// https://www.adafruit.com/product/4900
|
||||
i2c := machine.I2C1
|
||||
i2c.Configure(machine.I2CConfig{
|
||||
SCL: machine.I2C1_QT_SCL_PIN,
|
||||
SDA: machine.I2C1_QT_SDA_PIN,
|
||||
})
|
||||
|
||||
dev := seesaw.New(i2c)
|
||||
dev.Address = 0x36
|
||||
|
||||
for {
|
||||
time.Sleep(time.Second)
|
||||
|
||||
pos, err := dev.GetEncoderPosition(0, false)
|
||||
if err != nil {
|
||||
println(err)
|
||||
continue
|
||||
}
|
||||
|
||||
println(pos)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package seesaw
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
var errInvalidEncoderNumber = errors.New("invalid encoder choice, 0-15 are supported")
|
||||
|
||||
// GetEncoderPosition returns the absolute position (or delta since the previous call) of the specified rotary encoder.
|
||||
func (d *Device) GetEncoderPosition(encoder uint, asDelta bool) (int32, error) {
|
||||
if encoder >= 16 {
|
||||
return 0, errInvalidEncoderNumber
|
||||
}
|
||||
|
||||
// The function address' upper nibble is the function, the lower nibble selects which encoder to communicate with
|
||||
fnAddr := FunctionAddress(encoder)
|
||||
if asDelta {
|
||||
fnAddr |= FunctionEncoderDelta
|
||||
} else {
|
||||
fnAddr |= FunctionEncoderPosition
|
||||
}
|
||||
|
||||
var buf [4]byte
|
||||
err := d.Read(ModuleEncoderBase, fnAddr, buf[:])
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return int32(buf[0])<<24 | int32(buf[1])<<16 | int32(buf[2])<<8 | int32(buf[3]), nil
|
||||
}
|
||||
|
||||
// SetEncoderPosition calibrate's the encoder's current absolute position to be whatever the provided position is.
|
||||
func (d *Device) SetEncoderPosition(encoder uint, position int32) error {
|
||||
if encoder >= 16 {
|
||||
return errInvalidEncoderNumber
|
||||
}
|
||||
|
||||
// The function address' upper nibble is the function, the lower nibble selects which encoder to communicate with
|
||||
fnAddr := FunctionEncoderPosition | FunctionAddress(encoder)
|
||||
|
||||
buf := [4]byte{
|
||||
byte(position >> 24),
|
||||
byte(position >> 16),
|
||||
byte(position >> 8),
|
||||
byte(position),
|
||||
}
|
||||
|
||||
return d.Write(ModuleEncoderBase, fnAddr, buf[:])
|
||||
}
|
||||
@@ -98,3 +98,13 @@ const (
|
||||
FunctionKeypadCount FunctionAddress = 0x04
|
||||
FunctionKeypadFifo FunctionAddress = 0x10
|
||||
)
|
||||
|
||||
// encoder module function address registers
|
||||
// these are the defaults for encoder 0, change the lower nibble to address other encoders
|
||||
// see the Device.GetEncoderPosition and SetEncoderPosition methods for examples.
|
||||
const (
|
||||
FunctionEncoderIntenset FunctionAddress = 0x10
|
||||
FunctionEncoderIntenclr FunctionAddress = 0x20
|
||||
FunctionEncoderPosition FunctionAddress = 0x30
|
||||
FunctionEncoderDelta FunctionAddress = 0x40
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user