mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-16 04:43:25 +00:00
ssd1306: improve bus error handling
This commit is contained in:
+24
-17
@@ -45,9 +45,9 @@ type SPIBus struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Buser interface {
|
type Buser interface {
|
||||||
configure()
|
configure() error
|
||||||
tx(data []byte, isCommand bool)
|
tx(data []byte, isCommand bool) error
|
||||||
setAddress(address uint16)
|
setAddress(address uint16) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type VccMode uint8
|
type VccMode uint8
|
||||||
@@ -193,8 +193,7 @@ func (d *Device) Display() error {
|
|||||||
d.Command(uint8(d.height/8) - 1)
|
d.Command(uint8(d.height/8) - 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
d.Tx(d.buffer, false)
|
return d.Tx(d.buffer, false)
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetPixel enables or disables a pixel in the buffer
|
// SetPixel enables or disables a pixel in the buffer
|
||||||
@@ -244,21 +243,23 @@ func (d *Device) Command(command uint8) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// setAddress sets the address to the I2C bus
|
// setAddress sets the address to the I2C bus
|
||||||
func (b *I2CBus) setAddress(address uint16) {
|
func (b *I2CBus) setAddress(address uint16) error {
|
||||||
b.Address = address
|
b.Address = address
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// setAddress does nothing, but it's required to avoid reflection
|
// setAddress does nothing, but it's required to avoid reflection
|
||||||
func (b *SPIBus) setAddress(address uint16) {
|
func (b *SPIBus) setAddress(address uint16) error {
|
||||||
// do nothing
|
// do nothing
|
||||||
println("trying to Configure an address on a SPI device")
|
println("trying to Configure an address on a SPI device")
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// configure does nothing, but it's required to avoid reflection
|
// configure does nothing, but it's required to avoid reflection
|
||||||
func (b *I2CBus) configure() {}
|
func (b *I2CBus) configure() error { return nil }
|
||||||
|
|
||||||
// configure configures some pins with the SPI bus
|
// configure configures some pins with the SPI bus
|
||||||
func (b *SPIBus) configure() {
|
func (b *SPIBus) configure() error {
|
||||||
b.csPin.Low()
|
b.csPin.Low()
|
||||||
b.dcPin.Low()
|
b.dcPin.Low()
|
||||||
b.resetPin.Low()
|
b.resetPin.Low()
|
||||||
@@ -268,31 +269,35 @@ func (b *SPIBus) configure() {
|
|||||||
b.resetPin.Low()
|
b.resetPin.Low()
|
||||||
time.Sleep(10 * time.Millisecond)
|
time.Sleep(10 * time.Millisecond)
|
||||||
b.resetPin.High()
|
b.resetPin.High()
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tx sends data to the display
|
// Tx sends data to the display
|
||||||
func (d *Device) Tx(data []byte, isCommand bool) {
|
func (d *Device) Tx(data []byte, isCommand bool) error {
|
||||||
d.bus.tx(data, isCommand)
|
return d.bus.tx(data, isCommand)
|
||||||
}
|
}
|
||||||
|
|
||||||
// tx sends data to the display (I2CBus implementation)
|
// tx sends data to the display (I2CBus implementation)
|
||||||
func (b *I2CBus) tx(data []byte, isCommand bool) {
|
func (b *I2CBus) tx(data []byte, isCommand bool) error {
|
||||||
if isCommand {
|
if isCommand {
|
||||||
legacy.WriteRegister(b.wire, uint8(b.Address), 0x00, data)
|
return legacy.WriteRegister(b.wire, uint8(b.Address), 0x00, data)
|
||||||
} else {
|
} else {
|
||||||
legacy.WriteRegister(b.wire, uint8(b.Address), 0x40, data)
|
return legacy.WriteRegister(b.wire, uint8(b.Address), 0x40, data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// tx sends data to the display (SPIBus implementation)
|
// tx sends data to the display (SPIBus implementation)
|
||||||
func (b *SPIBus) tx(data []byte, isCommand bool) {
|
func (b *SPIBus) tx(data []byte, isCommand bool) error {
|
||||||
|
var err error
|
||||||
|
|
||||||
if isCommand {
|
if isCommand {
|
||||||
b.csPin.High()
|
b.csPin.High()
|
||||||
time.Sleep(1 * time.Millisecond)
|
time.Sleep(1 * time.Millisecond)
|
||||||
b.dcPin.Low()
|
b.dcPin.Low()
|
||||||
b.csPin.Low()
|
b.csPin.Low()
|
||||||
|
|
||||||
b.wire.Tx(data, nil)
|
err = b.wire.Tx(data, nil)
|
||||||
b.csPin.High()
|
b.csPin.High()
|
||||||
} else {
|
} else {
|
||||||
b.csPin.High()
|
b.csPin.High()
|
||||||
@@ -300,9 +305,11 @@ func (b *SPIBus) tx(data []byte, isCommand bool) {
|
|||||||
b.dcPin.High()
|
b.dcPin.High()
|
||||||
b.csPin.Low()
|
b.csPin.Low()
|
||||||
|
|
||||||
b.wire.Tx(data, nil)
|
err = b.wire.Tx(data, nil)
|
||||||
b.csPin.High()
|
b.csPin.High()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Size returns the current size of the display.
|
// Size returns the current size of the display.
|
||||||
|
|||||||
Reference in New Issue
Block a user