diff --git a/apa102/apa102.go b/apa102/apa102.go index 9de5e63..0cc22e0 100644 --- a/apa102/apa102.go +++ b/apa102/apa102.go @@ -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) diff --git a/apa102/softspi.go b/apa102/softspi.go index 48dcf99..f221d47 100644 --- a/apa102/softspi.go +++ b/apa102/softspi.go @@ -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 diff --git a/bmi160/bmi160.go b/bmi160/bmi160.go index 9649377..2ea0b01 100644 --- a/bmi160/bmi160.go +++ b/bmi160/bmi160.go @@ -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() } diff --git a/buzzer/buzzer.go b/buzzer/buzzer.go index b7f92f6..daa8ddf 100644 --- a/buzzer/buzzer.go +++ b/buzzer/buzzer.go @@ -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 } diff --git a/ft6336/ft6336.go b/ft6336/ft6336.go index 4a8ad8f..9252f3c 100644 --- a/ft6336/ft6336.go +++ b/ft6336/ft6336.go @@ -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), diff --git a/gc9a01/gc9a01.go b/gc9a01/gc9a01.go index ef9d2be..fd9d2b9 100644 --- a/gc9a01/gc9a01.go +++ b/gc9a01/gc9a01.go @@ -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() } diff --git a/hcsr04/hcsr04.go b/hcsr04/hcsr04.go index e9129a2..c2f6e1f 100644 --- a/hcsr04/hcsr04.go +++ b/hcsr04/hcsr04.go @@ -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() { diff --git a/internal/legacy/pinhal.go b/internal/legacy/pinhal.go index fe7a64c..1a9da9a 100644 --- a/internal/legacy/pinhal.go +++ b/internal/legacy/pinhal.go @@ -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) } diff --git a/internal/legacy/pinhal_go.go b/internal/legacy/pinhal_go.go index 457c24b..f76cc8b 100644 --- a/internal/legacy/pinhal_go.go +++ b/internal/legacy/pinhal_go.go @@ -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 } diff --git a/internal/legacy/pinhal_tinygo.go b/internal/legacy/pinhal_tinygo.go index 9fbf79b..d0068b2 100644 --- a/internal/legacy/pinhal_tinygo.go +++ b/internal/legacy/pinhal_tinygo.go @@ -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 { diff --git a/internal/pin/pin.go b/internal/pin/pin.go new file mode 100644 index 0000000..3eb0e7e --- /dev/null +++ b/internal/pin/pin.go @@ -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) +} diff --git a/max6675/max6675.go b/max6675/max6675.go index ce4b09d..a712df0 100644 --- a/max6675/max6675.go +++ b/max6675/max6675.go @@ -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 { diff --git a/max72xx/max72xx.go b/max72xx/max72xx.go index d1fc0a6..815ff7b 100644 --- a/max72xx/max72xx.go +++ b/max72xx/max72xx.go @@ -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() } diff --git a/mcp2515/mcp2515.go b/mcp2515/mcp2515.go index 4f5bcc8..48ebaf1 100644 --- a/mcp2515/mcp2515.go +++ b/mcp2515/mcp2515.go @@ -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 diff --git a/pcd8544/pcd8544.go b/pcd8544/pcd8544.go index 699d59e..73a17f0 100644 --- a/pcd8544/pcd8544.go +++ b/pcd8544/pcd8544.go @@ -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 diff --git a/pin.go b/pin.go index 35080e9..33ea5ac 100644 --- a/pin.go +++ b/pin.go @@ -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). // diff --git a/shiftregister/shiftregister.go b/shiftregister/shiftregister.go index d19d46d..c315387 100644 --- a/shiftregister/shiftregister.go +++ b/shiftregister/shiftregister.go @@ -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 diff --git a/ssd1289/ssd1289.go b/ssd1289/ssd1289.go index e95a375..c649d84 100644 --- a/ssd1289/ssd1289.go +++ b/ssd1289/ssd1289.go @@ -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() } diff --git a/ssd1331/ssd1331.go b/ssd1331/ssd1331.go index 07614fe..9775819 100644 --- a/ssd1331/ssd1331.go +++ b/ssd1331/ssd1331.go @@ -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 diff --git a/ssd1351/ssd1351.go b/ssd1351/ssd1351.go index d17b4da..96a6f0c 100644 --- a/ssd1351/ssd1351.go +++ b/ssd1351/ssd1351.go @@ -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 diff --git a/st7735/st7735.go b/st7735/st7735.go index 51a556f..235bcd6 100644 --- a/st7735/st7735.go +++ b/st7735/st7735.go @@ -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() } } diff --git a/st7789/st7789.go b/st7789/st7789.go index f466208..7602b52 100644 --- a/st7789/st7789.go +++ b/st7789/st7789.go @@ -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() } } diff --git a/sx127x/sx127x.go b/sx127x/sx127x.go index f12a435..e820f3a 100644 --- a/sx127x/sx127x.go +++ b/sx127x/sx127x.go @@ -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) } diff --git a/uc8151/uc8151.go b/uc8151/uc8151.go index bf6f4ce..6da8866 100644 --- a/uc8151/uc8151.go +++ b/uc8151/uc8151.go @@ -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. diff --git a/waveshare-epd/epd1in54/epd1in54.go b/waveshare-epd/epd1in54/epd1in54.go index b228c6b..4db9d76 100644 --- a/waveshare-epd/epd1in54/epd1in54.go +++ b/waveshare-epd/epd1in54/epd1in54.go @@ -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() } diff --git a/waveshare-epd/epd2in13/epd2in13.go b/waveshare-epd/epd2in13/epd2in13.go index 23873d9..408af5e 100644 --- a/waveshare-epd/epd2in13/epd2in13.go +++ b/waveshare-epd/epd2in13/epd2in13.go @@ -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 diff --git a/waveshare-epd/epd2in13x/epd2in13x.go b/waveshare-epd/epd2in13x/epd2in13x.go index a5f0836..8469f0b 100644 --- a/waveshare-epd/epd2in13x/epd2in13x.go +++ b/waveshare-epd/epd2in13x/epd2in13x.go @@ -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. diff --git a/waveshare-epd/epd2in66b/dev.go b/waveshare-epd/epd2in66b/dev.go index 64c1b38..32cebd8 100644 --- a/waveshare-epd/epd2in66b/dev.go +++ b/waveshare-epd/epd2in66b/dev.go @@ -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 } diff --git a/waveshare-epd/epd2in9/epd2in9.go b/waveshare-epd/epd2in9/epd2in9.go index 4b5f9a7..b90f5f3 100644 --- a/waveshare-epd/epd2in9/epd2in9.go +++ b/waveshare-epd/epd2in9/epd2in9.go @@ -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 diff --git a/waveshare-epd/epd4in2/epd4in2.go b/waveshare-epd/epd4in2/epd4in2.go index 24ee273..772b834 100644 --- a/waveshare-epd/epd4in2/epd4in2.go +++ b/waveshare-epd/epd4in2/epd4in2.go @@ -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