The code is written in CircuitPython and is fairly brief. If you're just getting started with CircuitPython and/or the PyPortal it's recommended to check out this Learn guide to help get you started:

# SPDX-FileCopyrightText: 2019 Liz Clark for Adafruit Industries
#
# SPDX-License-Identifier: MIT

# Adafruit PyPortal display of Twin Peaks
# Liz (BlitzCityDIY) for Adafruit Industries  MIT License
# Tutorial: https://learn.adafruit.com/twin-peaks-light-reactive-pyportal-picture-frame
import time
import board
from analogio import AnalogIn
from adafruit_pyportal import PyPortal

analogin = AnalogIn(board.LIGHT)

cwd = ("/"+__file__).rsplit('/', 1)[0]

laura = (cwd+"/laura.bmp")

woodsman = (cwd+"/woodsman.bmp")

gottaLight = (cwd+"/gottaLight.wav")

pyportal = PyPortal(default_bg=laura)

def getVoltage(pin):  # helper
    return (pin.value * 3.3) / 65536

while True:

    if getVoltage(analogin) > 0.175:
        pyportal.set_background(laura)
        time.sleep(1)
    else:
        pyportal.set_background(woodsman)
        pyportal.play_file(gottaLight)
        time.sleep(1)

At this point, everything should be copied over to the CIRCUITPY drive. It should look something like this:

CIRCUITPY

First, we're importing the necessary libraries: time, board, displayio, analogio and adafruit_pyportal. We begin with declaring the built-in light sensor as an analog input with analogin = AnalogIn(board.LIGHT). Utilizing board.LIGHT for the light sensor is possible thanks to the adafruit_pyportal library.

Next, we're going to take care of our audio and picture files. We declare cwd as the current working directory, which is where our files will be saved on the PyPortal. We're then going to declare our files to be variables so that we can use them later in the code. The PyPortal is then setup to have the PyPortal settings from the library along with the "laura" image as the default background so that on first boot, the laura.bmp file will be displayed.

The final step before the loop is the function that allows us to read the analog input data from the light sensor.

In the loop, there is an if/else statement. It begins with if the analog input from the light sensor is greater than 0.175, then the laura.bmp file will be displayed. Else, the woodsman.bmp file will be displayed and the gottaLight.wav file will also be played. Each portion also has a one second delay.

This guide was first published on Apr 11, 2019. It was last updated on Apr 11, 2019.

This page (CircuitPython Code) was last updated on Mar 04, 2023.

Text editor powered by tinymce.