summaryrefslogtreecommitdiff
path: root/guile/nanolight/state.scm
blob: f6f01933d19b283ced000607882fa546edfdc72b (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
(define-module (nanolight state)
  #:use-module (oop goops)
  #:export (print-state define-state exec-state!
             merge-states merge-rule-htp merge-rule-ltp
             merge-htp merge-ltp
             int flash pan tilt
             fixture attribute value-func))

(use-modules (nanolight fixture))
(use-modules (srfi srfi-1))


(define-class <state-assignment> (<object>)

  (fixture
    #:init-value #f
    #:init-keyword #:fixture
    #:getter fixture)

  (attribute
    #:init-value #f
    #:init-keyword #:attribute
    #:getter attribute)

  (value-func
    #:init-value #f
    #:init-keyword #:value-func
    #:getter value-func))


(define (state-assignment-string a)
  (string-append
    (fixture-string (fixture a))
    " / "
    (symbol->string (attribute a))
    " ---> "
    (number->string ((value-func a)))))


; Return #t if the two state assignments target the same parameter
; (i.e. in the same fixture)
(define-method (same-attr (a <state-assignment>) (b <state-assignment>))
  (and
    (eq? (fixture a) (fixture b))
    (eq? (attribute a) (attribute b))))


; Convenience functions
(define merge-htp
  (lambda a
    (apply merge-states merge-rule-htp a)))


(define merge-ltp
  (lambda a
    (apply merge-states merge-rule-ltp a)))


; Highest takes precedence: for intensity (only), create a new function
; which returns the highest value from the two inputs
; Otherwise, revert to LTP
(define (merge-rule-htp a b)
  (let (
         (funca (value-func a))
         (funcb (value-func b)))
    (if (eq? (attribute a) 'intensity)
      (lambda () (max (funca) (funcb)))
      funcb)))


; Latest takes precedence: just take whichever one comes last
(define (merge-rule-ltp a b)
  (value-func b))


; Merge states according to rule 'merge-rule'
(define (merge-states merge-rule . list-of-states)
  (fold
    (lambda (assignment-to-add combined-state)
      (let ((assignment-in-state (find
                                   (lambda (a)
                                     (same-attr assignment-to-add a))
                                   combined-state)))
        (cons (if assignment-in-state
                (make <state-assignment>
                  #:fixture (fixture assignment-to-add)
                  #:attribute (attribute assignment-to-add)
                  #:value-func (merge-rule
                                 assignment-in-state
                                 assignment-to-add))
                assignment-to-add)
          (delq assignment-in-state combined-state))))
    '() (apply append list-of-states)))


(define (find-attribute fix attr)
  (find (lambda (a)
          (eq? (name a) attr))
    (attributes fix)))


; Execute the state, i.e. apply it to the physical lighting rig
(define (exec-state! state)
  (for-each
    (lambda (a)
      (assign-attr!
        (fixture a)
        (attribute a)
        (value-func a)))
    state))


(define (sort-by-dmx-addr state)
  (stable-sort state (lambda (a b)
                       (or
                         (<
                           (get-fixture-universe (fixture a))
                           (get-fixture-universe (fixture b)))
                         (and
                           (eq?
                             (get-fixture-universe (fixture a))
                             (get-fixture-universe (fixture b)))
                           (<
                             (get-fixture-start-addr (fixture a))
                             (get-fixture-start-addr (fixture b))))))))


(define (print-state st)
  (for-each
    (lambda (a)
      (display (state-assignment-string a))
      (newline))
    (sort-by-dmx-addr st)))


(define-syntax define-state
  (syntax-rules ()

    [(_) #f]

    [(_ n) (define n '())]

    [(_ n st ...) (define n
                    (merge-states merge-rule-htp st ...))]))


; Helper functions

(define (hirestime)
  (let ((a (gettimeofday)))
    (+
      (car a)
      (/
        (cdr a)
        1000000))))

(define pi (* 2 (acos 0)))

(define (square-wave hz)
  (if (> (sin (* 2 pi hz (hirestime))) 0) 100 0))

(define (static-value attr value fix)
  (list (make <state-assignment>
          #:fixture fix
          #:attribute attr
          #:value-func (lambda () value))))


; Useful source functions

(define (int value fix)
  (static-value 'intensity value fix))

(define (pan value fix)
  (static-value 'pan value fix))

(define (tilt value fix)
  (static-value 'tilt value fix))

(define (flash hz fix)
  (list (make <state-assignment>
          #:fixture fix
          #:attribute 'intensity
          #:value-func (lambda ()
                         (square-wave hz)))))