Compare commits

...

4 Commits

Author SHA1 Message Date
soypat 25eecd27d1 whoops, forgot to set isOut 2025-10-13 18:32:25 -03:00
soypat 293de2b61a show how one-wire driver could be rewritten to use function HAL 2025-10-13 18:23:21 -03:00
Patricio Whittingslow 3183013cbb whoops, forgot to change easystepper method calls 2025-09-16 17:59:29 -03:00
Patricio Whittingslow 876a3150f3 legacy.PinOutput->pin.Output and add High+Low methods on drivers.PinOutput and use them in drivers 2025-09-16 17:53:18 -03:00
33 changed files with 382 additions and 310 deletions
+2 -1
View File
@@ -8,6 +8,7 @@ import (
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
)
const (
@@ -37,7 +38,7 @@ func New(b drivers.SPI) *Device {
// NewSoftwareSPI returns a new APA102 driver that will use a software based
// implementation of the SPI protocol.
func NewSoftwareSPI(sckPin, sdoPin legacy.PinOutput, delay uint32) *Device {
func NewSoftwareSPI(sckPin, sdoPin pin.Output, delay uint32) *Device {
return New(&bbSPI{SCK: sckPin.Set, SDO: sdoPin.Set, Delay: delay, configurePins: func() {
legacy.ConfigurePinOut(sckPin)
legacy.ConfigurePinOut(sdoPin)
+6 -6
View File
@@ -23,8 +23,8 @@ func (s *bbSPI) Configure() {
panic(legacy.ErrConfigBeforeInstantiated)
}
s.configurePins()
s.SCK(false)
s.SDO(false)
s.SCK.Low()
s.SDO.Low()
if s.Delay == 0 {
s.Delay = 1
}
@@ -53,19 +53,19 @@ func (s *bbSPI) Transfer(b byte) (byte, error) {
for i := uint8(0); i < 8; i++ {
// half clock cycle high to start
s.SCK(true)
s.SCK.High()
s.delay()
// write the value to SDO (MSB first)
if b&(1<<(7-i)) == 0 {
s.SDO(false)
s.SDO.Low()
} else {
s.SDO(true)
s.SDO.High()
}
s.delay()
// half clock cycle low
s.SCK(false)
s.SCK.Low()
s.delay()
// for actual SPI would try to read the SDI value here
+13 -12
View File
@@ -5,6 +5,7 @@ import (
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
)
// DeviceSPI is the SPI interface to a BMI160 accelerometer/gyroscope. There is
@@ -23,7 +24,7 @@ type DeviceSPI struct {
// NewSPI returns a new device driver. The pin and SPI interface are not
// touched, provide a fully configured SPI object and call Configure to start
// using this device.
func NewSPI(csb legacy.PinOutput, spi drivers.SPI) *DeviceSPI {
func NewSPI(csb pin.Output, spi drivers.SPI) *DeviceSPI {
return &DeviceSPI{
csb: csb.Set, // chip select
bus: spi,
@@ -41,7 +42,7 @@ func (d *DeviceSPI) Configure() error {
return legacy.ErrConfigBeforeInstantiated
}
d.configurePins()
d.csb(true)
d.csb.High()
// The datasheet recommends doing a register read from address 0x7F to get
// SPI communication going:
@@ -93,9 +94,9 @@ func (d *DeviceSPI) ReadTemperature() (temperature int32, err error) {
data[0] = 0x80 | reg_TEMPERATURE_0
data[1] = 0
data[2] = 0
d.csb(false)
d.csb.Low()
err = d.bus.Tx(data, data)
d.csb(true)
d.csb.High()
if err != nil {
return
}
@@ -130,9 +131,9 @@ func (d *DeviceSPI) ReadAcceleration() (x int32, y int32, z int32, err error) {
for i := 1; i < len(data); i++ {
data[i] = 0
}
d.csb(false)
d.csb.Low()
err = d.bus.Tx(data, data)
d.csb(true)
d.csb.High()
if err != nil {
return
}
@@ -160,9 +161,9 @@ func (d *DeviceSPI) ReadRotation() (x int32, y int32, z int32, err error) {
for i := 1; i < len(data); i++ {
data[i] = 0
}
d.csb(false)
d.csb.Low()
err = d.bus.Tx(data, data)
d.csb(true)
d.csb.High()
if err != nil {
return
}
@@ -208,9 +209,9 @@ func (d *DeviceSPI) readRegister(address uint8) uint8 {
data := d.buf[:2]
data[0] = 0x80 | address
data[1] = 0
d.csb(false)
d.csb.Low()
d.bus.Tx(data, data)
d.csb(true)
d.csb.High()
return data[1]
}
@@ -224,7 +225,7 @@ func (d *DeviceSPI) writeRegister(address, data uint8) {
buf[0] = address
buf[1] = data
d.csb(false)
d.csb.Low()
d.bus.Tx(buf, buf)
d.csb(true)
d.csb.High()
}
+4 -4
View File
@@ -5,7 +5,7 @@ import (
"time"
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
)
// Device wraps a GPIO connection to a buzzer.
@@ -16,7 +16,7 @@ type Device struct {
}
// New returns a new buzzer driver given which pin to use
func New(pin legacy.PinOutput) Device {
func New(pin pin.Output) Device {
return Device{
pin: pin.Set,
High: false,
@@ -26,14 +26,14 @@ func New(pin legacy.PinOutput) Device {
// On sets the buzzer to a high state.
func (l *Device) On() (err error) {
l.pin(true)
l.pin.High()
l.High = true
return
}
// Off sets the buzzer to a low state.
func (l *Device) Off() (err error) {
l.pin(false)
l.pin.Low()
l.High = false
return
}
+49 -49
View File
@@ -63,7 +63,7 @@ func (d *Device) Move(steps int32) {
// Off turns off all motor pins
func (d *Device) Off() {
for _, pin := range d.pins {
pin(false)
pin.Low()
}
}
@@ -124,28 +124,28 @@ func (d *Device) stepMotor(step uint8) {
func (d *Device) stepMotor4(step uint8) {
switch step {
case 0:
d.pins[0](true)
d.pins[1](false)
d.pins[2](true)
d.pins[3](false)
d.pins[0].High()
d.pins[1].Low()
d.pins[2].High()
d.pins[3].Low()
break
case 1:
d.pins[0](false)
d.pins[1](true)
d.pins[2](true)
d.pins[3](false)
d.pins[0].Low()
d.pins[1].High()
d.pins[2].High()
d.pins[3].Low()
break
case 2:
d.pins[0](false)
d.pins[1](true)
d.pins[2](false)
d.pins[3](true)
d.pins[0].Low()
d.pins[1].High()
d.pins[2].Low()
d.pins[3].High()
break
case 3:
d.pins[0](true)
d.pins[1](false)
d.pins[2](false)
d.pins[3](true)
d.pins[0].High()
d.pins[1].Low()
d.pins[2].Low()
d.pins[3].High()
break
}
d.stepNumber = step
@@ -155,45 +155,45 @@ func (d *Device) stepMotor4(step uint8) {
func (d *Device) stepMotor8(step uint8) {
switch step {
case 0:
d.pins[0](true)
d.pins[2](false)
d.pins[1](false)
d.pins[3](false)
d.pins[0].High()
d.pins[2].Low()
d.pins[1].Low()
d.pins[3].Low()
case 1:
d.pins[0](true)
d.pins[2](true)
d.pins[1](false)
d.pins[3](false)
d.pins[0].High()
d.pins[2].High()
d.pins[1].Low()
d.pins[3].Low()
case 2:
d.pins[0](false)
d.pins[2](true)
d.pins[1](false)
d.pins[3](false)
d.pins[0].Low()
d.pins[2].High()
d.pins[1].Low()
d.pins[3].Low()
case 3:
d.pins[0](false)
d.pins[2](true)
d.pins[1](true)
d.pins[3](false)
d.pins[0].Low()
d.pins[2].High()
d.pins[1].High()
d.pins[3].Low()
case 4:
d.pins[0](false)
d.pins[2](false)
d.pins[1](true)
d.pins[3](false)
d.pins[0].Low()
d.pins[2].Low()
d.pins[1].High()
d.pins[3].Low()
case 5:
d.pins[0](false)
d.pins[2](false)
d.pins[1](true)
d.pins[3](true)
d.pins[0].Low()
d.pins[2].Low()
d.pins[1].High()
d.pins[3].High()
case 6:
d.pins[0](false)
d.pins[2](false)
d.pins[1](false)
d.pins[3](true)
d.pins[0].Low()
d.pins[2].Low()
d.pins[1].Low()
d.pins[3].High()
case 7:
d.pins[0](true)
d.pins[2](false)
d.pins[1](false)
d.pins[3](true)
d.pins[0].High()
d.pins[2].Low()
d.pins[1].Low()
d.pins[3].High()
}
d.stepNumber = step
}
+2 -1
View File
@@ -7,6 +7,7 @@ package ft6336
import (
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
"tinygo.org/x/drivers/touch"
)
@@ -19,7 +20,7 @@ type Device struct {
}
// New returns FT6336 device for the provided I2C bus using default address.
func New(i2c drivers.I2C, intPin legacy.PinInput) *Device {
func New(i2c drivers.I2C, intPin pin.Input) *Device {
return &Device{
bus: i2c,
buf: make([]byte, 11),
+12 -11
View File
@@ -11,6 +11,7 @@ import (
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
)
// Rotation controls the rotation used by the display.
@@ -52,7 +53,7 @@ type Config struct {
}
// New creates a new ST7789 connection. The SPI wire must already be configured.
func New(bus drivers.SPI, resetPin, dcPin, csPin, blPin legacy.PinOutput) Device {
func New(bus drivers.SPI, resetPin, dcPin, csPin, blPin pin.Output) Device {
legacy.ConfigurePinOut(resetPin)
legacy.ConfigurePinOut(dcPin)
legacy.ConfigurePinOut(csPin)
@@ -68,11 +69,11 @@ func New(bus drivers.SPI, resetPin, dcPin, csPin, blPin legacy.PinOutput) Device
// Reset the Device
func (d *Device) Reset() {
d.resetPin(true)
d.resetPin.High()
time.Sleep(100 * time.Millisecond)
d.resetPin(false)
d.resetPin.Low()
time.Sleep(100 * time.Millisecond)
d.resetPin(true)
d.resetPin.High()
time.Sleep(100 * time.Millisecond)
}
@@ -232,14 +233,14 @@ func (d *Device) Tx(data []byte, isCommand bool) {
// Rx reads data from the display
func (d *Device) Rx(command uint8, data []byte) {
d.dcPin(false)
d.csPin(false)
d.dcPin.Low()
d.csPin.Low()
d.bus.Transfer(command)
d.dcPin(true)
d.dcPin.High()
for i := range data {
data[i], _ = d.bus.Transfer(0xFF)
}
d.csPin(true)
d.csPin.High()
}
// Size returns the current size of the display.
@@ -250,9 +251,9 @@ func (d *Device) Size() (w, h int16) {
// EnableBacklight enables or disables the backlight
func (d *Device) EnableBacklight(enable bool) {
if enable {
d.blPin(true)
d.blPin.High()
} else {
d.blPin(false)
d.blPin.Low()
}
}
@@ -563,5 +564,5 @@ func (d *Device) Configure(cfg Config) {
d.Command(DISPON)
time.Sleep(20 * time.Millisecond)
d.blPin(true)
d.blPin.High()
}
+5 -4
View File
@@ -9,6 +9,7 @@ import (
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
)
const TIMEOUT = 23324 // max sensing distance (4m)
@@ -21,7 +22,7 @@ type Device struct {
}
// New returns a new ultrasonic driver given 2 pins
func New(trigger legacy.PinOutput, echo legacy.PinInput) Device {
func New(trigger pin.Output, echo pin.Input) Device {
return Device{
trigger: trigger.Set,
echo: echo.Get,
@@ -54,11 +55,11 @@ func (d *Device) ReadDistance() int32 {
// ReadPulse returns the time of the pulse (roundtrip) in microseconds
func (d *Device) ReadPulse() int32 {
t := time.Now()
d.trigger(false)
d.trigger.Low()
time.Sleep(2 * time.Microsecond)
d.trigger(true)
d.trigger.High()
time.Sleep(10 * time.Microsecond)
d.trigger(false)
d.trigger.Low()
i := uint8(0)
for {
if d.echo() {
+8 -29
View File
@@ -1,38 +1,17 @@
package legacy
import "errors"
import (
"errors"
// PinOutput represents a pin hardware abstraction layer for a pin that can output a digital signal.
// This is an alternative to drivers.PinOutput abstraction which is a function type. Pros and cons
// of both approaches have been discussed in the [relevant issue]. PinOutput should only be used
// to expose an initialization function of a driver that receives pins of this type. Ideally
// driver developers should also expose the initialization with drivers.Pin type:
//
// func New(p1, p2, p3 legacy.PinOutput) *Device {
// return NewWithPinfuncs(p1.Set, p2.Set, p3.Set)
// }
//
// func NewWithPinfuncs(p1, p2, p3 drivers.PinOutput) *Device {
// return &Device{p1:p1, p2:p2, p3:p3}
// }
//
// [relevant issue]: https://github.com/tinygo-org/drivers/pull/749/files
type PinOutput interface {
Set(level bool)
}
// PinInput represents a pin hardware abstraction layer. See [PinOutput] for
// more information on why this is "legacy".
type PinInput interface {
Get() (level bool)
}
"tinygo.org/x/drivers/internal/pin"
)
// ConfigurePinOut is a legacy function used to configure pins as outputs.
//
// Deprecated: Do not configure pins in drivers.
// This is a legacy feature and should only be used by drivers that
// previously configured pins in initialization to avoid breaking users.
func ConfigurePinOut(po PinOutput) {
func ConfigurePinOut(po pin.Output) {
configurePinOut(po)
}
@@ -41,7 +20,7 @@ func ConfigurePinOut(po PinOutput) {
// Deprecated: Do not configure pins in drivers.
// This is a legacy feature and should only be used by drivers that
// previously configured pins in initialization to avoid breaking users.
func ConfigurePinInputPulldown(pi PinInput) {
func ConfigurePinInputPulldown(pi pin.Input) {
configurePinInputPulldown(pi)
}
@@ -50,7 +29,7 @@ func ConfigurePinInputPulldown(pi PinInput) {
// Deprecated: Do not configure pins in drivers.
// This is a legacy feature and should only be used by drivers that
// previously configured pins in initialization to avoid breaking users.
func ConfigurePinInput(pi PinInput) {
func ConfigurePinInput(pi pin.Input) {
configurePinInput(pi)
}
@@ -59,7 +38,7 @@ func ConfigurePinInput(pi PinInput) {
// Deprecated: Do not configure pins in drivers.
// This is a legacy feature and should only be used by drivers that
// previously configured pins in initialization to avoid breaking users.
func ConfigurePinInputPullup(pi PinInput) {
func ConfigurePinInputPullup(pi pin.Input) {
configurePinInputPullup(pi)
}
+7 -5
View File
@@ -2,8 +2,10 @@
package legacy
func configurePinOut(p PinOutput) {}
func configurePinInput(p PinInput) {}
func configurePinInputPulldown(p PinInput) {}
func configurePinInputPullup(p PinInput) {}
func pinIsNoPin(a any) bool { return false }
import "tinygo.org/x/drivers/internal/pin"
func configurePinOut(p pin.Output) {}
func configurePinInput(p pin.Input) {}
func configurePinInputPulldown(p pin.Input) {}
func configurePinInputPullup(p pin.Input) {}
func pinIsNoPin(a any) bool { return false }
+13 -9
View File
@@ -2,22 +2,26 @@
package legacy
import "machine"
import (
"machine"
func configurePinOut(p PinOutput) {
configurePin(p, machine.PinOutput)
"tinygo.org/x/drivers/internal/pin"
)
func configurePinOut(po pin.Output) {
configurePin(po, machine.PinOutput)
}
func configurePinInputPulldown(p PinInput) {
configurePin(p, pulldown) // some chips do not have pull down, in which case pulldown==machine.PinInput.
func configurePinInputPulldown(pi pin.Input) {
configurePin(pi, pulldown) // some chips do not have pull down, in which case pulldown==machine.PinInput.
}
func configurePinInput(p PinInput) {
configurePin(p, machine.PinInput)
func configurePinInput(pi pin.Input) {
configurePin(pi, machine.PinInput)
}
func configurePinInputPullup(p PinInput) {
configurePin(p, pullup) // some chips do not have pull up, in which case pullup==machine.PinInput.
func configurePinInputPullup(pi pin.Input) {
configurePin(pi, pullup) // some chips do not have pull up, in which case pullup==machine.PinInput.
}
func pinIsNoPin(a any) bool {
+23
View File
@@ -0,0 +1,23 @@
package pin
import "tinygo.org/x/drivers"
// Here to aid relevant documentation links of [drivers.PinOutput] and [drivers.PinInput].
var _ drivers.PinOutput
// Output represents a pin hardware abstraction layer for a pin that can output a digital signal.
//
// This is an alternative to [drivers.PinOutput] abstraction which is a function type and has
// not been standardized as of yet as a standard HAL in the drivers package,
// [discussion ongoing here].
//
// [discussion ongoing here]: https://github.com/orgs/tinygo-org/discussions/5043
type Output interface {
Set(level bool)
}
// Input represents a pin hardware abstraction layer.
// See [Output] for more information on why this type exists separate to drivers.
type Input interface {
Get() (level bool)
}
+4 -4
View File
@@ -5,7 +5,7 @@ import (
"errors"
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
)
// ErrThermocoupleOpen is returned when the thermocouple input is open.
@@ -20,7 +20,7 @@ type Device struct {
// Create a new Device to read from a MAX6675 thermocouple.
// Pins must be configured before use. Frequency for SPI
// should be 4.3MHz maximum.
func NewDevice(bus drivers.SPI, cs legacy.PinOutput) *Device {
func NewDevice(bus drivers.SPI, cs pin.Output) *Device {
return &Device{
bus: bus,
cs: cs.Set,
@@ -34,11 +34,11 @@ func (d *Device) Read() (float32, error) {
value uint16
)
d.cs(false)
d.cs.Low()
if err := d.bus.Tx([]byte{0, 0}, read); err != nil {
return 0, err
}
d.cs(true)
d.cs.High()
// datasheet: Bit D2 is normally low and goes high if the thermocouple input is open.
if read[1]&0x04 == 0x04 {
+4 -3
View File
@@ -5,6 +5,7 @@ package max72xx
import (
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
)
type Device struct {
@@ -16,7 +17,7 @@ type Device struct {
// NewDriver creates a new max7219 connection. The SPI wire must already be configured
// The SPI frequency must not be higher than 10MHz.
// parameter cs: the datasheet also refers to this pin as "load" pin.
func NewDevice(bus drivers.SPI, cs legacy.PinOutput) *Device {
func NewDevice(bus drivers.SPI, cs pin.Output) *Device {
return &Device{
bus: bus,
cs: cs.Set,
@@ -93,8 +94,8 @@ func (driver *Device) writeByte(data byte) {
// WriteCommand write data to a given register.
func (driver *Device) WriteCommand(register, data byte) {
driver.cs(false)
driver.cs.Low()
driver.writeByte(register)
driver.writeByte(data)
driver.cs(true)
driver.cs.High()
}
+19 -18
View File
@@ -12,6 +12,7 @@ import (
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
)
// Device wraps MCP2515 SPI CAN Module.
@@ -37,7 +38,7 @@ const (
)
// New returns a new MCP2515 driver. Pass in a fully configured SPI bus.
func New(b drivers.SPI, csPin legacy.PinOutput) *Device {
func New(b drivers.SPI, csPin pin.Output) *Device {
d := &Device{
spi: SPI{
bus: b,
@@ -166,9 +167,9 @@ func (d *Device) init(speed, clock byte) error {
// Reset resets mcp2515.
func (d *Device) Reset() error {
d.cs(false)
d.cs.Low()
_, err := d.spi.readWrite(mcpReset)
d.cs(true)
d.cs.High()
// time.Sleep(time.Microsecond * 4)
if err != nil {
return err
@@ -456,8 +457,8 @@ func (d *Device) readMsg() error {
func (d *Device) readRxBuffer(loadAddr uint8) error {
msg := d.msg
d.cs(false)
defer d.cs(true)
d.cs.Low()
defer d.cs.High()
_, err := d.spi.readWrite(loadAddr)
if err != nil {
return err
@@ -524,8 +525,8 @@ func (d *Device) getNextFreeTxBuf() (uint8, uint8, error) {
}
func (d *Device) writeCANMsg(bufNum uint8, canid uint32, ext, rtrBit, dlc uint8, data []byte) error {
d.cs(false)
defer d.cs(true)
d.cs.Low()
defer d.cs.High()
_, err := d.spi.readWrite(txSidhToLoad(bufNum))
if err != nil {
return err
@@ -544,7 +545,7 @@ func (d *Device) writeCANMsg(bufNum uint8, canid uint32, ext, rtrBit, dlc uint8,
}
// Since cs.Low and cs.High are executed in d.startTransmission,
// it is necessary to set cs.High once to separate the instruction of mcp2515.
d.cs(true)
d.cs.High()
err = d.startTransmission(bufNum)
if err != nil {
@@ -612,9 +613,9 @@ func (s *SPI) setTxBufData(canid uint32, ext, rtrBit, dlc uint8, data []byte) er
}
func (d *Device) startTransmission(bufNum uint8) error {
d.cs(false)
d.cs.Low()
_, err := d.spi.readWrite(txSidhToRTS(bufNum))
d.cs(true)
d.cs.High()
if err != nil {
return err
}
@@ -708,8 +709,8 @@ func txSidhToLoad(i uint8) uint8 {
}
func (d *Device) setRegister(addr, value byte) error {
d.cs(false)
defer d.cs(true)
d.cs.Low()
defer d.cs.High()
_, err := d.spi.readWrite(mcpWrite)
if err != nil {
return err
@@ -728,8 +729,8 @@ func (d *Device) setRegister(addr, value byte) error {
}
func (d *Device) readRegister(addr byte) (byte, error) {
d.cs(false)
defer d.cs(true)
d.cs.Low()
defer d.cs.High()
_, err := d.spi.readWrite(mcpRead)
if err != nil {
return 0, err
@@ -747,8 +748,8 @@ func (d *Device) readRegister(addr byte) (byte, error) {
}
func (d *Device) modifyRegister(addr, mask, data byte) error {
d.cs(false)
defer d.cs(true)
d.cs.Low()
defer d.cs.High()
_, err := d.spi.readWrite(mcpBitMod)
if err != nil {
return err
@@ -790,8 +791,8 @@ func (d *Device) requestNewMode(newMode byte) error {
}
func (d *Device) readStatus() (byte, error) {
d.cs(false)
defer d.cs(true)
d.cs.Low()
defer d.cs.High()
_, err := d.spi.readWrite(mcpReadStatus)
if err != nil {
return 0, err
+24 -16
View File
@@ -5,8 +5,9 @@ package onewire // import "tinygo.org/x/drivers/onewire"
import (
"errors"
"machine"
"time"
"tinygo.org/x/drivers"
)
// OneWire ROM commands
@@ -19,7 +20,8 @@ const (
// Device wraps a connection to an 1-Wire devices.
type Device struct {
p machine.Pin
set drivers.PinOutput
get drivers.PinInput
}
// Config wraps a configuration to an 1-Wire devices.
@@ -32,24 +34,30 @@ var (
errReadAddress = errors.New("Error: OneWire. Read address error: CRC mismatch.")
)
// New creates a new GPIO 1-Wire connection.
// The pin must be pulled up to the VCC via a resistor greater than 500 ohms (default 4.7k).
func New(p machine.Pin) Device {
return Device{
p: p,
// NewFromFuncs expects pin setter and getter for DQ line. Ideally this driver should receive
// a one-wire bus HAL abstraction, but I was asked to show how this could be done using pin HAL so here goes.
func NewFromFuncs(getPinLevel drivers.PinInput, setPinLevel drivers.PinOutput) *Device {
return &Device{
set: setPinLevel,
get: getPinLevel,
}
}
// Configure initializes the protocol.
func (d *Device) Configure(config Config) {}
// By setting the pin value one should configure as output and also expect a more
// consistent behaviour across all tinygo hosts by pulling DQ line low consistently. Win-win.
func (d *Device) cfgOut() { d.set.Low() }
func (d *Device) cfgIn() { d.get() }
// Reset pull DQ line low, then up.
func (d Device) Reset() error {
d.p.Configure(machine.PinConfig{Mode: machine.PinOutput})
d.cfgOut()
time.Sleep(480 * time.Microsecond)
d.p.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
d.cfgIn()
time.Sleep(70 * time.Microsecond)
precence := d.p.Get()
precence := d.get()
time.Sleep(410 * time.Microsecond)
if precence {
return errNoPresence
@@ -59,14 +67,14 @@ func (d Device) Reset() error {
// WriteBit transmits a bit to 1-Wire bus.
func (d Device) WriteBit(data uint8) {
d.p.Configure(machine.PinConfig{Mode: machine.PinOutput})
d.cfgOut()
if data&1 == 1 { // Send '1'
time.Sleep(5 * time.Microsecond)
d.p.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
d.cfgIn()
time.Sleep(60 * time.Microsecond)
} else { // Send '0'
time.Sleep(60 * time.Microsecond)
d.p.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
d.cfgIn()
time.Sleep(5 * time.Microsecond)
}
}
@@ -81,11 +89,11 @@ func (d Device) Write(data uint8) {
// ReadBit receives a bit from 1-Wire bus.
func (d Device) ReadBit() (data uint8) {
d.p.Configure(machine.PinConfig{Mode: machine.PinOutput})
d.cfgOut()
time.Sleep(3 * time.Microsecond)
d.p.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
d.cfgIn()
time.Sleep(8 * time.Microsecond)
if d.p.Get() {
if d.get() {
data = 1
}
time.Sleep(60 * time.Microsecond)
+29
View File
@@ -0,0 +1,29 @@
package onewire
import (
"machine"
"tinygo.org/x/drivers/internal/legacy"
)
// New creates a new GPIO 1-Wire connection.
// The pin must be pulled up to the VCC via a resistor greater than 500 ohms (default 4.7k).
func New(p machine.Pin) Device {
isOut := false
return Device{
set: func(level bool) {
if !isOut {
legacy.ConfigurePinOut(p)
isOut = true
}
p.Set(level)
},
get: func() (level bool) {
if isOut {
legacy.ConfigurePinInputPullup(p)
isOut = false
}
return p.Get()
},
}
}
+8 -8
View File
@@ -9,7 +9,7 @@ import (
"time"
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
)
// Device wraps an SPI connection.
@@ -30,7 +30,7 @@ type Config struct {
}
// New creates a new PCD8544 connection. The SPI bus must already be configured.
func New(bus drivers.SPI, dcPin, rstPin, scePin legacy.PinOutput) *Device {
func New(bus drivers.SPI, dcPin, rstPin, scePin pin.Output) *Device {
return &Device{
bus: bus,
dcPin: dcPin.Set,
@@ -54,9 +54,9 @@ func (d *Device) Configure(cfg Config) {
d.bufferSize = d.width * d.height / 8
d.buffer = make([]byte, d.bufferSize)
d.rstPin(false)
d.rstPin.Low()
time.Sleep(100 * time.Nanosecond)
d.rstPin(true)
d.rstPin.High()
d.SendCommand(FUNCTIONSET | EXTENDEDINSTRUCTION) // H = 1
d.SendCommand(SETVOP | 0x3f) // 0x3f : Vop6 = 0, Vop5 to Vop0 = 1
d.SendCommand(SETTEMP | 0x03) // Experimentally determined
@@ -91,13 +91,13 @@ func (d *Device) Display() error {
// sendDataCommand sends image data or a command to the screen
func (d *Device) sendDataCommand(isCommand bool, data uint8) {
if isCommand {
d.dcPin(false)
d.dcPin.Low()
} else {
d.dcPin(true)
d.dcPin.High()
}
d.scePin(false)
d.scePin.Low()
d.bus.Transfer(data)
d.scePin(true)
d.scePin.High()
}
// SetPixel enables or disables a pixel in the buffer
+10
View File
@@ -9,6 +9,16 @@ package drivers
// var pin drivers.PinOutput = led.Set // Going from a machine.Pin to a drivers.PinOutput
type PinOutput func(level bool)
// High sets the underlying pin's level to high. This is equivalent to calling PinOutput(true).
func (po PinOutput) High() {
po(true)
}
// Low sets the underlying pin's level to low. This is equivalent to calling PinOutput(false).
func (po PinOutput) Low() {
po(false)
}
// PinInput is hardware abstraction for a pin which receives a
// digital signal and reads it (high or low level).
//
+7 -6
View File
@@ -4,6 +4,7 @@ package shiftregister
import (
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
)
type NumberBit int8
@@ -31,7 +32,7 @@ type ShiftPin struct {
}
// New returns a new shift output register device
func New(Bits NumberBit, Latch, Clock, Out legacy.PinOutput) *Device {
func New(Bits NumberBit, Latch, Clock, Out pin.Output) *Device {
return &Device{
latch: Latch.Set,
clock: Clock.Set,
@@ -50,21 +51,21 @@ func (d *Device) Configure() {
if d.config == nil {
panic(legacy.ErrConfigBeforeInstantiated)
}
d.latch(true)
d.latch.High()
}
// WriteMask applies mask's bits to register's outputs pin
// mask's MSB set Q1, LSB set Q8 (for 8 bits mask)
func (d *Device) WriteMask(mask uint32) {
d.mask = mask // Keep the mask for individual addressing
d.latch(false)
d.latch.Low()
for i := 0; i < int(d.bits); i++ {
d.clock(false)
d.clock.Low()
d.out(mask&1 != 0)
mask = mask >> 1
d.clock(true)
d.clock.High()
}
d.latch(true)
d.latch.High()
}
// GetShiftPin return an individually addressable pin
+16 -16
View File
@@ -48,12 +48,12 @@ func New(rs machine.Pin, wr machine.Pin, cs machine.Pin, rst machine.Pin, bus Bu
}
func (d *Device) lcdWriteCom(cmd Command) {
d.rs(false)
d.rs.Low()
d.lcdWriteBusInt(uint16(cmd))
}
func (d *Device) lcdWriteDataInt(data uint16) {
d.rs(true)
d.rs.High()
d.lcdWriteBusInt(data)
}
@@ -63,8 +63,8 @@ func (d *Device) lcdWriteComData(cmd Command, data uint16) {
}
func (d *Device) tx() {
d.wr(false)
d.wr(true)
d.wr.Low()
d.wr.High()
}
func (d *Device) lcdWriteBusInt(data uint16) {
@@ -73,13 +73,13 @@ func (d *Device) lcdWriteBusInt(data uint16) {
}
func (d *Device) Configure() {
d.rst(true)
d.rst.High()
time.Sleep(time.Millisecond * 5)
d.rst(false)
d.rst.Low()
time.Sleep(time.Millisecond * 15)
d.rst(true)
d.rst.High()
time.Sleep(time.Millisecond * 15)
d.cs(false)
d.cs.Low()
//Power supply setting
d.lcdWriteComData(POWERCONTROL1, 0xA8A4)
@@ -139,7 +139,7 @@ func (d *Device) Configure() {
//MUX 319 --> Number of lines in display
d.lcdWriteComData(DRIVEROUTPUTCONTROL, 0x233F)
d.cs(true)
d.cs.High()
}
@@ -169,25 +169,25 @@ func (d *Device) SetPixel(x, y int16, c color.RGBA) {
encoded := encodeColor(c)
d.cs(false)
d.cs.Low()
d.setXY(uint16(x), uint16(y), uint16(x), uint16(y))
d.rs(true)
d.rs.High()
d.lcdWriteBusInt(encoded)
d.cs(true)
d.cs.High()
}
func (d *Device) FillRect(x, y, w, h int16, c color.RGBA) {
encoded := encodeColor(c)
d.cs(false)
d.cs.Low()
d.setXY(uint16(x), uint16(y), uint16(x+(w-1)), uint16(y+(h-1)))
d.rs(true)
d.rs.High()
d.bus.Set(encoded)
for i := int64(0); i < int64(w)*int64(h); i++ {
d.tx()
}
d.cs(true)
d.rs(false)
d.cs.High()
d.rs.Low()
}
+3 -3
View File
@@ -70,11 +70,11 @@ func (d *Device) Configure(cfg Config) {
d.batchData = make([]uint8, d.batchLength*2)
// reset the device
d.resetPin(true)
d.resetPin.High()
time.Sleep(100 * time.Millisecond)
d.resetPin(false)
d.resetPin.Low()
time.Sleep(100 * time.Millisecond)
d.resetPin(true)
d.resetPin.High()
time.Sleep(200 * time.Millisecond)
// Initialization
+10 -9
View File
@@ -10,6 +10,7 @@ import (
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
)
var (
@@ -42,7 +43,7 @@ type Config struct {
}
// New creates a new SSD1351 connection. The SPI wire must already be configured.
func New(bus drivers.SPI, resetPin, dcPin, csPin, enPin, rwPin legacy.PinOutput) Device {
func New(bus drivers.SPI, resetPin, dcPin, csPin, enPin, rwPin pin.Output) Device {
return Device{
bus: bus,
dcPin: dcPin.Set,
@@ -87,16 +88,16 @@ func (d *Device) Configure(cfg Config) {
d.configurePins()
// reset the device
d.resetPin(true)
d.resetPin.High()
time.Sleep(100 * time.Millisecond)
d.resetPin(false)
d.resetPin.Low()
time.Sleep(100 * time.Millisecond)
d.resetPin(true)
d.resetPin.High()
time.Sleep(200 * time.Millisecond)
d.rwPin(false)
d.dcPin(false)
d.enPin(true)
d.rwPin.Low()
d.dcPin.Low()
d.enPin.High()
// Initialization
d.Command(SET_COMMAND_LOCK)
@@ -286,9 +287,9 @@ func (d *Device) Data(data uint8) {
// Tx sends data to the display
func (d *Device) Tx(data []byte, isCommand bool) {
d.dcPin(!isCommand)
d.csPin(false)
d.csPin.Low()
d.bus.Tx(data, nil)
d.csPin(true)
d.csPin.High()
}
// Size returns the current size of the display
+9 -8
View File
@@ -11,6 +11,7 @@ import (
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
"tinygo.org/x/drivers/pixel"
)
@@ -65,13 +66,13 @@ type Config struct {
}
// New creates a new ST7735 connection. The SPI wire must already be configured.
func New(bus drivers.SPI, resetPin, dcPin, csPin, blPin legacy.PinOutput) Device {
func New(bus drivers.SPI, resetPin, dcPin, csPin, blPin pin.Output) Device {
return NewOf[pixel.RGB565BE](bus, resetPin, dcPin, csPin, blPin)
}
// NewOf creates a new ST7735 connection with a particular pixel format. The SPI
// wire must already be configured.
func NewOf[T Color](bus drivers.SPI, resetPin, dcPin, csPin, blPin legacy.PinOutput) DeviceOf[T] {
func NewOf[T Color](bus drivers.SPI, resetPin, dcPin, csPin, blPin pin.Output) DeviceOf[T] {
legacy.ConfigurePinOut(dcPin)
legacy.ConfigurePinOut(resetPin)
legacy.ConfigurePinOut(csPin)
@@ -114,11 +115,11 @@ func (d *DeviceOf[T]) Configure(cfg Config) {
d.batchData = pixel.NewImage[T](int(d.batchLength), 1)
// reset the device
d.resetPin(true)
d.resetPin.High()
time.Sleep(5 * time.Millisecond)
d.resetPin(false)
d.resetPin.Low()
time.Sleep(20 * time.Millisecond)
d.resetPin(true)
d.resetPin.High()
time.Sleep(150 * time.Millisecond)
// Common initialization
@@ -226,7 +227,7 @@ func (d *DeviceOf[T]) Configure(cfg Config) {
d.SetRotation(d.rotation)
d.blPin(true)
d.blPin.High()
}
// Display does nothing, there's no buffer as it might be too big for some boards
@@ -438,9 +439,9 @@ func (d *DeviceOf[T]) Size() (w, h int16) {
// EnableBacklight enables or disables the backlight
func (d *DeviceOf[T]) EnableBacklight(enable bool) {
if enable {
d.blPin(true)
d.blPin.High()
} else {
d.blPin(false)
d.blPin.Low()
}
}
+15 -14
View File
@@ -14,6 +14,7 @@ import (
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
"tinygo.org/x/drivers/pixel"
)
@@ -83,13 +84,13 @@ type Config struct {
}
// New creates a new ST7789 connection. The SPI wire must already be configured.
func New(bus drivers.SPI, resetPin, dcPin, csPin, blPin legacy.PinOutput) Device {
func New(bus drivers.SPI, resetPin, dcPin, csPin, blPin pin.Output) Device {
return NewOf[pixel.RGB565BE](bus, resetPin, dcPin, csPin, blPin)
}
// NewOf creates a new ST7789 connection with a particular pixel format. The SPI
// wire must already be configured.
func NewOf[T Color](bus drivers.SPI, resetPin, dcPin, csPin, blPin legacy.PinOutput) DeviceOf[T] {
func NewOf[T Color](bus drivers.SPI, resetPin, dcPin, csPin, blPin pin.Output) DeviceOf[T] {
legacy.ConfigurePinOut(dcPin)
legacy.ConfigurePinOut(resetPin)
legacy.ConfigurePinOut(csPin)
@@ -143,11 +144,11 @@ func (d *DeviceOf[T]) Configure(cfg Config) {
d.batchLength += d.batchLength & 1
// Reset the device
d.resetPin(true)
d.resetPin.High()
time.Sleep(50 * time.Millisecond)
d.resetPin(false)
d.resetPin.Low()
time.Sleep(50 * time.Millisecond)
d.resetPin(true)
d.resetPin.High()
time.Sleep(50 * time.Millisecond)
// Common initialization
@@ -213,7 +214,7 @@ func (d *DeviceOf[T]) Configure(cfg Config) {
time.Sleep(10 * time.Millisecond) //
d.endWrite()
d.blPin(true) // Backlight ON
d.blPin.High() // Backlight ON
}
// Send a command with data to the display. It does not change the chip select
@@ -221,9 +222,9 @@ func (d *DeviceOf[T]) Configure(cfg Config) {
// meaning that data can be sent right away.
func (d *DeviceOf[T]) sendCommand(command uint8, data []byte) error {
d.cmdBuf[0] = command
d.dcPin(false)
d.dcPin.Low()
err := d.bus.Tx(d.cmdBuf[:1], nil)
d.dcPin(true)
d.dcPin.High()
if len(data) != 0 {
err = d.bus.Tx(data, nil)
}
@@ -234,7 +235,7 @@ func (d *DeviceOf[T]) sendCommand(command uint8, data []byte) error {
// chip select pin low.
func (d *DeviceOf[T]) startWrite() {
if d.csPin != nil {
d.csPin(false)
d.csPin.Low()
}
}
@@ -242,7 +243,7 @@ func (d *DeviceOf[T]) startWrite() {
// select pin high.
func (d *DeviceOf[T]) endWrite() {
if d.csPin != nil {
d.csPin(true)
d.csPin.High()
}
}
@@ -304,9 +305,9 @@ func (d *DeviceOf[T]) SyncToScanLine(scanline uint16) {
func (d *DeviceOf[T]) GetScanLine() uint16 {
d.startWrite()
data := []uint8{0x00, 0x00}
d.dcPin(false)
d.dcPin.Low()
d.bus.Transfer(GSCAN)
d.dcPin(true)
d.dcPin.High()
for i := range data {
data[i], _ = d.bus.Transfer(0xFF)
}
@@ -545,9 +546,9 @@ func (d *DeviceOf[T]) Size() (w, h int16) {
// EnableBacklight enables or disables the backlight
func (d *DeviceOf[T]) EnableBacklight(enable bool) {
if enable {
d.blPin(true)
d.blPin.High()
} else {
d.blPin(false)
d.blPin.Low()
}
}
+4 -4
View File
@@ -9,7 +9,7 @@ import (
"time"
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
"tinygo.org/x/drivers/lora"
)
@@ -43,7 +43,7 @@ func (d *Device) GetRadioEventChan() chan lora.RadioEvent {
}
// New creates a new SX127x connection. The SPI bus must already be configured.
func New(spi drivers.SPI, rstPin legacy.PinOutput) *Device {
func New(spi drivers.SPI, rstPin pin.Output) *Device {
k := Device{
spi: spi,
rstPin: rstPin.Set,
@@ -67,9 +67,9 @@ func (d *Device) SetRadioController(rc RadioController) error {
// Reset re-initialize the sx127x device
func (d *Device) Reset() {
d.rstPin(false)
d.rstPin.Low()
time.Sleep(100 * time.Millisecond)
d.rstPin(true)
d.rstPin.High()
time.Sleep(100 * time.Millisecond)
}
+10 -9
View File
@@ -12,6 +12,7 @@ import (
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
"tinygo.org/x/drivers/pixel"
)
@@ -49,7 +50,7 @@ type Device struct {
type Speed uint8
// New returns a new uc8151 driver. Pass in a fully configured SPI bus.
func New(bus drivers.SPI, csPin, dcPin, rstPin legacy.PinOutput, busyPin legacy.PinInput) Device {
func New(bus drivers.SPI, csPin, dcPin, rstPin pin.Output, busyPin pin.Input) Device {
legacy.ConfigurePinOut(csPin)
legacy.ConfigurePinOut(dcPin)
legacy.ConfigurePinOut(rstPin)
@@ -133,9 +134,9 @@ func (d *Device) Configure(cfg Config) {
// Reset resets the device
func (d *Device) Reset() {
d.rst(false)
d.rst.Low()
time.Sleep(10 * time.Millisecond)
d.rst(true)
d.rst.High()
time.Sleep(10 * time.Millisecond)
d.WaitUntilIdle()
}
@@ -152,18 +153,18 @@ func (d *Device) PowerOn() {
// SendCommand sends a command to the display
func (d *Device) SendCommand(command uint8) {
d.dc(false)
d.cs(false)
d.dc.Low()
d.cs.Low()
d.bus.Transfer(command)
d.cs(true)
d.cs.High()
}
// SendData sends a data byte to the display
func (d *Device) SendData(data ...uint8) {
d.dc(true)
d.cs(false)
d.dc.High()
d.cs.Low()
d.bus.Tx(data, nil)
d.cs(true)
d.cs.High()
}
// SetPixel modifies the internal buffer in a single pixel.
+10 -9
View File
@@ -15,6 +15,7 @@ import (
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
)
type Config struct {
@@ -82,7 +83,7 @@ var partialRefresh = [159]uint8{
}
// New returns a new epd1in54 driver. Pass in a fully configured SPI bus.
func New(bus *machine.SPI, csPin, dcPin, rstPin legacy.PinOutput, busyPin legacy.PinInput) Device {
func New(bus *machine.SPI, csPin, dcPin, rstPin pin.Output, busyPin pin.Input) Device {
return Device{
buffer: make([]uint8, (uint32(Width)*uint32(Height))/8),
bus: bus,
@@ -240,11 +241,11 @@ func (d *Device) setLUT(lut [159]uint8) {
// Reset resets the display.
func (d *Device) Reset() {
d.rst(true)
d.rst.High()
time.Sleep(20 * time.Millisecond)
d.rst(false)
d.rst.Low()
time.Sleep(5 * time.Millisecond)
d.rst(true)
d.rst.High()
time.Sleep(20 * time.Millisecond)
}
@@ -261,13 +262,13 @@ func (d *Device) SendData(data uint8) {
// sendDataCommand sends image data or a command to the screen
func (d *Device) sendDataCommand(isCommand bool, data uint8) {
if isCommand {
d.dc(false)
d.dc.Low()
} else {
d.dc(true)
d.dc.High()
}
d.cs(false)
d.cs.Low()
d.bus.Transfer(data)
d.cs(true)
d.cs.High()
}
// SetPixel modifies the internal buffer in a single pixel.
@@ -429,5 +430,5 @@ func (d *Device) Sleep() {
d.SendData(0x01)
time.Sleep(200 * time.Millisecond)
d.rst(false)
d.rst.Low()
}
+11 -10
View File
@@ -10,6 +10,7 @@ import (
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
)
type Config struct {
@@ -53,7 +54,7 @@ var lutPartialUpdate = [30]uint8{
}
// New returns a new epd2in13x driver. Pass in a fully configured SPI bus.
func New(bus drivers.SPI, csPin, dcPin, rstPin legacy.PinOutput, busyPin legacy.PinInput) Device {
func New(bus drivers.SPI, csPin, dcPin, rstPin pin.Output, busyPin pin.Input) Device {
legacy.ConfigurePinOut(csPin)
legacy.ConfigurePinOut(dcPin)
legacy.ConfigurePinOut(rstPin)
@@ -91,9 +92,9 @@ func (d *Device) Configure(cfg Config) {
d.buffer[i] = 0xFF
}
d.cs(false)
d.dc(false)
d.rst(false)
d.cs.Low()
d.dc.Low()
d.rst.Low()
d.Reset()
@@ -119,9 +120,9 @@ func (d *Device) Configure(cfg Config) {
// Reset resets the device
func (d *Device) Reset() {
d.rst(false)
d.rst.Low()
time.Sleep(200 * time.Millisecond)
d.rst(true)
d.rst.High()
time.Sleep(200 * time.Millisecond)
}
@@ -155,13 +156,13 @@ func (d *Device) SendData(data uint8) {
// sendDataCommand sends image data or a command to the screen
func (d *Device) sendDataCommand(isCommand bool, data uint8) {
if isCommand {
d.dc(false)
d.dc.Low()
} else {
d.dc(true)
d.dc.High()
}
d.cs(false)
d.cs.Low()
d.bus.Transfer(data)
d.cs(true)
d.cs.High()
}
// SetLUT sets the look up tables for full or partial updates
+11 -10
View File
@@ -10,6 +10,7 @@ import (
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
)
type Config struct {
@@ -33,7 +34,7 @@ type Device struct {
type Color uint8
// New returns a new epd2in13x driver. Pass in a fully configured SPI bus.
func New(bus drivers.SPI, csPin, dcPin, rstPin legacy.PinOutput, busyPin legacy.PinInput) Device {
func New(bus drivers.SPI, csPin, dcPin, rstPin pin.Output, busyPin pin.Input) Device {
legacy.ConfigurePinOut(csPin)
legacy.ConfigurePinOut(dcPin)
legacy.ConfigurePinOut(rstPin)
@@ -75,9 +76,9 @@ func (d *Device) Configure(cfg Config) {
}
}
d.cs(false)
d.dc(false)
d.rst(false)
d.cs.Low()
d.dc.Low()
d.rst.Low()
d.Reset()
@@ -99,9 +100,9 @@ func (d *Device) Configure(cfg Config) {
// Reset resets the device
func (d *Device) Reset() {
d.rst(false)
d.rst.Low()
time.Sleep(200 * time.Millisecond)
d.rst(true)
d.rst.High()
time.Sleep(200 * time.Millisecond)
}
@@ -126,13 +127,13 @@ func (d *Device) SendData(data uint8) {
// sendDataCommand sends image data or a command to the screen
func (d *Device) sendDataCommand(isCommand bool, data uint8) {
if isCommand {
d.dc(false)
d.dc.Low()
} else {
d.dc(true)
d.dc.High()
}
d.cs(false)
d.cs.Low()
d.bus.Transfer(data)
d.cs(true)
d.cs.High()
}
// SetPixel modifies the internal buffer in a single pixel.
+12 -12
View File
@@ -176,11 +176,11 @@ func (d *Device) setCursor(x, y uint16) error {
}
func (d *Device) hwReset() {
d.rst(true)
d.rst.High()
time.Sleep(50 * time.Millisecond)
d.rst(false)
d.rst.Low()
time.Sleep(2 * time.Millisecond)
d.rst(true)
d.rst.High()
time.Sleep(50 * time.Millisecond)
}
@@ -232,26 +232,26 @@ func (d *Device) sendCommandSequence(seq []byte) error {
}
func (d *Device) sendCommandByte(b byte) error {
d.dc(false)
d.cs(false)
d.dc.Low()
d.cs.Low()
_, err := d.bus.Transfer(b)
d.cs(true)
d.cs.High()
return err
}
func (d *Device) sendDataByte(b byte) error {
d.dc(true)
d.cs(false)
d.dc.High()
d.cs.Low()
_, err := d.bus.Transfer(b)
d.cs(true)
d.cs.High()
return err
}
func (d *Device) sendData(b []byte) error {
d.dc(true)
d.cs(false)
d.dc.High()
d.cs.Low()
err := d.bus.Tx(b, nil)
d.cs(true)
d.cs.High()
return err
}
+11 -10
View File
@@ -17,6 +17,7 @@ import (
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
)
type Config struct {
@@ -61,7 +62,7 @@ var lutPartialUpdate = [30]uint8{
}
// New returns a new epd2in9 driver. Pass in a fully configured SPI bus.
func New(bus drivers.SPI, csPin, dcPin, rstPin legacy.PinOutput, busyPin legacy.PinInput) Device {
func New(bus drivers.SPI, csPin, dcPin, rstPin pin.Output, busyPin pin.Input) Device {
legacy.ConfigurePinOut(csPin)
legacy.ConfigurePinOut(dcPin)
legacy.ConfigurePinOut(rstPin)
@@ -99,9 +100,9 @@ func (d *Device) Configure(cfg Config) {
d.buffer[i] = 0xFF
}
d.cs(false)
d.dc(false)
d.rst(false)
d.cs.Low()
d.dc.Low()
d.rst.Low()
d.Reset()
@@ -127,9 +128,9 @@ func (d *Device) Configure(cfg Config) {
// Reset resets the device
func (d *Device) Reset() {
d.rst(false)
d.rst.Low()
time.Sleep(200 * time.Millisecond)
d.rst(true)
d.rst.High()
time.Sleep(200 * time.Millisecond)
}
@@ -152,13 +153,13 @@ func (d *Device) SendData(data uint8) {
// sendDataCommand sends image data or a command to the screen
func (d *Device) sendDataCommand(isCommand bool, data uint8) {
if isCommand {
d.dc(false)
d.dc.Low()
} else {
d.dc(true)
d.dc.High()
}
d.cs(false)
d.cs.Low()
d.bus.Transfer(data)
d.cs(true)
d.cs.High()
}
// SetLUT sets the look up tables for full or partial updates
+11 -10
View File
@@ -14,6 +14,7 @@ import (
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
)
type Config struct {
@@ -40,7 +41,7 @@ type Device struct {
type Rotation uint8
// New returns a new epd4in2 driver. Pass in a fully configured SPI bus.
func New(bus drivers.SPI, csPin, dcPin, rstPin legacy.PinOutput, busyPin legacy.PinInput) Device {
func New(bus drivers.SPI, csPin, dcPin, rstPin pin.Output, busyPin pin.Input) Device {
legacy.ConfigurePinOut(csPin)
legacy.ConfigurePinOut(dcPin)
legacy.ConfigurePinOut(rstPin)
@@ -78,9 +79,9 @@ func (d *Device) Configure(cfg Config) {
d.buffer[i] = 0xFF
}
d.cs(false)
d.dc(false)
d.rst(false)
d.cs.Low()
d.dc.Low()
d.rst.Low()
d.Reset()
d.SendCommand(POWER_SETTING)
@@ -104,9 +105,9 @@ func (d *Device) Configure(cfg Config) {
// Reset resets the device
func (d *Device) Reset() {
d.rst(false)
d.rst.Low()
time.Sleep(200 * time.Millisecond)
d.rst(true)
d.rst.High()
time.Sleep(200 * time.Millisecond)
}
@@ -145,13 +146,13 @@ func (d *Device) SendData(data uint8) {
// sendDataCommand sends image data or a command to the screen
func (d *Device) sendDataCommand(isCommand bool, data uint8) {
if isCommand {
d.dc(false)
d.dc.Low()
} else {
d.dc(true)
d.dc.High()
}
d.cs(false)
d.cs.Low()
d.bus.Transfer(data)
d.cs(true)
d.cs.High()
}
// SetLUT sets the look up tables for full or partial updates