lib/arduino_firmata/arduino.rb in arduino_firmata-0.3.4 vs lib/arduino_firmata/arduino.rb in arduino_firmata-0.3.5
- old
+ new
@@ -103,18 +103,21 @@
end
write END_SYSEX
end
def digital_read(pin)
+ raise ArgumentError, "invalid pin number (#{pin})" if pin.class != Fixnum or pin < 0
(@digital_input_data[pin >> 3] >> (pin & 0x07)) & 0x01 > 0
end
def analog_read(pin)
+ raise ArgumentError, "invalid pin number (#{pin})" if pin.class != Fixnum or pin < 0
@analog_input_data[pin]
end
def pin_mode(pin, mode)
+ raise ArgumentError, "invalid pin number (#{pin})" if pin.class != Fixnum or pin < 0
write SET_PIN_MODE
write pin
mode = case mode
when true
OUTPUT
@@ -127,10 +130,11 @@
return mode
end
end
def digital_write(pin, value)
+ raise ArgumentError, "invalid pin number (#{pin})" if pin.class != Fixnum or pin < 0
pin_mode pin, OUTPUT
port_num = (pin >> 3) & 0x0F
if value == 0 or value == false
@digital_output_data[port_num] &= ~(1 << (pin & 0x07))
else
@@ -143,18 +147,22 @@
return value
end
end
def analog_write(pin, value)
+ raise ArgumentError, "invalid pin number (#{pin})" if pin.class != Fixnum or pin < 0
+ raise ArgumentError, "invalid analog value (#{value})" if value.class != Fixnum or value < 0
pin_mode pin, PWM
write(ANALOG_MESSAGE | (pin & 0x0F))
write(value & 0x7F)
if write(value >> 7) == 1
return value
end
end
def servo_write(pin, angle)
+ raise ArgumentError, "invalid pin number (#{pin})" if pin.class != Fixnum or pin < 0
+ raise ArgumentError, "invalid angle (#{angle})" if angle.class != Fixnum or angle < 0
pin_mode pin, SERVO
write(ANALOG_MESSAGE | (pin & 0x0F))
write(angle & 0x7F)
if write(angle >> 7) == 1
return angle