-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdisplay.go
127 lines (113 loc) · 2.16 KB
/
display.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
package pixoo64
import (
"encoding/json"
"strconv"
)
const (
Width = 64
Height = 64
)
// Display manages the graphics on the pixoo64 device.
type Display struct {
client *Client
pictures []*Picture
}
// NewDisplay create a new Display.
func NewDisplay(client *Client) (*Display, error) {
d := &Display{
client: client,
}
err := d.Reset()
if err != nil {
return nil, err
}
return d, nil
}
type GetHttpGifId struct {
ErrorCode int `json:"error_code"`
PicId int `json:"PicId"`
}
func (d *Display) GetHttpGifId() (int, error) {
var jsonData = []byte(` {
"Command":"Draw/GetHttpGifId"
}`)
body, err := d.client.Post(jsonData)
if err != nil {
return 0, err
}
result := GetHttpGifId{}
err = json.Unmarshal([]byte(body), &result)
if err != nil {
return 0, err
}
return result.PicId, nil
}
func (d *Display) CreatePicture() (*Picture, error) {
nextId, err := d.GetHttpGifId()
if err != nil {
return nil, err
}
picture := &Picture{
client: d.client,
id: nextId,
frames: nil,
}
d.pictures = append(d.pictures, picture)
return picture, nil
}
func (d *Display) Reset() error {
err := d.ResetText()
if err != nil {
return err
}
for _, p := range d.pictures {
err = p.Reset()
if err != nil {
return err
}
}
return err
}
func (d *Display) Update() error {
for _, p := range d.pictures {
err := p.Update()
if err != nil {
return err
}
}
return nil
}
func (d *Display) ResetText() error {
var jsonData = []byte(` {
"Command":"Draw/ClearHttpText"
}`)
_, err := d.client.Post(jsonData)
return err
}
func (d *Display) SetBrightness(brightness int) error {
var jsonData = []byte(` {
"Command":"Channel/SetBrightness",
"Brightness": ` + strconv.FormatInt(int64(brightness), 10) + `
}`)
_, err := d.client.Post(jsonData)
if err != nil {
return err
}
return nil
}
type Switch int
const (
Off Switch = 0
On Switch = 1
)
func (d *Display) Switch(onoff Switch) error {
var jsonData = []byte(` {
"Command":"Channel/OnOffScreen",
"OnOff": ` + strconv.FormatInt(int64(onoff), 10) + `
}`)
_, err := d.client.Post(jsonData)
if err != nil {
return err
}
return nil
}