Skip to content

Commit

Permalink
I2C calls to Board now have i2c dev index as first arg
Browse files Browse the repository at this point in the history
Ignored on microcontrollers but will be used on Linux
  • Loading branch information
vickash committed Sep 27, 2024
1 parent 10bb91d commit 7583940
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 39 deletions.
14 changes: 7 additions & 7 deletions lib/denko/board/i2c.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def i2c_convert_frequency(freq)
freq = 100000 unless freq

unless I2C_FREQUENCIES.include?(freq)
raise ArgumentError, "I2C frequency must be in: #{I2C_FREQUENCIES.keys.inspect}"
raise ArgumentError, "I2C frequency must be in: #{I2C_FREQUENCIES.keys.inspect}"
end
I2C_FREQUENCIES[freq]
end
Expand All @@ -25,22 +25,22 @@ def i2c_search
end

# CMD = 34
def i2c_write(address, bytes, frequency=100000, repeated_start=false)
def i2c_write(i2c_index, address, bytes, frequency=100000, repeated_start=false)
bytes = [bytes] unless bytes.class == Array
raise ArgumentError, "I2C write must be 1..#{i2c_limit} bytes long" if (bytes.length > i2c_limit || bytes.length < 1)

# Use top bit of address to select stop condition (1), or repated start (0).
send_stop = repeated_start ? 0 : 1

write Message.encode command: 34,
aux_message: pack(:uint8, i2c_convert_frequency(frequency)) +
aux_message: pack(:uint8, i2c_convert_frequency(frequency)) +
pack(:uint8, address | (send_stop << 7)) +
pack(:uint8, bytes.length) +
pack(:uint8, bytes)
end

# CMD = 35
def i2c_read(address, register, read_length, frequency=100000, repeated_start=false)
def i2c_read(i2c_index, address, register, read_length, frequency=100000, repeated_start=false)
raise ArgumentError, "I2C read must be 1..#{i2c_limit} bytes long" if (read_length > i2c_limit || read_length < 1)

# Use top bit of address to select stop condition (1), or repated start (0).
Expand All @@ -56,7 +56,7 @@ def i2c_read(address, register, read_length, frequency=100000, repeated_start=fa
end

write Message.encode command: 35,
aux_message: pack(:uint8, i2c_convert_frequency(frequency)) +
aux_message: pack(:uint8, i2c_convert_frequency(frequency)) +
pack(:uint8, address | (send_stop << 7)) +
pack(:uint8, read_length) +
register_packed
Expand Down
4 changes: 2 additions & 2 deletions lib/denko/i2c/bus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ def search
end

def write(address, bytes, frequency=100000, repeated_start=false)
board.i2c_write(address, bytes, frequency, repeated_start)
board.i2c_write(i2c_index, address, bytes, frequency, repeated_start)
end

def _read(address, register, num_bytes, frequency=100000, repeated_start=false)
board.i2c_read(address, register, num_bytes, frequency, repeated_start)
board.i2c_read(i2c_index, address, register, num_bytes, frequency, repeated_start)
end

def bubble_callbacks
Expand Down
6 changes: 3 additions & 3 deletions test/behaviors/subcomponents_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ def test_calls_stop_on_remove

def test_disallows_duplicate_hw_i2c_buses
i2c0 = Denko::I2C::Bus.new(board: board, pin: 0)
assert_raises { Denko::I2C::Bus.new(board: board, pin: 1) }
assert_raises { Denko::I2C::Bus.new(board: board) }
end

def test_hw_i2c_buses_add_and_remove_properly
i2c0 = Denko::I2C::Bus.new(board: board, pin: 10, i2c_index: 0)
i2c1 = Denko::I2C::Bus.new(board: board, pin: 11, i2c_index: 1)
i2c0 = Denko::I2C::Bus.new(board: board, i2c_index: 0)
i2c1 = Denko::I2C::Bus.new(board: board, i2c_index: 1)
assert_equal [i2c0, i2c1], board.components

# Have the right hash keys.
Expand Down
44 changes: 22 additions & 22 deletions test/board/i2c_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

class APII2CTest < Minitest::Test
include TestPacker

def connection
@connection ||= ConnectionMock.new
end

def board
@board ||= Denko::Board.new(connection)
end

def test_search
board
message = Denko::Message.encode command: 33

mock = Minitest::Mock.new.expect :call, nil, [message]
connection.stub(:write, mock) do
board.i2c_search
Expand All @@ -37,17 +37,17 @@ def test_write
mock = Minitest::Mock.new
mock.expect :call, nil, [message1]
mock.expect :call, nil, [message2]

connection.stub(:write, mock) do
board.i2c_write(0x30, [1,2,3,4])
board.i2c_write(0x30, [1,2,3,4], 100000, true)
board.i2c_write(0, 0x30, [1,2,3,4])
board.i2c_write(0, 0x30, [1,2,3,4], 100000, true)
end
mock.verify
end

def test_write_limits
assert_raises { board.i2c_write(0x30, Array.new(33) {0x00}) }
assert_raises { board.i2c_write(0x30, Array.new(0) {0x00}) }
assert_raises { board.i2c_write(0, 0x30, Array.new(33) {0x00}) }
assert_raises { board.i2c_write(0, 0x30, Array.new(0) {0x00}) }
end

def test_read
Expand All @@ -64,38 +64,38 @@ def test_read
mock = Minitest::Mock.new
mock.expect :call, nil, [message1]
mock.expect :call, nil, [message2]

connection.stub(:write, mock) do
board.i2c_read(0x30, 0x03, 4)
board.i2c_read(0x30, 0x03, 4, 100000, true)
board.i2c_read(0, 0x30, 0x03, 4)
board.i2c_read(0, 0x30, 0x03, 4, 100000, true)
end
mock.verify
end

def test_read_without_register
board
aux = pack(:uint8, 0x00) + pack(:uint8, 0x30 | (1 << 7)) + pack(:uint8, 4) + pack(:uint8, [0])
message = Denko::Message.encode command: 35, aux_message: aux

mock = Minitest::Mock.new
mock.expect :call, nil, [message]

connection.stub(:write, mock) do
board.i2c_read(0x30, nil, 4)
board.i2c_read(0, 0x30, nil, 4)
end
mock.verify
end

def test_read_limits
assert_raises { board.i2c_read(0x30, nil, 33) }
assert_raises { board.i2c_read(0x30, nil, 0) }
assert_raises { board.i2c_read(0, 0x30, nil, 33) }
assert_raises { board.i2c_read(0, 0x30, nil, 0) }
end

def test_frequencies
board
data = [1,2,3,4]
address = 0x30

messages = []
# 100 kHz, 400 kHz, 1 Mhz, 3.4 MHz
aux_after_config = pack(:uint8, 0x30 | (1 << 7)) + pack(:uint8, data.length) + pack(:uint8, data)
Expand All @@ -108,13 +108,13 @@ def test_frequencies
mock.expect :call, nil, [message]
end
connection.stub(:write, mock) do
board.i2c_write(address, data, 100000)
board.i2c_write(address, data, 400000)
board.i2c_write(address, data, 1000000)
board.i2c_write(address, data, 3400000)
board.i2c_write(0, address, data, 100000)
board.i2c_write(0, address, data, 400000)
board.i2c_write(0, address, data, 1000000)
board.i2c_write(0, address, data, 3400000)
end
mock.verify

assert_raises(ArgumentError) { board.i2c_write(0x30, [1,2,3,4], 5000000, false) }
assert_raises(ArgumentError) { board.i2c_write(0, 0x30, [1,2,3,4], 5000000, false) }
end
end
10 changes: 5 additions & 5 deletions test/i2c/bus_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def board
end

def part
@part ||= Denko::I2C::Bus.new(board: board, pin:5)
@part ||= Denko::I2C::Bus.new(board: board)
end

def peripheral
Expand All @@ -22,10 +22,10 @@ def test_initialize
assert_equal 0, part.i2c_index
refute_nil part.callbacks[:bus_controller]

part2 = Denko::I2C::Bus.new(board: board, pin: 6, index: 10)
part2 = Denko::I2C::Bus.new(board: board, index: 10)
assert_equal 10, part2.i2c_index

part3 = Denko::I2C::Bus.new(board: board, pin: 7, i2c_index: 11)
part3 = Denko::I2C::Bus.new(board: board, i2c_index: 11)
assert_equal 11, part3.i2c_index
end

Expand All @@ -42,7 +42,7 @@ def test_search
end

def test_write
mock = Minitest::Mock.new.expect :call, nil, [0x30, [0x01, 0x02], 100000, false]
mock = Minitest::Mock.new.expect :call, nil, [0, 0x30, [0x01, 0x02], 100000, false]
board.stub(:i2c_write, mock) do
part.write 0x30, [0x01, 0x02]
end
Expand All @@ -52,7 +52,7 @@ def test_write
def test__read
board.inject_read_for_i2c(0, "48-255,0,255,0,255,0")

mock = Minitest::Mock.new.expect :call, nil, [0x32, 0x03, 6, 100000, false]
mock = Minitest::Mock.new.expect :call, nil, [0, 0x32, 0x03, 6, 100000, false]
board.stub(:i2c_read, mock) do
part.read 0x32, 0x03, 6
end
Expand Down

0 comments on commit 7583940

Please sign in to comment.