-
Notifications
You must be signed in to change notification settings - Fork 1
/
generic.go
96 lines (86 loc) · 2.29 KB
/
generic.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
package display
import (
"errors"
"log"
"strings"
)
type (
LCD interface {
// Reopen the instance after a Close call.
Open() error
// Write a string message on line one or two.
// If text is longer than supported, it will be cut.
Write(line Line, text string) error
// Enable(turn on) or disable(turn off) the display.
Enable(yes bool) error
// Listen blocking for button events.
// Please note, not all devices support released=true.
Listen(l func(btn int, released bool) bool)
// Close the connection to the display.
Close() error
}
// The line on the display. Most of them support only 0 and 1.
Line int
// Placeholder for an actual implementation
dummy struct{}
)
var (
DummyLCD = &dummy{}
ErrClosed = errors.New("display closed")
ErrDisplayNotWorking = errors.New("display not working")
ErrMsgSizeMismatch = errors.New("msg size mismatch")
filledSquare = string([]byte{0xff})
)
const (
LineOne Line = 0
LineTwo Line = 1
DefaultTTy = "/dev/ttyS1"
c16 = 16
)
// Factory function to probe the correct implementation
func Find() LCD {
var (
lcd LCD
err error
)
lcd, err = NewAsustorLCD("")
if err == nil {
log.Println("Using Asustor LCD")
return lcd
} else {
lcd, err = NewQnapLCD("")
if err == nil {
log.Println("Using Qnap LCD")
return lcd
} else {
log.Println(err)
}
}
log.Println("Using Dummy LCD")
return DummyLCD
}
/*
Dummy functions to use as an actual display.
As the display is mostly a nice to have feature anyways.
*/
func (d *dummy) Open() error { return nil }
func (d *dummy) Write(line Line, text string) error { return nil }
func (d *dummy) Enable(yes bool) error { return nil }
func (d *dummy) Listen(l func(btn int, released bool) bool) {}
func (d *dummy) Close() error { return nil }
func prepareTxt(txt string) string {
l := len(txt)
if l > c16 {
txt = txt[0:c16]
} else if l < c16 {
txt += strings.Repeat(" ", c16-l)
}
return txt
}
func Progress(perc int) string {
chars := percentOf(c16, 100, perc)
return strings.Repeat(filledSquare, chars) + strings.Repeat("-", c16-chars)
}
func percentOf(maxVal, maxPercent, currentPercent int) int {
return (maxVal * currentPercent) / maxPercent
}