Skip to content

Commit e70a8f9

Browse files
authored
Merge pull request #76 from haydenroche5/prepare_for_cobs
2 parents 04ecdeb + 1f783b8 commit e70a8f9

File tree

10 files changed

+1956
-506
lines changed

10 files changed

+1956
-506
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ flake8:
2929
# F403 'from module import *' used; unable to detect undefined names https://www.flake8rules.com/rules/F403.html
3030
# W503 Line break occurred before a binary operator https://www.flake8rules.com/rules/W503.html
3131
# E501 Line too long (>79 characters) https://www.flake8rules.com/rules/E501.html
32-
${PYTHON} -m flake8 test/ notecard/ examples/ mpy_board/ --count --ignore=E722,F401,F403,W503,E501 --show-source --statistics
32+
${PYTHON} -m flake8 test/ notecard/ examples/ mpy_board/ --count --ignore=E722,F401,F403,W503,E501,E502 --show-source --statistics
3333

3434
coverage:
3535
${RUN_VENV_ACTIVATE}

examples/notecard-basics/cpy_example.py

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,6 @@
1414
import busio # noqa: E402
1515

1616

17-
def NotecardExceptionInfo(exception):
18-
"""Construct a formatted Exception string.
19-
20-
Args:
21-
exception (Exception): An exception object.
22-
23-
Returns:
24-
string: a summary of the exception with line number and details.
25-
"""
26-
name = exception.__class__.__name__
27-
return sys.platform + ": " + name \
28-
+ ": " + " ".join(map(str, exception.args))
29-
30-
3117
def configure_notecard(card, product_uid):
3218
"""Submit a simple JSON-based request to the Notecard.
3319
@@ -39,11 +25,7 @@ def configure_notecard(card, product_uid):
3925
req["product"] = product_uid
4026
req["mode"] = "continuous"
4127

42-
try:
43-
card.Transaction(req)
44-
except Exception as exception:
45-
print("Transaction error: " + NotecardExceptionInfo(exception))
46-
time.sleep(5)
28+
card.Transaction(req)
4729

4830

4931
def get_temp_and_voltage(card):
@@ -53,20 +35,13 @@ def get_temp_and_voltage(card):
5335
card (object): An instance of the Notecard class
5436
5537
"""
56-
temp = 0
57-
voltage = 0
58-
59-
try:
60-
req = {"req": "card.temp"}
61-
rsp = card.Transaction(req)
62-
temp = rsp["value"]
38+
req = {"req": "card.temp"}
39+
rsp = card.Transaction(req)
40+
temp = rsp["value"]
6341

64-
req = {"req": "card.voltage"}
65-
rsp = card.Transaction(req)
66-
voltage = rsp["value"]
67-
except Exception as exception:
68-
print("Transaction error: " + NotecardExceptionInfo(exception))
69-
time.sleep(5)
42+
req = {"req": "card.voltage"}
43+
rsp = card.Transaction(req)
44+
voltage = rsp["value"]
7045

7146
return temp, voltage
7247

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

examples/notecard-basics/mpy_example.py

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,6 @@
1616
from machine import Pin
1717

1818

19-
def NotecardExceptionInfo(exception):
20-
"""Construct a formatted Exception string.
21-
22-
Args:
23-
exception (Exception): An exception object.
24-
25-
Returns:
26-
string: a summary of the exception with line number and details.
27-
"""
28-
name = exception.__class__.__name__
29-
return sys.platform + ": " + name + ": " \
30-
+ " ".join(map(str, exception.args))
31-
32-
3319
def configure_notecard(card, product_uid):
3420
"""Submit a simple JSON-based request to the Notecard.
3521
@@ -41,11 +27,7 @@ def configure_notecard(card, product_uid):
4127
req["product"] = product_uid
4228
req["mode"] = "continuous"
4329

44-
try:
45-
card.Transaction(req)
46-
except Exception as exception:
47-
print("Transaction error: " + NotecardExceptionInfo(exception))
48-
time.sleep(5)
30+
card.Transaction(req)
4931

5032

5133
def get_temp_and_voltage(card):
@@ -55,20 +37,13 @@ def get_temp_and_voltage(card):
5537
card (object): An instance of the Notecard class
5638
5739
"""
58-
temp = 0
59-
voltage = 0
60-
61-
try:
62-
req = {"req": "card.temp"}
63-
rsp = card.Transaction(req)
64-
temp = rsp["value"]
65-
66-
req = {"req": "card.voltage"}
67-
rsp = card.Transaction(req)
68-
voltage = rsp["value"]
69-
except Exception as exception:
70-
print("Transaction error: " + NotecardExceptionInfo(exception))
71-
time.sleep(5)
40+
req = {"req": "card.temp"}
41+
rsp = card.Transaction(req)
42+
temp = rsp["value"]
43+
44+
req = {"req": "card.voltage"}
45+
rsp = card.Transaction(req)
46+
voltage = rsp["value"]
7247

7348
return temp, voltage
7449

notecard/crc32.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""Module for computing the CRC32 of arbitrary data."""
2+
3+
crc32_lookup_table = [
4+
0x00000000, 0x1DB71064, 0x3B6E20C8, 0x26D930AC, 0x76DC4190, 0x6B6B51F4,
5+
0x4DB26158, 0x5005713C, 0xEDB88320, 0xF00F9344, 0xD6D6A3E8, 0xCB61B38C,
6+
0x9B64C2B0, 0x86D3D2D4, 0xA00AE278, 0xBDBDF21C
7+
]
8+
9+
10+
def _logical_rshift(val, shift_amount, num_bits=32):
11+
"""Logcally right shift `val` by `shift_amount` bits.
12+
13+
Logical right shift (i.e. right shift that fills with 0s instead of the
14+
sign bit) isn't supported natively in Python. This is a simple
15+
implementation. See:
16+
https://realpython.com/python-bitwise-operators/#arithmetic-vs-logical-shift
17+
"""
18+
unsigned_val = val % (1 << num_bits)
19+
return unsigned_val >> shift_amount
20+
21+
22+
def crc32(data):
23+
"""Compute CRC32 of the given data.
24+
25+
Small lookup-table half-byte CRC32 algorithm based on:
26+
https://create.stephan-brumme.com/crc32/#half-byte
27+
"""
28+
crc = ~0
29+
for idx in range(len(data)):
30+
crc = crc32_lookup_table[(crc ^ data[idx]) & 0x0F] ^ _logical_rshift(crc, 4)
31+
crc = crc32_lookup_table[(crc ^ _logical_rshift(data[idx], 4)) & 0x0F] ^ _logical_rshift(crc, 4)
32+
33+
return ~crc & 0xffffffff

0 commit comments

Comments
 (0)