gobot.io/x/gobot/v2@v2.1.0/examples/firmata_tm1638.go (about)

     1  //go:build example
     2  // +build example
     3  
     4  //
     5  // Do not build by default.
     6  
     7  /*
     8   How to run
     9   Pass serial port to use as the first param:
    10  
    11  	go run examples/firmata_tm1638.go /dev/ttyACM0
    12  */
    13  
    14  package main
    15  
    16  import (
    17  	"os"
    18  	"time"
    19  
    20  	"gobot.io/x/gobot/v2"
    21  	"gobot.io/x/gobot/v2/drivers/gpio"
    22  	"gobot.io/x/gobot/v2/platforms/firmata"
    23  )
    24  
    25  func main() {
    26  	firmataAdaptor := firmata.NewAdaptor(os.Args[1])
    27  
    28  	// Even thought the modules are connected among them, they are independent (not chained)
    29  	modules := make([]*gpio.TM1638Driver, 4)
    30  	modules[0] = gpio.NewTM1638Driver(firmataAdaptor, "9", "8", "7")
    31  	modules[1] = gpio.NewTM1638Driver(firmataAdaptor, "9", "8", "6")
    32  	modules[2] = gpio.NewTM1638Driver(firmataAdaptor, "9", "8", "5")
    33  	modules[3] = gpio.NewTM1638Driver(firmataAdaptor, "9", "8", "4")
    34  
    35  	var ledInt byte
    36  	var color byte
    37  	var offset int
    38  
    39  	// Repeat and concat strings until long enough that with scroll it still shows text
    40  	const showText = "  HELLO WORLD    -    gobot.io    -    TM1638  "
    41  	text := showText
    42  	for len(text)-len(showText) < len(modules)*8 {
    43  		text += showText
    44  	}
    45  
    46  	work := func() {
    47  		gobot.Every(400*time.Millisecond, func() {
    48  			// Enable and change the color of the LEDs
    49  			modules[0].SetLED(color, ledInt)
    50  			modules[1].SetLED(color, ledInt)
    51  			modules[2].SetLED(color, ledInt)
    52  			modules[3].SetLED(color, ledInt)
    53  
    54  			ledInt++
    55  			if ledInt > 7 {
    56  				ledInt = 0
    57  				color++
    58  				if color > 2 {
    59  					color = 0
    60  				}
    61  			}
    62  
    63  			// Scroll the text
    64  			for i := 0; i < 4; i++ {
    65  				modules[i].SetDisplayText(text[offset+8*i : offset+8*i+8])
    66  			}
    67  			offset++
    68  			if offset >= len(showText) {
    69  				offset = 0
    70  			}
    71  		})
    72  	}
    73  
    74  	robot := gobot.NewRobot("tm1638Bot",
    75  		[]gobot.Connection{firmataAdaptor},
    76  		[]gobot.Device{modules[0], modules[1], modules[2], modules[3]},
    77  		work,
    78  	)
    79  
    80  	robot.Start()
    81  }