Commit b5492828 authored by mad's avatar mad
Browse files

Initialer Kommit.

parents
# coding: UTF-8
class Bar < Lamp
def initialize(*args)
super(*args)
end
private
def set_color
@color_payload = [
(color_correction(@red)*0.5).to_i,
(color_correction(@green)*0.5).to_i,
(color_correction(@blue)*0.5).to_i,
color_correction(@red),
color_correction(@green),
color_correction(@blue),
(color_correction(@red)*0.5).to_i,
(color_correction(@green)*0.5).to_i,
(color_correction(@blue)*0.5).to_i,
]
@socket.emit(
:color,
{
id: @id,
values: @color_payload
}
)
end
def color_correction(color)
((color / 255.0) * 255).to_i
end
end
# coding: UTF-8
class Lamp
def initialize(id, name, socket)
@id = id
@name = name
@socket = socket
color
end
attr_accessor :socket
attr_reader :red, :green, :blue
def red=(value)
@red = clamp(value)
end
def green=(value)
@green = clamp(value)
end
def blue=(value)
@blue = clamp(value)
end
def on
color(red: 255, green: 255, blue: 255)
end
alias fullbright on
def off
color
end
def color(options = { red: 0, blue: 0, green: 0 })
@red = clamp(options[:red].to_i) if options[:red]
@green = clamp(options[:green].to_i) if options[:green]
@blue = clamp(options[:blue].to_i) if options[:blue]
set_color
end
private
def set_color
@socket.emit(
:color,
{
id: @id,
values: [
color_correction(@red),
color_correction(@green),
color_correction(@blue),
]
}
)
end
def color_correction(color)
((color / 255.0) * 192).to_i
end
def clamp(value, minimum = 0, maximum = 255)
[minimum, [value, maximum].min].max
end
end
#!/usr/bin/env ruby
require "bundler"
Bundler.require
require_relative "lamp"
require_relative "bar"
require_relative "program"
socket = SocketIO::Client::Simple.connect 'http://10.42.23.7:80'
SPOTS = [
Lamp.new("Sfk", :spot_1, socket),
Lamp.new("Sft", :spot_2, socket),
Lamp.new("Stt", :spot_3, socket),
Lamp.new("Stk", :spot_4, socket),
]
BARS = [
Bar.new("Bfk", :bar_1, socket),
Bar.new("Bft", :bar_2, socket),
Bar.new("Btt", :bar_3, socket),
Bar.new("Btk", :bar_4, socket),
]
CIRCLE = [BARS[1], SPOTS[0], SPOTS[3], BARS[2], BARS[3], SPOTS[2], SPOTS[1], BARS[0]]
socket.on :connect do
puts "[!] Connected to light control."
print "> "
@control_thread = Thread.new do
counter = 0
loop do
Program.send($program, counter)
counter += 1
sleep 0.04
end
end
end
socket.on(:disconnect) { @control_thread.kill; puts "[!] Connection to light control closed." }
socket.on(:error) { |error| p error }
$program = :lsd
$program = ARGV.pop.to_sym if ARGV.first
trap :INT do
puts "[!] Bye!"
exit true
end
loop do
print "> "
input = gets.chomp
case input
when /exit/, /quit/
puts "[!] Bye!"
exit true
when /list/
puts "programs:"
Program.public_methods(true).sort.each do |program|
puts program
end
when /program (.*)/, /play (.*)/
if Program.methods.include?($1.to_sym)
puts "[!] Changing program from #{$program} to #{$1}"
$program = $1.to_sym
end
end
end
# coding: UTF-8
def range(value, limit = 128)
limit/4 - (limit/2 - value%limit).abs
end
module Program
def self.lsd(ticks)
ticks = ticks/2
CIRCLE.each_with_index do |bar, index|
offset = 160
range_value = 8
red = offset + range_value * range(ticks+85+index*32)
green = offset + range_value * range(ticks+170+index*32)
blue = offset + range_value * range(ticks+index*32)
bar.color(
red: red,
green: green,
blue: blue,
)
end
end
def self.forrest(ticks)
(SPOTS+BARS).each_with_index do |bar, index|
green = 128 + 12 * range(ticks+index*32)
bar.color(
green: green,
blue: 0,
red: (green * (0.15 * index/8.0)).to_i,
)
end
end
def self.ocean(ticks)
(SPOTS+BARS).each_with_index do |bar, index|
blue = (128 + 3.5 * range(ticks+index*32)).to_i
green = ((128 + 3.5 * range(ticks+(index+2)*32))*0.33).to_i
green = blue if green >= blue
bar.color(
blue: blue,
red: 0,
green: green
)
end
end
def self.fireplace(ticks)
(SPOTS.reverse+BARS).each_with_index do |bar, index|
red = 172 + 3 * range(ticks+index*8)
green = ((172 + 3 * range(ticks+index*8)) * 0.20).to_i
bar.color(
red: red,
blue: 0,
green: green
)
end
end
def self.beach(ticks)
blue = 128 + 15*range(ticks+1*16)
BARS[0].color(blue: blue, red: 0, green: (blue * (0.25 * 1/8.0).to_i))
BARS[1].color(blue: blue, red: 0, green: (blue * (0.25 * 1/8.0).to_i))
BARS[2].color(blue: 20, red: blue, green: blue)
BARS[3].color(blue: 20, red: blue, green: blue)
SPOTS[0].color(blue: blue, red: 0, green: (blue * (0.25 * 1/8.0).to_i))
SPOTS[1].color(blue: blue, red: 0, green: (blue * (0.25 * 1/8.0).to_i))
SPOTS[2].color(blue: 20, red: blue, green: blue)
SPOTS[3].color(blue: 20, red: blue, green: blue)
end
def self.movie(ticks)
BARS[0].color(blue: 0, red: 0, green: 0)
BARS[1].color(blue: 0, red: 0, green: 0)
BARS[2].color(blue: 0, red: 0, green: 0)
BARS[3].color(blue: 0, red: 0, green: 0)
SPOTS[0].color(blue: 0, red: 48, green: 0)
SPOTS[1].color(blue: 0, red: 0, green: 0)
SPOTS[2].color(blue: 0, red: 0, green: 0)
SPOTS[3].color(blue: 0, red: 48, green: 0)
end
def self.schleichfahrt(ticks)
[SPOTS[1],SPOTS[2]].each_with_index do |bar, index|
bar.color(
red: 0,
blue: 0,
green: 0,
)
end
[SPOTS[0],SPOTS[3]].each_with_index do |bar, index|
red = 64 + range((ticks*2).to_i, 96)
bar.color(
red: red,
blue: 0,
green: 0,
)
end
BARS.each_with_index do |bar, index|
red = 48 + range(ticks*2, 96)
bar.color(
red: red,
blue: 0,
green: 0,
)
end
end
def self.redalert(ticks)
SPOTS.each_with_index do |bar, index|
red = 64 + range(ticks*2, 128)
bar.color(
red: red,
blue: 0,
green: 0,
)
end
BARS.each_with_index do |bar, index|
red = 64 + range(ticks*2, 128)
bar.color(
red: red,
blue: 0,
green: 0,
)
end
end
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment