From ff4d15cea98467be3e190489593c562ce1e010fe Mon Sep 17 00:00:00 2001 From: soypat Date: Wed, 2 Jul 2025 10:44:06 -0300 Subject: [PATCH 1/5] this is what I meant --- examples/ssd1306/i2c_128x32/main.go | 51 ------------------- examples/ssd1306/i2c_128x64/main.go | 60 ----------------------- examples/ssd1306/{spi_128x64 => }/main.go | 26 +++++----- examples/ssd1306/main_i2c.go | 29 +++++++++++ examples/ssd1306/main_spi_thumby.go | 22 +++++++++ examples/ssd1306/spi_thumby/main.go | 50 ------------------- 6 files changed, 63 insertions(+), 175 deletions(-) delete mode 100644 examples/ssd1306/i2c_128x32/main.go delete mode 100644 examples/ssd1306/i2c_128x64/main.go rename examples/ssd1306/{spi_128x64 => }/main.go (59%) create mode 100644 examples/ssd1306/main_i2c.go create mode 100644 examples/ssd1306/main_spi_thumby.go delete mode 100644 examples/ssd1306/spi_thumby/main.go diff --git a/examples/ssd1306/i2c_128x32/main.go b/examples/ssd1306/i2c_128x32/main.go deleted file mode 100644 index 8baf37d75..000000000 --- a/examples/ssd1306/i2c_128x32/main.go +++ /dev/null @@ -1,51 +0,0 @@ -package main - -import ( - "machine" - - "image/color" - "time" - - "tinygo.org/x/drivers/ssd1306" -) - -func main() { - machine.I2C0.Configure(machine.I2CConfig{ - Frequency: machine.TWI_FREQ_400KHZ, - }) - - display := ssd1306.NewI2C(machine.I2C0) - display.Configure(ssd1306.Config{ - Address: ssd1306.Address_128_32, - Width: 128, - Height: 32, - }) - - display.ClearDisplay() - - x := int16(0) - y := int16(0) - deltaX := int16(1) - deltaY := int16(1) - for { - pixel := display.GetPixel(x, y) - c := color.RGBA{255, 255, 255, 255} - if pixel { - c = color.RGBA{0, 0, 0, 255} - } - display.SetPixel(x, y, c) - display.Display() - - x += deltaX - y += deltaY - - if x == 0 || x == 127 { - deltaX = -deltaX - } - - if y == 0 || y == 31 { - deltaY = -deltaY - } - time.Sleep(1 * time.Millisecond) - } -} diff --git a/examples/ssd1306/i2c_128x64/main.go b/examples/ssd1306/i2c_128x64/main.go deleted file mode 100644 index a17010def..000000000 --- a/examples/ssd1306/i2c_128x64/main.go +++ /dev/null @@ -1,60 +0,0 @@ -// This example shows how to use 128x64 display over I2C -// Tested on Seeeduino XIAO Expansion Board https://wiki.seeedstudio.com/Seeeduino-XIAO-Expansion-Board/ -// -// According to manual, I2C address of the display is 0x78, but that's 8-bit address. -// TinyGo operates on 7-bit addresses and respective 7-bit address would be 0x3C, which we use below. -// -// To learn more about different types of I2C addresses, please see following page -// https://www.totalphase.com/support/articles/200349176-7-bit-8-bit-and-10-bit-I2C-Slave-Addressing - -package main - -import ( - "machine" - - "image/color" - "time" - - "tinygo.org/x/drivers/ssd1306" -) - -func main() { - machine.I2C0.Configure(machine.I2CConfig{ - Frequency: machine.TWI_FREQ_400KHZ, - }) - - display := ssd1306.NewI2C(machine.I2C0) - display.Configure(ssd1306.Config{ - Address: 0x3C, - Width: 128, - Height: 64, - }) - - display.ClearDisplay() - - x := int16(0) - y := int16(0) - deltaX := int16(1) - deltaY := int16(1) - for { - pixel := display.GetPixel(x, y) - c := color.RGBA{255, 255, 255, 255} - if pixel { - c = color.RGBA{0, 0, 0, 255} - } - display.SetPixel(x, y, c) - display.Display() - - x += deltaX - y += deltaY - - if x == 0 || x == 127 { - deltaX = -deltaX - } - - if y == 0 || y == 63 { - deltaY = -deltaY - } - time.Sleep(1 * time.Millisecond) - } -} diff --git a/examples/ssd1306/spi_128x64/main.go b/examples/ssd1306/main.go similarity index 59% rename from examples/ssd1306/spi_128x64/main.go rename to examples/ssd1306/main.go index 094f5cab6..e4fac3af6 100644 --- a/examples/ssd1306/spi_128x64/main.go +++ b/examples/ssd1306/main.go @@ -2,26 +2,24 @@ package main import ( "image/color" - "machine" "time" "tinygo.org/x/drivers/ssd1306" ) func main() { - machine.SPI0.Configure(machine.SPIConfig{ - Frequency: 8000000, - }) - display := ssd1306.NewSPI(machine.SPI0, machine.P8, machine.P7, machine.P9) - display.Configure(ssd1306.Config{ - Width: 128, - Height: 64, - }) - + const height = 32 + const width = 128 + var display *ssd1306.Device + var err error + display, err = makeSSD1306(width, height) + if err != nil { + panic(err) + } display.ClearDisplay() - x := int16(64) - y := int16(32) + x := int16(36) + y := int16(20) deltaX := int16(1) deltaY := int16(1) for { @@ -36,11 +34,11 @@ func main() { x += deltaX y += deltaY - if x == 0 || x == 127 { + if x == 0 || x == width-1 { deltaX = -deltaX } - if y == 0 || y == 63 { + if y == 0 || y == height-1 { deltaY = -deltaY } time.Sleep(1 * time.Millisecond) diff --git a/examples/ssd1306/main_i2c.go b/examples/ssd1306/main_i2c.go new file mode 100644 index 000000000..5c4c5d667 --- /dev/null +++ b/examples/ssd1306/main_i2c.go @@ -0,0 +1,29 @@ +//go:build !thumby + +package main + +import ( + "machine" + + "tinygo.org/x/drivers/ssd1306" +) + +func makeSSD1306(width, height int16) (*ssd1306.Device, error) { + err := machine.I2C0.Configure(machine.I2CConfig{ + Frequency: 400 * machine.KHz, + }) + if err != nil { + return nil, err + } + address := uint16(ssd1306.Address) + if width == 128 && (height == 32 || height == 64) { + address = ssd1306.Address_128_32 + } + display := ssd1306.NewI2C(machine.I2C0) + display.Configure(ssd1306.Config{ + Address: address, + Width: width, + Height: height, + }) + return &display, nil +} diff --git a/examples/ssd1306/main_spi_thumby.go b/examples/ssd1306/main_spi_thumby.go new file mode 100644 index 000000000..6a7a680a3 --- /dev/null +++ b/examples/ssd1306/main_spi_thumby.go @@ -0,0 +1,22 @@ +//go:build thumby + +package main + +import ( + "machine" + + "tinygo.org/x/drivers/ssd1306" +) + +func makeSSD1306(_, _ int16, address uint16) (*ssd1306.Device, error) { + // width and height are known for thumby. + machine.SPI0.Configure(machine.SPIConfig{}) + display := ssd1306.NewSPI(machine.SPI0, machine.THUMBY_DC_PIN, machine.THUMBY_RESET_PIN, machine.THUMBY_CS_PIN) + display.Configure(ssd1306.Config{ + Width: 72, + Height: 40, + ResetCol: ssd1306.ResetValue{28, 99}, + ResetPage: ssd1306.ResetValue{0, 5}, + }) + return &display, nil +} diff --git a/examples/ssd1306/spi_thumby/main.go b/examples/ssd1306/spi_thumby/main.go deleted file mode 100644 index b2a41e503..000000000 --- a/examples/ssd1306/spi_thumby/main.go +++ /dev/null @@ -1,50 +0,0 @@ -// This example using the SSD1306 OLED display over SPI on the Thumby board -// A very tiny 72x40 display. -package main - -import ( - "image/color" - "machine" - "time" - - "tinygo.org/x/drivers/ssd1306" -) - -func main() { - machine.SPI0.Configure(machine.SPIConfig{}) - display := ssd1306.NewSPI(machine.SPI0, machine.THUMBY_DC_PIN, machine.THUMBY_RESET_PIN, machine.THUMBY_CS_PIN) - display.Configure(ssd1306.Config{ - Width: 72, - Height: 40, - ResetCol: ssd1306.ResetValue{28, 99}, - ResetPage: ssd1306.ResetValue{0, 5}, - }) - - display.ClearDisplay() - - x := int16(36) - y := int16(20) - deltaX := int16(1) - deltaY := int16(1) - for { - pixel := display.GetPixel(x, y) - c := color.RGBA{255, 255, 255, 255} - if pixel { - c = color.RGBA{0, 0, 0, 255} - } - display.SetPixel(x, y, c) - display.Display() - - x += deltaX - y += deltaY - - if x == 0 || x == 71 { - deltaX = -deltaX - } - - if y == 0 || y == 39 { - deltaY = -deltaY - } - time.Sleep(1 * time.Millisecond) - } -} From 589bf19b0117c697893ad3f801505195a7dc34c5 Mon Sep 17 00:00:00 2001 From: soypat Date: Wed, 2 Jul 2025 10:51:29 -0300 Subject: [PATCH 2/5] fix bug --- examples/ssd1306/main.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/examples/ssd1306/main.go b/examples/ssd1306/main.go index e4fac3af6..1619c309c 100644 --- a/examples/ssd1306/main.go +++ b/examples/ssd1306/main.go @@ -8,18 +8,21 @@ import ( ) func main() { - const height = 32 - const width = 128 + // Thumby will have preset size. + // If not compiling for thumby the width and height will be whatever we suggest + const suggestHeight = 32 + const suggestWidth = 128 var display *ssd1306.Device var err error - display, err = makeSSD1306(width, height) + display, err = makeSSD1306(suggestWidth, suggestHeight) if err != nil { panic(err) } display.ClearDisplay() - x := int16(36) - y := int16(20) + width, height := display.Size() + x := int16(width) + y := int16(height) deltaX := int16(1) deltaY := int16(1) for { From e2868616613cd2cbaa61db425bb0aeedbb5b26e8 Mon Sep 17 00:00:00 2001 From: soypat Date: Wed, 2 Jul 2025 11:05:07 -0300 Subject: [PATCH 3/5] fix smoke tests --- smoketest.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/smoketest.sh b/smoketest.sh index f21e2be55..d89235700 100755 --- a/smoketest.sh +++ b/smoketest.sh @@ -65,8 +65,7 @@ tinygo build -size short -o ./build/test.hex -target=pybadge ./examples/shifter/ tinygo build -size short -o ./build/test.hex -target=microbit ./examples/sht3x/main.go tinygo build -size short -o ./build/test.hex -target=microbit ./examples/sht4x/main.go tinygo build -size short -o ./build/test.hex -target=microbit ./examples/shtc3/main.go -tinygo build -size short -o ./build/test.hex -target=microbit ./examples/ssd1306/i2c_128x32/main.go -tinygo build -size short -o ./build/test.hex -target=microbit ./examples/ssd1306/spi_128x64/main.go +tinygo build -size short -o ./build/test.hex -target=microbit ./examples/ssd1306/ tinygo build -size short -o ./build/test.hex -target=microbit ./examples/ssd1331/main.go tinygo build -size short -o ./build/test.hex -target=microbit ./examples/st7735/main.go tinygo build -size short -o ./build/test.hex -target=microbit ./examples/st7789/main.go From 1e4110b5a2ebde14080fd2b7cb5b8d861bd0b745 Mon Sep 17 00:00:00 2001 From: soypat Date: Wed, 2 Jul 2025 11:05:47 -0300 Subject: [PATCH 4/5] add thumby case to smoke tests --- smoketest.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/smoketest.sh b/smoketest.sh index d89235700..55cf4b027 100755 --- a/smoketest.sh +++ b/smoketest.sh @@ -66,6 +66,7 @@ tinygo build -size short -o ./build/test.hex -target=microbit ./examples/sht3x/m tinygo build -size short -o ./build/test.hex -target=microbit ./examples/sht4x/main.go tinygo build -size short -o ./build/test.hex -target=microbit ./examples/shtc3/main.go tinygo build -size short -o ./build/test.hex -target=microbit ./examples/ssd1306/ +tinygo build -size short -o ./build/test.hex -target=thumby ./examples/ssd1306/ tinygo build -size short -o ./build/test.hex -target=microbit ./examples/ssd1331/main.go tinygo build -size short -o ./build/test.hex -target=microbit ./examples/st7735/main.go tinygo build -size short -o ./build/test.hex -target=microbit ./examples/st7789/main.go From 5c305f9b6f1f74d871c08eefcf973a3ce4975a40 Mon Sep 17 00:00:00 2001 From: soypat Date: Wed, 2 Jul 2025 11:16:40 -0300 Subject: [PATCH 5/5] ssd1306: fix thumby function arguments --- examples/ssd1306/main_spi_thumby.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/ssd1306/main_spi_thumby.go b/examples/ssd1306/main_spi_thumby.go index 6a7a680a3..86f0d2647 100644 --- a/examples/ssd1306/main_spi_thumby.go +++ b/examples/ssd1306/main_spi_thumby.go @@ -8,7 +8,7 @@ import ( "tinygo.org/x/drivers/ssd1306" ) -func makeSSD1306(_, _ int16, address uint16) (*ssd1306.Device, error) { +func makeSSD1306(_, _ int16) (*ssd1306.Device, error) { // width and height are known for thumby. machine.SPI0.Configure(machine.SPIConfig{}) display := ssd1306.NewSPI(machine.SPI0, machine.THUMBY_DC_PIN, machine.THUMBY_RESET_PIN, machine.THUMBY_CS_PIN)