SHOW:
|
|
- or go back to the newest paste.
1 | os.loadAPI('apis.lua') | |
2 | - | |
2 | + | |
3 | args = { ... } | |
4 | if #args < 1 then | |
5 | -- error('radio <direction of turtle in relationship to radio>') | |
6 | direction = 'down' | |
7 | else | |
8 | direction = args[1] | |
9 | end | |
10 | - | |
10 | + | |
11 | radio = Peripheral.wrap('openblocks_radio') | |
12 | rs.setAnalogOutput('top', 0) | |
13 | - | |
13 | + | |
14 | local monitor = UI.Device({ | |
15 | deviceType = 'monitor', | |
16 | textScale = 0.5, | |
17 | }) | |
18 | - | |
18 | + | |
19 | UI.setDefaultDevice(monitor) | |
20 | - | |
20 | + | |
21 | page = UI.Page({ | |
22 | - | volume = 1, |
22 | + | volume = 6, |
23 | stationBackground = UI.Window({ | |
24 | y = 2, | |
25 | - | width = monitor.width, |
25 | + | |
26 | width = monitor.width - 14, | |
27 | height = 3, | |
28 | - | y = 6, |
28 | + | backgroundColor = colors.brown, |
29 | }), | |
30 | stationName = UI.Text({ | |
31 | y = 3, | |
32 | - | text = ' > ', |
32 | + | x = 3, |
33 | width = monitor.width-16, | |
34 | backgroundColor = colors.brown, | |
35 | - | y = 6, |
35 | + | |
36 | seek = UI.Button({ | |
37 | y = 7, | |
38 | x = 13, | |
39 | height = 3, | |
40 | event = 'seek', | |
41 | text = ' >> ', | |
42 | - | y = 8, |
42 | + | |
43 | - | x = 30, |
43 | + | |
44 | y = 7, | |
45 | x = 2, | |
46 | - | text = ' + ', |
46 | + | |
47 | event = 'play', | |
48 | text = '> / ll', | |
49 | - | y = 8, |
49 | + | |
50 | - | x = 24, |
50 | + | |
51 | y = 7, | |
52 | x = monitor.width - 15, | |
53 | - | text = ' - ', |
53 | + | |
54 | height = 3, | |
55 | event = 'louder', | |
56 | text = '+', | |
57 | - | x = 24, |
57 | + | |
58 | quiet = UI.Button({ | |
59 | y = 7, | |
60 | x = monitor.width - 20, | |
61 | event = 'quiet', | |
62 | width = 3, | |
63 | - | x = 25, |
63 | + | |
64 | text = '-', | |
65 | }), | |
66 | volumeDisplay = UI.Text({ | |
67 | y = 3, | |
68 | x = monitor.width - 9, | |
69 | - | x = 26, |
69 | + | width = 4, |
70 | }), | |
71 | volume1 = UI.Window({ | |
72 | y = monitor.height - 1, | |
73 | x = monitor.width - 8, | |
74 | height = 1, | |
75 | - | x = 27, |
75 | + | |
76 | color = colors.white | |
77 | - | width = 1 |
77 | + | |
78 | volume2 = UI.Window({ | |
79 | y = monitor.height - 2, | |
80 | - | |
80 | + | x = monitor.width - 7, |
81 | height = 2, | |
82 | width = 1, | |
83 | - | page.volume4, page.volume5 |
83 | + | color = colors.white |
84 | }), | |
85 | - | |
85 | + | |
86 | y = monitor.height - 3, | |
87 | x = monitor.width - 6, | |
88 | height = 3, | |
89 | width = 1, | |
90 | color = colors.yellow | |
91 | }), | |
92 | volume4 = UI.Window({ | |
93 | y = monitor.height - 4, | |
94 | x = monitor.width - 5, | |
95 | height = 4, | |
96 | width = 1, | |
97 | color = colors.yellow, | |
98 | }), | |
99 | volume5 = UI.Window({ | |
100 | y = monitor.height - 5, | |
101 | x = monitor.width - 4, | |
102 | height = 5, | |
103 | width = 1, | |
104 | color = colors.orange, | |
105 | }), | |
106 | volume6 = UI.Window({ | |
107 | y = monitor.height - 6, | |
108 | x = monitor.width - 3, | |
109 | - | |
109 | + | height = 6, |
110 | - | function page:setVolume(volume) |
110 | + | |
111 | - | volume = math.min(volume, #self.volumeControls) |
111 | + | color = colors.orange, |
112 | }), | |
113 | volume7 = UI.Window({ | |
114 | - | print(self.volume) |
114 | + | |
115 | x = monitor.width - 2, | |
116 | - | self.volumeControls[i].backgroundColor = colors.red |
116 | + | height = 7, |
117 | width = 1, | |
118 | color = colors.red, | |
119 | }), | |
120 | volume8 = UI.Window({ | |
121 | y = monitor.height - 8, | |
122 | x = monitor.width - 1, | |
123 | height = 8, | |
124 | - | rs.setAnalogOutput('top', volume) |
124 | + | |
125 | color = colors.red, | |
126 | - | |
126 | + | |
127 | }) | |
128 | ||
129 | page.volumeControls = { | |
130 | page.volume1, page.volume2, | |
131 | page.volume3, page.volume4, | |
132 | page.volume5, page.volume6, | |
133 | page.volume7, page.volume8, | |
134 | } | |
135 | ||
136 | function page:eventHandler(event) | |
137 | if event.type == 'play' then | |
138 | if self.playing then | |
139 | - | |
139 | + | |
140 | else | |
141 | if not radio.getStackInSlot(1) then | |
142 | self:seek() | |
143 | end | |
144 | self:play(true) | |
145 | end | |
146 | - | self.stationName.value = 'Paused' |
146 | + | |
147 | self:seek() | |
148 | self:play(true) | |
149 | elseif event.type == 'louder' then | |
150 | if self.playing then | |
151 | - | |
151 | + | |
152 | end | |
153 | elseif event.type == 'quiet' then | |
154 | if self.playing then | |
155 | self:setVolume(self.volume - 1) | |
156 | - | |
156 | + | |
157 | end | |
158 | end | |
159 | ||
160 | function page:setVolume(volume, displayOnly) | |
161 | volume = math.min(volume, 15) | |
162 | volume = math.max(volume, 1) | |
163 | self.volume = volume | |
164 | - | |
164 | + | volume = math.ceil(volume / 2) |
165 | ||
166 | - | |
166 | + | |
167 | self.volumeControls[i].backgroundColor = | |
168 | self.volumeControls[i].color | |
169 | - | |
169 | + | |
170 | - | Event.pullEvents() |
170 | + | |
171 | self.volumeControls[i].backgroundColor = colors.black | |
172 | end | |
173 | for i = 1, #self.volumeControls do | |
174 | self.volumeControls[i]:clear() | |
175 | end | |
176 | local percent = math.ceil(self.volume / 15 * 100) | |
177 | self.volumeDisplay.value = percent .. '%' | |
178 | self.volumeDisplay:draw() | |
179 | if not displayOnly then | |
180 | rs.setAnalogOutput('top', self.volume) | |
181 | end | |
182 | end | |
183 | ||
184 | function page:seek() | |
185 | local slot = TL2.selectOpenSlot() or 0 | |
186 | repeat | |
187 | slot = slot + 1 | |
188 | if (slot > 16) then | |
189 | slot = 1 | |
190 | end | |
191 | until turtle.getItemCount(slot) >= 1 | |
192 | radio.pushItem(direction, 1, 1) | |
193 | radio.pullItem(direction, slot, 1) | |
194 | self:updateStationName() | |
195 | end | |
196 | ||
197 | function page:play(onOff) | |
198 | self.playing = onOff | |
199 | if self.playing then | |
200 | self:setVolume(self.volume) | |
201 | self:updateStationName() | |
202 | else | |
203 | -- self.stationName.value = 'Paused' | |
204 | self.stationName:draw() | |
205 | rs.setAnalogOutput('top', 0) | |
206 | end | |
207 | end | |
208 | ||
209 | function page.stationName:draw() | |
210 | self:clear() | |
211 | self:centeredWrite(1, self.value) | |
212 | end | |
213 | ||
214 | function page:updateStationName() | |
215 | local stack = radio.getStackInSlot(1) | |
216 | ||
217 | if stack then | |
218 | self.stationName.value = stack.name | |
219 | self.stationName:draw() | |
220 | end | |
221 | end | |
222 | ||
223 | Event.addHandler('heartbeat', function() | |
224 | if not page.playing then | |
225 | if page.stationName.value == '' then | |
226 | page:updateStationName() | |
227 | else | |
228 | page.stationName.value = '' | |
229 | page.stationName:draw() | |
230 | end | |
231 | end | |
232 | end) | |
233 | ||
234 | UI.pager:setPage(page) | |
235 | ||
236 | page:updateStationName() | |
237 | page:play(false) | |
238 | page:setVolume(page.volume, true) | |
239 | ||
240 | Logger.filter('event') | |
241 | ||
242 | Event.heartbeat(1) |