working i²c example, everything tested

This commit is contained in:
Daniel Esteban 2026-03-29 19:58:08 +02:00
parent 56cdda74f1
commit 814cb785f4
4 changed files with 131 additions and 1 deletions

6
demo/colors.go Normal file

File diff suppressed because one or more lines are too long

View file

@ -1,15 +1,36 @@
package main
import (
"image"
"machine"
"time"
"image/color"
draw2 "golang.org/x/image/draw"
"tinygo.org/x/drivers/amg88xx"
"tinygo.org/x/drivers/encoders"
"tinygo.org/x/drivers/st7789"
"tinygo.org/x/drivers/tone"
"tinygo.org/x/drivers/ws2812"
)
const AMG88XX_SIZE = 8
const AMG88XX_PIXELS = 24
var data [AMG88XX_SIZE * AMG88XX_SIZE]int16
var pixels [AMG88XX_PIXELS * AMG88XX_PIXELS]int16
var adj [16]int16
func main() {
time.Sleep(2 * time.Second)
i2c := machine.I2C1
i2c.Configure(machine.I2CConfig{
Frequency: 2.0 * machine.MHz,
SDA: machine.P0_17,
SCL: machine.P0_20,
})
machine.SPI0.Configure(machine.SPIConfig{
SCK: machine.P1_01,
@ -36,19 +57,47 @@ func main() {
width, height := display.Size()
width = 240
height = 135
println(width, height)
white := color.RGBA{255, 255, 255, 255}
red := color.RGBA{255, 0, 0, 255}
blue := color.RGBA{0, 0, 255, 255}
green := color.RGBA{0, 255, 0, 255}
black := color.RGBA{0, 0, 0, 255}
wsPin := machine.P1_11
wsPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
ws := ws2812.NewWS2812(wsPin)
wsLeds := [2]color.RGBA{}
for i := range wsLeds {
wsLeds[i] = black
}
machine.InitADC()
ax := machine.ADC{Pin: machine.P0_29}
ay := machine.ADC{Pin: machine.P0_02}
ax.Configure(machine.ADCConfig{})
ay.Configure(machine.ADCConfig{})
bzrPin := machine.P0_31
pwm := machine.PWM0
speaker, _ := tone.New(pwm, bzrPin)
enc := encoders.NewQuadratureViaInterrupt(machine.P1_00, machine.P0_24)
enc.Configure(encoders.QuadratureConfig{
Precision: 4,
})
buttons := []machine.Pin{machine.P0_22, machine.P1_06, machine.P1_04}
for c := range buttons {
buttons[c].Configure(machine.PinConfig{Mode: machine.PinInputPullup})
}
println("STARTING CAMERA")
camera := amg88xx.New(machine.I2C1)
camera.Configure(amg88xx.Config{})
println("CAMERA STARTED")
var value int16
display.FillScreen(black)
display.FillRectangle(0, 0, width/2, height/2, white)
@ -57,8 +106,65 @@ func main() {
display.FillRectangle(width/2, height/2, width/2, height/2, blue)
display.FillRectangle(width/4, height/4, width/2, height/2, black)
redgreen := true
f := 100
rotaryOldValue := 0
for {
println(ax.Get(), ay.Get())
if redgreen {
wsLeds[0] = red
wsLeds[1] = green
} else {
wsLeds[0] = green
wsLeds[1] = red
}
redgreen = !redgreen
ws.WriteColors(wsLeds[:])
f++
if f >= 100 {
f = 0
speaker.SetNote(tone.C5)
time.Sleep(100 * time.Millisecond)
speaker.Stop()
}
if newValue := enc.Position(); newValue != rotaryOldValue {
println("value: ", newValue)
rotaryOldValue = newValue
}
println("ROTARY BTN:", !buttons[0].Get(), "UP:", !buttons[1].Get(), "DOWN:", !buttons[2].Get())
// get the values of the sensor in millicelsius
camera.ReadPixels(&data)
srcImage := image.NewRGBA(image.Rect(0, 0, 8, 8))
for j := 0; j < AMG88XX_SIZE; j++ {
for i := 0; i < AMG88XX_SIZE; i++ {
value = data[63-(i+j*AMG88XX_SIZE)]
// treat anything below 18°C as 18°C
if value < 18000 {
value = 0
} else {
value = (value - 18000) / 36
// our color array only have 433 values, avoid getting a value that doesn't exist
if value > 432 {
value = 432
}
}
srcImage.Set(i, j, colors[value])
}
}
dst := image.NewRGBA(image.Rect(0, 0, 24, 24))
draw2.BiLinear.Scale(dst, image.Rect(0, 0, 24, 24), srcImage, image.Rect(0, 0, 8, 8), draw2.Over, nil)
for j := 0; j < AMG88XX_PIXELS; j++ {
for i := 0; i < AMG88XX_PIXELS; i++ {
display.FillRectangle(int16(60+(AMG88XX_PIXELS-i-1)*5), 7+int16(j*5), 5, 5, dst.RGBAAt(i, j))
}
}
time.Sleep(100 * time.Millisecond)
}
}

12
go.mod Normal file
View file

@ -0,0 +1,12 @@
module github.com/conejoninja/nicebadge
go 1.25.0
require tinygo.org/x/drivers v0.34.0
replace tinygo.org/x/drivers => /home/conejo/go/src/tinygo.org/x/drivers
require (
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
golang.org/x/image v0.38.0 // indirect
)

6
go.sum Normal file
View file

@ -0,0 +1,6 @@
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
golang.org/x/image v0.38.0 h1:5l+q+Y9JDC7mBOMjo4/aPhMDcxEptsX+Tt3GgRQRPuE=
golang.org/x/image v0.38.0/go.mod h1:/3f6vaXC+6CEanU4KJxbcUZyEePbyKbaLoDOe4ehFYY=
tinygo.org/x/drivers v0.34.0 h1:lw8ePJeUSn9oICKBvQXHC9TIE+J00OfXfkGTrpXM9Iw=
tinygo.org/x/drivers v0.34.0/go.mod h1:ZdErNrApSABdVXjA1RejD67R8SNRI6RKVfYgQDZtKtk=