Class: Lorraine::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/lorraine/connection.rb

Direct Known Subclasses

FakeConnection

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(port = Lorraine::Connection.first_available_port) ⇒ Connection

Returns a new instance of Connection.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/lorraine/connection.rb', line 9

def initialize(port = Lorraine::Connection.first_available_port)
  
  #simplest ruby program to read from arduino serial, 
  #using the SerialPort gem
  #(http://rubygems.org/gems/serialport)


  #params for serial port
  port_str = port  # may be different for you
  baud_rate = 115200
  # data_bits = 8
  # stop_bits = 1
  # parity = SerialPort::NONE

  self.port = SerialPort.new port_str, baud_rate#, data_bits, stop_bits, parity
  
end

Instance Attribute Details

#portObject

Returns the value of attribute port.



7
8
9
# File 'lib/lorraine/connection.rb', line 7

def port
  @port
end

Class Method Details

.first_available_portObject



27
28
29
30
31
# File 'lib/lorraine/connection.rb', line 27

def self.first_available_port
  p = Dir.glob("/dev/tty.usb*").first
  raise "No available ports." unless p
  p
end

Instance Method Details

#animate_to_image(other_image, duration = 1, fps = 24) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/lorraine/connection.rb', line 59

def animate_to_image(other_image, duration = 1, fps = 24)
  frame_time = duration.to_f / fps.to_f
  frame_count = duration * fps
  frames = Lorraine::Image.frames_between(@current_image, other_image, frame_count)
  frames.each do |frame|
    display_image frame
    sleep frame_time
  end
end

#display_image(img) ⇒ Object



54
55
56
57
# File 'lib/lorraine/connection.rb', line 54

def display_image(img)
  @current_image = img
  display_pixels img.rgb_pixels(4095)
end

#display_pixels(pixel_array) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/lorraine/connection.rb', line 42

def display_pixels(pixel_array)
  commands = []
  pixel_array.each_with_index do |pixel, i|
    commands << Lorraine::Message.new(:set_pixel, i, pixel[0], pixel[1], pixel[2])
  end
  commands << Lorraine::Message.new(:refresh)
  commands.each do |command|
    self.write_message command
    #self.read_line
  end
end

#read_lineObject



69
70
71
72
73
# File 'lib/lorraine/connection.rb', line 69

def read_line
  m = self.port.gets
  puts "Lorraine::Connection Message: #{m}"
  m
end

#sever!Object



75
76
77
# File 'lib/lorraine/connection.rb', line 75

def sever!
  self.port.close # see note 1
end

#write_binary_string(binary_string) ⇒ Object



33
34
35
# File 'lib/lorraine/connection.rb', line 33

def write_binary_string(binary_string)
  self.port.write binary_string
end

#write_message(msg) ⇒ Object



37
38
39
40
# File 'lib/lorraine/connection.rb', line 37

def write_message(msg)
  puts "Writing message: #{msg}"
  write_binary_string msg.to_binary
end