This repository has been archived by the owner on Oct 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
147 lines (122 loc) · 3.04 KB
/
client.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
136
137
138
139
140
141
142
143
144
145
146
147
package unicorn
import (
"fmt"
"github.com/lunixbochs/struc"
"net"
)
// Client is the unix socket client for `unicornd`.
type Client struct {
Path string
sock net.Conn
verbose bool
}
// NewClient returns a new unicorn Client.
// If no socket is specified, it will use a default. Omit if unsure.
func NewClient(verbose bool, socketPath string) *Client {
if socketPath == "" {
socketPath = SocketPath
}
return &Client{
Path: socketPath,
verbose: verbose,
}
}
// Connect opens a connection to the Client.Path
func (c *Client) Connect() (err error) {
if c.verbose {
fmt.Printf("Connecting to (%v)...\n", SocketPath)
}
c.sock, err = net.Dial("unix", SocketPath)
return err
}
// SetBrightness of the display, 0..255
func (c Client) SetBrightness(v uint) error {
if c.verbose {
fmt.Printf("Setting brightness to (%v)\n", v)
}
if v > 255 {
return fmt.Errorf("brightness must be 0..255, passed: %v", v)
}
b := brightness{
Code: CMDSetBrightness,
Val: v,
}
return struc.Pack(c.sock, &b)
}
// SetPixel sets the color of an individual pixel.
func (c Client) SetPixel(x, y, r, g, b uint) error {
if c.verbose {
fmt.Printf("Setting pixel `x:%v`, `y:%v` to rgb(%v, %v, %v)\n", x, y, r, g, b)
}
switch {
case x > 7 || y > 7:
return fmt.Errorf("`x`, `y` must be 0..7, passed `x: %v`, `y: %v`", x, y)
case r > 255 || g > 255 || b > 255:
return fmt.Errorf("`r`, `g`, `b` must be 0..255, passed `r:%v`, `g:%v`, `b:%v`", r, g, b)
}
sp := setPixel{
Code: CMDSetPixel,
Pos: position{
X: x,
Y: y,
},
Col: Pixel{
R: g, // check out https://github.com/pimoroni/unicorn-hat/blob/master/library_c/unicornd/unicornd.c
G: r,
B: b,
},
}
//buf := bytes.NewBuffer([]byte{})
//buf.Write([]byte{byte(sp.Code)})
//buf.Write([]byte{byte(sp.Pos.X), byte(sp.Pos.Y)})
//buf.Write([]byte{byte(sp.Col.R), byte(sp.Col.G), byte(sp.Col.B)})
//bw, err := c.sock.Write(buf.Bytes())
//return err
return struc.Pack(c.sock, &sp)
}
// SetAllPixels allows you to set the color of all pixels in a single call.
func (c Client) SetAllPixels(ps [64]Pixel) error {
if c.verbose {
fmt.Println("Setting all pixels...")
}
for i := range ps {
if ps[i].R > 255 || ps[i].G > 255 || ps[i].B > 255 {
return fmt.Errorf("pixel out of range; `r`, `g`, `b` must be 0..255, passed: `r:%v`, `g:%v`, `b:%v`",
ps[i].R, ps[i].G, ps[i].B,
)
}
}
sp := setAll{
Code: CMDSetAllPixels,
Pixels: ps,
}
return struc.Pack(c.sock, &sp)
}
// Show the pixels written to the buffer.
func (c Client) Show() error {
if c.verbose {
fmt.Println("Displaying pixels")
}
s := show{
Code: CMDShow,
}
return struc.Pack(c.sock, &s)
}
// Clear sets all pixels to 0,0,0.
// wrapper for Client.SetAllPixels([64]Pixel{})
func (c Client) Clear() error {
if c.verbose {
fmt.Println("Display cleared")
}
c.SetAllPixels([64]Pixel{})
c.Show()
return nil
}
// Silent suppresses writing to stdout.
func (c *Client) Silent() {
c.verbose = false
}
// Verbose shows the true therapeutic nature of this program.
func (c *Client) Verbose() {
c.verbose = true
}