Skip to content

Instantly share code, notes, and snippets.

@FoamyGuy
Created April 18, 2026 16:40
Show Gist options
  • Select an option

  • Save FoamyGuy/6b84f000b1501af7c25370950f7f16d9 to your computer and use it in GitHub Desktop.

Select an option

Save FoamyGuy/6b84f000b1501af7c25370950f7f16d9 to your computer and use it in GitHub Desktop.
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
"""
This test will initialize the display using displayio and draw a solid green
background, a smaller purple rectangle, and some yellow text.
"""
import aesio
import board
import displayio
import terminalio
from adafruit_display_text.text_box import TextBox
from fourwire import FourWire
import neopixel
from adafruit_hx8357 import HX8357
from adafruit_max44009 import MAX44009
from adafruit_seesaw import digitalio, rotaryio, seesaw
import adafruit_binascii
# secret_message = "This message is\nencoded with light."
cipher_hex = "542bd5432e5c60ab9018ef5574f673abfb3fec187c62ebf0a1679aec32c30894c6146b"
ciphertext_bytes = adafruit_binascii.unhexlify(cipher_hex)
# print(ciphertext_bytes.decode("utf-8"))
key = b'Sixteen byte key'
iv = b'InitializationVe'
# inp = secret_message.encode('utf-8')
outp = bytearray(len(ciphertext_bytes))
# cipher2 = aesio.AES(key, aesio.MODE_CTR, iv)
# decrypted = bytearray(len(outp))
# cipher2.decrypt_into(bytes(outp), decrypted)
# Release any resources currently in use for the displays
displayio.release_displays()
i2c = board.I2C()
lux_sensor = MAX44009(board.I2C())
seesaw = seesaw.Seesaw(i2c, addr=0x36)
seesaw_product = (seesaw.get_version() >> 16) & 0xFFFF
print(f"Found product {seesaw_product}")
if seesaw_product != 4991:
print("Wrong firmware loaded? Expected 4991")
# Configure seesaw pin used to read knob button presses
# The internal pull up is enabled to prevent floating input
seesaw.pin_mode(24, seesaw.INPUT_PULLUP)
button = digitalio.DigitalIO(seesaw, 24)
last_button_val = button.value
encoder = rotaryio.IncrementalEncoder(seesaw)
last_position = None
encoder_position = 0
pixel_brightness = 0.0
pixels = neopixel.NeoPixel(board.D12, 8, brightness=pixel_brightness, auto_write=True)
pixels.fill(0xffffff)
spi = board.SPI()
tft_cs = board.D9
tft_dc = board.D10
display_bus = FourWire(spi, command=tft_dc, chip_select=tft_cs)
display = HX8357(display_bus, width=480, height=320)
display.rotation = 180
# Make the display context
main_group = displayio.Group()
display.root_group = main_group
# Draw a label
text_group = displayio.Group(scale=3, x=0, y=0)
# text_area = label.Label(terminalio.FONT, text="", color=0xffffff)
text_area = TextBox(terminalio.FONT, display.width // 3, display.height // 3, align=TextBox.ALIGN_CENTER)
text_area.anchor_point = (0, 0)
text_area.anchored_position = (0, 0)
text_group.append(text_area) # Subgroup for text scaling
main_group.append(text_group)
def try_decrypt():
current_reading = int(lux_sensor.lux)
hundreds_place = current_reading // 100
key_lux_range = f"{hundreds_place}00-{hundreds_place + 1}00"
key_array = bytearray(key)
for idx, b in enumerate(key_lux_range.encode("utf-8")):
key_array[idx] = b
decrypt_key = bytes(key_array)
#print(decrypt_key)
cipher2 = aesio.AES(decrypt_key, aesio.MODE_CTR, iv)
decrypted = bytearray(len(ciphertext_bytes))
cipher2.decrypt_into(ciphertext_bytes, decrypted)
decrypted_bytestring = bytes(decrypted)
try:
decrypted_str = decrypted_bytestring.decode("utf-8")
if text_area._original_text != decrypted_str:
# print("setting text")
text_area.text = decrypted_str
except UnicodeError:
# print(adafruit_binascii.hexlify(decrypted).decode("uff-8"))
hex_str = adafruit_binascii.hexlify(decrypted).decode("uff-8")
# print("cur_text:", text_area._original_text)
# print("hex_str :", hex_str)
if text_area._original_text != hex_str:
# print("setting text")
text_area.text = hex_str
#
while True:
# negate the position to make clockwise rotation positive
encoder.position = min(0, encoder.position)
encoder_position = -encoder.position
# encoder_position = abs(encoder.position)
if encoder_position != last_position:
last_position = encoder_position
print(f"Position: {encoder_position}")
# text_area.text = str(encoder_position)
pixels.brightness = min(encoder_position * 0.01, 1.0)
cur_button_val = button.value
if cur_button_val != last_button_val and not cur_button_val:
print("button pressed")
# key_array = bytearray(key)
# for idx, b in enumerate(b'300-400'):
# key_array[idx] = b
# key = bytes(key_array)
# print(key)
# cipher = aesio.AES(key, aesio.MODE_CTR, iv)
# print(f"mode: {cipher.mode}")
# cipher.encrypt_into(inp, outp)
# print(f"ciphertext_hex: {outp.hex()}")
last_button_val = cur_button_val
# text_area.text = str(lux_sensor.lux)
try_decrypt()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment