forked from google/periph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
135 lines (127 loc) · 3.11 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright 2016 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.
// tm1637 writes to a digits LED display.
package main
import (
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"strconv"
"periph.io/x/periph/conn/gpio/gpioreg"
"periph.io/x/periph/devices/tm1637"
"periph.io/x/periph/host"
)
func mainImpl() error {
clk := flag.String("c", "4", "CLK pin number")
data := flag.String("d", "5", "DIO pin number")
off := flag.Bool("o", false, "set display as off")
b1 := flag.Bool("b1", false, "set PWM to 1/16")
b2 := flag.Bool("b2", false, "set PWM to 2/16")
b4 := flag.Bool("b4", false, "set PWM to 4/16")
b10 := flag.Bool("b10", false, "set PWM to 10/16 (default)")
b12 := flag.Bool("b12", false, "set PWM to 12/16")
b13 := flag.Bool("b13", false, "set PWM to 13/16")
b14 := flag.Bool("b14", false, "set PWM to 14/16")
verbose := flag.Bool("v", false, "verbose mode")
asSeg := flag.Bool("s", false, "use hex encoded segments instead of numbers")
asTime := flag.Bool("t", false, "expect two numbers representing time")
showDot := flag.Bool("dot", false, "when -t is used, show dots")
flag.Parse()
if !*verbose {
log.SetOutput(ioutil.Discard)
}
log.SetFlags(log.Lmicroseconds)
b := tm1637.Brightness10
switch {
case *off:
b = tm1637.Off
case *b1:
b = tm1637.Brightness1
case *b2:
b = tm1637.Brightness2
case *b4:
b = tm1637.Brightness4
case *b10:
b = tm1637.Brightness10
case *b12:
b = tm1637.Brightness12
case *b13:
b = tm1637.Brightness13
case *b14:
b = tm1637.Brightness14
}
if flag.NArg() > 6 {
return errors.New("too many digits")
}
if b != tm1637.Off && flag.NArg() == 0 {
// Turn it off
b = tm1637.Off
}
var segments []byte
if *asTime {
if flag.NArg() != 2 {
return errors.New("provide hh and mm")
}
x, err := strconv.ParseUint(flag.Arg(0), 10, 8)
if err != nil {
return err
}
hour := int(x)
x, err = strconv.ParseUint(flag.Arg(1), 10, 8)
if err != nil {
return err
}
minute := int(x)
segments = tm1637.Clock(hour, minute, *showDot)
} else if *asSeg {
segments = make([]byte, flag.NArg())
for i, d := range flag.Args() {
x, err := strconv.ParseUint(d, 16, 8)
if err != nil {
return err
}
segments[i] = byte(x)
}
} else {
digits := make([]int, flag.NArg())
for i, d := range flag.Args() {
x, err := strconv.ParseUint(d, 16, 8)
if err != nil {
return err
}
digits[i] = int(x)
}
segments = tm1637.Digits(digits...)
}
if _, err := host.Init(); err != nil {
return err
}
pClk := gpioreg.ByName(*clk)
if pClk == nil {
return errors.New("specify a valid pin for clock")
}
pData := gpioreg.ByName(*data)
if pData == nil {
return errors.New("specify a valid pin for data")
}
// TODO(maruel): Print where the pins are located.
d, err := tm1637.New(pClk, pData)
if err != nil {
return err
}
if err = d.SetBrightness(b); err != nil {
return err
}
_, err = d.Write(segments)
return err
}
func main() {
if err := mainImpl(); err != nil {
fmt.Fprintf(os.Stderr, "tm1637: %s.\n", err)
os.Exit(1)
}
}