-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathled_string.rb
More file actions
200 lines (163 loc) · 3.54 KB
/
led_string.rb
File metadata and controls
200 lines (163 loc) · 3.54 KB
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
require 'rubyserial'
require_relative './gamma.rb'
class LedString
TYPES = [
RGB = :RGB,
RGBW = :RGBW
]
attr_reader :led_count,
:tty,
:baudrate,
:serial,
:leds,
:gamma
attr_accessor :verbose, :gamma_correction, :color_adjust
DEFAULT_OPTIONS = {
led_count: 30,
type: RGB,
tty: "/dev/tty.SLAB_USBtoUART",
baudrate: 115200,
verbose: false,
gamma_correction: true,
gamma: 2.2,
leds: [],
silent_start: false
}
def initialize options={}
options = DEFAULT_OPTIONS.merge options
@led_count = options[:led_count]
@tty = options[:tty]
@baudrate = options[:baudrate]
@pattern = options[:pattern]
@repeat = options[:repeat]
@verbose = options[:verbose]
@gamma_correction = options[:gamma_correction]
self.gamma = options[:gamma]
@type = options[:type]
@serial = Serial.new @tty, @baudrate
# initialize the leds
self.leds = options[:leds]
# finally display the string
sync! unless options[:silent_start]
end
# if extras are supplied, ignore them, if not enough are supplied, fill with zeros
def leds= leds
@leds = leds.clone.fill(blank_led, leds.length..@led_count-1).first(@led_count)
end
def gamma= gamma
@gamma = gamma
@gamma_lookup = Gamma.generate_table(256, @gamma).map{|x| (x * 255).round}
end
def set_leds leds
self.leds = leds
end
def set_leds! leds
set_leds leds
sync!
end
def set_led n, led
@leds[n] = led
end
def set_led! n, led
set_led n, led
sync_single! n
end
def clear!
clear
sync!
end
def clear
self.leds= []
end
#### SERIAL INTERFACE FUNCTIONS ###
# sync specific led to string
# format is iirrggbb; (index, red, green, blue; 2 hex digits each)
def sync_single index
led = @leds[index]
tmp = [index] + led.map{|v|_gamma(v)};
tmp = tmp.map{|byte| "00#{byte.to_s(16)}"[-2..-1]}
serial_write "#{tmp.join};"
end
def sync_single! index
sync_single index
render!
end
# sync all leds to string
def sync
@leds.each_with_index {|_, index| sync_single index}
end
def sync!
sync
render!
end
def load!
load
render!
end
# tell the led string to render
def render!
serial_write "r;"
end
# spit out the state as read from the LED string
def dump
serial_read # clear out buffer
chunks = []
serial_write "d;"
sleep 1
extract_dump serial_read
end
def extract_dump dump
regex = /([0-9a-fA-F]+):([0-9a-fA-F]+);/
lines = dump.split("\n").select{|l| l =~ regex}
lines.map do |line|
match = line.match regex
if match
match[2].to_i(16)
else
0
end
end.map do |i|
int_to_arr i
end
end
def int_to_arr i
arr = [
(i & 0xFF0000) >> 16,
(i & 0xFF00) >> 8,
(i & 0xFF)
]
arr << ((i & 0xFF000000) >> 24) if @type == RGBW
arr
end
def save
serial_write "s;"
end
def load
serial_write "l;"
end
def serial_write s
puts "serial_write: #{s}" if @verbose
@serial.write s
nil
end
def serial_read
chunks = []
chunk = nil
until (chunk = @serial.read(512)).empty?
chunks << chunk
end
chunks.join ""
end
private
def _gamma n
return n unless @gamma_correction
@gamma_lookup[n]
end
def blank_led
if @type == RGBW
[0,0,0,0]
else
[0,0,0]
end
end
end