GoLand2021
, 所以按照官网的配置GoLand2021
里File -> Settings -> Plugins -> Marketplace -> 输入 tinygo -> 点击安装即可
)File -> Settings -> Go -> TinyGo
这里选择自己对应的开发板, 我这里选择pico
GoLand2021
, 再次打开之后就会有针对pico
的代码提示TinyGo插件
GoLand2021
中可以查看对pico
有这样的代码定义
// I2C Default pins on Raspberry Pico.
const (
I2C0_SDA_PIN = GP4
I2C0_SCL_PIN = GP5
I2C1_SDA_PIN = GP2
I2C1_SCL_PIN = GP3
)
128x32
mkdir display_demo && cd display_demo
go mod init display_demo
main.go
文件 代码参考https://github.com/tinygo-org/drivers/blob/release/examples/ssd1306/i2c_128x32/main.gopackage main
import (
"machine"
"image/color"
"time"
"tinygo.org/x/drivers/ssd1306"
)
func main() {
machine.I2C0.Configure(machine.I2CConfig{
Frequency: machine.TWI_FREQ_400KHZ,
SDA: machine.I2C0_SDA_PIN,
SCL: machine.I2C0_SCL_PIN,
})
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)
}
}
树莓派Pico
开发板上的BOOTSEL
按钮,然后将树莓派Pico
插入到电脑, 树莓派的指示灯亮代表通电cmd
执行以下命令# 更新依赖
go mod tidy
# 烧录代码
tinygo flash -target=pico
TinyGo
提供有一个文字库https://github.com/tinygo-org/tinyfont, 但尝试了很久没生成成功, 所以最后还是用最原始的办法(取模工具然后画点)main.go
代码替换成下面的package main
import (
"image/color"
"machine"
"time"
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/ssd1306"
)
func main() {
machine.I2C0.Configure(machine.I2CConfig{
Frequency: machine.TWI_FREQ_400KHZ,
SDA: machine.I2C0_SDA_PIN,
SCL: machine.I2C0_SCL_PIN,
})
display := ssd1306.NewI2C(machine.I2C0)
display.Configure(ssd1306.Config{
Address: ssd1306.Address_128_32,
Width: 128,
Height: 32,
})
display.ClearDisplay()
x, y := 25, 10
for _, word := range wordData {
displayText(&display, x, y, word)
display.Display()
// 每写一个字就 x 偏移 16 个点
x += 16
time.Sleep(time.Second)
}
time.Sleep(time.Hour)
}
var wordData = [][][]byte{
{
{0x08, 0x80, 0x08, 0x80, 0x08, 0x80, 0x11, 0xFE, 0x11, 0x02, 0x32, 0x04, 0x34, 0x20, 0x50, 0x20},
{0x91, 0x28, 0x11, 0x24, 0x12, 0x24, 0x12, 0x22, 0x14, 0x22, 0x10, 0x20, 0x10, 0xA0, 0x10, 0x40}, /*"你",0*/
},
{
{0x10, 0x00, 0x10, 0xFC, 0x10, 0x04, 0x10, 0x08, 0xFC, 0x10, 0x24, 0x20, 0x24, 0x20, 0x25, 0xFE},
{0x24, 0x20, 0x48, 0x20, 0x28, 0x20, 0x10, 0x20, 0x28, 0x20, 0x44, 0x20, 0x84, 0xA0, 0x00, 0x40}, /*"好",0*/
},
{
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x20, 0x20, 0x40}, /*",",0*/
},
{
{0x02, 0x20, 0x12, 0x20, 0x12, 0x20, 0x12, 0x20, 0x12, 0x20, 0xFF, 0xFE, 0x12, 0x20, 0x12, 0x20},
{0x12, 0x20, 0x12, 0x20, 0x13, 0xE0, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x1F, 0xFC, 0x00, 0x00}, /*"世",0*/
},
{
{0x00, 0x00, 0x1F, 0xF0, 0x11, 0x10, 0x11, 0x10, 0x1F, 0xF0, 0x11, 0x10, 0x11, 0x10, 0x1F, 0xF0},
{0x02, 0x80, 0x0C, 0x60, 0x34, 0x58, 0xC4, 0x46, 0x04, 0x40, 0x08, 0x40, 0x08, 0x40, 0x10, 0x40}, /*"界",0*/
},
{
{0x00, 0x00, 0x00, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00}, /*"!",0*/
},
}
var Key = [8]byte{
0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01,
}
func displayText(display drivers.Displayer, x, y int, wordData [][]byte) {
for _, line := range wordData {
// 每次读取两个作为一行
for i := 0; i < len(line); i += 2 {
for j := 0; j < 2; j++ {
val := line[i+j]
for k := byte(0); k < 8; k++ {
if val&Key[k] == Key[k] {
pointX := int16(x + (j * 8) + int(k))
pointY := int16(y)
display.SetPixel(pointX, pointY, color.RGBA{
R: 255,
G: 255,
B: 255,
A: 255,
})
}
}
}
y++
}
}
}
树莓派Pico
开发板上的BOOTSEL
按钮,然后将树莓派Pico
插入到电脑cmd
执行以下命令# 更新依赖
go mod tidy
# 烧录代码
tinygo flash -target=pico