hybridgroup.gobot/examples/firmata_tm1638.go

95 lines
2.0 KiB
Go
Raw Permalink Normal View History

//go:build example
2018-02-12 02:34:47 +08:00
// +build example
2018-02-12 02:34:47 +08:00
//
// Do not build by default.
/*
How to run
Pass serial port to use as the first param:
go run examples/firmata_tm1638.go /dev/ttyACM0
*/
package main
import (
"fmt"
2018-02-12 02:34:47 +08:00
"os"
"time"
"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/drivers/gpio"
"gobot.io/x/gobot/v2/platforms/firmata"
2018-02-12 02:34:47 +08:00
)
func main() {
firmataAdaptor := firmata.NewAdaptor(os.Args[1])
// Even thought the modules are connected among them, they are independent (not chained)
modules := make([]*gpio.TM1638Driver, 4)
2022-09-25 22:26:12 +08:00
modules[0] = gpio.NewTM1638Driver(firmataAdaptor, "9", "8", "7")
modules[1] = gpio.NewTM1638Driver(firmataAdaptor, "9", "8", "6")
modules[2] = gpio.NewTM1638Driver(firmataAdaptor, "9", "8", "5")
modules[3] = gpio.NewTM1638Driver(firmataAdaptor, "9", "8", "4")
2018-02-12 02:34:47 +08:00
var ledInt byte
var color byte
var offset int
// Repeat and concat strings until long enough that with scroll it still shows text
const showText = " HELLO WORLD - gobot.io - TM1638 "
text := showText
2018-02-12 02:45:28 +08:00
for len(text)-len(showText) < len(modules)*8 {
2018-02-12 02:34:47 +08:00
text += showText
}
work := func() {
gobot.Every(400*time.Millisecond, func() {
// Enable and change the color of the LEDs
if err := modules[0].SetLED(color, ledInt); err != nil {
fmt.Println(err)
}
if err := modules[1].SetLED(color, ledInt); err != nil {
fmt.Println(err)
}
if err := modules[2].SetLED(color, ledInt); err != nil {
fmt.Println(err)
}
if err := modules[3].SetLED(color, ledInt); err != nil {
fmt.Println(err)
}
2018-02-12 02:34:47 +08:00
ledInt++
if ledInt > 7 {
ledInt = 0
color++
if color > 2 {
color = 0
}
}
// Scroll the text
for i := 0; i < 4; i++ {
if err := modules[i].SetDisplayText(text[offset+8*i : offset+8*i+8]); err != nil {
fmt.Println(err)
}
2018-02-12 02:34:47 +08:00
}
offset++
if offset >= len(showText) {
offset = 0
}
})
}
robot := gobot.NewRobot("tm1638Bot",
[]gobot.Connection{firmataAdaptor},
[]gobot.Device{modules[0], modules[1], modules[2], modules[3]},
work,
)
if err := robot.Start(); err != nil {
panic(err)
}
2018-02-12 02:34:47 +08:00
}