forked from periph/devices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtm1637_test.go
89 lines (77 loc) · 1.91 KB
/
tm1637_test.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
// 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.
package tm1637
import (
"bytes"
"errors"
"log"
"testing"
"time"
"periph.io/x/conn/v3/gpio"
"periph.io/x/conn/v3/gpio/gpiotest"
)
func TestNew(t *testing.T) {
var clk, data gpiotest.Pin
dev, err := New(&clk, &data)
if err != nil {
t.Fatalf("failed to initialize tm1637: %v", err)
}
if s := dev.String(); s != "TM1637{clk:(0), data:(0)}" {
t.Fatal(s)
}
if _, err := dev.Write(Clock(12, 00, true)); err != nil {
log.Fatalf("failed to write to tm1637: %v", err)
}
if err := dev.SetBrightness(Brightness10); err != nil {
log.Fatalf("failed to write to tm1637: %v", err)
}
if err := dev.Halt(); err != nil {
t.Fatal(err)
}
// TODO(maruel): Check the state of the pins. That's hard since it has to
// emulate the quasi-I²C protocol.
}
func TestDigits(t *testing.T) {
expected := []byte{0x3F, 0x06}
if b := Digits(0, 1); !bytes.Equal(b, expected) {
t.Fatalf("%v != %v", b, expected)
}
}
func TestNew_clk_fail(t *testing.T) {
clk := failPin{fail: true}
data := gpiotest.Pin{}
if dev, err := New(&clk, &data); dev != nil || err == nil {
t.Fatal("data pin is not usable")
}
}
func TestNew_data_fail(t *testing.T) {
clk := gpiotest.Pin{}
data := failPin{fail: true}
if dev, err := New(&clk, &data); dev != nil || err == nil {
t.Fatal("data pin is not usable")
}
}
func TestWrite_fail(t *testing.T) {
dev, err := New(&gpiotest.Pin{}, &gpiotest.Pin{})
if err != nil {
t.Fatalf("failed to initialize tm1637: %v", err)
}
if n, err := dev.Write(make([]byte, 7)); n != 0 || err == nil {
t.Fatal("buffer too long")
}
}
//
type failPin struct {
gpiotest.Pin
fail bool
}
func (f *failPin) Out(l gpio.Level) error {
if f.fail {
return errors.New("injected error")
}
return nil
}
func init() {
spin = func(time.Duration) {}
}