Class: Duino
- Inherits:
-
Object
- Object
- Duino
- Defined in:
- lib/duino.rb
Instance Method Summary collapse
- #analog_read(pin) ⇒ Object
- #analog_write(pin, value) ⇒ Object
- #close ⇒ Object
- #digital_input(*pin_list) ⇒ Object
- #digital_output(*pin_list) ⇒ Object
-
#get_state(pin) ⇒ Object
Returns true if high and false if low.
-
#initialize(port, baudrate = 115200) ⇒ Duino
constructor
A new instance of Duino.
- #save_state(pin, state) ⇒ Object
- #set_high(pin) ⇒ Object
- #set_low(pin) ⇒ Object
- #to_s ⇒ Object
- #turn_off ⇒ Object
Constructor Details
#initialize(port, baudrate = 115200) ⇒ Duino
Returns a new instance of Duino.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/duino.rb', line 4 def initialize(port, baudrate=115200) data_bits = 8 stop_bits = 1 parity = SerialPort::NONE @serial = SerialPort.new(port, baudrate, data_bits, stop_bits, parity) @serial.read_timeout = 2 @serial.sync @port = port @output_pins = [] @input_pins = [] @pin_states = {} puts "Initialized" end |
Instance Method Details
#analog_read(pin) ⇒ Object
88 89 90 91 92 |
# File 'lib/duino.rb', line 88 def analog_read(pin) send_data(4) send_pin(pin) get_data() end |
#analog_write(pin, value) ⇒ Object
81 82 83 84 85 86 |
# File 'lib/duino.rb', line 81 def analog_write(pin, value) send_data(3) send_pin(pin) send_data((value / 16).to_s(16)) send_data((value % 16).to_s(16)) end |
#close ⇒ Object
101 102 103 104 105 106 |
# File 'lib/duino.rb', line 101 def close send_data(5) @serial.dtr = (0) @serial.close puts "Connection closed" end |
#digital_input(*pin_list) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/duino.rb', line 37 def digital_input(*pin_list) send_data(pin_list.length) if(pin_list.class == Array) @input_pins = pin_list pin_list.each do |pin| send_pin(pin) end else raise ArgumentError, "Arguments must be a list of pin numbers" end return pin_list end |
#digital_output(*pin_list) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/duino.rb', line 24 def digital_output(*pin_list) send_data(pin_list.length) if(pin_list.class == Array) @output_pins = pin_list pin_list.each do |pin| send_pin(pin) end else raise ArgumentError, "Arguments must be a list of pin numbers" end return pin_list end |
#get_state(pin) ⇒ Object
Returns true if high and false if low.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/duino.rb', line 66 def get_state(pin) # Returns true if high and false if low. send_data(2) send_pin(pin) while true rval = @serial.getbyte() if(rval == 0) return("Low") end if(rval == 1) return("High") end end end |
#save_state(pin, state) ⇒ Object
62 63 64 |
# File 'lib/duino.rb', line 62 def save_state(pin, state) @pin_states[pin.to_s] = state end |
#set_high(pin) ⇒ Object
50 51 52 53 54 |
# File 'lib/duino.rb', line 50 def set_high(pin) save_state(pin, true) send_data(1) send_pin(pin) end |
#set_low(pin) ⇒ Object
56 57 58 59 60 |
# File 'lib/duino.rb', line 56 def set_low(pin) save_state(pin, false) send_data(0) send_pin(pin) end |
#to_s ⇒ Object
20 21 22 |
# File 'lib/duino.rb', line 20 def to_s "Arduino is on port #{@port} at #{@serial.baud} baudrate" end |
#turn_off ⇒ Object
94 95 96 97 98 99 |
# File 'lib/duino.rb', line 94 def turn_off @output_pins.each do |pin| set_low(pin) end puts "All pins set to low" end |