Adapted recorder.rb and generate_gifs.sh to websockets.

This commit is contained in:
Fabian Schlenz 2019-10-23 12:37:25 +02:00
parent f5d47fe7da
commit 2a6f68cc45
4 changed files with 111 additions and 75 deletions

View File

@ -1,7 +1,7 @@
#!/bin/bash #!/bin/bash
IP="$1" IP="$1"
EFFECTS=`egrep "case" ../effects.cpp | tr -s "\t" " " | cut -d" " -f 7 | sort` EFFECTS=`./list_effects.rb $IP | sort`
mkdir effects mkdir effects

26
src/tools/list_effects.rb Executable file
View File

@ -0,0 +1,26 @@
#!/usr/bin/env ruby
require 'websocket-eventmachine-client'
require 'json'
IP = ARGV[0] or raise "No IP given"
uri = "ws://#{IP}:80/ws"
EM.run do
ws = WebSocket::EventMachine::Client.connect(uri: uri)
ws.onopen do
ws.send "effects?"
end
ws.onmessage do |msg, type|
if type==:text
j = JSON.parse(msg)
if j["effects"]
j["effects"].each do |effect|
puts effect
end
exit
end
end
end
end

46
src/tools/monitor.rb Normal file → Executable file
View File

@ -13,6 +13,7 @@ def rgb2ansi(r, g, b)
end end
IP = ARGV[0] IP = ARGV[0]
EFFECT = ARGV[1]
uri = "ws://#{IP}:80/ws" uri = "ws://#{IP}:80/ws"
puts "Connecting to #{uri}..." puts "Connecting to #{uri}..."
@ -21,31 +22,36 @@ EM.run do
ws.onopen do ws.onopen do
puts "\033[2J\033[H\n Connected." puts "\033[2J\033[H\n Connected."
ws.send "E:blur2d" ws.send "effect:#{EFFECT}" if EFFECT
ws.send "monitor:1"
end end
ws.onmessage do |msg, type| ws.onmessage do |msg, type|
raise "Unexpected message type #{type.inspect}" if type!=:binary if type==:binary
data = msg.unpack("C*") data = msg.unpack("C*")
width = data.shift width = data.shift
height = data.shift height = data.shift
splitter = data.shift splitter = data.shift
raise "Unexpected value #{splitter} at index 2" unless splitter==0xFF raise "Unexpected value #{splitter} at index 2" unless splitter==0xFF
expected_length = width * height * 3 + 4 expected_length = width * height * 3 + 4
raise "Unexpected message length. Expected: #{expected_length}, was: #{data.count + 3}" unless data.count + 3==expected_length raise "Unexpected message length. Expected: #{expected_length}, was: #{data.count + 3}" unless data.count + 3==expected_length
str = "\033[H+#{"-"*width}+\n" str = "\033[H+#{"-"*width}+\n"
(0...height).each do |y| (0...height).each do |y|
str += "|" str += "|"
(0...width).each do |x| (0...width).each do |x|
r, g, b = *data.shift(3) r, g, b = *data.shift(3)
color_code = rgb2ansi(r, g, b) color_code = rgb2ansi(r, g, b)
str += "\033[48;5;#{color_code}m " str += "\033[48;5;#{color_code}m "
end
str += "\033[0m|\n"
end end
str += "\033[0m|\n" str += "+#{"-"*width}+\n"
puts str
else
puts msg
exit
end end
str += "+#{"-"*width}+\n"
puts str
end end
ws.onclose do |msg, reason| ws.onclose do |msg, reason|

View File

@ -1,39 +1,19 @@
#!/usr/bin/env ruby #!/usr/bin/env ruby
require 'socket' require 'websocket-eventmachine-client'
require 'pp' require 'pp'
require 'rmagick' require 'rmagick'
include Magick include Magick
IP = ARGV[0] IP = ARGV[0]
PORT = 2122
FILE = ARGV[1] or raise "No filename given" FILE = ARGV[1] or raise "No filename given"
EFFECT = ARGV[2] EFFECT = ARGV[2]
FRAMES = 125 FRAMES = 125
FACTOR = 2 FACTOR = 2
delay = 50 delay = 50
puts "Connecting to #{IP}:#{PORT}..." uri = "ws://#{IP}:80/ws"
puts "Connecting to #{uri}..."
s = TCPSocket.new(IP, PORT)
puts "Connected."
init = s.recv(3).unpack("C*")
raise "Initial data packet wasn't usable!" if init[2] != 0xFF
puts "Got initial data packet."
dim_x, dim_y = *init
len = dim_x * dim_y * 3 + 3
puts "Size: #{dim_x}x#{dim_y}. Expecting packet length #{len}."
puts "Opening local UDP socket..."
udp = UDPSocket.new
udp.bind("10.10.2.1", 13330)
puts "Waiting for UDP packets on port 13330..."
s.sendmsg("P\x12\x34\x00")
s.sendmsg("E#{EFFECT}\x00") if EFFECT
gif = ImageList.new gif = ImageList.new
last_id = 255 last_id = 255
@ -41,42 +21,66 @@ last_frame_time = nil
img = nil img = nil
last_diff = nil last_diff = nil
while gif.length < FRAMES do EM.run do
data = udp.recvfrom(1024)[0].unpack("C*") ws = WebSocket::EventMachine::Client.connect(uri: uri)
if delay > 0
delay -= 1 ws.onopen do
next puts "Connected."
puts "Waiting for delay..." if delay>0
ws.send "effect:#{EFFECT}" if EFFECT
ws.send "monitor:1"
end end
#puts "Packet. ID: #{data[0]}, length: #{data.length}"
raise "Unexpected packet length" unless data.count == len
raise "Invalid data packet" unless data[len - 1]==0xFF
id = data.shift << 8 | data.shift ws.onmessage do |msg, type|
if last_id != id-1 && last_id != id-2 if type==:binary
puts "Skipped from #{last_id} to #{id}." if delay > 0
gif = ImageList.new delay -= 1
end next
last_id = id end
data = msg.unpack("C*")
img = Image.new(dim_x, dim_y) width = data.shift
gc = Draw.new height = data.shift
splitter = data.shift
#next raise "Unexpected value #{splitter} at index 2" unless splitter==0xFF
print "." expected_length = width * height * 3 + 4
print "#{gif.length}" if gif.length%50==0 raise "Unexpected message length. Expected: #{expected_length}, was: #{data.count + 3}" unless data.count + 3==expected_length
(0...dim_y).each do |y|
(0...dim_x).each do |x| img = Image.new(width, height)
r, g, b = *data.shift(3) gc = Draw.new
gc.fill("rgb(#{r}, #{g}, #{b})")
gc.point(x, y) print "."
#img.pixel_color(x, y, Pixel.new(r, g, b, 255)) print "#{gif.length}" if gif.length%50==0
(0...height).each do |y|
(0...width).each do |x|
r, g, b = *data.shift(3)
gc.fill("rgb(#{r}, #{g}, #{b})")
gc.point(x, y)
#img.pixel_color(x, y, Pixel.new(r, g, b, 255))
end
end
gc.draw(img)
img.sample!(FACTOR)
gif << img
if gif.length >= FRAMES
ws.close
end
else
puts "-->#{msg}"
exit
end end
end end
gc.draw(img)
img.sample!(FACTOR) ws.onerror do |error|
gif << img puts "Error: #{error}"
end
ws.onclose do |msg, reason|
puts "Disconnected."
EventMachine.stop_event_loop
end
end end
s.close
puts puts
puts "Generating gif..." puts "Generating gif..."
gif.ticks_per_second = 100 gif.ticks_per_second = 100