aboutsummaryrefslogtreecommitdiff
path: root/guile/starlet/utils.scm
blob: 1506553304b6dcf570d88c4971b88cf22254cda5 (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
;;
;; starlet/utils.scm
;;
;; Copyright © 2020-2021 Thomas White <taw@bitwiz.org.uk>
;;
;; This file is part of Starlet.
;;
;; Starlet is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
;;
(define-module (starlet utils)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-8)
  #:use-module (ice-9 exceptions)
  #:use-module (ice-9 control)
  #:export (print-hash-table
            copy-hash-table
            in-range
            mean
            flatten-sublists
            more-than-one
            hirestime
            lsb
            msb
            ensure-number
            round-dmx
            scale-to-range
            scale-and-clamp-to-range
            percent->dmxval8
            percent->dmxval16
            comment
            hash-table-empty?
            lookup
            add-and-run-hook!
            cat-with-spaces
            next-item-in-list))


(define (print-hash-table ht)
  (hash-for-each (lambda (key value)
                   (display key)
                   (display " ---> ")
                   (display value)
                   (newline))
                 ht))

(define (copy-hash-table ht)
  (let ((new-ht (make-hash-table)))
    (hash-for-each (lambda (key value)
                     (hash-set! new-ht key value))
                   ht)
    new-ht))



(define (in-range a val1 val2)
  (or
   (and (>= a val1)
        (<= a val2))
   (and (>= a val2)
        (<= a val1))))


(define (mean vals)
  (/ (fold + 0 vals)
     (length vals)))


(define (flatten-sublists l)
  (fold
    (lambda (el prev)
      (if (list? el)
        (append (flatten-sublists el) prev)
        (cons el prev)))
    '() l))


(define (more-than-one a)
  (if (nil? a)
      #f
      (not (nil? (cdr a)))))


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


(define (msb val)
  (round-dmx (euclidean-quotient val 256)))

(define (lsb val)
  (round-dmx (euclidean-remainder val 256)))


(define (round-dmx a)
  (inexact->exact
   (min 255 (max 0 (round a)))))


(define (ensure-number value irritating)
  (unless (number? value)
    (raise-exception (make-exception
                       (make-exception-with-message "Value is not a number")
                       (make-exception-with-irritants irritating)))))


(define (percent->dmxval8 val)
  (round-dmx
   (scale-to-range val '(0 100) '(0 255))))


(define (percent->dmxval16 val)
  (scale-to-range val '(0 100) '(0 65535)))


(define (scale-to-range val orig-range dest-range)

  (define (range r)
    (- (cadr r) (car r)))

  (+ (car dest-range)
     (* (range dest-range)
        (/ (- val (car orig-range))
           (range orig-range)))))


(define (clamp-to-range val val1 val2)
  (let ((minval (min val1 val2))
        (maxval (max val1 val2)))
  (max minval
       (min val maxval))))


;; Like scale-to-range, but result is clamped within dest-range
(define (scale-and-clamp-to-range val orig-range dest-range)
  (clamp-to-range
    (scale-to-range val orig-range dest-range)
    (car dest-range)
    (cadr dest-range)))


(define-syntax comment
  (syntax-rules ()
    ((_ body ...)
     #f)))


(define (hash-table-empty? ht)
  (let/ec
    return
    (hash-for-each-handle
      (lambda (key)
        (return #f))
      ht)
    #t))


(define (lookup key dictionary)
  (cond
    ((nil? dictionary)
     #f)
    ((eq? key (caar dictionary))
     (cadr (car dictionary)))
    (else
      (lookup key (cdr dictionary)))))


(define (add-and-run-hook! hook proc . initial-args)
  (add-hook! hook proc)
  (apply proc initial-args))


(define (cat-with-spaces lst)
  (reduce
    (lambda (b a)
      (string-append a " " b))
    "" lst))


(define (next-item-in-list the-list cval)
  (let ((sl (memq cval the-list)))
    (if (nil? (cdr sl))
      (first the-list)
      (second sl))))