tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/examples/hd44780i2c/main.go (about)

     1  package main
     2  
     3  import (
     4  	"machine"
     5  	"strconv"
     6  	"time"
     7  
     8  	"tinygo.org/x/drivers/hd44780i2c"
     9  )
    10  
    11  func main() {
    12  
    13  	// Note: most HD44780 LCD modules requires 5V power, however some variations
    14  	// use 3.3V (and may be damaged by 5V).
    15  
    16  	machine.I2C0.Configure(machine.I2CConfig{
    17  		Frequency: machine.TWI_FREQ_400KHZ,
    18  	})
    19  
    20  	lcd := hd44780i2c.New(machine.I2C0, 0x27) // some modules have address 0x3F
    21  
    22  	lcd.Configure(hd44780i2c.Config{
    23  		Width:       16, // required
    24  		Height:      2,  // required
    25  		CursorOn:    true,
    26  		CursorBlink: true,
    27  	})
    28  
    29  	lcd.Print([]byte(" TinyGo\n  LCD Test "))
    30  
    31  	// CGRAM address 0x0-0x7 can be used to store 8 custom characters
    32  	lcd.CreateCharacter(0x0, []byte{0x00, 0x11, 0x0E, 0x1F, 0x15, 0x1F, 0x1F, 0x1F})
    33  	lcd.Print([]byte{0x0})
    34  
    35  	// You can use https://maxpromer.github.io/LCD-Character-Creator/
    36  	// to crete your own characters.
    37  
    38  	time.Sleep(time.Millisecond * 7000)
    39  
    40  	for i := 0; i < 5; i++ {
    41  		lcd.BacklightOn(false)
    42  		time.Sleep(time.Millisecond * 250)
    43  		lcd.BacklightOn(true)
    44  		time.Sleep(time.Millisecond * 250)
    45  	}
    46  
    47  	lcd.CursorOn(false)
    48  	lcd.CursorBlink(false)
    49  
    50  	i := 0
    51  	for {
    52  
    53  		lcd.ClearDisplay()
    54  		lcd.SetCursor(2, 1)
    55  		lcd.Print([]byte(strconv.FormatInt(int64(i), 10)))
    56  		i++
    57  		time.Sleep(time.Millisecond * 100)
    58  
    59  	}
    60  }