Method: HardwareLibrary#software_serial

Defined in:
lib/rad/hardware_library.rb

#software_serial(rx, tx, opts = {}) ⇒ Object

Treat a pair of digital I/O pins as a serial line. See: www.arduino.cc/en/Tutorial/SoftwareSerial

Raises:

  • (ArgumentError)


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
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rad/hardware_library.rb', line 8

def software_serial(rx, tx, opts={})
  raise ArgumentError, "can only define rx from Fixnum, got #{rx.class}" unless rx.is_a?(Fixnum)
  raise ArgumentError, "can only define tx from Fixnum, got #{tx.class}" unless tx.is_a?(Fixnum)

  output_pin(tx)
  input_pin(rx)

  rate = opts[:rate] ? opts[:rate] : 9600
  if opts[:as]
    @declarations << "SoftwareSerial _#{opts[ :as ]} = SoftwareSerial(#{rx}, #{tx});"
    accessor = <<-STR
      SoftwareSerial& #{opts[ :as ]}() {
      return _#{opts[ :as ]};
      }
    STR
    @@swser_inc ||= FALSE
    if (@@swser_inc == FALSE) # on second instance this stuff can't be repeated
      @@swser_inc = TRUE
      accessor += <<-STR
      int read(SoftwareSerial& s) {
        return s.read();
      }
      void println( SoftwareSerial& s, char* str ) {
        return s.println( str );
      }
      void print( SoftwareSerial& s, char* str ) {
        return s.print( str );
      }
      void println( SoftwareSerial& s, int i ) {
        return s.println( i );
      }
      void print( SoftwareSerial& s, int i ) {
        return s.print( i );
      }
      STR
    end
    @accessors << accessor

    @signatures << "SoftwareSerial& #{opts[ :as ]}();"

    @other_setup << "\t_#{opts[ :as ]}.begin(#{rate});"
  end
end