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

     1  //go:build example
     2  // +build example
     3  
     4  //
     5  // Do not build by default.
     6  
     7  /*
     8   How to setup
     9   This examples requires you to daisy-chain 4 led matrices based on MAX7219.
    10   It will turn on one led at a time, from the first led at the first matrix to the last led of the last matrix.
    11  
    12   How to run
    13   Pass serial port to use as the first param:
    14  
    15  	go run examples/firmata_gpio_max7219.go /dev/ttyACM0
    16  */
    17  
    18  package main
    19  
    20  import (
    21  	"os"
    22  	"time"
    23  
    24  	"gobot.io/x/gobot/v2"
    25  	"gobot.io/x/gobot/v2/drivers/gpio"
    26  	"gobot.io/x/gobot/v2/platforms/firmata"
    27  )
    28  
    29  func main() {
    30  	firmataAdaptor := firmata.NewAdaptor(os.Args[1])
    31  	max := gpio.NewMAX7219Driver(firmataAdaptor, "11", "10", "9", 4)
    32  
    33  	var digit byte = 1 // digit address goes from 0x01 (MAX7219Digit0) to 0x08 (MAX7219Digit8)
    34  	var bits byte = 1
    35  	var module uint
    36  	count := 0
    37  
    38  	work := func() {
    39  		gobot.Every(100*time.Millisecond, func() {
    40  			max.ClearAll()
    41  			max.One(module, digit, bits)
    42  			bits = bits << 1
    43  
    44  			count++
    45  			if count > 7 {
    46  				count = 0
    47  				digit++
    48  				bits = 1
    49  				if digit > 8 {
    50  					digit = 1
    51  					module++
    52  					if module >= 4 {
    53  						module = 0
    54  						count = 0
    55  					}
    56  				}
    57  			}
    58  		})
    59  	}
    60  
    61  	robot := gobot.NewRobot("Max7219Bot",
    62  		[]gobot.Connection{firmataAdaptor},
    63  		[]gobot.Device{max},
    64  		work,
    65  	)
    66  
    67  	robot.Start()
    68  }