forked from google/periph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
84 lines (68 loc) · 1.88 KB
/
main.go
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
// Copyright 2018 The Periph Authors. All rights reserved.
// Use of this source code is governed under the Apache License, Version 2.0
// that can be found in the LICENSE file.
package main
import (
"log"
"time"
"periph.io/x/periph/conn/i2c/i2creg"
"periph.io/x/periph/experimental/devices/sn3218"
"periph.io/x/periph/host"
)
func main() {
if _, err := host.Init(); err != nil {
log.Fatal(err)
}
b, err := i2creg.Open("")
if err != nil {
log.Fatal(err)
}
defer b.Close()
d, err := sn3218.New(b)
if err != nil {
log.Fatal(err)
}
defer d.Halt()
if err := d.WakeUp(); err != nil {
log.Fatal("Error while enabling device", err)
}
if err := d.BrightnessAll(1); err != nil {
log.Fatal("Error while setting brightness", err)
}
// Switch LED 7 on
if err := d.Switch(7, true); err != nil {
log.Fatal("Error while switching LED", err)
}
time.Sleep(1000 * time.Millisecond)
//Increase brightness for LED 7 to max
if err := d.Brightness(7, 255); err != nil {
log.Fatal("Error while changing LED brightness", err)
}
time.Sleep(1000 * time.Millisecond)
//Get state of LED 7
state, brightness, err := d.GetState(7)
if err != nil {
log.Fatal("Error while reading LED state", err)
}
log.Println("State: ", state, " - Brightness: ", brightness)
// Switch all LEDs on
if err := d.SwitchAll(true); err != nil {
log.Fatal("Error while switching all LEDs", err)
}
time.Sleep(1000 * time.Millisecond)
// Increase brightness for all
if err := d.BrightnessAll(125); err != nil {
log.Fatal("Error while changing globalBrightness", err)
}
time.Sleep(1000 * time.Millisecond)
// Sleep mode to save energy, but keep state
if err := d.Sleep(); err != nil {
log.Fatal("Error while disabling device")
}
time.Sleep(1000 * time.Millisecond)
// WakeUp again
if err := d.WakeUp(); err != nil {
log.Fatal("Error while enabling device")
}
time.Sleep(1000 * time.Millisecond)
}