Class: MonomeSerial::SerialCommunicator::RealCommunicator

Inherits:
Communicator
  • Object
show all
Defined in:
lib/monome_serial/serial_communicator/real_communicator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tty_path) ⇒ RealCommunicator

Returns a new instance of RealCommunicator.

Raises:

  • (ArgumentError)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/monome_serial/serial_communicator/real_communicator.rb', line 5

def initialize(tty_path)
  #make sure tty_path exists (such as "/dev/tty.usbserial-m256-203")
  raise ArgumentError, "path to tty IO file does not exist" unless File.exists?(tty_path)

  #match = tty_path.match /m(\d+)-(\d+)/
  #@model = match[1]

  #pull out this Monome's individual serial number
  #@serial = match[2]

  #Open up the virtual serial port
  @dev = dev_open(tty_path)

  #bless the file with Termios powers
  @dev.extend Termios

  #create a new Termios struct
  newtio = Termios::new_termios()
  newtio.iflag = Termios::IGNPAR
  newtio.oflag = 0
  newtio.cflag = Termios::CLOCAL
  newtio.lflag = 0
  newtio.cc[Termios::VTIME] = 0
  newtio.cc[Termios::VMIN] = 1
  newtio.ispeed = Termios::B115200
  newtio.ospeed = Termios::B115200

  #discard data on both the input and output queues
  flush

  #set the termios struct in the OS with the newly
  #defined info in newtio
  @dev.tcsetattr(Termios::TCSANOW, newtio)
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



4
5
6
# File 'lib/monome_serial/serial_communicator/real_communicator.rb', line 4

def model
  @model
end

#serialObject (readonly)

Returns the value of attribute serial.



4
5
6
# File 'lib/monome_serial/serial_communicator/real_communicator.rb', line 4

def serial
  @serial
end

Instance Method Details

#readObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/monome_serial/serial_communicator/real_communicator.rb', line 44

def read
  #read action message
  action_bin = @dev.read(1)

  #interpret action:
  #0000000 => keydown
  #0001000 => keyup
  action = action_bin.unpack('B8').first[3] == "1" ? :keyup : :keydown

  #read position message
  pos_bin    = @dev.read(1)

  #convert it to a binary string
  pos_bin_s = pos_bin.unpack('B8').first

  #interpret position:
  #xxxxyyyy => x position and y position in base 2
  x = Integer("0b" + pos_bin_s[0..3])
  y = Integer("0b" + pos_bin_s[4..8])

  return action, x, y
end

#real?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/monome_serial/serial_communicator/real_communicator.rb', line 40

def real?
  true
end

#write(strings) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/monome_serial/serial_communicator/real_communicator.rb', line 67

def write(strings)

  super

  #convert integer params to 8bit binary representation
  bin_strings = strings.map{|s| s.to_s}

  case bin_strings.size
  when 1 then @dev.write(bin_strings.pack('B8'))
  when 2 then @dev.write(bin_strings.pack('B8B8'))
  when 3 then @dev.write(bin_strings.pack('B8B8B8'))
  when 9 then @dev.write(bin_strings.pack('B8B8B8B8B8B8B8B8B8'))
  else raise ArgumentError, "SerialCommunicator#write only supports sending one, two, three or nine bytes at a time. You tried to send #{bin_strings.size} bytes."
  end
end