Exemplo n.º 1
0
def generate_qr_code_display_group(url):
    """Generate and display a QR code for the given URL"""
    qr_code = adafruit_miniqr.QRCode(qr_type=2)
    qr_code.add_data(bytearray(url))
    qr_code.make()

    palette = displayio.Palette(2)
    palette[0] = 0xFFFFFF
    palette[1] = 0x000000

    qr_bitmap = pybadger.bitmap_qr(qr_code.matrix)
    qr_img = displayio.TileGrid(
        qr_bitmap,
        pixel_shader=palette,
        x=int(qr_bitmap.width / 2),
        y=2,
    )

    qr_code = displayio.Group(scale=3)
    qr_code.append(qr_img)

    qr_and_text_group = displayio.Group()
    qr_and_text_group.append(qr_code)
    qr_and_text_group.append(
        pybadger._create_label_group(text=url,
                                     font=terminalio.FONT,
                                     scale=1,
                                     height_adjustment=0.84))
    return qr_and_text_group
    def show_qr_code(self, data=b'https://circuitpython.org', dwell=20):
        """Generate a QR code and display it for ``dwell`` seconds.

        :param bytearray data: A bytearray of data for the QR code
        :param int dwell: The amount of time in seconds to display the QR code

        """
        qr_code = adafruit_miniqr.QRCode(qr_type=3,
                                         error_correct=adafruit_miniqr.L)
        qr_code.add_data(data)
        qr_code.make()
        qr_bitmap = self.bitmap_qr(qr_code.matrix)
        palette = displayio.Palette(2)
        palette[0] = 0xFFFFFF
        palette[1] = 0x000000
        qr_code_scale = min(self.display.width // qr_bitmap.width,
                            self.display.height // qr_bitmap.height)
        qr_position_x = int(
            ((self.display.width / qr_code_scale) - qr_bitmap.width) / 2)
        qr_position_y = int(
            ((self.display.height / qr_code_scale) - qr_bitmap.height) / 2)
        qr_img = displayio.TileGrid(qr_bitmap,
                                    pixel_shader=palette,
                                    x=qr_position_x,
                                    y=qr_position_y)
        qr_code = displayio.Group(scale=qr_code_scale)
        qr_code.append(qr_img)
        self.display.show(qr_code)
        time.sleep(dwell)
Exemplo n.º 3
0
    def qrcode(
        self, qr_data, *, qr_size=1, x=0, y=0, qr_color=0x000000
    ):  # pylint: disable=invalid-name
        """Display a QR code on the eInk

        :param qr_data: The data for the QR code.
        :param int qr_size: The scale of the QR code.
        :param x: The x position of upper left corner of the QR code on the display.
        :param y: The y position of upper left corner of the QR code on the display.

        """
        import adafruit_miniqr  # pylint: disable=import-outside-toplevel

        # generate the QR code
        for qrtype in range(1, 5):
            try:
                qrcode = adafruit_miniqr.QRCode(qr_type=qrtype)
                qrcode.add_data(qr_data)
                qrcode.make()
                break
            except RuntimeError:
                pass
                # print("Trying with larger code")
        else:
            raise RuntimeError("Could not make QR code")
        # monochrome (2 color) palette
        palette = displayio.Palette(2)
        palette[0] = 0xFFFFFF
        palette[1] = qr_color

        # pylint: disable=invalid-name
        # bitmap the size of the matrix, plus border, monochrome (2 colors)
        qr_bitmap = displayio.Bitmap(
            qrcode.matrix.width + 2, qrcode.matrix.height + 2, 2
        )
        for i in range(qr_bitmap.width * qr_bitmap.height):
            qr_bitmap[i] = 0

        # transcribe QR code into bitmap
        for xx in range(qrcode.matrix.width):
            for yy in range(qrcode.matrix.height):
                qr_bitmap[xx + 1, yy + 1] = 1 if qrcode.matrix[xx, yy] else 0

        # display the QR code
        qr_sprite = displayio.TileGrid(qr_bitmap, pixel_shader=palette)
        if self._qr_group:
            try:
                self._qr_group.pop()
            except IndexError:  # later test if empty
                pass
        else:
            self._qr_group = displayio.Group()
            self.splash.append(self._qr_group)
        self._qr_group.scale = qr_size
        self._qr_group.x = x
        self._qr_group.y = y
        self._qr_group.append(qr_sprite)
        if self.auto_refresh:
            self.display.refresh()
            sleep(5)
    def show_QR(
        self, qr_data, *, qr_size=1, x=0, y=0, hide_background=False
    ):  # pylint: disable=invalid-name
        """Display a QR code on the TFT

        :param qr_data: The data for the QR code.
        :param int qr_size: The scale of the QR code.
        :param x: The x position of upper left corner of the QR code on the display.
        :param y: The y position of upper left corner of the QR code on the display.
        :param hide_background: Show the QR code on a black background if True.

        """
        import adafruit_miniqr  # pylint: disable=import-outside-toplevel

        # generate the QR code
        qrcode = adafruit_miniqr.QRCode()
        qrcode.add_data(qr_data)
        qrcode.make()

        # monochrome (2 color) palette
        palette = displayio.Palette(2)
        palette[0] = 0xFFFFFF
        palette[1] = 0x000000

        # pylint: disable=invalid-name
        # bitmap the size of the matrix, plus border, monochrome (2 colors)
        qr_bitmap = displayio.Bitmap(
            qrcode.matrix.width + 2, qrcode.matrix.height + 2, 2
        )
        for i in range(qr_bitmap.width * qr_bitmap.height):
            qr_bitmap[i] = 0

        # transcribe QR code into bitmap
        for xx in range(qrcode.matrix.width):
            for yy in range(qrcode.matrix.height):
                qr_bitmap[xx + 1, yy + 1] = 1 if qrcode.matrix[xx, yy] else 0

        # display the QR code
        qr_sprite = displayio.TileGrid(qr_bitmap, pixel_shader=palette)
        if self._qr_group:
            try:
                self._qr_group.pop()
            except IndexError:  # later test if empty
                pass
        else:
            self._qr_group = displayio.Group()
            self.splash.append(self._qr_group)
        self._qr_group.scale = qr_size
        self._qr_group.x = x
        self._qr_group.y = y
        self._qr_group.append(qr_sprite)
        if hide_background:
            board.DISPLAY.show(self._qr_group)
        self._qr_only = hide_background
Exemplo n.º 5
0
    def qr_code(self, str, x, y, scale=2):
        qr = adafruit_miniqr.QRCode(qr_type=3, error_correct=adafruit_miniqr.L)
        qr.add_data(str.encode())
        qr.make()
        for _y in range(qr.matrix.height):  # each scanline in the height
            for _x in range(qr.matrix.width):
                if qr.matrix[_x, _y]:

                    self.display.fill_rect(_x * scale + x, _y * scale + y,
                                           scale, scale, fc)
                else:

                    self.display.fill_rect(_x * scale + x, _y * scale + y,
                                           scale, scale, bc)
        gc.collect()
Exemplo n.º 6
0
def display_qr(addr):
    global palette
    background = displayio.Bitmap(badge.display.width, badge.display.height, 1)
    background_sprite = displayio.TileGrid(background, pixel_shader=palette)

    qr = adafruit_miniqr.QRCode()
    qr.add_data(get_qr_url(addr).encode('utf-8'))
    qr.make()
    qr_bitmap = bitmap_QR(qr.matrix)
    qr_img = displayio.TileGrid(qr_bitmap, pixel_shader=palette)
    qr_group = displayio.Group(scale=4, x=96, y=6)
    qr_group.append(qr_img)

    frame = displayio.Group()
    frame.append(background_sprite)
    frame.append(qr_group)
    frame.append(banner())
    badge.display.show(frame)
    while badge.display.time_to_refresh > 0:
        pass
    badge.display.refresh()
    def show_qr_code(self, data="https://circuitpython.org"):
        """Generate a QR code.

        :param string data: A string of data for the QR code

        .. code-block:: python

            from adafruit_pybadger import pybadger

            while True:
                pybadger.show_qr_code("https://adafruit.com")

        """
        qr_code = adafruit_miniqr.QRCode(qr_type=3, error_correct=adafruit_miniqr.L)
        qr_code.add_data(bytearray(data))
        qr_code.make()
        qr_bitmap = self.bitmap_qr(qr_code.matrix)
        palette = displayio.Palette(2)
        palette[0] = 0xFFFFFF
        palette[1] = 0x000000
        qr_code_scale = min(
            self.display.width // qr_bitmap.width,
            self.display.height // qr_bitmap.height,
        )
        qr_position_x = int(
            ((self.display.width / qr_code_scale) - qr_bitmap.width) / 2
        )
        qr_position_y = int(
            ((self.display.height / qr_code_scale) - qr_bitmap.height) / 2
        )
        qr_img = displayio.TileGrid(
            qr_bitmap, pixel_shader=palette, x=qr_position_x, y=qr_position_y
        )
        qr_code = displayio.Group(scale=qr_code_scale)
        qr_code.append(qr_img)
        self.display.show(qr_code)
Exemplo n.º 8
0
print('Checking Charge Status Pin')
charge_status = DigitalInOut(m.pin.P0_16)
charge_status.direction = Direction.INPUT
charge_status.pull = Pull.UP
print("Charge Pin:\t%s"%(str(charge_status.value)))

print("Checking Battery Voltage")
analog_in = AnalogIn(m.pin.P0_31)
print("Voltage:\t%s"%(str(analog_in.value)))




qrstring = bytes("{'iic':"+str(addrs)+",'adc':"+str(analog_in.value)+",'post':1}".replace("'","\""),'utf-8')
print(qrstring)
qr = adafruit_miniqr.QRCode()
qr.add_data(qrstring)
qr.make()

palette = displayio.Palette(2)
palette[1] = 0x000000
palette[0] = 0xffffff

bitmap = bitmap_qr(qr.matrix)
tile_grid = displayio.TileGrid(bitmap, pixel_shader=palette)
tile_grid.flip_y=True
# Create a Group to hold the TileGrid
group = displayio.Group(scale=6, x=0, y=0)
group.append(tile_grid)
display.show(group)
while True:
# NOTE: If you are displaying this on a screen, ensure the text label fields are
# long enough to handle the user_code and verification_url.
# Details in link below:
# https://developers.google.com/identity/protocols/oauth2/limited-input-device#displayingthecode
print("1) Navigate to the following URL in a web browser:",
      google_auth.verification_url)
print("2) Enter the following code:", google_auth.user_code)

# modify display labels to show verification URL and user code
label_verification_url.text = (
    "1. On your computer or mobile device,\n    go to: %s" %
    google_auth.verification_url)
label_user_code.text = "2. Enter code: %s" % google_auth.user_code

# Create a QR code
qr = adafruit_miniqr.QRCode(qr_type=3, error_correct=adafruit_miniqr.L)
qr.add_data(google_auth.verification_url.encode())
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 = 15
# then center it!
qr_img = displayio.TileGrid(qr_bitmap, pixel_shader=palette, x=170, y=165)
group_verification.append(qr_img)
# show the group
Exemplo n.º 10
0
    def show_QR(self, qr_data, qr_size=128, position=None):  # pylint: disable=invalid-name
        """Display a QR code on the TFT

        :param qr_data: The data for the QR code.
        :param int qr_size: The size of the QR code in pixels.
        :param position: The (x, y) tuple position of the QR code on the display.

        """
        import adafruit_miniqr

        if not qr_data:  # delete it
            if self._qr_group:
                try:
                    self._qr_group.pop()
                except IndexError:
                    pass
                board.DISPLAY.refresh_soon()
                board.DISPLAY.wait_for_frame()
            return

        if not position:
            position = (0, 0)
        if qr_size % 32 != 0:
            raise RuntimeError("QR size must be divisible by 32")

        qrcode = adafruit_miniqr.QRCode()
        qrcode.add_data(qr_data)
        qrcode.make()

        # pylint: disable=invalid-name
        # how big each pixel is, add 2 blocks on either side
        BLOCK_SIZE = qr_size // (qrcode.matrix.width + 4)
        # Center the QR code in the middle
        X_OFFSET = (qr_size - BLOCK_SIZE * qrcode.matrix.width) // 2
        Y_OFFSET = (qr_size - BLOCK_SIZE * qrcode.matrix.height) // 2

        # monochome (2 color) palette
        palette = displayio.Palette(2)
        palette[0] = 0xFFFFFF
        palette[1] = 0x000000

        # bitmap the size of the matrix + borders, monochrome (2 colors)
        qr_bitmap = displayio.Bitmap(qr_size, qr_size, 2)

        # raster the QR code
        line = bytearray(qr_size // 8)  # monochrome means 8 pixels per byte
        for y in range(qrcode.matrix.height):  # each scanline in the height
            for i, _ in enumerate(line):  # initialize it to be empty
                line[i] = 0
            for x in range(qrcode.matrix.width):
                if qrcode.matrix[x, y]:
                    for b in range(BLOCK_SIZE):
                        _x = X_OFFSET + x * BLOCK_SIZE + b
                        line[_x // 8] |= 1 << (7 - (_x % 8))

            for b in range(BLOCK_SIZE):
                # load this line of data in, as many time as block size
                for i, byte in enumerate(line):
                    qr_bitmap[Y_OFFSET + y * BLOCK_SIZE + b + i] = byte
        # pylint: enable=invalid-name

        # display the bitmap using our palette
        qr_sprite = displayio.Sprite(qr_bitmap,
                                     pixel_shader=palette,
                                     position=position)
        if self._qr_group:
            try:
                self._qr_group.pop()
            except IndexError:  # later test if empty
                pass
        else:
            self._qr_group = displayio.Group()
            self.splash.append(self._qr_group)
        self._qr_group.append(qr_sprite)
        board.DISPLAY.refresh_soon()
        board.DISPLAY.wait_for_frame()