CircuitPython Setup

This project requires CircuitPython version 4.0 or higher.

Follow the instructions on the HalloWing CircuitPython Guide to ensure you have the correct version (4.0 or higher).

Adafruit really likes using the Mu editor to edit the CircuitPython code. See this guide on loading and using Mu.

Libraries

You'll also need to add a special library for this project. Follow this guide on adding libraries. The only one you'll need is the adafruit_slideshow.mpy file from the Circuit Python bundle in the lib folder, so just drag it from your downloaded, unzipped lib folder onto the HalloWing's lib folder.

Code

Here is the code we'll use. Copy it and then paste in Mu. Save it to your HalloWing as code.py

import board
from adafruit_slideshow import SlideShow, PlayBackDirection
import audioio
import audiocore
import digitalio
import touchio

# Create the slideshow object that plays through once alphabetically.
slideshow = SlideShow(board.DISPLAY)

# Set the touch objects to the first and last teeth
back_pin = board.TOUCH1
forward_pin = board.TOUCH4

# Perform a couple extra steps for the HalloWing M4
try:
    if getattr(board, "CAP_PIN"):
        # Create digitalio objects and pull low for HalloWing M4
        cap_pin = digitalio.DigitalInOut(board.CAP_PIN)
        cap_pin.direction = digitalio.Direction.OUTPUT
        cap_pin.value = False
    if getattr(board, "SPEAKER_ENABLE"):
        # Enable the Speaker
        speaker_enable = digitalio.DigitalInOut(board.SPEAKER_ENABLE)
        speaker_enable.direction = digitalio.Direction.OUTPUT
        speaker_enable.value = True
except AttributeError:
    pass

# Create the touchio objects for HalloWing M0
back_button = touchio.TouchIn(back_pin)
forward_button = touchio.TouchIn(forward_pin)

# Setup the speaker output
a = audioio.AudioOut(board.SPEAKER)

# Helper function that takes in the file name string, splits it at the period, and keeps only the
# beginning of the string. i.e. kitten.bmp becomes kitten.
def basename(file_name):
    return file_name.rsplit('.', 1)[0]


# Helper function to handle wav file playback
def play_file(wav_file_name):
    try:
        data = open(wav_file_name, "rb")
        wav = audiocore.WaveFile(data)
        a.play(wav)
        print("Playing: " + wav_file_name)
        while a.playing:
            pass
        a.stop()
    except OSError:  # Error thrown if it finds a .bmp file with no corresponding .wav file
        # Print the name of the .bmp file with no corresponding .wav file
        print("No corresponding wav file:", slideshow.current_image_name)


# Uses the basename() helper function to strip the .bmp from the file name and add .wav
wav_file = basename(slideshow.current_image_name) + ".wav"
# Uses the play_file() helper function to play the .wav name now saved to wav_file
play_file(wav_file)

while True:
    # Touch the tooth on the right:
    if forward_button.value:
        # Sets the slideshow playback direction to forward
        slideshow.direction = PlayBackDirection.FORWARD
        # Advance the slideshow to the next image
        slideshow.advance()
        # Sets wav_file to the new corresponding .wav file
        wav_file = basename(slideshow.current_image_name) + ".wav"
        # Plays back the new file with the new image
        play_file(wav_file)
    # Touch the tooth on the left:
    if back_button.value:
        # Sets the slideshow playback direction to backward
        slideshow.direction = PlayBackDirection.BACKWARD
        # Advance to the previous image
        slideshow.advance()
        wav_file = basename(slideshow.current_image_name) + ".wav"
        play_file(wav_file)

The code looks for .bmp files to play -- when it finds one it then parses that filename and looks for a .wav file with the same name. For example, renoir.bmp would cause the code to look for a renoir.wav file to play.

The capacitive touch pads are used to advance to the next image or the previous image.

Media

This is a media bundle you can use to try out your Tiny Museum Tour Device. Download the .zip file, and then uncompress it. Drag the .bmp and .wav files onto your HalloWing. They must be at the top level of your HalloWing, not inside a folder.

Try it out! You'll see the image fade up and the audio play. Touch either capacitive touch pad strip to switch images.

Now, let's learn how to create our own images and sound to play!

This guide was first published on Oct 19, 2018. It was last updated on Oct 19, 2018.

This page (Code with CircuitPython) was last updated on Oct 15, 2021.

Text editor powered by tinymce.