Skip to content

Commit e609120

Browse files
authored
Create code.py for new guide
Example of accessing the Tiny Code Reader from Useful Sensors on a Trinkey using CircuitPython. Pete Warden
1 parent 9e339cc commit e609120

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

Reading_QR_Codes/code.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# SPDX-FileCopyrightText: 2023 Pete Warden
2+
# SPDX-License-Identifier: Apache-2.0
3+
#
4+
# Example of accessing the Tiny Code Reader from Useful Sensors on a Trinkey
5+
# using CircuitPython. See https://usfl.ink/tcr_dev for the full developer guide.
6+
7+
import board
8+
import busio
9+
import struct
10+
import time
11+
import usb_hid
12+
13+
from adafruit_hid.keyboard import Keyboard
14+
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
15+
16+
# The code reader has the I2C ID of hex 0c, or decimal 12.
17+
TINY_CODE_READER_I2C_ADDRESS = 0x0C
18+
19+
# How long to pause between sensor polls.
20+
TINY_CODE_READER_DELAY = 0.2
21+
22+
TINY_CODE_READER_LENGTH_OFFSET = 0
23+
TINY_CODE_READER_LENGTH_FORMAT = "H"
24+
TINY_CODE_READER_MESSAGE_OFFSET = TINY_CODE_READER_LENGTH_OFFSET + struct.calcsize(TINY_CODE_READER_LENGTH_FORMAT)
25+
TINY_CODE_READER_MESSAGE_SIZE = 254
26+
TINY_CODE_READER_MESSAGE_FORMAT = "B" * TINY_CODE_READER_MESSAGE_SIZE
27+
TINY_CODE_READER_I2C_FORMAT = TINY_CODE_READER_LENGTH_FORMAT + TINY_CODE_READER_MESSAGE_FORMAT
28+
TINY_CODE_READER_I2C_BYTE_COUNT = struct.calcsize(TINY_CODE_READER_I2C_FORMAT)
29+
30+
i2c = board.I2C()
31+
32+
# Wait until we can access the bus.
33+
while not i2c.try_lock():
34+
pass
35+
36+
# For debugging purposes print out the peripheral addresses on the I2C bus.
37+
# 98 (0x62 in hex) is the address of our person sensor, and should be
38+
# present in the list. Uncomment the following three lines if you want to see
39+
# what I2C addresses are found.
40+
# while True:
41+
# print(i2c.scan())
42+
# time.sleep(TINY_CODE_READER_DELAY)
43+
44+
# Create a keyboard device so we can send the screen lock command.
45+
keyboard = Keyboard(usb_hid.devices)
46+
layout = KeyboardLayoutUS(keyboard)
47+
48+
last_message_string = None
49+
last_code_time = 0.0
50+
51+
while True:
52+
read_data = bytearray(TINY_CODE_READER_I2C_BYTE_COUNT)
53+
i2c.readfrom_into(TINY_CODE_READER_I2C_ADDRESS, read_data)
54+
55+
message_length, = struct.unpack_from(TINY_CODE_READER_LENGTH_FORMAT, read_data, TINY_CODE_READER_LENGTH_OFFSET)
56+
message_bytes = struct.unpack_from(TINY_CODE_READER_MESSAGE_FORMAT, read_data, TINY_CODE_READER_MESSAGE_OFFSET)
57+
58+
if message_length > 0:
59+
message_string = bytearray(message_bytes)[0:message_length].decode("utf-8")
60+
is_same = (message_string == last_message_string)
61+
last_message_string = message_string
62+
current_time = time.monotonic()
63+
time_since_last_code = current_time - last_code_time
64+
last_code_time = current_time
65+
# Debounce the input by making sure there's been a gap in time since we
66+
# last saw this code.
67+
if (not is_same) or (time_since_last_code > 1.0):
68+
print(message_string)
69+
try:
70+
layout.write(message_string)
71+
except ValueError as e:
72+
pass
73+
layout.write("\n")

0 commit comments

Comments
 (0)