Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ flake8:
# F403 'from module import *' used; unable to detect undefined names https://www.flake8rules.com/rules/F403.html
# W503 Line break occurred before a binary operator https://www.flake8rules.com/rules/W503.html
# E501 Line too long (>79 characters) https://www.flake8rules.com/rules/E501.html
${PYTHON} -m flake8 test/ notecard/ examples/ mpy_board/ --count --ignore=E722,F401,F403,W503,E501 --show-source --statistics
${PYTHON} -m flake8 test/ notecard/ examples/ mpy_board/ --count --ignore=E722,F401,F403,W503,E501,E502 --show-source --statistics

coverage:
${RUN_VENV_ACTIVATE}
Expand Down
42 changes: 9 additions & 33 deletions examples/notecard-basics/cpy_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,6 @@
import busio # noqa: E402


def NotecardExceptionInfo(exception):
"""Construct a formatted Exception string.

Args:
exception (Exception): An exception object.

Returns:
string: a summary of the exception with line number and details.
"""
name = exception.__class__.__name__
return sys.platform + ": " + name \
+ ": " + " ".join(map(str, exception.args))


def configure_notecard(card, product_uid):
"""Submit a simple JSON-based request to the Notecard.

Expand All @@ -39,11 +25,7 @@ def configure_notecard(card, product_uid):
req["product"] = product_uid
req["mode"] = "continuous"

try:
card.Transaction(req)
except Exception as exception:
print("Transaction error: " + NotecardExceptionInfo(exception))
time.sleep(5)
card.Transaction(req)


def get_temp_and_voltage(card):
Expand All @@ -53,20 +35,13 @@ def get_temp_and_voltage(card):
card (object): An instance of the Notecard class

"""
temp = 0
voltage = 0

try:
req = {"req": "card.temp"}
rsp = card.Transaction(req)
temp = rsp["value"]
req = {"req": "card.temp"}
rsp = card.Transaction(req)
temp = rsp["value"]

req = {"req": "card.voltage"}
rsp = card.Transaction(req)
voltage = rsp["value"]
except Exception as exception:
print("Transaction error: " + NotecardExceptionInfo(exception))
time.sleep(5)
req = {"req": "card.voltage"}
rsp = card.Transaction(req)
voltage = rsp["value"]

return temp, voltage

Expand All @@ -75,7 +50,8 @@ def run_example(product_uid, use_uart=True):
"""Connect to Notcard and run a transaction test."""
print("Opening port...")
if use_uart:
port = busio.UART(board.TX, board.RX, baudrate=9600)
port = busio.UART(board.TX, board.RX, baudrate=9600,
receiver_buffer_size=128)
else:
port = busio.I2C(board.SCL, board.SDA)

Expand Down
41 changes: 8 additions & 33 deletions examples/notecard-basics/mpy_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,6 @@
from machine import Pin


def NotecardExceptionInfo(exception):
"""Construct a formatted Exception string.

Args:
exception (Exception): An exception object.

Returns:
string: a summary of the exception with line number and details.
"""
name = exception.__class__.__name__
return sys.platform + ": " + name + ": " \
+ " ".join(map(str, exception.args))


def configure_notecard(card, product_uid):
"""Submit a simple JSON-based request to the Notecard.

Expand All @@ -41,11 +27,7 @@ def configure_notecard(card, product_uid):
req["product"] = product_uid
req["mode"] = "continuous"

try:
card.Transaction(req)
except Exception as exception:
print("Transaction error: " + NotecardExceptionInfo(exception))
time.sleep(5)
card.Transaction(req)


def get_temp_and_voltage(card):
Expand All @@ -55,20 +37,13 @@ def get_temp_and_voltage(card):
card (object): An instance of the Notecard class

"""
temp = 0
voltage = 0

try:
req = {"req": "card.temp"}
rsp = card.Transaction(req)
temp = rsp["value"]

req = {"req": "card.voltage"}
rsp = card.Transaction(req)
voltage = rsp["value"]
except Exception as exception:
print("Transaction error: " + NotecardExceptionInfo(exception))
time.sleep(5)
req = {"req": "card.temp"}
rsp = card.Transaction(req)
temp = rsp["value"]

req = {"req": "card.voltage"}
rsp = card.Transaction(req)
voltage = rsp["value"]

return temp, voltage

Expand Down
33 changes: 33 additions & 0 deletions notecard/crc32.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""Module for computing the CRC32 of arbitrary data."""

crc32_lookup_table = [
0x00000000, 0x1DB71064, 0x3B6E20C8, 0x26D930AC, 0x76DC4190, 0x6B6B51F4,
0x4DB26158, 0x5005713C, 0xEDB88320, 0xF00F9344, 0xD6D6A3E8, 0xCB61B38C,
0x9B64C2B0, 0x86D3D2D4, 0xA00AE278, 0xBDBDF21C
]


def _logical_rshift(val, shift_amount, num_bits=32):
"""Logcally right shift `val` by `shift_amount` bits.

Logical right shift (i.e. right shift that fills with 0s instead of the
sign bit) isn't supported natively in Python. This is a simple
implementation. See:
https://realpython.com/python-bitwise-operators/#arithmetic-vs-logical-shift
"""
unsigned_val = val % (1 << num_bits)
return unsigned_val >> shift_amount


def crc32(data):
"""Compute CRC32 of the given data.

Small lookup-table half-byte CRC32 algorithm based on:
https://create.stephan-brumme.com/crc32/#half-byte
"""
crc = ~0
for idx in range(len(data)):
crc = crc32_lookup_table[(crc ^ data[idx]) & 0x0F] ^ _logical_rshift(crc, 4)
crc = crc32_lookup_table[(crc ^ _logical_rshift(data[idx], 4)) & 0x0F] ^ _logical_rshift(crc, 4)

return ~crc & 0xffffffff
Loading