學 Go 玩創客,TinyGo 初體驗(4):使用 BBC micro:bit 與 TinyGo drivers 驅動周邊元件,Part II

Photo by Clark Tibbs on Unsplash

BH1750 亮度計

BH1750 是 I2C 介面的亮度計,能傳回環境的照度(illuminance,單位為 lux 或 lx)。市面上最容易找的版本是 GY-30 和 GY-302 兩種,外型和腳位有點差異,不過用起來是一模一樣的。

  1. Gnd -> G
  2. SCL -> P19
  3. SDA -> P20

不使用 TinyGo drivers

package mainimport (
"machine"
"time"
)
const ( // device address and some registers
ADDRESS = 0x23
POWER_ON = 0x01
CONTINUOUS_HIGH_RES_MODE = 0x10
)
func main() { i2c := machine.I2C0
i2c.Configure(machine.I2CConfig{})
i2c.WriteRegister(ADDRESS, POWER_ON, nil) // power on
buf := make([]byte, 2) // buffer to receive data (2 bytes) for {
// read raw data
i2c.ReadRegister(ADDRESS, CONTINUOUS_HIGH_RES_MODE, buf)
// calculate illuminance
lux := ((int32(buf[0]) << 24) | int32(buf[1])<<16) / 78642
println("Illuminance:", lux, "lx")
time.Sleep(time.Millisecond * 200)
}
}
Photo by Kaitlin Duffey on Unsplash

使用 TinyGo drivers

package mainimport (
"machine"
"time"
"tinygo.org/x/drivers/bh1750"
)
func main() { machine.I2C0.Configure(machine.I2CConfig{})
sensor := bh1750.New(machine.I2C0)
sensor.Configure()
for { lux := sensor.Illuminance() / 1000
println("Illuminance:", lux, "lx")
time.Sleep(time.Millisecond * 200)
}}

在 SSD1306 OLED 顯示文字

SSD1306 是個常見的小型 OLED 單色顯示模組,常見規格有 0.96 吋 128x64 或 0.91 吋 128x32 像素兩種。有些所謂的雙色模組是有上下兩塊不同顏色而已。此外記得買只有 4 根針腳的,多於這個數目就有可能是使用 SPI 通訊的版本。

Photo by Victória Kubiaki on Unsplash

下載 TinyFont 字庫

TinyFont 是 TinyGo 團隊開發的另一個東西,也就是多種顯示器元件可用的字庫。其下載方式和 TinyGo drivers 一樣:

go get tinygo.org/x/tinyfonts
package mainimport (
"image/color"
"machine"
"tinygo.org/x/drivers/ssd1306"
"tinygo.org/x/tinyfont"
"tinygo.org/x/tinyfont/freesans"
)
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: 64,
})
c := color.RGBA{0xff, 0xff, 0xff, 0xff} display.ClearDisplay()
tinyfont.WriteLine(&display, &freesans.Regular12pt7b, 20, 40, []byte("TinyGo"), c)
display.Display()
}
Photo by Calum MacAulay on Unsplash
  1. 寫入的字型名稱範例:

迷你專題:溫度與氣壓顯示器

Photo by yue su on Unsplash
package mainimport (
"image/color"
"machine"
"strconv"
"time"
"tinygo.org/x/drivers/bmp180"
"tinygo.org/x/drivers/ssd1306"
"tinygo.org/x/tinyfont"
"tinygo.org/x/tinyfont/freeserif"
)
func main() { machine.I2C0.Configure(machine.I2CConfig{
Frequency: machine.TWI_FREQ_400KHZ,
})
sensor := bmp180.New(machine.I2C0)
sensor.Configure()
connected := sensor.Connected()
if !connected {
println("BMP180 not detected")
return
}
println("BMP180 detected")
display := ssd1306.NewI2C(machine.I2C0)
display.Configure(ssd1306.Config{
Address: ssd1306.Address_128_32,
Width: 128,
Height: 64,
})
c := color.RGBA{0xff, 0xff, 0xff, 0xff}
display.ClearDisplay()
for { temp, _ := sensor.ReadTemperature()
pressure, _ := sensor.ReadPressure()
println("Temperature:", temp, " / Pressure:", pressure)
temp_str := strconv.FormatFloat(float64(temp)/1000, 'f', 1, 64) + " *C"
pressure_str := strconv.FormatFloat(float64(pressure)/100000, 'f', 2, 64) + " hPa"
display.ClearBuffer()
tinyfont.WriteLine(&display, &freeserif.Bold12pt7b, 0, 24, []byte(temp_str), c)
tinyfont.WriteLine(&display, &freeserif.Bold12pt7b, 0, 48, []byte(pressure_str), c)
display.Display()
time.Sleep(time.Millisecond * 100)
}
}

專題2:在 LCD1602 顯示 micro:bit 的旋轉角度、羅盤方位與溫度

Photo by Denise Jans on Unsplash
package mainimport (
"machine"
"strconv"
"time"
"tinygo.org/x/drivers/hd44780i2c"
"tinygo.org/x/drivers/lsm303agr"
)
func main() { machine.I2C0.Configure(machine.I2CConfig{
Frequency: machine.TWI_FREQ_400KHZ,
})
accel_mag := lsm303agr.New(machine.I2C0)
accel_mag.Configure(lsm303agr.Configuration{})
lcd := hd44780i2c.New(machine.I2C0, 0x27)
lcd.Configure(hd44780i2c.Config{Width: 16, Height: 2})
for { lcd.ClearDisplay() pitch, roll := accel_mag.ReadPitchRoll()
heading := accel_mag.ReadCompass()
temp, _ := accel_mag.ReadTemperature()
lcd.SetCursor(0, 0)
lcd.Print([]byte("Pit:"))
lcd.SetCursor(4, 0)
lcd.Print([]byte(toStr(float64(pitch/1000000), 0)))
lcd.SetCursor(8, 0)
lcd.Print([]byte("Rol:"))
lcd.SetCursor(12, 0)
lcd.Print([]byte(toStr(float64(roll/1000000), 0)))
lcd.SetCursor(0, 1)
lcd.Print([]byte("Dir:"))
lcd.SetCursor(4, 1)
lcd.Print([]byte(toStr(float64(heading/1000000), 0)))
lcd.SetCursor(8, 1)
lcd.Print([]byte("Tmp:"))
lcd.SetCursor(12, 1)
lcd.Print([]byte(toStr(float64(temp/1000), 0)))
time.Sleep(time.Millisecond * 200)
}
}func toStr(f float64, d int) string {
return strconv.FormatFloat(f, 'f', d, 64)
}

結語

這兩篇我們看了如何使用 TinyGo drivers 的幾種驅動程式及 TinyFont 字庫,並搭配 BBC micro:bit 來實作。(其實不只是上面這兩個套件存在,還有一個叫做 TinyDraw 的簡單繪圖庫,不過筆者在此沒有著墨就是。)

Photo by Jared Erondu on Unsplash

Former translator, after-hours Maker, sunny-day analog film shooter. Currently a junior tech-book editor based in Taiwan. https://krantasblog.blogspot.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store