Add tests
This commit is contained in:
parent
b809b88f53
commit
b06b162b2d
3 changed files with 49 additions and 16 deletions
33
mcp4.py
33
mcp4.py
|
@ -71,6 +71,22 @@ class TerminalControl:
|
|||
)
|
||||
|
||||
|
||||
def command_bytes(address: int, command: int, data: int = 0x0) -> bytearray:
|
||||
"""Translate an address, command, and data into bytes to send.
|
||||
|
||||
- Address is a 4-bit memory address.
|
||||
- Command is a 2-bit command code.
|
||||
- Data is 2 bits for increment and decrement operations (ignored), and
|
||||
10 bits for read and write operations.
|
||||
|
||||
"""
|
||||
command_byte = address << 4 & 0b11110000 | command << 2 & 0b00001100
|
||||
if command in (0b00, 0b11):
|
||||
# Include data byte for 10 total bits of data
|
||||
return bytearray([command_byte | (0b11 & data >> 8), data & 0xFF])
|
||||
return bytearray([command_byte])
|
||||
|
||||
|
||||
class MCP4:
|
||||
"""MicroPython MCP413X/415X/423X/425X SPI driver"""
|
||||
|
||||
|
@ -88,21 +104,6 @@ class MCP4:
|
|||
self.spi = spi
|
||||
self.cs = cs
|
||||
|
||||
def _bytes(self, address: int, command: int, data: int = 0x0) -> bytearray:
|
||||
"""Translate an address, command, and data into bytes to send.
|
||||
|
||||
- Address is a 4-bit memory address.
|
||||
- Command is a 2-bit command code.
|
||||
- Data is 2 bits for increment and decrement operations (ignored), and
|
||||
10 bits for read and write operations.
|
||||
|
||||
"""
|
||||
command_byte = address << 4 & 0b11110000 | command << 2 & 0b00001100
|
||||
if command in (0b00, 0b11):
|
||||
# Include data byte for 10 total bits of data
|
||||
return bytearray([command_byte | (0b11 & data >> 8), data & 0xFF])
|
||||
return bytearray([command_byte])
|
||||
|
||||
def _write(self, data: bytearray) -> bytearray:
|
||||
"""Write data to the SPI interface, returning its output."""
|
||||
output = bytearray(len(data))
|
||||
|
@ -112,7 +113,7 @@ class MCP4:
|
|||
def do(self, address: int, command: int, data: int = 0x0) -> int:
|
||||
"""Execute a command on the MCP4, returning its integer result."""
|
||||
self.cs(0)
|
||||
output = self._write(self._bytes(address, command, data))
|
||||
output = self._write(command_bytes(address, command, data))
|
||||
self.cs(1)
|
||||
|
||||
OK = 0b11111110
|
||||
|
|
1
tests/__init__.py
Normal file
1
tests/__init__.py
Normal file
|
@ -0,0 +1 @@
|
|||
from .test_mcp4 import *
|
31
tests/test_mcp4.py
Normal file
31
tests/test_mcp4.py
Normal file
|
@ -0,0 +1,31 @@
|
|||
import unittest
|
||||
|
||||
import mcp4
|
||||
|
||||
|
||||
class CommandTests(unittest.TestCase):
|
||||
def assertBitsEqual(self, data: bytearray, expected: str):
|
||||
self.assertEqual(
|
||||
"".join("{:08b}".format(byte) for byte in data),
|
||||
expected.replace(" ", ""),
|
||||
)
|
||||
|
||||
def test_increment(self) -> None:
|
||||
self.assertBitsEqual(mcp4.command_bytes(0b0000, 0b01, 0b00), "0000 01 00")
|
||||
self.assertBitsEqual(mcp4.command_bytes(0b0001, 0b01, 0b00), "0001 01 00")
|
||||
|
||||
def test_decrement(self) -> None:
|
||||
self.assertBitsEqual(mcp4.command_bytes(0b0000, 0b10, 0b00), "0000 10 00")
|
||||
self.assertBitsEqual(mcp4.command_bytes(0b0001, 0b10, 0b00), "0001 10 00")
|
||||
|
||||
def test_read(self) -> None:
|
||||
self.assertBitsEqual(
|
||||
mcp4.command_bytes(0b0000, 0b11, 0b0000000000),
|
||||
"0000 11 00 0000 0000",
|
||||
)
|
||||
|
||||
def test_write(self) -> None:
|
||||
self.assertBitsEqual(
|
||||
mcp4.command_bytes(0b0000, 0b00, 0b0001111111),
|
||||
"0000 00 00 0111 1111",
|
||||
)
|
Loading…
Reference in a new issue