-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrpi-backlight.go
354 lines (301 loc) · 8.92 KB
/
rpi-backlight.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// Created by: WestleyK
// email: [email protected]
// Date: Feb 22, 2019
// https://github.com/WestleyK/rpi-backlight
// Version-1.1.4
//
// Designed and tested for raspberry pi with official 7 inch touchscreen.
//
// The Clear BSD License
//
// Copyright (c) 2018-2019 WestleyK
// All rights reserved.
//
// This software is licensed under a Clear BSD License.
//
package main
import (
"os"
"fmt"
"strconv"
"time"
"bufio"
"strings"
"io/ioutil"
)
const (
SCRIPT_VERSION string = "v-1.1.4"
SCRIPT_DATE string = "Date: Feb 22, 2019"
MIN_BRIGHTNESS string = "15"
MAX_BRIGHTNESS string = "255"
DEFAULT_ON string = "180"
ADJUST_UP string = "25"
ADJUST_DOWN string = "25"
BRIGHTNESS_FILE string = "/sys/class/backlight/rpi_backlight/brightness"
POWER_FILE string = "/sys/class/backlight/rpi_backlight/bl_power"
colorReset string = "\x1b[0m"
red string = "\x1b[31m"
// yello string = "\x1b[33m"
// blue string = "\x1b[34m"
// pink string = "\x1b[35m"
// white string = "\x1b[37m"
// teal string = "\x1b[36m"
)
var (
BRIGHT string = ""
)
func help_menu() {
fmt.Printf("Usage: rpi-backlight [OPTION]\n")
fmt.Printf(" --help : print help menu.\n")
fmt.Printf(" [%v-%v] : adjust from: %v to: %s.\n", MIN_BRIGHTNESS, MAX_BRIGHTNESS, MIN_BRIGHTNESS, MAX_BRIGHTNESS)
fmt.Printf(" -s, --sleep : enter sleep mode, press <ENTER> to exit this mode.\n")
fmt.Printf(" -u, --up : adjust brightness up by: %v/%v.\n", ADJUST_UP, MAX_BRIGHTNESS)
fmt.Printf(" -d, --down : adjust brightness down by: %v/%v.\n", ADJUST_DOWN, MAX_BRIGHTNESS)
fmt.Printf(" -c, --current : print current brightness.\n")
fmt.Printf(" -n, --on : turn backlight on to: %v.\n", DEFAULT_ON)
fmt.Printf(" --version : print script version.\n")
fmt.Printf("\n")
fmt.Printf("Copyright (c) 2018-2019 WestleyK, All rights reserved.\n")
fmt.Printf("This software is licensed under a Clear BSD License.\n")
fmt.Printf("Source code: https://github.com/WestleyK/rpi-backlight\n")
os.Exit(0)
}
func script_version() {
fmt.Printf("%s, %s\n", SCRIPT_VERSION, SCRIPT_DATE)
os.Exit(0)
}
func is_bright_file() {
if _, err := os.Stat(BRIGHTNESS_FILE); os.IsNotExist(err) {
fmt.Print(red, "ERROR: ", colorReset)
fmt.Print("File does not exist:\n", BRIGHTNESS_FILE, "\n")
os.Exit(1)
}
}
func is_write() {
file, err := os.Create(BRIGHTNESS_FILE)
if os.IsPermission(err) {
fmt.Print(red, "ERROR: ", colorReset)
fmt.Print("Unable to write to: ", POWER_FILE, "\n")
fmt.Println(err)
os.Exit(1)
}
if err != nil {
fmt.Print(red, "ERROR: ", colorReset)
fmt.Println(err)
os.Exit(1)
}
defer file.Close()
}
func is_power_file() {
if _, err := os.Stat(POWER_FILE); os.IsNotExist(err) {
fmt.Print(red, "ERROR: ", colorReset)
fmt.Print("File does not exist:\n", POWER_FILE, "\n")
os.Exit(1)
}
file, err := os.Create(POWER_FILE)
if err != nil {
if os.IsPermission(err) {
fmt.Print(red, "ERROR: ", colorReset)
fmt.Print("Unable to write to: ", POWER_FILE, "\n")
fmt.Println(err)
os.Exit(1)
}
fmt.Print(red, "ERROR: ", colorReset)
fmt.Println(err)
os.Exit(1)
}
defer file.Close()
}
func is_power_file_perm() {
file_perm, err := os.Create(POWER_FILE)
if err != nil {
if os.IsPermission(err) {
fmt.Print(red, "ERROR: ", colorReset)
fmt.Print("Unable to write to: ", POWER_FILE, "\n")
fmt.Println(err)
os.Exit(1)
}
fmt.Print(red, "ERROR: ", colorReset)
fmt.Println(err)
os.Exit(1)
}
defer file_perm.Close()
}
func sleep_mode() {
is_power_file()
is_power_file_perm()
// sleep 1s, then write 1 to POWER_FILE
fmt.Print("Press <ENTER> to exit this mode:\n")
time.Sleep(1 * time.Second)
file_off, err := os.Create(POWER_FILE)
if err != nil {
fmt.Print(red, "ERROR: ", colorReset)
fmt.Println(err)
os.Exit(1)
}
defer file_off.Close()
// write 1 to POWER_FILE
fmt.Fprintf(file_off, "1")
// wait until user presses <ENTER>
reader := bufio.NewReader(os.Stdin)
_, err = reader.ReadString('\n')
if err != nil {
fmt.Print(red, "ERROR: ", colorReset)
fmt.Println(err)
os.Exit(1)
}
// then reopen the file, and write 0 to POWER_FILE
file_on, err := os.Create(POWER_FILE)
if err != nil {
fmt.Print(red, "ERROR: ", colorReset)
fmt.Println(err)
os.Exit(1)
}
defer file_on.Close()
// write 0 to POWER_FILE
fmt.Fprintf(file_on, "0")
os.Exit(0)
}
func current_bright() {
is_bright_file()
b, err := ioutil.ReadFile(BRIGHTNESS_FILE)
if err != nil {
fmt.Print(err)
}
CURRENT_STRING := string(b)
CURRENT_STRING = strings.TrimSuffix(CURRENT_STRING, "\n")
CURRENT, err := strconv.Atoi(CURRENT_STRING)
fmt.Print("Current brightness: ", CURRENT, "\n")
os.Exit(0)
}
func write_file() {
is_bright_file()
is_write()
file, err := os.Create(BRIGHTNESS_FILE)
if err != nil {
fmt.Print(red, "ERROR: ", colorReset)
fmt.Println(err)
os.Exit(1)
}
defer file.Close()
fmt.Fprintf(file, BRIGHT)
fmt.Print("Current brightness: ", BRIGHT, "\n")
os.Exit(0)
}
func adjust_up() {
is_bright_file()
b, err := ioutil.ReadFile(BRIGHTNESS_FILE)
if err != nil {
fmt.Print(err, "\n")
os.Exit(1)
}
CURRENT_STRING := string(b)
CURRENT_STRING = strings.TrimSuffix(CURRENT_STRING, "\n")
CURRENT, _ := strconv.Atoi(CURRENT_STRING)
ADJUST_UP, _ := strconv.Atoi(ADJUST_UP)
MAX_BRIGHTNESS, _ := strconv.Atoi(MAX_BRIGHTNESS)
CURRENT += ADJUST_UP
if CURRENT >= MAX_BRIGHTNESS {
fmt.Print("Max Brightness!\n")
CURRENT = MAX_BRIGHTNESS
}
BRIGHT = strconv.Itoa(CURRENT)
write_file()
}
func adjust_down() {
is_bright_file()
b, err := ioutil.ReadFile(BRIGHTNESS_FILE)
if err != nil {
fmt.Print(err, "\n")
os.Exit(1)
}
CURRENT_STRING := string(b)
CURRENT_STRING = strings.TrimSuffix(CURRENT_STRING, "\n")
CURRENT, _ := strconv.Atoi(CURRENT_STRING)
ADJUST_DOWN, _ := strconv.Atoi(ADJUST_DOWN)
MIN_BRIGHTNESS, _ := strconv.Atoi(MIN_BRIGHTNESS)
CURRENT -= ADJUST_DOWN
if CURRENT <= MIN_BRIGHTNESS {
fmt.Print("Min Brightness!\n")
CURRENT = MIN_BRIGHTNESS
}
BRIGHT = strconv.Itoa(CURRENT)
write_file()
}
func turn_on() {
is_bright_file()
is_write()
is_power_file()
file_bright, err := os.Create(BRIGHTNESS_FILE)
if err != nil {
fmt.Print(red, "ERROR: ", colorReset)
fmt.Println(err)
os.Exit(1)
}
defer file_bright.Close()
fmt.Fprintf(file_bright, DEFAULT_ON)
file_power, err := os.Create(POWER_FILE)
if err != nil {
fmt.Print(red, "ERROR: ", colorReset)
fmt.Println(err)
os.Exit(1)
}
defer file_power.Close()
fmt.Fprintf(file_power, "0")
os.Exit(0)
}
func adjust_bright() {
// check if theres a '-'
if strings.Contains(BRIGHT, "-") {
fmt.Print("Only whole numbers! ", BRIGHT, "\n")
os.Exit(1)
}
// convert strings to int
CURRENT, _ := strconv.Atoi(BRIGHT)
MIN_BRIGHTNESS, _ := strconv.Atoi(MIN_BRIGHTNESS)
MAX_BRIGHTNESS, _ := strconv.Atoi(MAX_BRIGHTNESS)
// check if in range
if CURRENT >= MIN_BRIGHTNESS && CURRENT <= MAX_BRIGHTNESS {
BRIGHT = strconv.Itoa(CURRENT)
write_file()
os.Exit(0)
}
fmt.Print("Not a valid number: ", CURRENT, "\n")
fmt.Print("Your options: [", MIN_BRIGHTNESS, "-", MAX_BRIGHTNESS, "]\n")
os.Exit(0)
}
func main() {
if len(os.Args[1:]) >= 1 {
if len(os.Args[1:]) >= 2 {
fmt.Print("Only one argument!\n")
os.Exit(1)
}
OPTION := os.Args[1]
if OPTION == "--help" {
help_menu()
} else if OPTION == "-u" || OPTION == "--up" {
adjust_up()
} else if OPTION == "-d" || OPTION == "--down" {
adjust_down()
} else if OPTION == "-s" || OPTION == "--sleep" {
sleep_mode()
} else if OPTION == "-c" || OPTION == "--current" {
current_bright()
} else if OPTION == "-n" || OPTION == "--on" {
turn_on()
} else if OPTION == "--version" {
script_version()
} else if _, err := strconv.Atoi(OPTION); err == nil {
BRIGHT = OPTION
adjust_bright()
} else {
fmt.Print("Option not found! :P ", OPTION, "\n")
fmt.Print("Try: $ rpi-backlight --help (for help)\n")
os.Exit(1)
}
}
current_bright()
}
//
// End source code
//