|
| 1 | +"""note-python binary loopback example. |
| 2 | +
|
| 3 | +This example writes an array of bytes to the binary data store on a Notecard and |
| 4 | +reads them back. It checks that what was written exactly matches what's read |
| 5 | +back. |
| 6 | +
|
| 7 | +Supports MicroPython, CircuitPython, and Raspberry Pi (Linux). |
| 8 | +""" |
| 9 | +import sys |
| 10 | + |
| 11 | + |
| 12 | +def run_example(product_uid, use_uart=True): |
| 13 | + """Connect to Notcard and run a binary loopback test.""" |
| 14 | + tx_buf = bytearray([ |
| 15 | + 0x67, 0x48, 0xa8, 0x1e, 0x9f, 0xbb, 0xb7, 0x27, 0xbb, 0x31, 0x89, 0x00, 0x1f, |
| 16 | + 0x60, 0x49, 0x8a, 0x63, 0xa1, 0x2b, 0xac, 0xb8, 0xa9, 0xb0, 0x59, 0x71, 0x65, |
| 17 | + 0xdd, 0x87, 0x73, 0x8a, 0x06, 0x9d, 0x40, 0xc1, 0xee, 0x24, 0xca, 0x31, 0xee, |
| 18 | + 0x88, 0xf7, 0xf1, 0x23, 0x60, 0xf2, 0x01, 0x98, 0x39, 0x21, 0x18, 0x25, 0x3c, |
| 19 | + 0x36, 0xf7, 0x93, 0xae, 0x50, 0xd6, 0x7d, 0x93, 0x55, 0xff, 0xcb, 0x56, 0xd3, |
| 20 | + 0xd3, 0xd5, 0xe9, 0xf0, 0x60, 0xf7, 0xe9, 0xd3, 0xa4, 0x40, 0xe7, 0x8a, 0x71, |
| 21 | + 0x72, 0x8b, 0x28, 0x5d, 0x57, 0x57, 0x8c, 0xc3, 0xd4, 0xe2, 0x05, 0xfa, 0x98, |
| 22 | + 0xd2, 0x26, 0x4f, 0x5d, 0xb3, 0x08, 0x02, 0xf2, 0x50, 0x23, 0x5d, 0x9c, 0x6e, |
| 23 | + 0x63, 0x7e, 0x03, 0x22, 0xa5, 0xb3, 0x5e, 0x95, 0xf2, 0x74, 0xfd, 0x3c, 0x2d, |
| 24 | + 0x06, 0xf8, 0xdc, 0x34, 0xe4, 0x3d, 0x42, 0x47, 0x7c, 0x61, 0xe6, 0xe1, 0x53 |
| 25 | + ]) |
| 26 | + |
| 27 | + biggest_notecard_response = 400 |
| 28 | + binary_chunk_size = 32 |
| 29 | + uart_rx_buf_size = biggest_notecard_response + binary_chunk_size |
| 30 | + |
| 31 | + if sys.implementation.name == 'micropython': |
| 32 | + from machine import UART |
| 33 | + from machine import I2C |
| 34 | + from machine import Pin |
| 35 | + import board |
| 36 | + |
| 37 | + if use_uart: |
| 38 | + port = UART(board.UART, 9600) |
| 39 | + port.init(9600, bits=8, parity=None, stop=1, |
| 40 | + timeout=3000, timeout_char=100, rxbuf=uart_rx_buf_size) |
| 41 | + else: |
| 42 | + port = I2C(board.I2C_ID, scl=Pin(board.SCL), sda=Pin(board.SDA)) |
| 43 | + elif sys.implementation.name == 'circuitpython': |
| 44 | + import busio |
| 45 | + import board |
| 46 | + |
| 47 | + if use_uart: |
| 48 | + port = busio.UART(board.TX, board.RX, baudrate=9600, receiver_buffer_size=uart_rx_buf_size) |
| 49 | + else: |
| 50 | + port = busio.I2C(board.SCL, board.SDA) |
| 51 | + else: |
| 52 | + import os |
| 53 | + |
| 54 | + sys.path.insert(0, os.path.abspath( |
| 55 | + os.path.join(os.path.dirname(__file__), '..'))) |
| 56 | + |
| 57 | + from periphery import I2C |
| 58 | + import serial |
| 59 | + |
| 60 | + if use_uart: |
| 61 | + port = serial.Serial('/dev/ttyACM0', 9600) |
| 62 | + else: |
| 63 | + port = I2C('/dev/i2c-1') |
| 64 | + |
| 65 | + import notecard |
| 66 | + from notecard import binary_helpers |
| 67 | + |
| 68 | + if use_uart: |
| 69 | + card = notecard.OpenSerial(port, debug=True) |
| 70 | + else: |
| 71 | + card = notecard.OpenI2C(port, 0, 0, debug=True) |
| 72 | + |
| 73 | + print('Clearing out any old data...') |
| 74 | + binary_helpers.binary_store_reset(card) |
| 75 | + |
| 76 | + print('Sending buffer...') |
| 77 | + binary_helpers.binary_store_transmit(card, tx_buf, 0) |
| 78 | + print(f'Sent {len(tx_buf)} bytes to the Notecard.') |
| 79 | + |
| 80 | + print('Reading it back...') |
| 81 | + rx_buf = bytearray() |
| 82 | + |
| 83 | + left = binary_helpers.binary_store_decoded_length(card) |
| 84 | + offset = 0 |
| 85 | + while left > 0: |
| 86 | + chunk_size = left if binary_chunk_size > left else binary_chunk_size |
| 87 | + chunk = binary_helpers.binary_store_receive(card, offset, chunk_size) |
| 88 | + rx_buf.extend(chunk) |
| 89 | + left -= chunk_size |
| 90 | + offset += chunk_size |
| 91 | + |
| 92 | + print(f'Received {len(rx_buf)} bytes from the Notecard.') |
| 93 | + |
| 94 | + print('Checking if received matches transmitted...') |
| 95 | + rx_len = len(rx_buf) |
| 96 | + tx_len = len(tx_buf) |
| 97 | + assert rx_len == tx_len, f'Length mismatch between sent and received data. Sent {tx_len} bytes. Received {rx_len} bytes.' |
| 98 | + |
| 99 | + for idx, (tx_byte, rx_byte) in enumerate(zip(tx_buf, rx_buf)): |
| 100 | + assert tx_byte == rx_byte, f'Data mismatch detected at index {idx}. Sent: {tx_byte}. Received: {rx_byte}.' |
| 101 | + |
| 102 | + print('Received matches transmitted.') |
| 103 | + print('Example complete.') |
| 104 | + |
| 105 | + |
| 106 | +if __name__ == '__main__': |
| 107 | + product_uid = 'com.your-company.your-project' |
| 108 | + # Choose either UART or I2C for Notecard |
| 109 | + use_uart = True |
| 110 | + run_example(product_uid, use_uart) |
0 commit comments