Module: CheekyDreams

Included in:
Effects::Effect, Light
Defined in:
lib/cheeky-dreams.rb

Defined Under Namespace

Modules: Dev, Effects Classes: AuditEvent, CompositeAuditor, ForwardingAuditor, StdIOAuditor, SuppressDuplicatesAuditor

Constant Summary collapse

COLOURS =
{ 
  :off => [0, 0, 0],
  :red => [255, 0, 0],
  :green => [0, 255, 0],
  :blue => [0, 0, 255],
  :yellow => [255,255,0],
  :aqua => [0,255,255],
  :purple => [255,0,255],
  :grey => [192,192,192],
  :white => [255,255,255]
}

Instance Method Summary collapse

Instance Method Details

#ansi_driverObject



126
127
128
129
130
131
132
133
134
# File 'lib/cheeky-dreams.rb', line 126

def ansi_driver
  require 'rainbow'
  Class.new do
    def go rgb
      print "     ".background(rgb)
      print "\r"
    end
  end.new
end

#audit_to(*auditors) ⇒ Object



100
101
102
# File 'lib/cheeky-dreams.rb', line 100

def audit_to *auditors
  CompositeAuditor.new *auditors
end

#crazy(freq = 10, new_effect_freq = 10) ⇒ Object



185
186
187
# File 'lib/cheeky-dreams.rb', line 185

def crazy freq = 10, new_effect_freq = 10
	Effects::Crazy.new(freq, new_effect_freq)
end

#cycle(colours, freq = 1) ⇒ Object



156
157
158
# File 'lib/cheeky-dreams.rb', line 156

def cycle colours, freq = 1
  Effects::Cycle.new(colours, freq)
end

#dev_nullObject



118
119
120
# File 'lib/cheeky-dreams.rb', line 118

def dev_null
  Dev::Null.new
end

#fade(from, to, steps = 10, freq = 1) ⇒ Object



160
161
162
# File 'lib/cheeky-dreams.rb', line 160

def fade from, to, steps = 10, freq = 1
  Effects::Fade.new from, to, steps, freq
end

#fade_to(to, steps = 10, freq = 1) ⇒ Object



164
165
166
# File 'lib/cheeky-dreams.rb', line 164

def fade_to to, steps = 10, freq = 1
  Effects::FadeTo.new to, steps, freq
end

#find_dream_cheeky_usb_deviceObject



136
137
138
139
140
141
142
143
144
145
146
# File 'lib/cheeky-dreams.rb', line 136

def find_dream_cheeky_usb_device
  reds = Dir.glob('/sys/devices/**/red')
  case reds.length
    when 0
      raise "Failed to find any dream cheeky devices"
    when 1
      Dev::DreamCheeky.new(File.dirname(reds.first))
    else
      reds.collect {|red| Dev::DreamCheeky.new(File.dirname(red)) }
  end
end

#forward(rules) ⇒ Object



96
97
98
# File 'lib/cheeky-dreams.rb', line 96

def forward rules
  ForwardingAuditor.new rules
end

#func(&block) ⇒ Object



168
169
170
# File 'lib/cheeky-dreams.rb', line 168

def func &block
  Effects::Func.new &block
end

#light_show(*effects) ⇒ Object



189
190
191
# File 'lib/cheeky-dreams.rb', line 189

def light_show *effects
  Effects::LightShow.new effects
end

#max(a, b) ⇒ Object



181
182
183
# File 'lib/cheeky-dreams.rb', line 181

def max a, b
  a > b ? a : b
end

#offObject



148
149
150
# File 'lib/cheeky-dreams.rb', line 148

def off
  solid :off
end

#position_between(a, b, ratio) ⇒ Object



31
32
33
34
# File 'lib/cheeky-dreams.rb', line 31

def position_between a, b, ratio
  return b if ratio >= 1.0
  (((b - a) * ratio) + a).floor
end

#rgb(*rgb_args) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cheeky-dreams.rb', line 36

def rgb *rgb_args
  raise 'Cannot give rgb for nil!' unless rgb_args
  args = rgb_args.flatten
  if args.length == 1 && args[0].is_a?(Symbol)
    raise "Unknown colour '#{args[0]}'" unless COLOURS.has_key?(args[0])
    COLOURS[args[0]]
  elsif (args.length == 3 && args.all? { |c| c.is_a? Numeric })
    r, g, b = args[0].floor, args[1].floor, args[2].floor
    [r, g, b].each { |c| raise "Invalid rgb value #{r}, #{g}, #{b}" if c < 0 || c > 255}
    [r, g, b]
  else 
    raise "Invalid rgb #{args}"
  end
end

#rgb_between(a, b, ratio) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/cheeky-dreams.rb', line 18

def rgb_between a, b, ratio
  [
    position_between(a[0], b[0], ratio),
    position_between(a[1], b[1], ratio),
    position_between(a[2], b[2], ratio),
    ]
end

#sleep_until(time) ⇒ Object



26
27
28
29
# File 'lib/cheeky-dreams.rb', line 26

def sleep_until time
  zzz_time = time - Time.now
  sleep(zzz_time) if zzz_time > 0
end

#solid(colour) ⇒ Object



152
153
154
# File 'lib/cheeky-dreams.rb', line 152

def solid colour
  Effects::Solid.new(colour)
end

#stdio_audit(out = STDOUT, err = STDERR) ⇒ Object



114
115
116
# File 'lib/cheeky-dreams.rb', line 114

def stdio_audit out = STDOUT, err = STDERR
  StdIOAuditor.new(out, err)
end

#stdout_driverObject



122
123
124
# File 'lib/cheeky-dreams.rb', line 122

def stdout_driver
	Dev::IO.new
end

#suppress_duplicates(auditor) ⇒ Object



92
93
94
# File 'lib/cheeky-dreams.rb', line 92

def suppress_duplicates auditor
  SuppressDuplicatesAuditor.new auditor
end

#throb(freq, from, to) ⇒ Object



172
173
174
# File 'lib/cheeky-dreams.rb', line 172

def throb freq, from, to
  Effects::Throb.new freq, from, to
end

#throbbing(colour, freq = 10) ⇒ Object



176
177
178
179
# File 'lib/cheeky-dreams.rb', line 176

def throbbing colour, freq = 10
  rgb_colour = rgb(colour)
  Effects::Throb.new freq, rgb_colour, [max(rgb_colour[0] - 200, 0), max(rgb_colour[1] - 200, 0), max(rgb_colour[2] - 200, 0)]
end