From 966210f1b0178152a330209832f86f60d8b489c1 Mon Sep 17 00:00:00 2001 From: sago35 Date: Thu, 18 Nov 2021 08:47:23 +0900 Subject: [PATCH] ili9341, ili9342: add support for m5stack-core2 --- examples/ili9341/initdisplay/m5stack_core2.go | 49 +++++++++++++++++++ ili9341/ili9341.go | 36 ++++++++++---- ili9341/registers.go | 5 ++ 3 files changed, 81 insertions(+), 9 deletions(-) create mode 100644 examples/ili9341/initdisplay/m5stack_core2.go diff --git a/examples/ili9341/initdisplay/m5stack_core2.go b/examples/ili9341/initdisplay/m5stack_core2.go new file mode 100644 index 0000000..70e2c57 --- /dev/null +++ b/examples/ili9341/initdisplay/m5stack_core2.go @@ -0,0 +1,49 @@ +//go:build m5stack_core2 +// +build m5stack_core2 + +package initdisplay + +import ( + "image/color" + "machine" + + axp192 "tinygo.org/x/drivers/axp192/m5stack-core2-axp192" + "tinygo.org/x/drivers/i2csoft" + "tinygo.org/x/drivers/ili9341" +) + +// InitDisplay initializes the display of each board. +func InitDisplay() *ili9341.Device { + machine.SPI2.Configure(machine.SPIConfig{ + SCK: machine.LCD_SCK_PIN, + SDO: machine.LCD_SDO_PIN, + SDI: machine.LCD_SDI_PIN, + Frequency: 40e6, + }) + + i2c := i2csoft.New(machine.SCL0_PIN, machine.SDA0_PIN) + i2c.Configure(i2csoft.I2CConfig{Frequency: 100e3}) + + axp := axp192.New(i2c) + led := axp.LED + led.Low() + + display := ili9341.NewSPI( + machine.SPI2, + machine.LCD_DC_PIN, + machine.LCD_SS_PIN, + machine.NoPin, + ) + + // configure display + display.Configure(ili9341.Config{ + Width: 320, + Height: 240, + DisplayInversion: true, + }) + display.FillScreen(color.RGBA{255, 255, 255, 255}) + + display.SetRotation(ili9341.Rotation0Mirror) + + return display +} diff --git a/ili9341/ili9341.go b/ili9341/ili9341.go index 5ea56a2..3941ee0 100644 --- a/ili9341/ili9341.go +++ b/ili9341/ili9341.go @@ -8,9 +8,10 @@ import ( ) type Config struct { - Width int16 - Height int16 - Rotation Rotation + Width int16 + Height int16 + Rotation Rotation + DisplayInversion bool } type Device struct { @@ -103,10 +104,19 @@ func (d *Device) Configure(config Config) { 0x4E, 0xF1, 0x37, 0x07, 0x10, 0x03, 0x0E, 0x09, 0x00, GMCTRN1, 15, 0x00, 0x0E, 0x14, 0x03, 0x11, 0x07, // Set Gamma 0x31, 0xC1, 0x48, 0x08, 0x0F, 0x0C, 0x31, 0x36, 0x0F, + } + + if config.DisplayInversion { + initCmd = append(initCmd, []byte{ + INVON, 0x80, + }...) + } + + initCmd = append(initCmd, []byte{ SLPOUT, 0x80, // Exit Sleep DISPON, 0x80, // Display on 0x00, // End of list - } + }...) for i, c := 0, len(initCmd); i < c; { cmd := initCmd[i] if cmd == 0x00 { @@ -239,14 +249,22 @@ func (d *Device) GetRotation() Rotation { // SetRotation changes the rotation of the device (clock-wise) func (d *Device) SetRotation(rotation Rotation) { madctl := uint8(0) - switch rotation % 4 { - case 0: + switch rotation % 8 { + case Rotation0: madctl = MADCTL_MX | MADCTL_BGR - case 1: + case Rotation90: madctl = MADCTL_MV | MADCTL_BGR - case 2: + case Rotation180: madctl = MADCTL_MY | MADCTL_BGR - case 3: + case Rotation270: + madctl = MADCTL_MX | MADCTL_MY | MADCTL_MV | MADCTL_BGR + case Rotation0Mirror: + madctl = MADCTL_BGR + case Rotation90Mirror: + madctl = MADCTL_MY | MADCTL_MV | MADCTL_BGR + case Rotation180Mirror: + madctl = MADCTL_MX | MADCTL_MY | MADCTL_BGR + case Rotation270Mirror: madctl = MADCTL_MX | MADCTL_MY | MADCTL_MV | MADCTL_BGR } d.sendCommand(MADCTL, []uint8{madctl}) diff --git a/ili9341/registers.go b/ili9341/registers.go index d458ee9..d4ffe4e 100644 --- a/ili9341/registers.go +++ b/ili9341/registers.go @@ -81,4 +81,9 @@ const ( Rotation90 Rotation = 1 // 90 degrees clock-wise rotation Rotation180 Rotation = 2 Rotation270 Rotation = 3 + + Rotation0Mirror Rotation = 4 + Rotation90Mirror Rotation = 5 + Rotation180Mirror Rotation = 6 + Rotation270Mirror Rotation = 7 )