| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package main |
| 8 |
|
| 9 |
import ( |
| 10 |
"fmt" |
| 11 |
"net" |
| 12 |
|
| 13 |
"golang.org/x/exp/io/i2c" |
| 14 |
) |
| 15 |
|
| 16 |
const ( |
| 17 |
DISPLAY_RGB_ADDR = 0x62 |
| 18 |
DISPLAY_TEXT_ADDR = 0x3e |
| 19 |
) |
| 20 |
|
| 21 |
func main() { |
| 22 |
d, err := i2c.Open(&i2c.Devfs{Dev: "/dev/i2c-1"}, DISPLAY_RGB_ADDR) |
| 23 |
if err != nil { |
| 24 |
panic(err) |
| 25 |
} |
| 26 |
|
| 27 |
td, err := i2c.Open(&i2c.Devfs{Dev: "/dev/i2c-1"}, DISPLAY_TEXT_ADDR) |
| 28 |
if err != nil { |
| 29 |
panic(err) |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
write(d, []byte{0, 0}) |
| 34 |
write(d, []byte{1, 0}) |
| 35 |
write(d, []byte{0x08, 0xaa}) |
| 36 |
write(d, []byte{4, 100}) |
| 37 |
write(d, []byte{3, 100}) |
| 38 |
write(d, []byte{2, 100}) |
| 39 |
|
| 40 |
ip, err := resolveIP() |
| 41 |
if err != nil { |
| 42 |
panic(err) |
| 43 |
} |
| 44 |
|
| 45 |
fmt.Printf("host machine IP is %v\n", ip) |
| 46 |
|
| 47 |
write(td, []byte{0x80, 0x02}) |
| 48 |
write(td, []byte{0x80, 0x01}) |
| 49 |
write(td, []byte{0x80, 0x08 | 0x04}) |
| 50 |
write(td, []byte{0x80, 0x28}) |
| 51 |
|
| 52 |
for _, s := range ip { |
| 53 |
write(td, []byte{0x40, byte(s)}) |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
func write(d *i2c.Device, buf []byte) { |
| 58 |
err := d.Write(buf) |
| 59 |
if err != nil { |
| 60 |
panic(err) |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
func resolveIP() (string, error) { |
| 65 |
var ip net.IP |
| 66 |
ifaces, err := net.Interfaces() |
| 67 |
if err != nil { |
| 68 |
panic(err) |
| 69 |
} |
| 70 |
for _, i := range ifaces { |
| 71 |
addrs, err := i.Addrs() |
| 72 |
if err != nil { |
| 73 |
panic(err) |
| 74 |
} |
| 75 |
for _, addr := range addrs { |
| 76 |
switch v := addr.(type) { |
| 77 |
case *net.IPNet: |
| 78 |
ip = v.IP |
| 79 |
case *net.IPAddr: |
| 80 |
ip = v.IP |
| 81 |
} |
| 82 |
ip = ip.To4() |
| 83 |
if ip != nil && ip.String() != "127.0.0.1" { |
| 84 |
return ip.String(), nil |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
return "", fmt.Errorf("cannot resolve the IP") |
| 89 |
} |