Skip to content

Commit

Permalink
feat: Add the update command
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeshiner committed Mar 6, 2020
1 parent 7a1f879 commit d97962a
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 7 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,11 @@ The **verify** command takes a binary file and compares it to the contents of th
eeprom verify binary_file.bin
$ 005A expected 2A found 3F
```

## update

The **update** command updates the contents of the EEPROM where it differs from the contents of a passed binary file. If they are similar this is much faster than writing the entire EEPROM, however it does add an extra read operation so if the contents differ greatly it can take longer.

```bash
eeprom update binary_file.bin
```
23 changes: 23 additions & 0 deletions eeprom/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,32 @@ def verify(binary_file):
exit(1)


@click.command()
@click.argument("binary_file", type=click.File("rb"))
def update(binary_file):
"""
Update the contents of the EEPROM with the contents of a binary file.
This will read the EEPROM and overwrite any data that does not match the provided
file. This is done in blocks of 16 addresses. When the existing contents are similar
to the contents of the file this is much quicker than write command, however when
they differ greatly the extra time taken to read the EEPROM should be taken into
account.
"""
programmer = Programmer()
new_data = list(binary_file.read())
existing_data = programmer.read()
for address in range(0, len(new_data), 16):
new_block = new_data[address : address + 16]
existing_block = existing_data[address : address + 16]
if new_block != existing_block:
programmer.write_block(address, new_block)


cli.add_command(version)
cli.add_command(read_byte)
cli.add_command(write_byte)
cli.add_command(write)
cli.add_command(read)
cli.add_command(verify)
cli.add_command(update)
7 changes: 7 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,10 @@ def _read_serial_responses(data):
return messages

return _read_serial_responses


@pytest.fixture
def altered_binary_file_contents(binary_file_contents):
altered_binary_file_contents = list(binary_file_contents)
altered_binary_file_contents[0x0050] = 0xFF
return altered_binary_file_contents
73 changes: 73 additions & 0 deletions tests/test_cli/test_update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import pytest

from eeprom import cli


@pytest.fixture
def altered_binary_file_contents(binary_file_contents):
altered_binary_file_contents = list(binary_file_contents)
altered_binary_file_contents[0x0050] = 0xFF
return altered_binary_file_contents


@pytest.fixture
def matching_update_result(
default_programmer,
runner,
read_serial_responses,
binary_file_path,
binary_file_contents,
):
default_programmer.arduino.serial_connection.readline.side_effect = read_serial_responses(
binary_file_contents
)
return runner.invoke(cli, f"update {binary_file_path}")


@pytest.fixture
def non_matching_update_result(
default_programmer,
runner,
read_serial_responses,
binary_file_path,
altered_binary_file_contents,
serial_ack,
):
default_programmer.arduino.serial_connection.readline.side_effect = read_serial_responses(
altered_binary_file_contents
) + [
serial_ack
]
return runner.invoke(cli, f"update {binary_file_path}")


def test_matching_exit_code(matching_update_result):
assert matching_update_result.exit_code == 0


def test_non_matching_exit_code(non_matching_update_result):
assert non_matching_update_result.exit_code == 0


def test_matching_serial_message_sent(
default_programmer,
matching_update_result,
assert_messages_sent,
read_serial_requests,
binary_file_contents,
):
assert_messages_sent(default_programmer, read_serial_requests(binary_file_contents))


def test_non_matching_serial_message_sent(
default_programmer,
non_matching_update_result,
assert_messages_sent,
read_serial_requests,
binary_file_contents,
):
block = "".join([f"{_:02X}" for _ in binary_file_contents[0x50 : 0x50 + 16]])
write_messages = [f"S0050{block}\n".encode()]
assert_messages_sent(
default_programmer, read_serial_requests(binary_file_contents) + write_messages
)
7 changes: 0 additions & 7 deletions tests/test_cli/test_verify.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@
from eeprom import cli


@pytest.fixture
def altered_binary_file_contents(binary_file_contents):
altered_binary_file_contents = list(binary_file_contents)
altered_binary_file_contents[0x0050] = 0xFF
return altered_binary_file_contents


@pytest.fixture
def matching_verify_result(
default_programmer,
Expand Down

0 comments on commit d97962a

Please sign in to comment.