Created
February 1, 2012 10:18
-
-
Save phorsfall/1716357 to your computer and use it in GitHub Desktop.
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
require 'drb' | |
# ruby-serial was crashing Shoes so run serial code in another process using DRb to | |
# communicate the state of the controller. | |
pid = Process.fork { exec('ruby', 'controller.rb') } | |
Shoes.app :width => 938, :height => 768, :resizable => false do | |
# Start the DRb client. | |
DRb.start_service | |
@controller = DRbObject.new nil, 'druby://localhost:7777' | |
background 'images/background.png' | |
x = y = _x = _y = nil | |
screen_width, screen_height = 670, 477 | |
default_pen_color = '#555' | |
@sketch = flow do | |
stroke default_pen_color | |
translate 138, 135 | |
keypress do |key| | |
case key | |
when 'c' | |
clear | |
when 'q' | |
# Quitting the app by any other method will leave the background thread behind. | |
Process.kill('HUP', pid) | |
exit | |
when '1'..'9' | |
strokewidth key.to_i | |
when "\e" | |
strokewidth 1 | |
stroke default_pen_color | |
when 'r' | |
stroke red | |
when 'b' | |
stroke blue | |
when 'g' | |
stroke green | |
when 'y' | |
stroke yellow | |
end | |
end | |
animate do | |
# Clear the screen if the tile switch is activated. | |
clear if @controller.t == 1 | |
x = @controller.x/1024.0 * screen_width | |
y = @controller.y/1024.0 * screen_height | |
if(_x and _y and (x != _x or y != _y)) | |
append do | |
line _x, _y, x, y | |
end | |
end | |
_x, _y = x, y | |
end | |
end | |
end |
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
#!/usr/bin/env ruby -w | |
require 'drb' | |
require 'serialport' | |
port = '/dev/tty.usbserial-A70061Un' | |
baud_rate = 9600 | |
data_bits = 8 | |
stop_bits = 1 | |
parity = SerialPort::NONE | |
sp = SerialPort.open(port, baud_rate, data_bits, stop_bits, parity) | |
# Simple class to represent the state of the controller. | |
class ControllerState | |
attr_accessor :x, :y, :t | |
end | |
state = ControllerState.new | |
DRb.start_service 'druby://:7777', state | |
puts DRb.uri | |
input = value = '' | |
while true do | |
c = sprintf("%c", sp.getc) | |
print c | |
if %w(x y t).include?(c) | |
state.send "#{input}=", value.to_i unless input.empty? | |
input = c | |
value = '' | |
else | |
value << c | |
end | |
end | |
# Never reached... | |
DRb.thread.join | |
sp.close |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment