Skip to content

Instantly share code, notes, and snippets.

@lugu
Created March 14, 2018 21:57
Show Gist options
  • Save lugu/fef421870bd4fb7c9b049dad0eeaa317 to your computer and use it in GitHub Desktop.
Save lugu/fef421870bd4fb7c9b049dad0eeaa317 to your computer and use it in GitHub Desktop.
GOARCH=arm go build temper.go
package main
import (
"fmt"
"golang.org/x/exp/io/i2c"
"os"
"strconv"
)
func main() {
file, err := os.Open("/dev/temper0")
if err != nil {
fmt.Printf("failed to open thermometer: %s\n", err)
return
}
defer file.Close()
device, err := i2c.Open(&i2c.Devfs{Dev: "/dev/i2c-1"}, 0x67)
if err != nil {
fmt.Printf("can not open display: %s\n", err)
return
}
defer device.Close()
buf := []byte{0, 0, 0, 0}
bytes, err := file.Read(buf)
if err != nil || bytes != 4 {
fmt.Println("failed to read input")
return
}
x1, err := strconv.Atoi(string(buf))
if err != nil {
fmt.Printf("can not convert input: %s\n", err)
return
}
temperature := float32(x1) * 125 / 32000
str := fmt.Sprintf("%2.2f", temperature)
fmt.Printf("temp: %s\n", str)
device.WriteReg(0x76, []byte{str[0], str[1], str[3], str[4]})
device.WriteReg(0x77, []byte{0x2})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment