Class: OverLayer
- Inherits:
-
Object
- Object
- OverLayer
- Defined in:
- lib/overlayer.rb
Instance Attribute Summary collapse
-
#all_sequences ⇒ Object
Returns the value of attribute all_sequences.
Class Method Summary collapse
- .new_raw(ruby_hash) ⇒ Object
- .translate_string_to_seconds(s) ⇒ Object
- .translate_yaml(raw_yaml) ⇒ Object
Instance Method Summary collapse
- #blank!(seconds) ⇒ Object
- #blank? ⇒ Boolean
- #check_reload_yaml ⇒ Object
- #continue_until_past_all(continue_forever) ⇒ Object
- #cur_english_time ⇒ Object
-
#cur_time ⇒ Object
returns seconds it’s at currently…
-
#discover_state(type, cur_time) ⇒ Object
returns [start, end, active|:done].
- #display_change(change) ⇒ Object
-
#get_current_state ⇒ Object
returns [true, false, next_moment_of_importance|:done] ( true, false for if it should be currently muted, blanked ).
-
#initialize(filename) ⇒ OverLayer
constructor
A new instance of OverLayer.
- #keyboard_input(char) ⇒ Object
- #kill_thread! ⇒ Object
- #mute! ⇒ Object
- #muted? ⇒ Boolean
- #pretty_sequences ⇒ Object
- #reload_yaml! ⇒ Object
-
#set_seconds(seconds) ⇒ Object
sets it to a new set of seconds…
- #set_states! ⇒ Object
- #signal_change ⇒ Object
-
#start_thread(continue_forever = false) ⇒ Object
we have a single scheduler thread, that is notified when the time may have changed like def restart new_time @current_time = xxx broadcast # things have changed end.
- #status ⇒ Object
- #timestamp_changed(to_this_exact_string_might_be_nil, delta) ⇒ Object
- #translate_time_to_human_readable(seconds) ⇒ Object
- #unblank! ⇒ Object
- #unmute! ⇒ Object
Constructor Details
#initialize(filename) ⇒ OverLayer
Returns a new instance of OverLayer.
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/overlayer.rb', line 84 def initialize filename @filename = filename @am_muted = false @am_blanked = false @mutex = Mutex.new @cv = ConditionVariable.new @file_mtime = nil check_reload_yaml @start_time = Time.now_f # assume they want to start immediately... end |
Instance Attribute Details
#all_sequences ⇒ Object
Returns the value of attribute all_sequences.
57 58 59 |
# File 'lib/overlayer.rb', line 57 def all_sequences @all_sequences end |
Class Method Details
.new_raw(ruby_hash) ⇒ Object
79 80 81 82 |
# File 'lib/overlayer.rb', line 79 def self.new_raw ruby_hash File.write 'temp.yml', YAML.dump(ruby_hash) OverLayer.new('temp.yml') end |
.translate_string_to_seconds(s) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/overlayer.rb', line 151 def self.translate_string_to_seconds s # might actually already be a float, or int, depending on the yaml # int for 8 => 9 and also for 1:09 => 1:10 if s.is_a? Numeric return s.to_f end # s is like 1:01:02.0 total = 0.0 seconds = s.split(":")[-1] total += seconds.to_f minutes = s.split(":")[-2] || "0" total += 60 * minutes.to_i hours = s.split(":")[-3] || "0" total += 60* 60 * hours.to_i total end |
.translate_yaml(raw_yaml) ⇒ Object
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 |
# File 'lib/overlayer.rb', line 95 def self.translate_yaml raw_yaml begin all = YAML.load(raw_yaml) || {} rescue NoMethodError p 'appears your file has a syntax error in it--perhaps missing quotation marks' return end # now it's like {:mutes => {"1:02.0" => "1:3.0"}} # translate to floats like 62.0 => 63.0 for type in [:mutes, :blank_outs] maps = all[type] || all[type.to_s] || {} new = {} maps.each{|start,endy| # both are like 1:02.0 start2 = translate_string_to_seconds(start) if start endy2 = translate_string_to_seconds(endy) if endy if start2 == 0 || endy2 == 0 || start == nil || endy == nil p 'warning--possible error in the scene descriptions file some line not parsed! (NB if you want one to start at time 0 please use 0.0001)', start, endy unless $AM_IN_UNIT_TEST # drop this line into the bitbucket... next end if start2 == endy2 || endy2 < start2 p 'warning--found a line that had poor interval', start, endy, type unless defined?($AM_IN_UNIT_TEST) next end if(endy2 > 60*60*3) p 'warning--found setting past 3 hours [?]', start, endy, type unless defined?($AM_IN_UNIT_TEST) end new[start2] = endy2 } all.delete(type.to_s) # cleanup all[type] = new.sort end all end |
Instance Method Details
#blank!(seconds) ⇒ Object
37 38 39 40 |
# File 'lib/overlayer.rb', line 37 def blank! seconds @am_blanked = true Blanker.blank_full_screen! seconds end |
#blank? ⇒ Boolean
21 22 23 |
# File 'lib/overlayer.rb', line 21 def blank? @am_blanked end |
#check_reload_yaml ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/overlayer.rb', line 47 def check_reload_yaml current_mtime = File.stat(@filename).mtime if @file_mtime != current_mtime reload_yaml! @file_mtime = current_mtime else #p 'same mtime:', @file_mtime if $DEBUG && $VERBOSE end end |
#continue_until_past_all(continue_forever) ⇒ Object
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 |
# File 'lib/overlayer.rb', line 310 def continue_until_past_all continue_forever if RUBY_VERSION < '1.9.2' raise 'need 1.9.2+ for MRI for the mutex stuff' unless RUBY_PLATFORM =~ /java/ end @mutex.synchronize { loop { muted, blanked, next_point = get_current_state if next_point == :done if continue_forever time_till_next_mute_starts = 1_000_000 else #set_states! # for the unit tests' sake return end else time_till_next_mute_starts = next_point - cur_time end # pps 'sleeping until next action (%s) begins in %fs (%f) %f' % [next_point, time_till_next_mute_starts, Time.now_f, cur_time] if $VERBOSE @cv.wait(@mutex, time_till_next_mute_starts) if time_till_next_mute_starts > 0 set_states! } } end |
#cur_english_time ⇒ Object
174 175 176 |
# File 'lib/overlayer.rb', line 174 def cur_english_time translate_time_to_human_readable(cur_time) end |
#cur_time ⇒ Object
returns seconds it’s at currently…
170 171 172 |
# File 'lib/overlayer.rb', line 170 def cur_time Time.now_f - @start_time end |
#discover_state(type, cur_time) ⇒ Object
returns [start, end, active|:done]
262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/overlayer.rb', line 262 def discover_state type, cur_time for start, endy in @all_sequences[type] if cur_time < endy # first one that we haven't passed the *end* of yet if(cur_time >= start) return [start, endy, true] else return [start, endy, false] end end end return [nil, nil, :done] end |
#display_change(change) ⇒ Object
337 338 339 340 341 342 |
# File 'lib/overlayer.rb', line 337 def display_change change puts '' unless defined?($AM_IN_UNIT_TEST) if $VERBOSE puts change + ' at ' + cur_english_time end end |
#get_current_state ⇒ Object
returns [true, false, next_moment_of_importance|:done] ( true, false for if it should be currently muted, blanked )
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 |
# File 'lib/overlayer.rb', line 280 def get_current_state all = [] time = cur_time for type in [:mutes, :blank_outs] do all << discover_state(type, time) end output = [] # all is [[start, end, active]...] or [:done, :done] earliest_moment = 1_000_000 all.each{|start, endy, active| if active == :done output << false next else output << active end if active earliest_moment = [earliest_moment, endy].min else earliest_moment = [earliest_moment, start].min end } if earliest_moment == 1_000_000 output << :done else output << earliest_moment end output end |
#keyboard_input(char) ⇒ Object
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 |
# File 'lib/overlayer.rb', line 195 def keyboard_input char delta = case char when 'h' then 60*60 when 'H' then -60*60 when 'm' then 60 when 'M' then -60 when 's' then 1 when 'S' then -1 when 't' then 0.1 when 'T' then -0.1 when 'v' then $VERBOSE = !$VERBOSE p 'set verbose to ', $VERBOSE return when 'd' $DEBUG = !$DEBUG p 'set debug to', $DEBUG return when ' ' then puts 'timestamp:' + cur_english_time return when 'r' then reload_yaml! puts return else nil end if delta set_seconds(cur_time + delta) else puts 'invalid char: [' + char + ']' end end |
#kill_thread! ⇒ Object
257 258 259 |
# File 'lib/overlayer.rb', line 257 def kill_thread! @thread.kill end |
#mute! ⇒ Object
25 26 27 28 29 |
# File 'lib/overlayer.rb', line 25 def mute! @am_muted = true puts 'muting!' if $VERBOSE Muter.mute! unless defined?($AM_IN_UNIT_TEST) end |
#muted? ⇒ Boolean
17 18 19 |
# File 'lib/overlayer.rb', line 17 def muted? @am_muted end |
#pretty_sequences ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/overlayer.rb', line 65 def pretty_sequences new_sequences = {} @all_sequences.each{|type, values| if values.is_a? Array new_sequences[type] = values.map{|s, f| [translate_time_to_human_readable(s), translate_time_to_human_readable(f)] } else new_sequences[type] = values end } new_sequences end |
#reload_yaml! ⇒ Object
59 60 61 62 63 |
# File 'lib/overlayer.rb', line 59 def reload_yaml! @all_sequences = OverLayer.translate_yaml(File.read(@filename)) puts '(re) loaded mute sequences from ' + File.basename(@filename) + ' as', pretty_sequences.pretty_inspect, "" unless defined?($AM_IN_UNIT_TEST) signal_change end |
#set_seconds(seconds) ⇒ Object
sets it to a new set of seconds…
230 231 232 233 234 235 236 |
# File 'lib/overlayer.rb', line 230 def set_seconds seconds seconds = [seconds, 0].max @mutex.synchronize { @start_time = Time.now_f - seconds } signal_change end |
#set_states! ⇒ Object
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 |
# File 'lib/overlayer.rb', line 344 def set_states! should_be_muted, should_be_blank, next_point = get_current_state if should_be_muted && !muted? mute! display_change 'muted' end if !should_be_muted && muted? unmute! display_change 'unmuted' end if should_be_blank && !blank? blank! "%.02f" % (next_point - cur_time) display_change 'blanked' end if !should_be_blank && blank? unblank! display_change 'unblanked' end end |
#signal_change ⇒ Object
238 239 240 241 242 243 244 |
# File 'lib/overlayer.rb', line 238 def signal_change @mutex.synchronize { # tell the driver thread to wake up and re-check state. # We're not super thread friendly but good enough for having two contact each other... @cv.signal } end |
#start_thread(continue_forever = false) ⇒ Object
we have a single scheduler thread, that is notified when the time may have changed like def restart new_time
@current_time = xxx
broadcast # things have changed
end
253 254 255 |
# File 'lib/overlayer.rb', line 253 def start_thread continue_forever = false @thread = Thread.new { continue_until_past_all continue_forever } end |
#status ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/overlayer.rb', line 178 def status time = "Current time: " + cur_english_time begin mute, blank, next_sig = get_current_state if next_sig == :done state = " no more actions after this point..." else state = " next action will be at #{translate_time_to_human_readable next_sig}s " end if blank? or muted? state += "(" + [muted? ? "muted" : nil, blank? ? "blanked" : nil ].compact.join(' ') + ") " end end check_reload_yaml time + state + "(r,d,v, or q to quit): " end |
#timestamp_changed(to_this_exact_string_might_be_nil, delta) ⇒ Object
147 148 149 |
# File 'lib/overlayer.rb', line 147 def to_this_exact_string_might_be_nil, delta set_seconds OverLayer.translate_string_to_seconds(to_this_exact_string_might_be_nil) + delta if to_this_exact_string_might_be_nil end |
#translate_time_to_human_readable(seconds) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/overlayer.rb', line 133 def translate_time_to_human_readable seconds # 3600 => "1:00:00" out = '' hours = seconds.to_i / 3600 out << "%d" % hours out << ":" seconds = seconds - hours*3600 minutes = seconds.to_i / 60 out << "%02d" % minutes seconds = seconds - minutes * 60 out << ":" out << "%04.1f" % seconds end |
#unblank! ⇒ Object
42 43 44 45 |
# File 'lib/overlayer.rb', line 42 def unblank! @am_blanked = false Blanker.unblank_full_screen! end |