summaryrefslogtreecommitdiffstats
path: root/dependencies/pkg/mod/golang.org/x/exp@v0.0.0-20220613132600-b0d781184e0d/io/i2c/example/displayip/main.go
blob: 58fe694774bcabe676e2a1a3e12efa88f0af05a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package main contains a program that displays the IPv4 address
// of the machine on an a Grove-LCD RGB backlight.
package main

import (
	"fmt"
	"net"

	"golang.org/x/exp/io/i2c"
)

const (
	DISPLAY_RGB_ADDR  = 0x62
	DISPLAY_TEXT_ADDR = 0x3e
)

func main() {
	d, err := i2c.Open(&i2c.Devfs{Dev: "/dev/i2c-1"}, DISPLAY_RGB_ADDR)
	if err != nil {
		panic(err)
	}

	td, err := i2c.Open(&i2c.Devfs{Dev: "/dev/i2c-1"}, DISPLAY_TEXT_ADDR)
	if err != nil {
		panic(err)
	}

	// Set the backlight color to 100,100,100.
	write(d, []byte{0, 0})
	write(d, []byte{1, 0})
	write(d, []byte{0x08, 0xaa})
	write(d, []byte{4, 100}) // R value
	write(d, []byte{3, 100}) // G value
	write(d, []byte{2, 100}) // B value

	ip, err := resolveIP()
	if err != nil {
		panic(err)
	}

	fmt.Printf("host machine IP is %v\n", ip)

	write(td, []byte{0x80, 0x02})        // return home
	write(td, []byte{0x80, 0x01})        // clean the display
	write(td, []byte{0x80, 0x08 | 0x04}) // no cursor
	write(td, []byte{0x80, 0x28})        // two lines

	for _, s := range ip {
		write(td, []byte{0x40, byte(s)})
	}
}

func write(d *i2c.Device, buf []byte) {
	err := d.Write(buf)
	if err != nil {
		panic(err)
	}
}

func resolveIP() (string, error) {
	var ip net.IP
	ifaces, err := net.Interfaces()
	if err != nil {
		panic(err)
	}
	for _, i := range ifaces {
		addrs, err := i.Addrs()
		if err != nil {
			panic(err)
		}
		for _, addr := range addrs {
			switch v := addr.(type) {
			case *net.IPNet:
				ip = v.IP
			case *net.IPAddr:
				ip = v.IP
			}
			ip = ip.To4()
			if ip != nil && ip.String() != "127.0.0.1" {
				return ip.String(), nil
			}
		}
	}
	return "", fmt.Errorf("cannot resolve the IP")
}