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

     1  // +build example
     2  //
     3  // Do not build by default.
     4  
     5  package main
     6  
     7  import (
     8  	"fmt"
     9  	"time"
    10  
    11  	"gobot.io/x/gobot"
    12  	"gobot.io/x/gobot/drivers/i2c"
    13  	"gobot.io/x/gobot/platforms/tinkerboard"
    14  )
    15  
    16  func main() {
    17  	// * I2C1: 3 (SDA), 5 (SCL) --> connected to plate
    18  	board := tinkerboard.NewAdaptor()
    19  	ada := i2c.NewAdafruit1109Driver(board, i2c.WithBus(1))
    20  
    21  	work := func() {
    22  		// set a custom character
    23  		smiley := [8]byte{0, 0, 10, 0, 0, 17, 14, 0}
    24  		ada.CreateChar(0, smiley)
    25  
    26  		ada.Clear()
    27  		ada.SetRGB(true, false, false)
    28  		ada.Write("   Hello from   \n  Tinker Board ")
    29  		// add the custom character at the end of the string
    30  		ada.Write(string(byte(0)))
    31  
    32  		// after 1 sec. activate rotation
    33  		direction := 1
    34  		gobot.After(1*time.Second, func() {
    35  			ada.SetRGB(false, true, false)
    36  			gobot.Every(400*time.Millisecond, func() {
    37  				if direction == 1 {
    38  					ada.ScrollLeft()
    39  				}
    40  				if direction == 2 {
    41  					ada.ScrollRight()
    42  				}
    43  			})
    44  		})
    45  
    46  		// after 7 sec. activate play with the buttons
    47  		gobot.After(7*time.Second, func() {
    48  			direction = 0
    49  			time.Sleep(1 * time.Second)
    50  			ada.LeftToRight()
    51  			ada.Clear()
    52  			ada.SetRGB(false, false, true)
    53  			ada.Write("Try the buttons!")
    54  
    55  			gobot.Every(500*time.Millisecond, func() {
    56  				if val, err := ada.SelectButton(); err != nil {
    57  					fmt.Println(err)
    58  				} else {
    59  					if val != 0 {
    60  						ada.Clear()
    61  						ada.Write("-Select Button-\nclear the screen")
    62  						ada.Blink(false)
    63  						direction = 0
    64  					}
    65  				}
    66  				if val, err := ada.UpButton(); err != nil {
    67  					fmt.Println(err)
    68  				} else {
    69  					if val != 0 {
    70  						ada.Clear()
    71  						ada.Write("  -Up Button-   \nset RGB to white")
    72  						ada.Blink(false)
    73  						ada.SetRGB(true, true, true)
    74  						direction = 0
    75  					}
    76  				}
    77  				if val, err := ada.DownButton(); err != nil {
    78  					fmt.Println(err)
    79  				} else {
    80  					if val != 0 {
    81  						ada.Clear()
    82  						ada.Write(" -Down Button-  \nset blink on")
    83  						ada.Blink(true)
    84  						direction = 0
    85  					}
    86  				}
    87  				if val, err := ada.LeftButton(); err != nil {
    88  					fmt.Println(err)
    89  				} else {
    90  					if val != 0 {
    91  						ada.Clear()
    92  						ada.Write("   -Left Button-  \nrotate left")
    93  						ada.Blink(false)
    94  						direction = 1
    95  					}
    96  				}
    97  				if val, err := ada.RightButton(); err != nil {
    98  					fmt.Println(err)
    99  				} else {
   100  					if val != 0 {
   101  						ada.Clear()
   102  						ada.Write("   -Right Button-  \nrotate right")
   103  						ada.Blink(false)
   104  						direction = 2
   105  					}
   106  				}
   107  			})
   108  
   109  		})
   110  
   111  	}
   112  
   113  	robot := gobot.NewRobot("adaBot",
   114  		[]gobot.Connection{board},
   115  		[]gobot.Device{ada},
   116  		work,
   117  	)
   118  
   119  	robot.Start()
   120  }