-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Get a websocket frame stream working (base64 encoded)
- Loading branch information
1 parent
b331fc3
commit 45ce6e2
Showing
7 changed files
with
138 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module IntegerPatches | ||
def to_hex_s | ||
"0x" + to_s(16).upcase.rjust(6, '0') | ||
end | ||
|
||
def to_chr_bytes | ||
((self & 0xFF0000) >> 16).chr + | ||
((self & 0x00FF00) >> 8).chr + | ||
((self & 0x0000FF) ).chr | ||
end | ||
end | ||
|
||
Integer.include IntegerPatches |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,89 @@ | ||
require 'sinatra' | ||
require 'sinatra-websocket' | ||
|
||
Dir[File.join("./lib", "**/*.rb")].each do |f| | ||
require f | ||
end | ||
|
||
module PrintHex | ||
def to_hex_s | ||
"0x" + to_s(16).upcase.rjust(6, '0') | ||
require 'pry' | ||
|
||
set :server, 'thin' | ||
set :sockets, [] | ||
set :num_leds, 300 | ||
set :version, File.read('.version').chomp | ||
|
||
class Renderer | ||
attr_accessor :pattern, :num_leds | ||
|
||
def initialize pattern, num_leds | ||
@pattern = pattern | ||
@num_leds = num_leds | ||
end | ||
|
||
def render | ||
t = Time.now | ||
c = Effects::Context.new(@num_leds, t: t) | ||
(0..@num_leds).collect do |i| | ||
c.position = i | ||
@pattern.apply(c) | ||
c.color | ||
end | ||
end | ||
end | ||
Integer.include PrintHex | ||
|
||
NUM_LEDS = 300 | ||
$version = File.read('.version').chomp | ||
$active_pattern = Compositions::MASTERAVERBAITER_V1 | ||
$s = Arduino::SerialOut.new do | ||
t = Time.now | ||
c = Effects::Context.new(NUM_LEDS, t: t) | ||
(0..NUM_LEDS).collect do |i| | ||
c.position = i | ||
$active_pattern.apply(c) | ||
c.color | ||
|
||
set :renderer, Renderer.new(Compositions::MASTERAVERBAITER_V1, settings.num_leds) | ||
|
||
set :playing, true | ||
|
||
def play | ||
serial_out = Arduino::SerialOut.new | ||
|
||
Thread.new do | ||
while(settings.playing) | ||
# Render frame | ||
frame = settings.renderer.render | ||
|
||
# Write out to serial ports | ||
serial_out.write frame | ||
|
||
# Write out to websockets | ||
settings.sockets.each do |s| | ||
byte_string = frame.map(&:to_chr_bytes).join("") | ||
s.send(Base64.encode64(byte_string)) | ||
end | ||
|
||
# Give other threads some time | ||
sleep(0.1) | ||
end | ||
end | ||
end | ||
|
||
get '/' do | ||
"Butterfly Server #{$version} #{NUM_LEDS} #{$s.playing}" | ||
"Butterfly Server #{settings.version} #{settings.num_leds}" | ||
end | ||
|
||
get '/frame' do | ||
$s.frame.map{|c| c.to_hex_s}.join("\n") | ||
#$frame.map{|c| c.to_hex_s}.join("\n") | ||
settings.renderer.render.map{|c| c.to_hex_s}.join("\n") | ||
end | ||
|
||
get '/subscribe' do | ||
if request.websocket? | ||
request.websocket do |ws| | ||
ws.onopen do | ||
settings.sockets << ws | ||
end | ||
|
||
ws.onclose do | ||
settings.sockets.delete(ws) | ||
end | ||
end | ||
else | ||
"for websockets only" | ||
end | ||
end | ||
|
||
# get '/debug' do | ||
# require 'pry' | ||
# binding.pry | ||
# $s.frame.map{|c| c.to_hex_s}.join("\n") | ||
# end | ||
get '/pry' do | ||
binding.pry | ||
end | ||
|
||
$t = Thread.new { $s.play } | ||
$t = play |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,28 @@ | ||
example_id | status | run_time | | ||
----------------------------------------------------- | ------ | --------------- | | ||
./spec/lib/colors_spec.rb[1:1:1] | passed | 0.00008 seconds | | ||
./spec/lib/colors_spec.rb[1:1:2] | passed | 0.00007 seconds | | ||
./spec/lib/colors_spec.rb[1:1:1] | passed | 0.00006 seconds | | ||
./spec/lib/colors_spec.rb[1:1:2] | passed | 0.00005 seconds | | ||
./spec/lib/colors_spec.rb[1:1:3] | passed | 0.00007 seconds | | ||
./spec/lib/colors_spec.rb[1:1:4] | passed | 0.00008 seconds | | ||
./spec/lib/compositions/masteraverbaiter_spec.rb[1:1] | passed | 0.00094 seconds | | ||
./spec/lib/compositions/masteraverbaiter_spec.rb[1:2] | passed | 0.00271 seconds | | ||
./spec/lib/effects/core/pulse_spec.rb[1:1] | passed | 0.00017 seconds | | ||
./spec/lib/effects/core/pulse_spec.rb[1:2] | passed | 0.00016 seconds | | ||
./spec/lib/effects/core/pulse_spec.rb[1:3] | passed | 0.00014 seconds | | ||
./spec/lib/effects/core/pulse_spec.rb[1:4] | passed | 0.00018 seconds | | ||
./spec/lib/effects/core/wheel_spec.rb[1:1] | passed | 0.00012 seconds | | ||
./spec/lib/effects/core/wheel_spec.rb[1:2] | passed | 0.00016 seconds | | ||
./spec/lib/effects/core/wheel_spec.rb[1:3] | passed | 0.00008 seconds | | ||
./spec/lib/compositions/masteraverbaiter_spec.rb[1:1] | passed | 0.00093 seconds | | ||
./spec/lib/compositions/masteraverbaiter_spec.rb[1:2] | passed | 0.00345 seconds | | ||
./spec/lib/effects/core/pulse_spec.rb[1:1] | passed | 0.00016 seconds | | ||
./spec/lib/effects/core/pulse_spec.rb[1:2] | passed | 0.00019 seconds | | ||
./spec/lib/effects/core/pulse_spec.rb[1:3] | passed | 0.00019 seconds | | ||
./spec/lib/effects/core/pulse_spec.rb[1:4] | passed | 0.00016 seconds | | ||
./spec/lib/effects/core/wheel_spec.rb[1:1] | passed | 0.00096 seconds | | ||
./spec/lib/effects/core/wheel_spec.rb[1:2] | passed | 0.00011 seconds | | ||
./spec/lib/effects/core/wheel_spec.rb[1:3] | passed | 0.00009 seconds | | ||
./spec/lib/effects/core/wipe_spec.rb[1:1] | passed | 0.00005 seconds | | ||
./spec/lib/effects/meta/composition_spec.rb[1:1] | passed | 0.00124 seconds | | ||
./spec/lib/effects/meta/composition_spec.rb[1:2:1] | passed | 0.00017 seconds | | ||
./spec/lib/effects/meta/composition_spec.rb[1:2:2] | passed | 0.00012 seconds | | ||
./spec/lib/effects/meta/moving_spec.rb[1:1] | passed | 0.00011 seconds | | ||
./spec/lib/effects/meta/moving_spec.rb[1:2] | passed | 0.00011 seconds | | ||
./spec/lib/effects/meta/composition_spec.rb[1:1] | passed | 0.00028 seconds | | ||
./spec/lib/effects/meta/composition_spec.rb[1:2:1] | passed | 0.00018 seconds | | ||
./spec/lib/effects/meta/composition_spec.rb[1:2:2] | passed | 0.00008 seconds | | ||
./spec/lib/effects/meta/moving_spec.rb[1:1] | passed | 0.00008 seconds | | ||
./spec/lib/effects/meta/moving_spec.rb[1:2] | passed | 0.00007 seconds | | ||
./spec/lib/effects/meta/moving_spec.rb[1:3] | passed | 0.00014 seconds | | ||
./spec/lib/effects/meta/moving_spec.rb[1:4] | passed | 0.00007 seconds | | ||
./spec/lib/effects/meta/moving_spec.rb[1:5] | passed | 0.00008 seconds | | ||
./spec/lib/effects/meta/moving_spec.rb[1:4] | passed | 0.00011 seconds | | ||
./spec/lib/effects/meta/moving_spec.rb[1:5] | passed | 0.00011 seconds | | ||
./spec/lib/effects/meta/moving_spec.rb[1:6] | passed | 0.0001 seconds | | ||
./spec/lib/effects_spec.rb[1:1] | passed | 0.00005 seconds | | ||
./spec/lib/effects_spec.rb[1:2] | passed | 0.00005 seconds | | ||
./spec/lib/effects_spec.rb[1:3] | passed | 0.00007 seconds | | ||
./spec/lib/effects_spec.rb[1:1] | passed | 0.00006 seconds | | ||
./spec/lib/effects_spec.rb[1:2] | passed | 0.00009 seconds | | ||
./spec/lib/effects_spec.rb[1:3] | passed | 0.00005 seconds | |