Skip to content

Instantly share code, notes, and snippets.

@ehntoo
Created May 5, 2015 02:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ehntoo/f0e72bb734b88ef8f470 to your computer and use it in GitHub Desktop.
Save ehntoo/f0e72bb734b88ef8f470 to your computer and use it in GitHub Desktop.
Raspberry Pi Device Prototyping
[config]
root = "./"
[screens."pic1.png"]
right = 'pic2.png'
left = 'pic4.png'
up = 'pic1.png'
down = 'pic3.png'
[screens."pic2.png"]
right = 'pic2.png'
left = 'pic4.png'
up = 'pic1.png'
down = 'pic3.png'
[screens."pic3.png"]
right = 'pic2.png'
left = 'pic4.png'
up = 'pic1.png'
down = 'pic3.png'
[screens."pic4.png"]
right = 'pic2.png'
left = 'pic4.png'
up = 'pic1.png'
down = 'pic3.png'
[screens.default]
r = 'pic1.png'
require 'io/console'
require 'tomlrb'
require 'socket'
# Taken from https://github.com/jkraemer/pi-slideshow/blob/master/lib/pi_slides/fim.rb
class Fim
def initialize(options = {})
@mutex = Mutex.new
options = {
:host => 'localhost', :port => 9999
}.merge options
@host = options[:host]
@port = options[:port]
end
# pushes the file, displays it and removes it from the list immediately
# shows and pops the next file from the list if path.nil?
def show(path=nil)
exec %{#{"push '#{path}';" if path}next;pop;}
end
# turn status line on/off
def status_line(switch_on = true)
if switch_on
set_vars :_display_busy => 1, :_display_status => 1
else # switch off
set_vars :_display_busy => 0, :_display_status => 0
end
end
def disable_autowidth
exec 'set autowidth 0;'
end
def toggle_autowidth
exec 'autowidth=1-autowidth;'
end
def push(*files)
exec files.map{|f| "push '#{f}';"}.join + "prefetch;"
end
def set_vars(vars = {})
exec vars.to_a.map{|name, value| %{#{name}=#{value};}}.join
end
def exec(cmd)
@mutex.synchronize do
TCPSocket.open(@host, @port).tap do |s|
s << cmd
s.close
end
end
rescue
sleep 1
retry
end
end
$file_tree = Tomlrb.load_file('config.toml')
$config = $file_tree['config']
$screens = $file_tree['screens']
$current_screen_key = $screens.first[0]
$default_actions = $screens['default']
$fim = Fim.new(host: '192.168.1.1', port: 9999)
$fim.status_line(false)
$fim.exec('v:auto_scale_v=0;autowidth=0;reload;')
# Reads keypresses from the user including 2 and 3 escape character sequences.
def read_char
STDIN.echo = false
STDIN.raw!
input = STDIN.getc.chr
if input == "\e" then
input << STDIN.read_nonblock(3) rescue nil
input << STDIN.read_nonblock(2) rescue nil
end
ensure
STDIN.echo = true
STDIN.cooked!
return input
end
# oringal case statement from:
# http://www.alecjacobson.com/weblog/?p=75
def show_single_key
c = read_char
aliased_char = case c
when " "
"space"
when "\t"
"tab"
when "\r"
"enter"
when "\n"
"enter"
when "\e"
"esc"
when "\e[A"
"up"
when "\e[B"
"down"
when "\e[C"
"right"
when "\e[D"
"left"
when "\177"
"backspace"
when "\004"
"delete"
when "\u0003"
puts "CONTROL-C - exiting"
exit 0
else
c
end
current_screen = $screens[$current_screen_key] || {}
target_screen = current_screen[aliased_char] || $default_actions[aliased_char]
if (target_screen)
puts "Moving to target screen: #{target_screen}"
$fim.show($config['root'] + target_screen)
$current_screen_key = target_screen
else
puts "No action found for key: #{aliased_char} from screen: #{current_screen}"
end
end
show_single_key while(true)
#!/bin/sh
fim -c 'while(1){popen "nc -l 9999";}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment