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))
44 (define pwm-resolution (1- (expt 2 16)))
46 (define serial-lock (make-mutex))
48 (define pi 3.141592653589793)
50 (define (deg->rad degrees)
51 (* degrees (/ pi 180)))
53 (define (hsv->rgb h s v)
54 (let* ((chroma (* v s))
56 (x (* chroma (- 1 (abs (- (euclidean-remainder hue' 2) 1)))))
59 (case (inexact->exact (truncate hue'))
60 ((0) (values chroma x 0))
61 ((1) (values x chroma 0))
62 ((2) (values 0 chroma x))
63 ((3) (values 0 x chroma))
64 ((4) (values x 0 chroma))
65 ((5) (values chroma 0 x))
66 (else => (lambda (x) (error "Bad hue" h hue' x))))
67 (values (+ r₁ m) (+ g₁ m) (+ b₁ m)))))
69 (define (set-led-color/primitive! r g b)
70 ;; if put-bytevector is not atomic, may need to have a writer thread
71 (with-mutex serial-lock
72 (put-bytevector serial-out (u8vector r g b))
73 (read-line serial-in 'concat)))
75 (define (set-led-hsv! h s v)
76 (with-mutex color-lock
77 (set! current-color (vector h s v))
80 (let ((r' (inexact->exact (truncate (* (* r 1) 255))))
81 (g' (inexact->exact (truncate (* (* g 1) 255))))
82 (b' (inexact->exact (truncate (* (* b 1) 255)))))
83 (set-led-color/primitive! r' g' b')))))
85 (define zzz (if (and (> (length (command-line)) 1)
86 (number? (string->number (second (command-line)))))
87 (string->number (second (command-line)))
90 (display (command-line))
92 (let ((zone-out (format #f "hey man i'm getting zzzzzzzzzzooooned out ~A~%" zzz)))
94 (system (format #f "echo \"~A\" | festival --tts" zone-out)))
96 (define tick-increment (/ (* 2 pi) (* 360 10)))
98 (define (random-ticks n) (* tick-increment (random n)))
102 (with-mutex color-lock
105 (let ((h (euclidean-remainder (+ h tick-increment) (* 2 pi))))
106 (display (set-led-hsv! h s v) (current-error-port))))))
112 (let ((h (euclidean-remainder tick (* 2 pi))))
113 (display (set-led-hsv! h 1.0 1.0) (current-error-port))
115 (loop (+ tick-increment tick)))))
118 ;;; Multi-processing is for hep cats
120 (define running-fx #f)
121 (define server-thread #f)
124 (with-mutex serial-lock
125 (cancel-thread running-fx)))
130 (set! running-fx (make-thread fx)))
132 (define (reverse-tick!)
133 (set! tick-increment (- tick-increment)))
136 (define (led-command-handler request body)
137 (match (split-and-decode-uri-path (uri-path (request-uri request)))
139 (set-led-color/primitive! (string->number r)
142 (display "Set rgb\n")
143 (values '((content-type . (text/plain)))
146 (set-led-hsv! (string->number h )
149 (display "Set hsv\n")
150 (values '((content-type . (text/plain)))
152 (("fx" "kill") (kill-fx))
153 (("fx" "run" "hsv-fade") (run-fx hsv-fade))
156 (values '((content-type . (text/plain)))
159 (set-current-error-port (open-file "/tmp/led.log" "w0"))
161 (define (start-led-server)
162 (set! server-thread (make-thread (lambda () (run-server led-command-handler 'http '(#:port 58080))))))