From 67b669f192a7dcff74b6f4761ae3d2b52c689d15 Mon Sep 17 00:00:00 2001 From: soypat Date: Tue, 8 Jul 2025 21:11:46 -0300 Subject: [PATCH] finish rewrites! --- easystepper/easystepper.go | 2 - internal/legacy/pinhal.go | 7 + internal/legacy/pinhal_baremetal.go | 5 + internal/legacy/pinhal_os.go | 1 + st7735/st7735.go | 44 +-- st7735/st7735v2.go | 475 ----------------------- st7789/st7789.go | 62 +-- sx127x/sx127x.go | 12 +- uc8151/uc8151.go | 48 +-- waveshare-epd/epd1in54/epd1in54.go | 67 ++-- waveshare-epd/epd2in13/epd2in13.go | 50 +-- waveshare-epd/epd2in13x/epd2in13x.go | 50 +-- waveshare-epd/epd2in66b/dev.go | 57 +-- waveshare-epd/epd2in66b/dev_baremetal.go | 30 ++ waveshare-epd/epd2in9/epd2in9.go | 50 +-- waveshare-epd/epd4in2/epd4in2.go | 50 +-- 16 files changed, 283 insertions(+), 727 deletions(-) delete mode 100644 st7735/st7735v2.go create mode 100644 waveshare-epd/epd2in66b/dev_baremetal.go diff --git a/easystepper/easystepper.go b/easystepper/easystepper.go index 899ff1e..4b1626f 100644 --- a/easystepper/easystepper.go +++ b/easystepper/easystepper.go @@ -1,5 +1,3 @@ -//go:build baremetal - // Package easystepper provides a simple driver to rotate a 4-wire stepper motor. package easystepper // import "tinygo.org/x/drivers/easystepper" diff --git a/internal/legacy/pinhal.go b/internal/legacy/pinhal.go index 20dad4e..fe7a64c 100644 --- a/internal/legacy/pinhal.go +++ b/internal/legacy/pinhal.go @@ -63,6 +63,13 @@ func ConfigurePinInputPullup(pi PinInput) { configurePinInputPullup(pi) } +// PinIsNoPin returns true if the argument is a machine.Pin type and is the machine.NoPin predeclared type. +// +// Deprecated: Drivers do not require pin knowledge from now on. +func PinIsNoPin(pin any) bool { + return pinIsNoPin(pin) +} + var ( ErrConfigBeforeInstantiated = errors.New("device must be instantiated with New before calling Configure method") ) diff --git a/internal/legacy/pinhal_baremetal.go b/internal/legacy/pinhal_baremetal.go index 70fe0f4..ee4b09a 100644 --- a/internal/legacy/pinhal_baremetal.go +++ b/internal/legacy/pinhal_baremetal.go @@ -20,6 +20,11 @@ func configurePinInputPullup(p PinInput) { configurePin(p, machine.PinInputPullup) } +func pinIsNoPin(a any) bool { + p, ok := a.(machine.Pin) + return ok && p == machine.NoPin +} + func configurePin(p any, mode machine.PinMode) { machinePin, ok := p.(machine.Pin) if ok { diff --git a/internal/legacy/pinhal_os.go b/internal/legacy/pinhal_os.go index 394f42f..457c24b 100644 --- a/internal/legacy/pinhal_os.go +++ b/internal/legacy/pinhal_os.go @@ -6,3 +6,4 @@ func configurePinOut(p PinOutput) {} func configurePinInput(p PinInput) {} func configurePinInputPulldown(p PinInput) {} func configurePinInputPullup(p PinInput) {} +func pinIsNoPin(a any) bool { return false } diff --git a/st7735/st7735.go b/st7735/st7735.go index 6f15781..51a556f 100644 --- a/st7735/st7735.go +++ b/st7735/st7735.go @@ -5,12 +5,12 @@ package st7735 // import "tinygo.org/x/drivers/st7735" import ( "image/color" - "machine" "time" "errors" "tinygo.org/x/drivers" + "tinygo.org/x/drivers/internal/legacy" "tinygo.org/x/drivers/pixel" ) @@ -39,10 +39,10 @@ type Device = DeviceOf[pixel.RGB565BE] // formats. type DeviceOf[T Color] struct { bus drivers.SPI - dcPin machine.Pin - resetPin machine.Pin - csPin machine.Pin - blPin machine.Pin + dcPin drivers.PinOutput + resetPin drivers.PinOutput + csPin drivers.PinOutput + blPin drivers.PinOutput width int16 height int16 columnOffset int16 @@ -65,23 +65,23 @@ 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 machine.Pin) Device { +func New(bus drivers.SPI, resetPin, dcPin, csPin, blPin legacy.PinOutput) 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 machine.Pin) DeviceOf[T] { - dcPin.Configure(machine.PinConfig{Mode: machine.PinOutput}) - resetPin.Configure(machine.PinConfig{Mode: machine.PinOutput}) - csPin.Configure(machine.PinConfig{Mode: machine.PinOutput}) - blPin.Configure(machine.PinConfig{Mode: machine.PinOutput}) +func NewOf[T Color](bus drivers.SPI, resetPin, dcPin, csPin, blPin legacy.PinOutput) DeviceOf[T] { + legacy.ConfigurePinOut(dcPin) + legacy.ConfigurePinOut(resetPin) + legacy.ConfigurePinOut(csPin) + legacy.ConfigurePinOut(blPin) return DeviceOf[T]{ bus: bus, - dcPin: dcPin, - resetPin: resetPin, - csPin: csPin, - blPin: blPin, + dcPin: dcPin.Set, + resetPin: resetPin.Set, + csPin: csPin.Set, + blPin: blPin.Set, } } @@ -114,11 +114,11 @@ func (d *DeviceOf[T]) Configure(cfg Config) { d.batchData = pixel.NewImage[T](int(d.batchLength), 1) // reset the device - d.resetPin.High() + d.resetPin(true) time.Sleep(5 * time.Millisecond) - d.resetPin.Low() + d.resetPin(false) time.Sleep(20 * time.Millisecond) - d.resetPin.High() + d.resetPin(true) time.Sleep(150 * time.Millisecond) // Common initialization @@ -226,7 +226,7 @@ func (d *DeviceOf[T]) Configure(cfg Config) { d.SetRotation(d.rotation) - d.blPin.High() + d.blPin(true) } // Display does nothing, there's no buffer as it might be too big for some boards @@ -423,7 +423,7 @@ func (d *DeviceOf[T]) Data(data uint8) { // Tx sends data to the display func (d *DeviceOf[T]) Tx(data []byte, isCommand bool) { - d.dcPin.Set(!isCommand) + d.dcPin(!isCommand) d.bus.Tx(data, nil) } @@ -438,9 +438,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.High() + d.blPin(true) } else { - d.blPin.Low() + d.blPin(false) } } diff --git a/st7735/st7735v2.go b/st7735/st7735v2.go deleted file mode 100644 index 91a27f3..0000000 --- a/st7735/st7735v2.go +++ /dev/null @@ -1,475 +0,0 @@ -// Package st7735 implements a driver for the ST7735 TFT displays, it comes in various screen sizes. -// -// Datasheet: https://www.crystalfontz.com/controllers/Sitronix/ST7735R/319/ -package st7735 // import "tinygo.org/x/drivers/st7735" - -import ( - "image/color" - "time" - - "errors" - - "tinygo.org/x/drivers" - "tinygo.org/x/drivers/pixel" -) - -type Model uint8 - -// Pixel formats supported by the st7735 driver. -type Color interface { - pixel.RGB444BE | pixel.RGB565BE - - pixel.BaseColor -} - -var ( - errOutOfBounds = errors.New("rectangle coordinates outside display area") -) - -// Device wraps an SPI connection. -type Device = DeviceOf[pixel.RGB565BE] - -// DeviceOf is a generic version of Device, which supports different pixel -// formats. -type DeviceOf[T Color] struct { - bus drivers.SPI - dcPin drivers.PinOutput - resetPin drivers.PinOutput - csPin drivers.PinOutput - blPin drivers.PinOutput - width int16 - height int16 - columnOffset int16 - rowOffset int16 - rotation drivers.Rotation - batchLength int16 - model Model - isBGR bool - batchData pixel.Image[T] // "image" with width, height of (batchLength, 1) -} - -// Config is the configuration for the display -type Config struct { - Width int16 - Height int16 - Rotation drivers.Rotation - Model Model - RowOffset int16 - ColumnOffset int16 -} - -// New creates a new ST7735 connection. The SPI wire must already be configured. -func New(bus drivers.SPI, resetPin, dcPin, csPin, blPin drivers.PinOutput) 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 drivers.PinOutput) DeviceOf[T] { - return DeviceOf[T]{ - bus: bus, - dcPin: dcPin, - resetPin: resetPin, - csPin: csPin, - blPin: blPin, - } -} - -func (d *DeviceOf[T]) Reset() { - d.resetPin(true) - time.Sleep(5 * time.Millisecond) - d.resetPin(false) - time.Sleep(20 * time.Millisecond) - d.resetPin(true) - time.Sleep(150 * time.Millisecond) -} - -// Configure initializes the display with default configuration -func (d *DeviceOf[T]) Configure(cfg Config) { - d.model = cfg.Model - if cfg.Width != 0 { - d.width = cfg.Width - } else { - if d.model == MINI80x160 { - d.width = 80 - } else { - d.width = 128 - } - } - if cfg.Height != 0 { - d.height = cfg.Height - } else { - d.height = 160 - } - d.rotation = cfg.Rotation - d.rowOffset = cfg.RowOffset - d.columnOffset = cfg.ColumnOffset - - d.batchLength = d.width - if d.height > d.width { - d.batchLength = d.height - } - d.batchLength += d.batchLength & 1 - d.batchData = pixel.NewImage[T](int(d.batchLength), 1) - - d.Reset() - - // Common initialization - d.Command(SWRESET) - time.Sleep(150 * time.Millisecond) - d.Command(SLPOUT) - time.Sleep(500 * time.Millisecond) - d.Command(FRMCTR1) - d.Data(0x01) - d.Data(0x2C) - d.Data(0x2D) - d.Command(FRMCTR2) - d.Data(0x01) - d.Data(0x2C) - d.Data(0x2D) - d.Command(FRMCTR3) - d.Data(0x01) - d.Data(0x2C) - d.Data(0x2D) - d.Data(0x01) - d.Data(0x2C) - d.Data(0x2D) - d.Command(INVCTR) - d.Data(0x07) - d.Command(PWCTR1) - d.Data(0xA2) - d.Data(0x02) - d.Data(0x84) - d.Command(PWCTR2) - d.Data(0xC5) - d.Command(PWCTR3) - d.Data(0x0A) - d.Data(0x00) - d.Command(PWCTR4) - d.Data(0x8A) - d.Data(0x2A) - d.Command(PWCTR5) - d.Data(0x8A) - d.Data(0xEE) - d.Command(VMCTR1) - d.Data(0x0E) - - // Set the color format depending on the generic type. - d.Command(COLMOD) - var zeroColor T - switch any(zeroColor).(type) { - case pixel.RGB444BE: - d.Data(0x03) // 12 bits per pixel - default: - d.Data(0x05) // 16 bits per pixel - } - - if d.model == GREENTAB { - d.InvertColors(false) - } else if d.model == MINI80x160 { - d.isBGR = true - d.InvertColors(true) - } - - // common color adjustment - d.Command(GMCTRP1) - - d.Data(0x02) - d.Data(0x1C) - d.Data(0x07) - d.Data(0x12) - d.Data(0x37) - d.Data(0x32) - d.Data(0x29) - d.Data(0x2D) - d.Data(0x29) - d.Data(0x25) - d.Data(0x2B) - d.Data(0x39) - d.Data(0x00) - d.Data(0x01) - d.Data(0x03) - d.Data(0x10) - d.Command(GMCTRN1) - d.Data(0x03) - d.Data(0x1D) - d.Data(0x07) - d.Data(0x06) - d.Data(0x2E) - d.Data(0x2C) - d.Data(0x29) - d.Data(0x2D) - d.Data(0x2E) - d.Data(0x2E) - d.Data(0x37) - d.Data(0x3F) - d.Data(0x00) - d.Data(0x00) - d.Data(0x02) - d.Data(0x10) - - d.Command(NORON) - time.Sleep(10 * time.Millisecond) - d.Command(DISPON) - time.Sleep(500 * time.Millisecond) - - if cfg.Model == MINI80x160 { - d.Command(MADCTL) - d.Data(0xC0) - } - - d.SetRotation(d.rotation) - - d.blPin(true) -} - -// Display does nothing, there's no buffer as it might be too big for some boards -func (d *DeviceOf[T]) Display() error { - return nil -} - -// SetPixel sets a pixel in the screen -func (d *DeviceOf[T]) SetPixel(x int16, y int16, c color.RGBA) { - w, h := d.Size() - if x < 0 || y < 0 || x >= w || y >= h { - return - } - d.FillRectangle(x, y, 1, 1, c) -} - -// setWindow prepares the screen to be modified at a given rectangle -func (d *DeviceOf[T]) setWindow(x, y, w, h int16) { - if d.rotation == drivers.Rotation0 || d.rotation == drivers.Rotation180 { - x += d.columnOffset - y += d.rowOffset - } else { - x += d.rowOffset - y += d.columnOffset - } - d.Tx([]uint8{CASET}, true) - d.Tx([]uint8{uint8(x >> 8), uint8(x), uint8((x + w - 1) >> 8), uint8(x + w - 1)}, false) - d.Tx([]uint8{RASET}, true) - d.Tx([]uint8{uint8(y >> 8), uint8(y), uint8((y + h - 1) >> 8), uint8(y + h - 1)}, false) - d.Command(RAMWR) -} - -// SetScrollWindow sets an area to scroll with fixed top and bottom parts of the display -func (d *DeviceOf[T]) SetScrollArea(topFixedArea, bottomFixedArea int16) { - // TODO: this code is broken, see the st7789 and ili9341 implementations for - // how to do this correctly. - d.Command(VSCRDEF) - d.Tx([]uint8{ - uint8(topFixedArea >> 8), uint8(topFixedArea), - uint8(d.height - topFixedArea - bottomFixedArea>>8), uint8(d.height - topFixedArea - bottomFixedArea), - uint8(bottomFixedArea >> 8), uint8(bottomFixedArea)}, - false) -} - -// SetScroll sets the vertical scroll address of the display. -func (d *DeviceOf[T]) SetScroll(line int16) { - d.Command(VSCRSADD) - d.Tx([]uint8{uint8(line >> 8), uint8(line)}, false) -} - -// SpotScroll returns the display to its normal state -func (d *DeviceOf[T]) StopScroll() { - d.Command(NORON) -} - -// FillRectangle fills a rectangle at a given coordinates with a color -func (d *DeviceOf[T]) FillRectangle(x, y, width, height int16, c color.RGBA) error { - k, i := d.Size() - if x < 0 || y < 0 || width <= 0 || height <= 0 || - x >= k || (x+width) > k || y >= i || (y+height) > i { - return errors.New("rectangle coordinates outside display area") - } - d.setWindow(x, y, width, height) - - d.batchData.FillSolidColor(pixel.NewColor[T](c.R, c.G, c.B)) - i = width * height - for i > 0 { - if i >= d.batchLength { - d.Tx(d.batchData.RawBuffer(), false) - } else { - d.Tx(d.batchData.Rescale(int(i), 1).RawBuffer(), false) - } - i -= d.batchLength - } - return nil -} - -// DrawRGBBitmap8 copies an RGB bitmap to the internal buffer at given coordinates -// -// Deprecated: use DrawBitmap instead. -func (d *DeviceOf[T]) DrawRGBBitmap8(x, y int16, data []uint8, w, h int16) error { - k, i := d.Size() - if x < 0 || y < 0 || w <= 0 || h <= 0 || - x >= k || (x+w) > k || y >= i || (y+h) > i { - return errOutOfBounds - } - d.setWindow(x, y, w, h) - d.Tx(data, false) - return nil -} - -// DrawBitmap copies the bitmap to the internal buffer on the screen at the -// given coordinates. It returns once the image data has been sent completely. -func (d *DeviceOf[T]) DrawBitmap(x, y int16, bitmap pixel.Image[T]) error { - width, height := bitmap.Size() - return d.DrawRGBBitmap8(x, y, bitmap.RawBuffer(), int16(width), int16(height)) -} - -// FillRectangle fills a rectangle at a given coordinates with a buffer -func (d *DeviceOf[T]) FillRectangleWithBuffer(x, y, width, height int16, buffer []color.RGBA) error { - k, l := d.Size() - if x < 0 || y < 0 || width <= 0 || height <= 0 || - x >= k || (x+width) > k || y >= l || (y+height) > l { - return errors.New("rectangle coordinates outside display area") - } - k = width * height - l = int16(len(buffer)) - if k != l { - return errors.New("buffer length does not match with rectangle size") - } - - d.setWindow(x, y, width, height) - - offset := int16(0) - for k > 0 { - for i := int16(0); i < d.batchLength; i++ { - if offset+i < l { - c := buffer[offset+i] - d.batchData.Set(int(i), 0, pixel.NewColor[T](c.R, c.G, c.B)) - } - } - if k >= d.batchLength { - d.Tx(d.batchData.RawBuffer(), false) - } else { - d.Tx(d.batchData.Rescale(int(k), 1).RawBuffer(), false) - } - k -= d.batchLength - offset += d.batchLength - } - return nil -} - -// DrawFastVLine draws a vertical line faster than using SetPixel -func (d *DeviceOf[T]) DrawFastVLine(x, y0, y1 int16, c color.RGBA) { - if y0 > y1 { - y0, y1 = y1, y0 - } - d.FillRectangle(x, y0, 1, y1-y0+1, c) -} - -// DrawFastHLine draws a horizontal line faster than using SetPixel -func (d *DeviceOf[T]) DrawFastHLine(x0, x1, y int16, c color.RGBA) { - if x0 > x1 { - x0, x1 = x1, x0 - } - d.FillRectangle(x0, y, x1-x0+1, 1, c) -} - -// FillScreen fills the screen with a given color -func (d *DeviceOf[T]) FillScreen(c color.RGBA) { - if d.rotation == drivers.Rotation0 || d.rotation == drivers.Rotation180 { - d.FillRectangle(0, 0, d.width, d.height, c) - } else { - d.FillRectangle(0, 0, d.height, d.width, c) - } -} - -// Rotation returns the currently configured rotation. -func (d *DeviceOf[T]) Rotation() drivers.Rotation { - return d.rotation -} - -// SetRotation changes the rotation of the device (clock-wise) -func (d *DeviceOf[T]) SetRotation(rotation drivers.Rotation) error { - d.rotation = rotation - madctl := uint8(0) - switch rotation % 4 { - case drivers.Rotation0: - madctl = MADCTL_MX | MADCTL_MY - case drivers.Rotation90: - madctl = MADCTL_MY | MADCTL_MV - case drivers.Rotation180: - // nothing to do - case drivers.Rotation270: - madctl = MADCTL_MX | MADCTL_MV - } - if d.isBGR { - madctl |= MADCTL_BGR - } - d.Command(MADCTL) - d.Data(madctl) - return nil -} - -// Command sends a command to the display -func (d *DeviceOf[T]) Command(command uint8) { - d.Tx([]byte{command}, true) -} - -// Command sends a data to the display -func (d *DeviceOf[T]) Data(data uint8) { - d.Tx([]byte{data}, false) -} - -func (d *DeviceOf[T]) TxData(data []byte) error { - return d.Tx(data, false) -} - -// Tx sends data to the display -func (d *DeviceOf[T]) Tx(data []byte, isCommand bool) error { - d.dcPin(!isCommand) - return d.bus.Tx(data, nil) -} - -// Size returns the current size of the display. -func (d *DeviceOf[T]) Size() (w, h int16) { - if d.rotation == drivers.Rotation0 || d.rotation == drivers.Rotation180 { - return d.width, d.height - } - return d.height, d.width -} - -// EnableBacklight enables or disables the backlight -func (d *DeviceOf[T]) EnableBacklight(enable bool) { - if enable { - d.blPin(true) - } else { - d.blPin(false) - } -} - -// Set the sleep mode for this LCD panel. When sleeping, the panel uses a lot -// less power. The LCD won't display an image anymore, but the memory contents -// will be kept. -func (d *DeviceOf[T]) Sleep(sleepEnabled bool) error { - if sleepEnabled { - // Shut down LCD panel. - d.Command(SLPIN) - time.Sleep(5 * time.Millisecond) // 5ms required by the datasheet - } else { - // Turn the LCD panel back on. - d.Command(SLPOUT) - // The st7735 datasheet says it is necessary to wait 120ms before - // sending another command. - time.Sleep(120 * time.Millisecond) - } - return nil -} - -// InverColors inverts the colors of the screen -func (d *DeviceOf[T]) InvertColors(invert bool) { - if invert { - d.Command(INVON) - } else { - d.Command(INVOFF) - } -} - -// IsBGR changes the color mode (RGB/BGR) -func (d *DeviceOf[T]) IsBGR(bgr bool) { - d.isBGR = bgr -} diff --git a/st7789/st7789.go b/st7789/st7789.go index 5db2402..f466208 100644 --- a/st7789/st7789.go +++ b/st7789/st7789.go @@ -7,13 +7,13 @@ package st7789 // import "tinygo.org/x/drivers/st7789" import ( "image/color" - "machine" "math" "time" "errors" "tinygo.org/x/drivers" + "tinygo.org/x/drivers/internal/legacy" "tinygo.org/x/drivers/pixel" ) @@ -46,10 +46,10 @@ type Device = DeviceOf[pixel.RGB565BE] // formats. type DeviceOf[T Color] struct { bus drivers.SPI - dcPin machine.Pin - resetPin machine.Pin - csPin machine.Pin - blPin machine.Pin + dcPin drivers.PinOutput + resetPin drivers.PinOutput + csPin drivers.PinOutput + blPin drivers.PinOutput width int16 height int16 columnOffsetCfg int16 @@ -83,23 +83,27 @@ 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 machine.Pin) Device { +func New(bus drivers.SPI, resetPin, dcPin, csPin, blPin legacy.PinOutput) 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 machine.Pin) DeviceOf[T] { - dcPin.Configure(machine.PinConfig{Mode: machine.PinOutput}) - resetPin.Configure(machine.PinConfig{Mode: machine.PinOutput}) - csPin.Configure(machine.PinConfig{Mode: machine.PinOutput}) - blPin.Configure(machine.PinConfig{Mode: machine.PinOutput}) +func NewOf[T Color](bus drivers.SPI, resetPin, dcPin, csPin, blPin legacy.PinOutput) DeviceOf[T] { + legacy.ConfigurePinOut(dcPin) + legacy.ConfigurePinOut(resetPin) + legacy.ConfigurePinOut(csPin) + legacy.ConfigurePinOut(blPin) + var cs drivers.PinOutput + if !legacy.PinIsNoPin(csPin) { + cs = csPin.Set + } return DeviceOf[T]{ bus: bus, - dcPin: dcPin, - resetPin: resetPin, - csPin: csPin, - blPin: blPin, + dcPin: dcPin.Set, + resetPin: resetPin.Set, + csPin: cs, + blPin: blPin.Set, } } @@ -139,11 +143,11 @@ func (d *DeviceOf[T]) Configure(cfg Config) { d.batchLength += d.batchLength & 1 // Reset the device - d.resetPin.High() + d.resetPin(true) time.Sleep(50 * time.Millisecond) - d.resetPin.Low() + d.resetPin(false) time.Sleep(50 * time.Millisecond) - d.resetPin.High() + d.resetPin(true) time.Sleep(50 * time.Millisecond) // Common initialization @@ -209,7 +213,7 @@ func (d *DeviceOf[T]) Configure(cfg Config) { time.Sleep(10 * time.Millisecond) // d.endWrite() - d.blPin.High() // Backlight ON + d.blPin(true) // Backlight ON } // Send a command with data to the display. It does not change the chip select @@ -217,9 +221,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.Low() + d.dcPin(false) err := d.bus.Tx(d.cmdBuf[:1], nil) - d.dcPin.High() + d.dcPin(true) if len(data) != 0 { err = d.bus.Tx(data, nil) } @@ -229,16 +233,16 @@ func (d *DeviceOf[T]) sendCommand(command uint8, data []byte) error { // startWrite must be called at the beginning of all exported methods to set the // chip select pin low. func (d *DeviceOf[T]) startWrite() { - if d.csPin != machine.NoPin { - d.csPin.Low() + if d.csPin != nil { + d.csPin(false) } } // endWrite must be called at the end of all exported methods to set the chip // select pin high. func (d *DeviceOf[T]) endWrite() { - if d.csPin != machine.NoPin { - d.csPin.High() + if d.csPin != nil { + d.csPin(true) } } @@ -300,9 +304,9 @@ func (d *DeviceOf[T]) SyncToScanLine(scanline uint16) { func (d *DeviceOf[T]) GetScanLine() uint16 { d.startWrite() data := []uint8{0x00, 0x00} - d.dcPin.Low() + d.dcPin(false) d.bus.Transfer(GSCAN) - d.dcPin.High() + d.dcPin(true) for i := range data { data[i], _ = d.bus.Transfer(0xFF) } @@ -541,9 +545,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.High() + d.blPin(true) } else { - d.blPin.Low() + d.blPin(false) } } diff --git a/sx127x/sx127x.go b/sx127x/sx127x.go index f2c2a87..f12a435 100644 --- a/sx127x/sx127x.go +++ b/sx127x/sx127x.go @@ -6,10 +6,10 @@ package sx127x import ( "errors" - "machine" "time" "tinygo.org/x/drivers" + "tinygo.org/x/drivers/internal/legacy" "tinygo.org/x/drivers/lora" ) @@ -22,7 +22,7 @@ const ( // Device wraps an SPI connection to a SX127x device. type Device struct { spi drivers.SPI // SPI bus for module communication - rstPin machine.Pin // GPIO for reset + rstPin drivers.PinOutput // GPIO for reset radioEventChan chan lora.RadioEvent // Channel for Receiving events loraConf lora.Config // Current Lora configuration controller RadioController // to manage interactions with the radio @@ -43,10 +43,10 @@ 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 machine.Pin) *Device { +func New(spi drivers.SPI, rstPin legacy.PinOutput) *Device { k := Device{ spi: spi, - rstPin: rstPin, + rstPin: rstPin.Set, radioEventChan: make(chan lora.RadioEvent, RADIOEVENTCHAN_SIZE), spiTxBuf: make([]byte, SPI_BUFFER_SIZE), spiRxBuf: make([]byte, SPI_BUFFER_SIZE), @@ -67,9 +67,9 @@ func (d *Device) SetRadioController(rc RadioController) error { // Reset re-initialize the sx127x device func (d *Device) Reset() { - d.rstPin.Low() + d.rstPin(false) time.Sleep(100 * time.Millisecond) - d.rstPin.High() + d.rstPin(true) time.Sleep(100 * time.Millisecond) } diff --git a/uc8151/uc8151.go b/uc8151/uc8151.go index 38c48f9..bee7c56 100644 --- a/uc8151/uc8151.go +++ b/uc8151/uc8151.go @@ -8,10 +8,10 @@ package uc8151 // import "tinygo.org/x/drivers/uc8151" import ( "errors" "image/color" - "machine" "time" "tinygo.org/x/drivers" + "tinygo.org/x/drivers/internal/legacy" "tinygo.org/x/drivers/pixel" ) @@ -31,10 +31,10 @@ type Config struct { type Device struct { bus drivers.SPI - cs machine.Pin - dc machine.Pin - rst machine.Pin - busy machine.Pin + cs drivers.PinOutput + dc drivers.PinOutput + rst drivers.PinOutput + busy drivers.PinInput width int16 height int16 buffer []uint8 @@ -49,17 +49,17 @@ 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, busyPin machine.Pin) Device { - csPin.Configure(machine.PinConfig{Mode: machine.PinOutput}) - dcPin.Configure(machine.PinConfig{Mode: machine.PinOutput}) - rstPin.Configure(machine.PinConfig{Mode: machine.PinOutput}) - busyPin.Configure(machine.PinConfig{Mode: machine.PinInput}) +func New(bus drivers.SPI, csPin, dcPin, rstPin legacy.PinOutput, busyPin legacy.PinInput) Device { + legacy.ConfigurePinOut(csPin) + legacy.ConfigurePinOut(dcPin) + legacy.ConfigurePinOut(rstPin) + legacy.ConfigurePinInput(busyPin) return Device{ bus: bus, - cs: csPin, - dc: dcPin, - rst: rstPin, - busy: busyPin, + cs: csPin.Set, + dc: dcPin.Set, + rst: rstPin.Set, + busy: busyPin.Get, } } @@ -133,9 +133,9 @@ func (d *Device) Configure(cfg Config) { // Reset resets the device func (d *Device) Reset() { - d.rst.Low() + d.rst(false) time.Sleep(10 * time.Millisecond) - d.rst.High() + d.rst(true) time.Sleep(10 * time.Millisecond) d.WaitUntilIdle() } @@ -152,18 +152,18 @@ func (d *Device) PowerOn() { // SendCommand sends a command to the display func (d *Device) SendCommand(command uint8) { - d.dc.Low() - d.cs.Low() + d.dc(false) + d.cs(false) d.bus.Transfer(command) - d.cs.High() + d.cs(true) } // SendData sends a data byte to the display func (d *Device) SendData(data ...uint8) { - d.dc.High() - d.cs.Low() + d.dc(true) + d.cs(false) d.bus.Tx(data, nil) - d.cs.High() + d.cs(true) } // SetPixel modifies the internal buffer in a single pixel. @@ -313,14 +313,14 @@ func (d *Device) ClearDisplay() { // WaitUntilIdle waits until the display is ready func (d *Device) WaitUntilIdle() { - for !d.busy.Get() { + for !d.busy() { time.Sleep(10 * time.Millisecond) } } // IsBusy returns the busy status of the display func (d *Device) IsBusy() bool { - return d.busy.Get() + return d.busy() } // ClearBuffer sets the buffer to 0xFF (white) diff --git a/waveshare-epd/epd1in54/epd1in54.go b/waveshare-epd/epd1in54/epd1in54.go index 120b356..86c7d9b 100644 --- a/waveshare-epd/epd1in54/epd1in54.go +++ b/waveshare-epd/epd1in54/epd1in54.go @@ -12,6 +12,9 @@ import ( "image/color" "machine" "time" + + "tinygo.org/x/drivers" + "tinygo.org/x/drivers/internal/legacy" ) type Config struct { @@ -22,12 +25,12 @@ type Config struct { } type Device struct { - bus *machine.SPI - cs machine.Pin - dc machine.Pin - rst machine.Pin - busy machine.Pin - + bus *machine.SPI + cs drivers.PinOutput + dc drivers.PinOutput + rst drivers.PinOutput + busy drivers.PinInput + config func() buffer []uint8 rotation Rotation } @@ -79,22 +82,28 @@ 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, busyPin machine.Pin) Device { +func New(bus *machine.SPI, csPin, dcPin, rstPin legacy.PinOutput, busyPin legacy.PinInput) Device { return Device{ buffer: make([]uint8, (uint32(Width)*uint32(Height))/8), bus: bus, - cs: csPin, - dc: dcPin, - rst: rstPin, - busy: busyPin, + cs: csPin.Set, + dc: dcPin.Set, + rst: rstPin.Set, + busy: busyPin.Get, + config: func() { + legacy.ConfigurePinOut(csPin) + legacy.ConfigurePinOut(dcPin) + legacy.ConfigurePinOut(rstPin) + legacy.ConfigurePinInput(busyPin) + }, } } func (d *Device) LDirInit(cfg Config) { - d.cs.Configure(machine.PinConfig{Mode: machine.PinOutput}) - d.rst.Configure(machine.PinConfig{Mode: machine.PinOutput}) - d.dc.Configure(machine.PinConfig{Mode: machine.PinOutput}) - d.busy.Configure(machine.PinConfig{Mode: machine.PinInput}) + if d.config == nil { + panic(legacy.ErrConfigBeforeInstantiated) + } + d.config() d.bus.Configure(machine.SPIConfig{ Frequency: 2000000, @@ -150,10 +159,10 @@ func (d *Device) LDirInit(cfg Config) { } func (d *Device) HDirInit(cfg Config) { - d.cs.Configure(machine.PinConfig{Mode: machine.PinOutput}) - d.rst.Configure(machine.PinConfig{Mode: machine.PinOutput}) - d.dc.Configure(machine.PinConfig{Mode: machine.PinOutput}) - d.busy.Configure(machine.PinConfig{Mode: machine.PinInput}) + if d.config == nil { + panic(legacy.ErrConfigBeforeInstantiated) + } + d.config() d.bus.Configure(machine.SPIConfig{ Frequency: 2000000, @@ -231,11 +240,11 @@ func (d *Device) setLUT(lut [159]uint8) { // Reset resets the display. func (d *Device) Reset() { - d.rst.High() + d.rst(true) time.Sleep(20 * time.Millisecond) - d.rst.Low() + d.rst(false) time.Sleep(5 * time.Millisecond) - d.rst.High() + d.rst(true) time.Sleep(20 * time.Millisecond) } @@ -252,13 +261,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.Low() + d.dc(false) } else { - d.dc.High() + d.dc(true) } - d.cs.Low() + d.cs(false) d.bus.Transfer(data) - d.cs.High() + d.cs(true) } // SetPixel modifies the internal buffer in a single pixel. @@ -369,7 +378,7 @@ func (d *Device) Clear() { // WaitUntilIdle waits until the display is ready func (d *Device) WaitUntilIdle() { - for d.busy.Get() { + for d.busy() { time.Sleep(100 * time.Millisecond) } time.Sleep(200 * time.Millisecond) @@ -377,7 +386,7 @@ func (d *Device) WaitUntilIdle() { // IsBusy returns the busy status of the display func (d *Device) IsBusy() bool { - return d.busy.Get() + return d.busy() } // ClearBuffer sets the buffer to 0xFF (white) @@ -420,5 +429,5 @@ func (d *Device) Sleep() { d.SendData(0x01) time.Sleep(200 * time.Millisecond) - d.rst.Low() + d.rst(false) } diff --git a/waveshare-epd/epd2in13/epd2in13.go b/waveshare-epd/epd2in13/epd2in13.go index e49fd42..6fd089a 100644 --- a/waveshare-epd/epd2in13/epd2in13.go +++ b/waveshare-epd/epd2in13/epd2in13.go @@ -6,10 +6,10 @@ package epd2in13 // import "tinygo.org/x/drivers/waveshare-epd/epd2in13" import ( "errors" "image/color" - "machine" "time" "tinygo.org/x/drivers" + "tinygo.org/x/drivers/internal/legacy" ) type Config struct { @@ -21,10 +21,10 @@ type Config struct { type Device struct { bus drivers.SPI - cs machine.Pin - dc machine.Pin - rst machine.Pin - busy machine.Pin + cs drivers.PinOutput + dc drivers.PinOutput + rst drivers.PinOutput + busy drivers.PinInput logicalWidth int16 width int16 height int16 @@ -53,17 +53,17 @@ 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, busyPin machine.Pin) Device { - csPin.Configure(machine.PinConfig{Mode: machine.PinOutput}) - dcPin.Configure(machine.PinConfig{Mode: machine.PinOutput}) - rstPin.Configure(machine.PinConfig{Mode: machine.PinOutput}) - busyPin.Configure(machine.PinConfig{Mode: machine.PinInput}) +func New(bus drivers.SPI, csPin, dcPin, rstPin legacy.PinOutput, busyPin legacy.PinInput) Device { + legacy.ConfigurePinOut(csPin) + legacy.ConfigurePinOut(dcPin) + legacy.ConfigurePinOut(rstPin) + legacy.ConfigurePinInput(busyPin) return Device{ bus: bus, - cs: csPin, - dc: dcPin, - rst: rstPin, - busy: busyPin, + cs: csPin.Set, + dc: dcPin.Set, + rst: rstPin.Set, + busy: busyPin.Get, } } @@ -91,9 +91,9 @@ func (d *Device) Configure(cfg Config) { d.buffer[i] = 0xFF } - d.cs.Low() - d.dc.Low() - d.rst.Low() + d.cs(false) + d.dc(false) + d.rst(false) d.Reset() @@ -119,9 +119,9 @@ func (d *Device) Configure(cfg Config) { // Reset resets the device func (d *Device) Reset() { - d.rst.Low() + d.rst(false) time.Sleep(200 * time.Millisecond) - d.rst.High() + d.rst(true) time.Sleep(200 * time.Millisecond) } @@ -155,13 +155,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.Low() + d.dc(false) } else { - d.dc.High() + d.dc(true) } - d.cs.Low() + d.cs(false) d.bus.Transfer(data) - d.cs.High() + d.cs(true) } // SetLUT sets the look up tables for full or partial updates @@ -298,14 +298,14 @@ func (d *Device) setMemoryPointer(x int16, y int16) { // WaitUntilIdle waits until the display is ready func (d *Device) WaitUntilIdle() { - for d.busy.Get() { + for d.busy() { time.Sleep(100 * time.Millisecond) } } // IsBusy returns the busy status of the display func (d *Device) IsBusy() bool { - return d.busy.Get() + return d.busy() } // ClearBuffer sets the buffer to 0xFF (white) diff --git a/waveshare-epd/epd2in13x/epd2in13x.go b/waveshare-epd/epd2in13x/epd2in13x.go index b84f5a5..ee3fc61 100644 --- a/waveshare-epd/epd2in13x/epd2in13x.go +++ b/waveshare-epd/epd2in13x/epd2in13x.go @@ -6,10 +6,10 @@ package epd2in13x // import "tinygo.org/x/drivers/waveshare-epd/epd2in13x" import ( "errors" "image/color" - "machine" "time" "tinygo.org/x/drivers" + "tinygo.org/x/drivers/internal/legacy" ) type Config struct { @@ -20,10 +20,10 @@ type Config struct { type Device struct { bus drivers.SPI - cs machine.Pin - dc machine.Pin - rst machine.Pin - busy machine.Pin + cs drivers.PinOutput + dc drivers.PinOutput + rst drivers.PinOutput + busy drivers.PinInput width int16 height int16 buffer [][]uint8 @@ -33,17 +33,17 @@ 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, busyPin machine.Pin) Device { - csPin.Configure(machine.PinConfig{Mode: machine.PinOutput}) - dcPin.Configure(machine.PinConfig{Mode: machine.PinOutput}) - rstPin.Configure(machine.PinConfig{Mode: machine.PinOutput}) - busyPin.Configure(machine.PinConfig{Mode: machine.PinInput}) +func New(bus drivers.SPI, csPin, dcPin, rstPin legacy.PinOutput, busyPin legacy.PinInput) Device { + legacy.ConfigurePinOut(csPin) + legacy.ConfigurePinOut(dcPin) + legacy.ConfigurePinOut(rstPin) + legacy.ConfigurePinInput(busyPin) return Device{ bus: bus, - cs: csPin, - dc: dcPin, - rst: rstPin, - busy: busyPin, + cs: csPin.Set, + dc: dcPin.Set, + rst: rstPin.Set, + busy: busyPin.Get, } } @@ -75,9 +75,9 @@ func (d *Device) Configure(cfg Config) { } } - d.cs.Low() - d.dc.Low() - d.rst.Low() + d.cs(false) + d.dc(false) + d.rst(false) d.Reset() @@ -99,9 +99,9 @@ func (d *Device) Configure(cfg Config) { // Reset resets the device func (d *Device) Reset() { - d.rst.Low() + d.rst(false) time.Sleep(200 * time.Millisecond) - d.rst.High() + d.rst(true) time.Sleep(200 * time.Millisecond) } @@ -126,13 +126,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.Low() + d.dc(false) } else { - d.dc.High() + d.dc(true) } - d.cs.Low() + d.cs(false) d.bus.Transfer(data) - d.cs.High() + d.cs(true) } // SetPixel modifies the internal buffer in a single pixel. @@ -277,14 +277,14 @@ func (d *Device) ClearDisplay() { // WaitUntilIdle waits until the display is ready func (d *Device) WaitUntilIdle() { - for !d.busy.Get() { + for !d.busy() { time.Sleep(100 * time.Millisecond) } } // IsBusy returns the busy status of the display func (d *Device) IsBusy() bool { - return d.busy.Get() + return d.busy() } // ClearBuffer sets the buffer to 0xFF (white) diff --git a/waveshare-epd/epd2in66b/dev.go b/waveshare-epd/epd2in66b/dev.go index cadff9c..86b93ae 100644 --- a/waveshare-epd/epd2in66b/dev.go +++ b/waveshare-epd/epd2in66b/dev.go @@ -5,7 +5,6 @@ package epd2in66b import ( "image/color" - "machine" "time" "tinygo.org/x/drivers" @@ -18,19 +17,12 @@ const ( const Baudrate = 4_000_000 // 4 MHz -type Config struct { - ResetPin machine.Pin - DataPin machine.Pin - ChipSelectPin machine.Pin - BusyPin machine.Pin -} - type Device struct { bus drivers.SPI - cs machine.Pin - dc machine.Pin - rst machine.Pin - busy machine.Pin + cs drivers.PinOutput + dc drivers.PinOutput + rst drivers.PinOutput + busy drivers.PinInput blackBuffer []byte redBuffer []byte @@ -50,21 +42,6 @@ func New(bus drivers.SPI) Device { } } -// Configure configures the device and its pins. -func (d *Device) Configure(c Config) error { - d.cs = c.ChipSelectPin - d.dc = c.DataPin - d.rst = c.ResetPin - d.busy = c.BusyPin - - d.cs.Configure(machine.PinConfig{Mode: machine.PinOutput}) - d.dc.Configure(machine.PinConfig{Mode: machine.PinOutput}) - d.rst.Configure(machine.PinConfig{Mode: machine.PinOutput}) - d.busy.Configure(machine.PinConfig{Mode: machine.PinInput}) - - return nil -} - func (d *Device) Size() (x, y int16) { return displayWidth, displayHeight } @@ -199,11 +176,11 @@ func (d *Device) setCursor(x, y uint16) error { } func (d *Device) hwReset() { - d.rst.High() + d.rst(true) time.Sleep(50 * time.Millisecond) - d.rst.Low() + d.rst(false) time.Sleep(2 * time.Millisecond) - d.rst.High() + d.rst(true) time.Sleep(50 * time.Millisecond) } @@ -229,7 +206,7 @@ func (d *Device) WaitUntilIdle() { // give it some time to get busy time.Sleep(50 * time.Millisecond) - for d.busy.Get() { // high = busy + for d.busy() { // high = busy time.Sleep(10 * time.Millisecond) } @@ -255,26 +232,26 @@ func (d *Device) sendCommandSequence(seq []byte) error { } func (d *Device) sendCommandByte(b byte) error { - d.dc.Low() - d.cs.Low() + d.dc(false) + d.cs(false) _, err := d.bus.Transfer(b) - d.cs.High() + d.cs(true) return err } func (d *Device) sendDataByte(b byte) error { - d.dc.High() - d.cs.Low() + d.dc(true) + d.cs(false) _, err := d.bus.Transfer(b) - d.cs.High() + d.cs(true) return err } func (d *Device) sendData(b []byte) error { - d.dc.High() - d.cs.Low() + d.dc(true) + d.cs(false) err := d.bus.Tx(b, nil) - d.cs.High() + d.cs(true) return err } diff --git a/waveshare-epd/epd2in66b/dev_baremetal.go b/waveshare-epd/epd2in66b/dev_baremetal.go new file mode 100644 index 0000000..7fa4a56 --- /dev/null +++ b/waveshare-epd/epd2in66b/dev_baremetal.go @@ -0,0 +1,30 @@ +//go:build baremetal + +package epd2in66b + +import "machine" + +type Config struct { + ResetPin machine.Pin + DataPin machine.Pin + ChipSelectPin machine.Pin + BusyPin machine.Pin +} + +// Configure configures the device and its pins. +func (d *Device) Configure(c Config) error { + cs := c.ChipSelectPin + dc := c.DataPin + rst := c.ResetPin + busy := c.BusyPin + + cs.Configure(machine.PinConfig{Mode: machine.PinOutput}) + dc.Configure(machine.PinConfig{Mode: machine.PinOutput}) + rst.Configure(machine.PinConfig{Mode: machine.PinOutput}) + busy.Configure(machine.PinConfig{Mode: machine.PinInput}) + d.cs = cs.Set + d.dc = dc.Set + d.rst = rst.Set + d.busy = busy.Get + return nil +} diff --git a/waveshare-epd/epd2in9/epd2in9.go b/waveshare-epd/epd2in9/epd2in9.go index 994cd79..9abdb77 100644 --- a/waveshare-epd/epd2in9/epd2in9.go +++ b/waveshare-epd/epd2in9/epd2in9.go @@ -13,10 +13,10 @@ package epd2in9 // import "tinygo.org/x/drivers/waveshare-epd/epd2in9" import ( "image/color" - "machine" "time" "tinygo.org/x/drivers" + "tinygo.org/x/drivers/internal/legacy" ) type Config struct { @@ -28,10 +28,10 @@ type Config struct { type Device struct { bus drivers.SPI - cs machine.Pin - dc machine.Pin - rst machine.Pin - busy machine.Pin + cs drivers.PinOutput + dc drivers.PinOutput + rst drivers.PinOutput + busy drivers.PinInput logicalWidth int16 width int16 height int16 @@ -61,17 +61,17 @@ 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, busyPin machine.Pin) Device { - csPin.Configure(machine.PinConfig{Mode: machine.PinOutput}) - dcPin.Configure(machine.PinConfig{Mode: machine.PinOutput}) - rstPin.Configure(machine.PinConfig{Mode: machine.PinOutput}) - busyPin.Configure(machine.PinConfig{Mode: machine.PinInput}) +func New(bus drivers.SPI, csPin, dcPin, rstPin legacy.PinOutput, busyPin legacy.PinInput) Device { + legacy.ConfigurePinOut(csPin) + legacy.ConfigurePinOut(dcPin) + legacy.ConfigurePinOut(rstPin) + legacy.ConfigurePinInput(busyPin) return Device{ bus: bus, - cs: csPin, - dc: dcPin, - rst: rstPin, - busy: busyPin, + cs: csPin.Set, + dc: dcPin.Set, + rst: rstPin.Set, + busy: busyPin.Get, } } @@ -99,9 +99,9 @@ func (d *Device) Configure(cfg Config) { d.buffer[i] = 0xFF } - d.cs.Low() - d.dc.Low() - d.rst.Low() + d.cs(false) + d.dc(false) + d.rst(false) d.Reset() @@ -127,9 +127,9 @@ func (d *Device) Configure(cfg Config) { // Reset resets the device func (d *Device) Reset() { - d.rst.Low() + d.rst(false) time.Sleep(200 * time.Millisecond) - d.rst.High() + d.rst(true) time.Sleep(200 * time.Millisecond) } @@ -152,13 +152,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.Low() + d.dc(false) } else { - d.dc.High() + d.dc(true) } - d.cs.Low() + d.cs(false) d.bus.Transfer(data) - d.cs.High() + d.cs(true) } // SetLUT sets the look up tables for full or partial updates @@ -245,14 +245,14 @@ func (d *Device) setMemoryPointer(x int16, y int16) { // WaitUntilIdle waits until the display is ready func (d *Device) WaitUntilIdle() { - for d.busy.Get() { + for d.busy() { time.Sleep(100 * time.Millisecond) } } // IsBusy returns the busy status of the display func (d *Device) IsBusy() bool { - return d.busy.Get() + return d.busy() } // ClearBuffer sets the buffer to 0xFF (white) diff --git a/waveshare-epd/epd4in2/epd4in2.go b/waveshare-epd/epd4in2/epd4in2.go index 902d21e..a731545 100644 --- a/waveshare-epd/epd4in2/epd4in2.go +++ b/waveshare-epd/epd4in2/epd4in2.go @@ -10,10 +10,10 @@ package epd4in2 import ( "image/color" - "machine" "time" "tinygo.org/x/drivers" + "tinygo.org/x/drivers/internal/legacy" ) type Config struct { @@ -25,10 +25,10 @@ type Config struct { type Device struct { bus drivers.SPI - cs machine.Pin - dc machine.Pin - rst machine.Pin - busy machine.Pin + cs drivers.PinOutput + dc drivers.PinOutput + rst drivers.PinOutput + busy drivers.PinInput logicalWidth int16 width int16 height int16 @@ -40,17 +40,17 @@ 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, busyPin machine.Pin) Device { - csPin.Configure(machine.PinConfig{Mode: machine.PinOutput}) - dcPin.Configure(machine.PinConfig{Mode: machine.PinOutput}) - rstPin.Configure(machine.PinConfig{Mode: machine.PinOutput}) - busyPin.Configure(machine.PinConfig{Mode: machine.PinInput}) +func New(bus drivers.SPI, csPin, dcPin, rstPin legacy.PinOutput, busyPin legacy.PinInput) Device { + legacy.ConfigurePinOut(csPin) + legacy.ConfigurePinOut(dcPin) + legacy.ConfigurePinOut(rstPin) + legacy.ConfigurePinInput(busyPin) return Device{ bus: bus, - cs: csPin, - dc: dcPin, - rst: rstPin, - busy: busyPin, + cs: csPin.Set, + dc: dcPin.Set, + rst: rstPin.Set, + busy: busyPin.Get, } } @@ -78,9 +78,9 @@ func (d *Device) Configure(cfg Config) { d.buffer[i] = 0xFF } - d.cs.Low() - d.dc.Low() - d.rst.Low() + d.cs(false) + d.dc(false) + d.rst(false) d.Reset() d.SendCommand(POWER_SETTING) @@ -104,9 +104,9 @@ func (d *Device) Configure(cfg Config) { // Reset resets the device func (d *Device) Reset() { - d.rst.Low() + d.rst(false) time.Sleep(200 * time.Millisecond) - d.rst.High() + d.rst(true) time.Sleep(200 * time.Millisecond) } @@ -145,13 +145,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.Low() + d.dc(false) } else { - d.dc.High() + d.dc(true) } - d.cs.Low() + d.cs(false) d.bus.Transfer(data) - d.cs.High() + d.cs(true) } // SetLUT sets the look up tables for full or partial updates @@ -311,14 +311,14 @@ func (d *Device) ClearDisplay() { // WaitUntilIdle waits until the display is ready func (d *Device) WaitUntilIdle() { - for d.busy.Get() { + for d.busy() { time.Sleep(100 * time.Millisecond) } } // IsBusy returns the busy status of the display func (d *Device) IsBusy() bool { - return d.busy.Get() + return d.busy() } // ClearBuffer sets the buffer to 0xFF (white)