summaryrefslogtreecommitdiff
path: root/guile/nanolight/fixture.scm
blob: b3f1395ee7b033b515db153a5e6f0bd22bd8841b (plain)
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
(define-module (nanolight fixture)
  #:use-module (oop goops)
  #:use-module (ice-9 threads)
  #:use-module (web client)
  #:use-module (web http)
  #:use-module (web uri)
  #:export (<fixture> <fixture-attribute>
             patch-fixture patch-many
             fixture-string fixture-address-string
             percent->dmxval msb lsb chan
             start-addr universe
             assign-attr!))

(use-modules (srfi srfi-1))

(define-class <fixture-attribute> (<object>)

  (name
    #:init-value 'unnamed-attribute
    #:init-keyword #:name
    #:getter name)

  (range
    #:init-value '()
    #:init-keyword #:range
    #:getter range)

  (type
    #:init-value 'continuous
    #:init-keyword #:type
    #:getter type)

  (home-value
    #:init-value 0
    #:init-keyword #:home-value
    #:getter home-value)

  (value-func
    #:init-value (lambda () 0)
    #:init-keyword #:value-func
    #:getter value-func
    #:setter set-value-func!)

  (translator
    #:init-value (lambda (universe start-addr value set-dmx) #f)
    #:init-keyword #:translator
    #:getter translator))


(define-class <fixture> (<object>)

  (attributes
    #:init-value '()
    #:init-keyword #:attributes
    #:getter get-attributes)

  (universe
    #:init-value #f
    #:init-keyword #:uni
    #:getter get-universe
    #:setter set-universe!)

  (start-addr
    #:init-value #f
    #:init-keyword #:sa
    #:getter get-start-addr
    #:setter set-start-addr!)

  (friendly-name
    #:init-value "Fixture"
    #:init-keyword #:friendly-name
    #:getter get-friendly-name
    #:setter set-friendly-name!))


;; Association list of fixtures
(define fixtures '())


(define (universe fixture-name)
  (get-universe (assq-ref fixtures fixture-name)))

(define (start-addr fixture-name)
  (get-start-addr (assq-ref fixtures fixture-name)))

(define (friendly-name fixture-name)
  (get-friendly-name (assq-ref fixtures fixture-name)))

(define (attributes fixture-name)
  (get-attributes (assq-ref fixtures fixture-name)))


(define (find-attribute-by-name attr-list attr-name)
  (find
    (lambda (a)
      (eq? (name a) attr-name))
    attr-list))


;; Place an attribute of the physical lighting fixture
;; under the control of the given function
(define (assign-attr! fix-name attr-name value-func)
  (set-value-func!
    (find-attribute-by-name
      (attributes fix-name)
      attr-name)
    value-func))


(define (fixture-address-string fix)
  (string-append
    (number->string (universe fix))
    "."
    (number->string (start-addr fix))))


(define (fixture-string fix)
  (string-append
    (friendly-name fix)
    " at "
    (fixture-address-string fix)))


(define (home-attribute attr)
  (let ((attr-home-value (home-value attr)))
    (set-value-func! attr (lambda () attr-home-value))))


(define (home-all-attributes fix)
  (for-each home-attribute
            (get-attributes fix)))


(define output #f)

(define (patch-fixture fixture-name attributes universe start-addr friendly-name)
  (let ((new-fixture (make <fixture>
                       #:attributes (copy-tree attributes)
                       #:uni universe
                       #:sa start-addr
                       #:friendly-name friendly-name)))
    (home-all-attributes new-fixture)
    (unless output
      (set! output (make-output)))
    (output 'add-fixture fixture-name new-fixture)
    new-fixture))


(define (round-dmx a)
  (min 255 (max 0 (round a))))


(define (percent->dmxval val)
  (round-dmx (/ (* 256 val) 100)))

(define (msb val)
  (round-dmx (/ val 256)))

(define (lsb val)
  (round-dmx (logand (round val) #b11111111)))

(define (chan channel start-addr)
  (- (+ channel start-addr) 1))


(define (bytevec->string bv)
  (string-join
    (map
      number->string
      (u8vector->list bv))
    ","))


(define (send-to-ola ola-uri ola-socket universe)
  (http-post
    ola-uri
    #:port ola-socket
    #:keep-alive? #t
    #:headers (acons 'content-type
                     (parse-header 'content-type
                                   "application/x-www-form-urlencoded")
                     '())
    #:body (string-append "u="
                          (number->string (car universe))
                          "&d="
                          (bytevec->string (cdr universe)))))

(define (make-output)
  (letrec* ((ola-uri (build-uri 'http
                                #:host "127.0.0.1"
                                #:port 9090
                                #:path "/set_dmx"))
            (ola-socket (open-socket-for-uri ola-uri)))

    (define (run-scanout)
      (let ((universes '()))

        ;; Helper function called by attribute translators
        ;; to set individual DMX values
        (define (set-dmx universe addr value)

          ;; Create DMX array for universe if it doesn't exist already
          (unless (assq universe universes)
            (set! universes (acons universe
                                   (make-u8vector 512 0)
                                   universes)))

          ;; Set the value in the DMX array
          (u8vector-set! (assq-ref universes universe)
                         (- addr 1)                   ; u8vector-set indexing starts from zero
                         (round-dmx value)))

        ;; Scan out all fixtures
        (for-each (lambda (fix-assoc-entry)

                    ;; Scan out one fixture
                    (let ((fix (cdr fix-assoc-entry)))
                      (for-each (lambda (attr)
                                  (let ((trans (translator attr)))
                                    (trans (get-universe fix)
                                           (get-start-addr fix)
                                           ((value-func attr))
                                           set-dmx)))
                                (get-attributes fix))))

          fixtures)

        ;; Send everything to OLA
        (for-each (lambda (a)
                    (send-to-ola ola-uri
                                 ola-socket
                                 a))
          universes))

      (yield)
      (run-scanout))

    ;; Start sending output
    (make-thread run-scanout)

    ;; Method functions
    (define (add-fixture fixture fixture-name)
      (set! fixtures (acons fixture fixture-name fixtures)))

    (lambda args
      (apply
        (case (car args)
          ((add-fixture) add-fixture)
          (else => (error "Invalid method")))
        (cdr args)))))


(define (symbol-append a b)
  (string->symbol
   (string-append
    (symbol->string a)
    (symbol->string b))))

(define (number->symbol n)
  (string->symbol
   (number->string n)))


(define-syntax patch-many
  (lambda (x)
    (syntax-case x ()

      ;; Base case: count = 0
      ((_ 0
          base-label
          offset
          base-addr
          fixture-class
          universe
          friendly-name)
       (and
        (number? (syntax->datum #'offset))
        (symbol? (syntax->datum #'base-label)))
       #'(if #f #f))      ; Return unspecified

      ;; General case: count is a number
      ((_ count
          base-label
          offset
          base-addr
          fixture-class
          universe
          friendly-name)

       (and
        (number? (syntax->datum #'count))
        (number? (syntax->datum #'offset))
        (symbol? (syntax->datum #'base-label)))

       (let ((make-id (lambda (first second)
                        (datum->syntax
                         x
                         (symbol-append (syntax->datum first)
                                        (number->symbol
                                         (syntax->datum second))))))
             (minus-one (lambda (num)
                           (- (syntax->datum num) 1)))

             (plus-one (lambda (num)
                          (+ (syntax->datum num) 1))))

         (with-syntax ((fixture-name (make-id #'base-label #'offset))
                       (next-count (minus-one #'count))
                       (next-offset (plus-one #'offset)))
           #'(begin
               (define fixture-name 'fixture-name)
               (patch-many next-count
                           base-label
                           next-offset
                           base-addr
                           fixture-class
                           universe
                           friendly-name))))))))