gobot.io/x/gobot@v1.16.0/examples/firmata_tm1638.go (about)

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