pi_driver 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/pi_driver.rb +3 -1
- data/lib/pi_driver/device.rb +6 -0
- data/lib/pi_driver/device/mcp23017.rb +129 -0
- data/lib/pi_driver/device/mcp23017/hardware_address.rb +37 -0
- data/lib/pi_driver/device/mcp23017/port.rb +23 -0
- data/lib/pi_driver/device/mcp23017/register.rb +98 -0
- data/lib/pi_driver/device/mcp23017/register/defval.rb +14 -0
- data/lib/pi_driver/device/mcp23017/register/gpinten.rb +14 -0
- data/lib/pi_driver/device/mcp23017/register/gpio.rb +14 -0
- data/lib/pi_driver/device/mcp23017/register/gppu.rb +14 -0
- data/lib/pi_driver/device/mcp23017/register/intcap.rb +14 -0
- data/lib/pi_driver/device/mcp23017/register/intcon.rb +14 -0
- data/lib/pi_driver/device/mcp23017/register/intf.rb +14 -0
- data/lib/pi_driver/device/mcp23017/register/iocon.rb +41 -0
- data/lib/pi_driver/device/mcp23017/register/iodir.rb +14 -0
- data/lib/pi_driver/device/mcp23017/register/ipol.rb +14 -0
- data/lib/pi_driver/device/mcp23017/register/olat.rb +14 -0
- data/lib/pi_driver/device/mcp23017/register/register_helper.rb +50 -0
- data/lib/pi_driver/i2c_master.rb +6 -15
- data/lib/pi_driver/pin.rb +25 -49
- data/lib/pi_driver/pin/board.rb +65 -0
- data/lib/pi_driver/pin/direction.rb +13 -0
- data/lib/pi_driver/pin/directory_helper.rb +32 -0
- data/lib/pi_driver/pin/file_helper.rb +32 -0
- data/lib/pi_driver/utils.rb +10 -0
- data/lib/pi_driver/utils/argument_helper.rb +19 -0
- data/lib/pi_driver/utils/byte.rb +21 -0
- data/lib/pi_driver/utils/edge.rb +17 -0
- data/lib/pi_driver/utils/interrupt.rb +60 -0
- data/lib/pi_driver/utils/state.rb +13 -0
- data/test/device/mcp23017/test_alias.rb +27 -0
- data/test/device/mcp23017/test_error.rb +37 -0
- data/test/device/mcp23017/test_hardware_address.rb +29 -0
- data/test/device/mcp23017/test_i2c_master.rb +10 -0
- data/test/device/mcp23017/test_read.rb +63 -0
- data/test/device/mcp23017/test_register.rb +118 -0
- data/test/device/mcp23017/test_write.rb +49 -0
- data/test/device/mcp23017_test_helper.rb +13 -0
- data/test/i2c_master/test_ack.rb +26 -0
- data/test/i2c_master/test_address.rb +15 -0
- data/test/i2c_master/test_alias.rb +7 -0
- data/test/i2c_master/test_clock_stretch.rb +26 -0
- data/test/i2c_master/test_read.rb +27 -0
- data/test/i2c_master/test_speed.rb +11 -0
- data/test/i2c_master/test_start.rb +11 -0
- data/test/i2c_master/test_stop.rb +24 -0
- data/test/i2c_master/test_write.rb +29 -0
- data/test/i2c_master_test_helper.rb +28 -0
- data/test/pin/test_alias.rb +23 -0
- data/test/pin/test_clear.rb +15 -0
- data/test/pin/test_clear_interrupt.rb +10 -0
- data/test/pin/test_error.rb +35 -0
- data/test/pin/test_input.rb +23 -0
- data/test/pin/test_interrupt.rb +51 -0
- data/test/pin/test_is_clear.rb +17 -0
- data/test/pin/test_is_input.rb +14 -0
- data/test/pin/test_is_output.rb +14 -0
- data/test/pin/test_is_set.rb +17 -0
- data/test/pin/test_output.rb +38 -0
- data/test/pin/test_set.rb +15 -0
- data/test/pin_test_helper.rb +45 -0
- data/test/test_helper.rb +14 -0
- data/test/utils/test_error.rb +31 -0
- data/test/utils/test_interrupt.rb +109 -0
- data/test/utils_test_helper.rb +6 -0
- metadata +73 -5
@@ -0,0 +1,19 @@
|
|
1
|
+
module PiDriver
|
2
|
+
module Utils
|
3
|
+
class ArgumentHelper
|
4
|
+
attr_accessor :prefix, :suffix
|
5
|
+
|
6
|
+
def initialize(options = {})
|
7
|
+
@prefix = options[:prefix]
|
8
|
+
@suffix = options[:suffix]
|
9
|
+
end
|
10
|
+
|
11
|
+
def check(type, arg, valid_options)
|
12
|
+
valid_options_for_message = valid_options.map { |value| "#{value.inspect}"}.join(', ')
|
13
|
+
middle = "invalid argument #{arg.inspect} for #{type.inspect} expected to be one of #{valid_options_for_message}"
|
14
|
+
message = "#{@prefix if @prefix} #{middle} #{@suffix if @suffix}"
|
15
|
+
raise ArgumentError, message unless valid_options.include?(arg)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module PiDriver
|
2
|
+
module Utils
|
3
|
+
class Byte
|
4
|
+
ALL_BITS_LOW = 0b00000000
|
5
|
+
ALL_BITS_HIGH = 0b11111111
|
6
|
+
NUM_BITS_PER_BYTE = 8
|
7
|
+
|
8
|
+
VALID_BYTES = (ALL_BITS_LOW..ALL_BITS_HIGH).to_a
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def self.byte_to_bits(byte)
|
13
|
+
byte.to_s(2).rjust(NUM_BITS_PER_BYTE, '0').chars.map(&:to_i)
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.bits_to_byte(bits)
|
17
|
+
bits.join.to_i(2)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
module PiDriver
|
2
|
+
module Utils
|
3
|
+
class Interrupt
|
4
|
+
def initialize(edge, &block)
|
5
|
+
@argument_helper = Utils::ArgumentHelper.new prefix: "Utils::Interrupt"
|
6
|
+
@edge = edge
|
7
|
+
|
8
|
+
@argument_helper.check(:block, block_given?, [true])
|
9
|
+
@check = block
|
10
|
+
end
|
11
|
+
|
12
|
+
def start
|
13
|
+
@argument_helper.check(:block, block_given?, [true])
|
14
|
+
|
15
|
+
@thread = Thread.new do
|
16
|
+
last_state = check
|
17
|
+
loop do
|
18
|
+
new_state = check
|
19
|
+
edge = get_interrupt_edge(new_state, last_state)
|
20
|
+
yield(edge) if edge
|
21
|
+
last_state = new_state
|
22
|
+
end
|
23
|
+
end
|
24
|
+
@thread.tap { |me| me.abort_on_exception = true }
|
25
|
+
end
|
26
|
+
|
27
|
+
def clear
|
28
|
+
@thread.kill
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def check
|
34
|
+
state = @check.call
|
35
|
+
@argument_helper.check(:state, state, Utils::State::VALID_STATES)
|
36
|
+
state
|
37
|
+
end
|
38
|
+
|
39
|
+
def get_interrupt_edge(new_state, last_state)
|
40
|
+
rising_trigger = new_state == Utils::State::HIGH && last_state == Utils::State::LOW
|
41
|
+
falling_trigger = new_state == Utils::State::LOW && last_state == Utils::State::HIGH
|
42
|
+
|
43
|
+
return Utils::Edge::RISING if rising_trigger && (both? || rising?)
|
44
|
+
return Utils::Edge::FALLING if falling_trigger && (both? || falling?)
|
45
|
+
end
|
46
|
+
|
47
|
+
def both?
|
48
|
+
@edge == Utils::Edge::BOTH
|
49
|
+
end
|
50
|
+
|
51
|
+
def rising?
|
52
|
+
@edge == Utils::Edge::RISING
|
53
|
+
end
|
54
|
+
|
55
|
+
def falling?
|
56
|
+
@edge == Utils::Edge::FALLING
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require_relative '../mcp23017_test_helper'
|
2
|
+
|
3
|
+
class MCP23017AliasTest < MCP23017Test
|
4
|
+
def test_register_aliases
|
5
|
+
assert_equal @mcp23017.iodira.method(:bit0), @mcp23017.iodira.method(:io0)
|
6
|
+
assert_equal @mcp23017.ipola.method(:bit0), @mcp23017.ipola.method(:ip0)
|
7
|
+
assert_equal @mcp23017.gpintena.method(:bit0), @mcp23017.gpintena.method(:gpint0)
|
8
|
+
assert_equal @mcp23017.defvala.method(:bit0), @mcp23017.defvala.method(:def0)
|
9
|
+
assert_equal @mcp23017.intcona.method(:bit0), @mcp23017.intcona.method(:ioc0)
|
10
|
+
|
11
|
+
assert_equal @mcp23017.gppua.method(:bit0), @mcp23017.gppua.method(:pu0)
|
12
|
+
assert_equal @mcp23017.intfa.method(:bit0), @mcp23017.intfa.method(:int0)
|
13
|
+
assert_equal @mcp23017.intcapa.method(:bit0), @mcp23017.intcapa.method(:icp0)
|
14
|
+
assert_equal @mcp23017.gpioa.method(:bit0), @mcp23017.gpioa.method(:gp0)
|
15
|
+
assert_equal @mcp23017.olata.method(:bit0), @mcp23017.olata.method(:ol0)
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_iocon_aliases
|
19
|
+
assert_equal @mcp23017.iocon.method(:bit7), @mcp23017.iocon.method(:bank)
|
20
|
+
assert_equal @mcp23017.iocon.method(:bit6), @mcp23017.iocon.method(:mirror)
|
21
|
+
assert_equal @mcp23017.iocon.method(:bit5), @mcp23017.iocon.method(:seqop)
|
22
|
+
assert_equal @mcp23017.iocon.method(:bit4), @mcp23017.iocon.method(:disslw)
|
23
|
+
assert_equal @mcp23017.iocon.method(:bit3), @mcp23017.iocon.method(:haen)
|
24
|
+
assert_equal @mcp23017.iocon.method(:bit2), @mcp23017.iocon.method(:odr)
|
25
|
+
assert_equal @mcp23017.iocon.method(:bit1), @mcp23017.iocon.method(:intpol)
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require_relative '../mcp23017_test_helper'
|
2
|
+
|
3
|
+
class MCP23017ErrorTest < MCP23017Test
|
4
|
+
def test_error_hardware_address
|
5
|
+
assert_raises ArgumentError do
|
6
|
+
@mcp23017.hardware_address.a0 = 2
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_error_register_bit
|
11
|
+
assert_raises ArgumentError do
|
12
|
+
@mcp23017.iodira.bit0 = 2
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_error_register_port
|
17
|
+
assert_raises ArgumentError do
|
18
|
+
@mcp23017.iodira.byte = 256
|
19
|
+
end
|
20
|
+
|
21
|
+
assert_raises ArgumentError do
|
22
|
+
@mcp23017.iodira.byte = -1
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_error_write
|
27
|
+
assert_raises ArgumentError do
|
28
|
+
@mcp23017.write :invalid_register
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_error_read
|
33
|
+
assert_raises ArgumentError do
|
34
|
+
@mcp23017.read :invalid_register
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require_relative '../mcp23017_test_helper'
|
2
|
+
|
3
|
+
class MCP23017HardwareAddressTest < MCP23017Test
|
4
|
+
def test_low_new_default
|
5
|
+
mcp23017 = PiDriver::Device::MCP23017.new i2c_master: @i2c_master
|
6
|
+
assert_equal 0, mcp23017.hardware_address.a0
|
7
|
+
assert_equal 0, mcp23017.hardware_address.a1
|
8
|
+
assert_equal 0, mcp23017.hardware_address.a2
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_set_high
|
12
|
+
mcp23017 = PiDriver::Device::MCP23017.new i2c_master: @i2c_master
|
13
|
+
|
14
|
+
mcp23017.hardware_address.a0 = 1
|
15
|
+
mcp23017.hardware_address.a1 = 1
|
16
|
+
mcp23017.hardware_address.a2 = 1
|
17
|
+
|
18
|
+
assert_equal 1, mcp23017.hardware_address.a0
|
19
|
+
assert_equal 1, mcp23017.hardware_address.a1
|
20
|
+
assert_equal 1, mcp23017.hardware_address.a2
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_opcode_change
|
24
|
+
PiDriver::I2CMaster.expects(:prepare_address_for_write).with(0b0100110)
|
25
|
+
PiDriver::I2CMaster.expects(:prepare_address_for_read).with(0b0100110)
|
26
|
+
|
27
|
+
@mcp23017.hardware_address.a0 = 0
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require_relative '../mcp23017_test_helper'
|
2
|
+
|
3
|
+
class MCP23017I2CMasterTest < MCP23017Test
|
4
|
+
def test_prepare_address_new
|
5
|
+
PiDriver::I2CMaster.expects(:prepare_address_for_write).with(0b0100000)
|
6
|
+
PiDriver::I2CMaster.expects(:prepare_address_for_read).with(0b0100000)
|
7
|
+
|
8
|
+
PiDriver::Device::MCP23017.new i2c_master: @i2c_master
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require_relative '../mcp23017_test_helper'
|
2
|
+
|
3
|
+
class MCP23017ReadTest < MCP23017Test
|
4
|
+
def test_single
|
5
|
+
seq = sequence('single')
|
6
|
+
|
7
|
+
@i2c_master.expects(:start).with(nil).in_sequence(seq)
|
8
|
+
|
9
|
+
@i2c_master.expects(:write).with(0b01001110).in_sequence(seq)
|
10
|
+
@i2c_master.expects(:ack).with(nil).in_sequence(seq)
|
11
|
+
@i2c_master.expects(:write).with(0x15).in_sequence(seq)
|
12
|
+
@i2c_master.expects(:ack).with(nil).in_sequence(seq)
|
13
|
+
|
14
|
+
@i2c_master.expects(:restart).with(nil).in_sequence(seq)
|
15
|
+
|
16
|
+
@i2c_master.expects(:write).with(0b01001111).in_sequence(seq)
|
17
|
+
@i2c_master.expects(:ack).with(nil).in_sequence(seq)
|
18
|
+
@i2c_master.expects(:read).with(nil).returns(0b10101010).in_sequence(seq)
|
19
|
+
|
20
|
+
@i2c_master.expects(:stop).with(nil).in_sequence(seq)
|
21
|
+
|
22
|
+
@mcp23017.read :olatb
|
23
|
+
|
24
|
+
assert_equal 0b10101010, @mcp23017.olatb.byte
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_multiple
|
28
|
+
seq = sequence('multiple')
|
29
|
+
|
30
|
+
@i2c_master.expects(:start).with(nil).in_sequence(seq)
|
31
|
+
|
32
|
+
@i2c_master.expects(:write).with(0b01001110).in_sequence(seq)
|
33
|
+
@i2c_master.expects(:ack).with(nil).in_sequence(seq)
|
34
|
+
@i2c_master.expects(:write).with(0x14).in_sequence(seq)
|
35
|
+
@i2c_master.expects(:ack).with(nil).in_sequence(seq)
|
36
|
+
|
37
|
+
@i2c_master.expects(:restart).with(nil).in_sequence(seq)
|
38
|
+
|
39
|
+
@i2c_master.expects(:write).with(0b01001111).in_sequence(seq)
|
40
|
+
@i2c_master.expects(:ack).with(nil).in_sequence(seq)
|
41
|
+
@i2c_master.expects(:read).with(nil).returns(0b01010101).in_sequence(seq)
|
42
|
+
|
43
|
+
@i2c_master.expects(:restart).with(nil).in_sequence(seq)
|
44
|
+
|
45
|
+
@i2c_master.expects(:write).with(0b01001110).in_sequence(seq)
|
46
|
+
@i2c_master.expects(:ack).with(nil).in_sequence(seq)
|
47
|
+
@i2c_master.expects(:write).with(0x15).in_sequence(seq)
|
48
|
+
@i2c_master.expects(:ack).with(nil).in_sequence(seq)
|
49
|
+
|
50
|
+
@i2c_master.expects(:restart).with(nil).in_sequence(seq)
|
51
|
+
|
52
|
+
@i2c_master.expects(:write).with(0b01001111).in_sequence(seq)
|
53
|
+
@i2c_master.expects(:ack).with(nil).in_sequence(seq)
|
54
|
+
@i2c_master.expects(:read).with(nil).returns(0b10101010).in_sequence(seq)
|
55
|
+
|
56
|
+
@i2c_master.expects(:stop).with(nil).in_sequence(seq)
|
57
|
+
|
58
|
+
@mcp23017.read :olata, :olatb
|
59
|
+
|
60
|
+
assert_equal 0b01010101, @mcp23017.olata.byte
|
61
|
+
assert_equal 0b10101010, @mcp23017.olatb.byte
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,118 @@
|
|
1
|
+
require_relative '../mcp23017_test_helper'
|
2
|
+
|
3
|
+
class MCP23017RegisterTest < MCP23017Test
|
4
|
+
def test_addresses_bank_low
|
5
|
+
assert_equal 0x00, @mcp23017.iodira.address
|
6
|
+
assert_equal 0x01, @mcp23017.iodirb.address
|
7
|
+
|
8
|
+
assert_equal 0x02, @mcp23017.ipola.address
|
9
|
+
assert_equal 0x03, @mcp23017.ipolb.address
|
10
|
+
|
11
|
+
assert_equal 0x04, @mcp23017.gpintena.address
|
12
|
+
assert_equal 0x05, @mcp23017.gpintenb.address
|
13
|
+
|
14
|
+
assert_equal 0x06, @mcp23017.defvala.address
|
15
|
+
assert_equal 0x07, @mcp23017.defvalb.address
|
16
|
+
|
17
|
+
assert_equal 0x08, @mcp23017.intcona.address
|
18
|
+
assert_equal 0x09, @mcp23017.intconb.address
|
19
|
+
|
20
|
+
assert_equal 0x0a, @mcp23017.iocon.address
|
21
|
+
|
22
|
+
assert_equal 0x0c, @mcp23017.gppua.address
|
23
|
+
assert_equal 0x0d, @mcp23017.gppub.address
|
24
|
+
|
25
|
+
assert_equal 0x0e, @mcp23017.intfa.address
|
26
|
+
assert_equal 0x0f, @mcp23017.intfb.address
|
27
|
+
|
28
|
+
assert_equal 0x10, @mcp23017.intcapa.address
|
29
|
+
assert_equal 0x11, @mcp23017.intcapb.address
|
30
|
+
|
31
|
+
assert_equal 0x12, @mcp23017.gpioa.address
|
32
|
+
assert_equal 0x13, @mcp23017.gpiob.address
|
33
|
+
|
34
|
+
assert_equal 0x14, @mcp23017.olata.address
|
35
|
+
assert_equal 0x15, @mcp23017.olatb.address
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_addresses_bank_high
|
39
|
+
@mcp23017.iocon.bank = 1
|
40
|
+
assert_equal 0x05, @mcp23017.iocon.address
|
41
|
+
|
42
|
+
assert_equal 0x00, @mcp23017.iodira.address
|
43
|
+
assert_equal 0x01, @mcp23017.ipola.address
|
44
|
+
assert_equal 0x02, @mcp23017.gpintena.address
|
45
|
+
assert_equal 0x03, @mcp23017.defvala.address
|
46
|
+
assert_equal 0x04, @mcp23017.intcona.address
|
47
|
+
assert_equal 0x06, @mcp23017.gppua.address
|
48
|
+
assert_equal 0x07, @mcp23017.intfa.address
|
49
|
+
assert_equal 0x08, @mcp23017.intcapa.address
|
50
|
+
assert_equal 0x09, @mcp23017.gpioa.address
|
51
|
+
assert_equal 0x0a, @mcp23017.olata.address
|
52
|
+
|
53
|
+
assert_equal 0x10, @mcp23017.iodirb.address
|
54
|
+
assert_equal 0x11, @mcp23017.ipolb.address
|
55
|
+
assert_equal 0x12, @mcp23017.gpintenb.address
|
56
|
+
assert_equal 0x13, @mcp23017.defvalb.address
|
57
|
+
assert_equal 0x14, @mcp23017.intconb.address
|
58
|
+
assert_equal 0x16, @mcp23017.gppub.address
|
59
|
+
assert_equal 0x17, @mcp23017.intfb.address
|
60
|
+
assert_equal 0x18, @mcp23017.intcapb.address
|
61
|
+
assert_equal 0x19, @mcp23017.gpiob.address
|
62
|
+
assert_equal 0x1a, @mcp23017.olatb.address
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_byte_new
|
66
|
+
assert_equal 0b11111111, @mcp23017.iodira.byte
|
67
|
+
assert_equal 0b11111111, @mcp23017.iodirb.byte
|
68
|
+
|
69
|
+
assert_equal 0b00000000, @mcp23017.ipola.byte
|
70
|
+
assert_equal 0b00000000, @mcp23017.ipolb.byte
|
71
|
+
|
72
|
+
assert_equal 0b00000000, @mcp23017.gpintena.byte
|
73
|
+
assert_equal 0b00000000, @mcp23017.gpintenb.byte
|
74
|
+
|
75
|
+
assert_equal 0b00000000, @mcp23017.defvala.byte
|
76
|
+
assert_equal 0b00000000, @mcp23017.defvalb.byte
|
77
|
+
|
78
|
+
assert_equal 0b00000000, @mcp23017.intcona.byte
|
79
|
+
assert_equal 0b00000000, @mcp23017.intconb.byte
|
80
|
+
|
81
|
+
assert_equal 0b00000000, @mcp23017.iocon.byte
|
82
|
+
|
83
|
+
assert_equal 0b00000000, @mcp23017.gppua.byte
|
84
|
+
assert_equal 0b00000000, @mcp23017.gppub.byte
|
85
|
+
|
86
|
+
assert_equal 0b00000000, @mcp23017.intfa.byte
|
87
|
+
assert_equal 0b00000000, @mcp23017.intfb.byte
|
88
|
+
|
89
|
+
assert_equal 0b00000000, @mcp23017.intcapa.byte
|
90
|
+
assert_equal 0b00000000, @mcp23017.intcapb.byte
|
91
|
+
|
92
|
+
assert_equal 0b00000000, @mcp23017.gpioa.byte
|
93
|
+
assert_equal 0b00000000, @mcp23017.gpiob.byte
|
94
|
+
|
95
|
+
assert_equal 0b00000000, @mcp23017.olata.byte
|
96
|
+
assert_equal 0b00000000, @mcp23017.olatb.byte
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_writing_byte_is_mirrored_on_bits
|
100
|
+
@mcp23017.iodira.byte = 0b00000000
|
101
|
+
assert_equal 0, @mcp23017.iodira.bit0
|
102
|
+
assert_equal 0, @mcp23017.iodira.bit1
|
103
|
+
assert_equal 0, @mcp23017.iodira.bit2
|
104
|
+
assert_equal 0, @mcp23017.iodira.bit3
|
105
|
+
assert_equal 0, @mcp23017.iodira.bit4
|
106
|
+
assert_equal 0, @mcp23017.iodira.bit5
|
107
|
+
assert_equal 0, @mcp23017.iodira.bit6
|
108
|
+
assert_equal 0, @mcp23017.iodira.bit7
|
109
|
+
end
|
110
|
+
|
111
|
+
def test_writing_bits_is_mirrored_on_byte
|
112
|
+
@mcp23017.iodira.bit1 = 0
|
113
|
+
@mcp23017.iodira.bit3 = 0
|
114
|
+
@mcp23017.iodira.bit5 = 0
|
115
|
+
@mcp23017.iodira.bit7 = 0
|
116
|
+
assert_equal 0b01010101, @mcp23017.iodira.byte
|
117
|
+
end
|
118
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require_relative '../mcp23017_test_helper'
|
2
|
+
|
3
|
+
class MCP23017WriteTest < MCP23017Test
|
4
|
+
def test_single
|
5
|
+
seq = sequence('single')
|
6
|
+
|
7
|
+
@i2c_master.expects(:start).with(nil).in_sequence(seq)
|
8
|
+
|
9
|
+
@i2c_master.expects(:write).with(0b01001110).in_sequence(seq)
|
10
|
+
@i2c_master.expects(:ack).with(nil).in_sequence(seq)
|
11
|
+
@i2c_master.expects(:write).with(0x15).in_sequence(seq)
|
12
|
+
@i2c_master.expects(:ack).with(nil).in_sequence(seq)
|
13
|
+
@i2c_master.expects(:write).with(0b10101010).in_sequence(seq)
|
14
|
+
@i2c_master.expects(:ack).with(nil).in_sequence(seq)
|
15
|
+
|
16
|
+
@i2c_master.expects(:stop).with(nil).in_sequence(seq)
|
17
|
+
|
18
|
+
@mcp23017.olatb.byte = 0b10101010
|
19
|
+
@mcp23017.write :olatb
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_multiple
|
23
|
+
seq = sequence('multiple')
|
24
|
+
|
25
|
+
@i2c_master.expects(:start).with(nil).in_sequence(seq)
|
26
|
+
|
27
|
+
@i2c_master.expects(:write).with(0b01001110).in_sequence(seq)
|
28
|
+
@i2c_master.expects(:ack).with(nil).in_sequence(seq)
|
29
|
+
@i2c_master.expects(:write).with(0x14).in_sequence(seq)
|
30
|
+
@i2c_master.expects(:ack).with(nil).in_sequence(seq)
|
31
|
+
@i2c_master.expects(:write).with(0b01010101).in_sequence(seq)
|
32
|
+
@i2c_master.expects(:ack).with(nil).in_sequence(seq)
|
33
|
+
|
34
|
+
@i2c_master.expects(:restart).with(nil).in_sequence(seq)
|
35
|
+
|
36
|
+
@i2c_master.expects(:write).with(0b01001110).in_sequence(seq)
|
37
|
+
@i2c_master.expects(:ack).with(nil).in_sequence(seq)
|
38
|
+
@i2c_master.expects(:write).with(0x15).in_sequence(seq)
|
39
|
+
@i2c_master.expects(:ack).with(nil).in_sequence(seq)
|
40
|
+
@i2c_master.expects(:write).with(0b10101010).in_sequence(seq)
|
41
|
+
@i2c_master.expects(:ack).with(nil).in_sequence(seq)
|
42
|
+
|
43
|
+
@i2c_master.expects(:stop).with(nil).in_sequence(seq)
|
44
|
+
|
45
|
+
@mcp23017.olata.byte = 0b01010101
|
46
|
+
@mcp23017.olatb.byte = 0b10101010
|
47
|
+
@mcp23017.write :olata, :olatb
|
48
|
+
end
|
49
|
+
end
|