adafruit_miniqr
is a A non-hardware dependant miniature QR generator library.
Once
Adafruit CircuitPython Libraries downloaded (as shown in
last post's video), there are two examples miniqr_simpletest.py and miniqr_displaytest.py in
extracted examples foler, or can be found
here.
cpyX_miniqr_ssd1306.py is modified from miniqr_displaytest.py,
to run on Seeed XIAO BLE Sense (nRF52840)/CircuitPython 7.2.3 to generate QR
Code and display on SSD1306 I2C OLED.
cpyX_miniqr_ssd1306.py
"""
CircuitPython 7.2.3 exercise run on
Seeed XIAO nRF52840 Sense with nRF52840
- generate QR Code using adafruit_miniqr
- display on SSD1306 I2C OLED
lib needed:
- adafruit_displayio_ssd1306.mpy
- adafruit_miniqr.mpy
"""
import os
import sys
import board
import busio
import displayio
import adafruit_displayio_ssd1306
import adafruit_miniqr
displayio.release_displays()
print("=================================================")
info = sys.implementation[0] + ' ' + os.uname()[3] + '\n' + \
'run on ' + os.uname()[4]
print(info)
print("=================================================")
print(adafruit_miniqr.__name__,
adafruit_miniqr.__version__)
print(adafruit_displayio_ssd1306.__name__,
adafruit_displayio_ssd1306.__version__)
# Create the I2C interface and display object of SSD1306_I2C.
i2c = busio.I2C(board.SCL, board.SDA)
ssd1306_i2c_addr = 60
display_width =128
display_height = 64
display_bus = displayio.I2CDisplay(
i2c, device_address=ssd1306_i2c_addr)
display = adafruit_displayio_ssd1306.SSD1306(
display_bus, width=display_width, height=display_height)
# remark by Erik:
# base on my testing on 128x64 SSD1306,
# A full white background can improve the recognition
# so I add a background in white
background_bitmap = displayio.Bitmap(display_width,
display_height, 1)
background_palette = displayio.Palette(1)
background_palette[0] = 0xFFFFFF
bg_sprite = displayio.TileGrid(background_bitmap,
pixel_shader=background_palette,
x=0, y=0)
def bitmap_QR(matrix):
# monochome (2 color) palette
BORDER_PIXELS = 2
# bitmap the size of the screen, monochrome (2 colors)
bitmap = displayio.Bitmap(
matrix.width + 2 * BORDER_PIXELS,
matrix.height + 2 * BORDER_PIXELS, 2
)
# raster the QR code
for y in range(matrix.height): # each scanline in the height
for x in range(matrix.width):
if matrix[x, y]:
bitmap[x + BORDER_PIXELS, y + BORDER_PIXELS] = 1
else:
bitmap[x + BORDER_PIXELS, y + BORDER_PIXELS] = 0
return bitmap
qr = adafruit_miniqr.QRCode(qr_type=3,
error_correct=adafruit_miniqr.L)
qr.add_data(b"http://embedded-things.blogspot.com/")
qr.make()
# generate the 1-pixel-per-bit bitmap
qr_bitmap = bitmap_QR(qr.matrix)
# We'll draw with a classic black/white palette
palette = displayio.Palette(2)
palette[0] = 0xFFFFFF
palette[1] = 0x000000
# we'll scale the QR code as big as the display can handle
scale = min(
display_width // qr_bitmap.width,
display_height // qr_bitmap.height
)
# then center it!
pos_x = int(((display_width / scale) - qr_bitmap.width) / 2)
pos_y = int(((display_height / scale) - qr_bitmap.height) / 2)
qr_img = displayio.TileGrid(qr_bitmap,
pixel_shader=palette,
x=pos_x, y=pos_y)
splash = displayio.Group(scale=scale)
splash.append(bg_sprite)
splash.append(qr_img)
display.show(splash)
# Hang out forever
while True:
pass
No comments:
Post a Comment