2 ;;; Copyright (c) 2014 Clinton Ebadi <clinton@unknownlamer.org>
4 ;;; This program is free software: you can redistribute it and/or modify
5 ;;; it under the terms of the GNU General Public License as published by
6 ;;; the Free Software Foundation, either version 3 of the License, or
7 ;;; (at your option) any later version.
9 ;;; This program is distributed in the hope that it will be useful,
10 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ;;; GNU General Public License for more details.
14 ;;; You should have received a copy of the GNU General Public License
15 ;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
17 (use-modules (ice-9 getopt-long)
33 ;;; stty is less of a pain, just be evil and system
34 (system "stty -F /dev/ttyACM0 cs8 115200 ignbrk -brkint -icrnl -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke noflsh -ixon -crtscts")
35 (sleep 3) ; wait for arduino to finish booting
37 (set! *random-state* (random-state-from-platform))
39 (define serial-out (open-file "/dev/ttyACM0" "w0"))
40 (define serial-in (open-file "/dev/ttyACM0" "r"))
42 (define color-lock (make-mutex 'recursive))
43 (define current-color (vector 0.0 1.0 1.0))
45 (define serial-lock (make-mutex))
47 (define pi 3.141592653589793)
49 (define (deg->rad degrees)
50 (* degrees (/ pi 180)))
52 (define (hsv->rgb h s v)
53 (let* ((chroma (* v s))
55 (x (* chroma (- 1 (abs (- (euclidean-remainder hue' 2) 1)))))
58 (case (inexact->exact (truncate hue'))
59 ((0) (values chroma x 0))
60 ((1) (values x chroma 0))
61 ((2) (values 0 chroma x))
62 ((3) (values 0 x chroma))
63 ((4) (values x 0 chroma))
64 ((5) (values chroma 0 x))
65 (else => (lambda (x) (error "Bad hue" h hue' x))))
66 (values (+ r₁ m) (+ g₁ m) (+ b₁ m)))))
68 (define (set-led-color/primitive! r g b)
69 ;; if put-bytevector is not atomic, may need to have a writer thread
70 (with-mutex serial-lock
71 (put-bytevector serial-out (u8vector r g b))
72 (read-line serial-in 'concat)))
74 (define (set-led-hsv! h s v)
75 (with-mutex color-lock
76 (set! current-color (vector h s v))
79 (let ((r' (inexact->exact (truncate (* (* r 1) 255))))
80 (g' (inexact->exact (truncate (* (* g 1) 255))))
81 (b' (inexact->exact (truncate (* (* b 1) 255)))))
82 (set-led-color/primitive! r' g' b')))))
84 (define zzz (if (and (> (length (command-line)) 1)
85 (number? (string->number (second (command-line)))))
86 (string->number (second (command-line)))
89 (display (command-line))
91 (let ((zone-out (format #f "hey man i'm getting zzzzzzzzzzooooned out ~A~%" zzz)))
93 (system (format #f "echo \"~A\" | festival --tts" zone-out)))
95 (define tick-increment (/ (* 2 pi) (* 360 10)))
97 (define (random-ticks n) (* tick-increment (random n)))
101 (with-mutex color-lock
104 (let ((h (euclidean-remainder (+ h tick-increment) (* 2 pi))))
105 (display (set-led-hsv! h s v) (current-error-port))))))
111 (let ((h (euclidean-remainder tick (* 2 pi))))
112 (display (set-led-hsv! h 1.0 1.0) (current-error-port))
114 (loop (+ tick-increment tick)))))
117 ;;; Multi-processing is for hep cats
119 (define running-fx #f)
120 (define server-thread #f)
123 (with-mutex serial-lock
124 (cancel-thread running-fx)))
129 (set! running-fx (make-thread fx)))
131 (define (reverse-tick!)
132 (set! tick-increment (- tick-increment)))
135 (define (led-command-handler request body)
136 (match (split-and-decode-uri-path (uri-path (request-uri request)))
138 (set-led-color/primitive! (string->number r)
141 (display "Set rgb\n")
142 (values '((content-type . (text/plain)))
145 (set-led-hsv! (string->number h )
148 (display "Set hsv\n")
149 (values '((content-type . (text/plain)))
151 (("fx" "kill") (kill-fx))
152 (("fx" "run" "hsv-fade") (run-fx hsv-fade))
155 (values '((content-type . (text/plain)))
158 (set-current-error-port (open-file "/tmp/led.log" "w0"))
160 (define (start-led-server)
161 (set! server-thread (make-thread (lambda () (run-server led-command-handler 'http '(#:port 58080))))))