-
Notifications
You must be signed in to change notification settings - Fork 0
/
screen.go
179 lines (153 loc) · 4.48 KB
/
screen.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main
import (
"encoding/json"
"errors"
"fmt"
"image"
"io/ioutil"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/ajstarks/openvg"
"github.com/electrocatstudios/PiMenu/screenservice"
)
type Screen struct {
Name string `json:"name"`
Background BackgroundDetails `json:"background"`
Line1 DisplayLine `json:"line1"`
Line2 DisplayLine `json:"line2"`
Line3 DisplayLine `json:"line3"`
Line4 DisplayLine `json:"line4"`
Line5 DisplayLine `json:"line5"`
Timeout Timeout `json:"timeout"`
Touches []TouchDetails `json:"touches"`
}
type ScreenDetails struct {
Width int
Height int
W2 openvg.VGfloat
Images map[string]image.Image
}
type TouchDetails struct {
X int `json:"x"`
Y int `json:"y"`
Width int `json:"width"`
Height int `json:"height"`
Command CommandDetails `json:"command"`
}
type CommandDetails struct {
Type string `json:"type"`
Value string `json:"value"`
ReturnScreen string `json:"returnScreen"`
}
type DisplayLine struct {
Type string `json:"type"`
Value string `json:"value"`
Color string `json:"color"`
}
type BackgroundDetails struct {
Color string `json:"color"`
FadeIn bool `json:"fadeIn"`
FadeOut bool `json:"fadeOut"`
}
type Timeout struct {
Length int `json:"length"`
ReturnScreen string `json:"returnScreen"`
ShowCountDown int `json:"showCountDown"`
}
type InterruptScreen struct {
Screens []Screen
RecentFrame *image.Image
IncomingImage *image.Image
Lock sync.Mutex
LastShown time.Time
}
type PMImage struct {
X int
Y int
Width int
Height int
Filename string
}
// Load in the screen data from json file
func GetScreenByName(name string) (Screen, error) {
var ret Screen
filename := fmt.Sprintf("screens/%s.json", name)
if _, err := os.Stat(filename); os.IsNotExist(err) {
ret.Line1.Type = "text"
ret.Line1.Value = "File not found"
return ret, errors.New("File " + name + ".json not found")
}
file, _ := ioutil.ReadFile(filename)
err := json.Unmarshal([]byte(file), &ret)
if err != nil {
return ret, err
}
return ret, nil
}
func GetImageFromString(input string) (PMImage, error) {
ret := PMImage{}
pieceArray := strings.Split(input, ";")
if len(pieceArray) < 5 || len(pieceArray) > 5 {
errString := fmt.Sprintf("Incorrect number of items in image string expected 5 but got %d", len(pieceArray))
err := errors.New(errString)
return ret, err
}
x, err := strconv.Atoi(pieceArray[0])
if err != nil {
return ret, err
}
y, err := strconv.Atoi(pieceArray[1])
if err != nil {
return ret, err
}
width, err := strconv.Atoi(pieceArray[2])
if err != nil {
return ret, err
}
height, err := strconv.Atoi(pieceArray[3])
if err != nil {
return ret, err
}
ret.X = x
ret.Y = y
ret.Width = width
ret.Height = height
ret.Filename = pieceArray[4]
return ret, nil
}
func GetScreenFromScreenRequest(screen *screenservice.ScreenRequest) Screen {
var newScreen Screen
if screen.Line1 != nil {
newScreen.Line1 = DisplayLine{Type: screen.Line1.LineType, Value: screen.Line1.LineValue, Color: screen.Line1.LineColor}
}
if screen.Line2 != nil {
newScreen.Line2 = DisplayLine{Type: screen.Line2.LineType, Value: screen.Line2.LineValue, Color: screen.Line2.LineColor}
}
if screen.Line3 != nil {
newScreen.Line3 = DisplayLine{Type: screen.Line3.LineType, Value: screen.Line3.LineValue, Color: screen.Line3.LineColor}
}
if screen.Line4 != nil {
newScreen.Line4 = DisplayLine{Type: screen.Line4.LineType, Value: screen.Line4.LineValue, Color: screen.Line4.LineColor}
}
if screen.Line5 != nil {
newScreen.Line5 = DisplayLine{Type: screen.Line5.LineType, Value: screen.Line5.LineValue, Color: screen.Line5.LineColor}
}
if screen.Timeout != nil {
newScreen.Timeout = Timeout{Length: int(screen.Timeout.Length), ShowCountDown: int(screen.Timeout.Showtimeout), ReturnScreen: screen.Timeout.Returnscreen}
}
if screen.Touches != nil {
for _, touch := range screen.Touches {
var newCommand CommandDetails
newCommand.Type = touch.Command.Commandtype
newCommand.Value = touch.Command.Commandvalue
newScreen.Touches = append(newScreen.Touches, TouchDetails{X: int(touch.X), Y: int(touch.Y), Width: int(touch.Width), Height: int(touch.Height), Command: newCommand})
}
}
if screen.Background != nil {
newScreen.Background = BackgroundDetails{Color: screen.Background.Color}
}
return newScreen
}