-
Notifications
You must be signed in to change notification settings - Fork 0
/
blightness.rkt
68 lines (57 loc) · 2.03 KB
/
blightness.rkt
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
#lang racket/base
(require racket/gui/easy
racket/gui/easy/operator)
; configure screen brightness file, this one works with my toshiba laptop
(define @brightness-file (@ "/sys/class/backlight/intel_backlight/brightness"))
; read and return first line from screen brightness file,
; converted from string to number
(define (load-brightness file)
(call-with-input-file file
(lambda (in)
(string->number (read-line in)))))
; observable holding the current screen brightness
(define @bright (@ (load-brightness (obs-peek @brightness-file))))
; write to the screen brightness file, takes a
; string representing a number, e.g. "1500"
(define (write-brightness numstring)
(call-with-output-file (obs-peek @brightness-file)
(lambda (out)
(display numstring out))
#:exists 'truncate))
; subtract 500 from val if bigger than 550
; at 0 screen is black, hence the +50
(define (sub500 val)
(if (> val 550)
(- val 500)
500))
; add 500 to val if less than 4000
(define (add500 val)
(if (< val 4000)
(+ val 500)
4000))
; update @bright and write to the screen brightness file,
; takes a function that works with obs-update!
(define (update-brightness! action)
(obs-update! @bright action)
(write-brightness (number->string (obs-peek @bright))))
; write arbitrary value to screen brightness file
(define (set-brightness! value)
(write-brightness (number->string value)))
(define (app)
(window
#:title "Blightness Adjustor Robot"
(vpanel
(hpanel
(button "-" (lambda () (update-brightness! sub500)))
(button "+" (lambda () (update-brightness! add500)))
(button "500" (lambda () (update-brightness! (lambda (ev) 500))))
(button "1500" (lambda () (update-brightness! (lambda (ev) 1500))))
(button "3500" (lambda () (update-brightness! (lambda (ev) 3500)))))
(vpanel
(slider
#:min-value 0
#:max-value 5000
@bright (lambda (val) (and (set-brightness! val) val)) ; (λ:= @bright)
#:style '(horizontal plain))))))
(module+ main
(render (app)))