Interactive Snow Globe Touch: a Festive DIY Raspberry Pi Project

by feiticeir0 in Circuits > Raspberry Pi

27 Views, 0 Favorites, 0 Comments

Interactive Snow Globe Touch: a Festive DIY Raspberry Pi Project

ChristmasSnowGlobe.gif
20241219_171411.jpg
20241219_171433.jpg
0ef068fc-9698-4c81-86ec-49aa77f07795.jpg
20241223_121053.jpg
20241223_121103.jpg

Imagine bringing a winter wonderland to life with falling snow, interactive backgrounds, and a touchscreen experience—all powered by a Raspberry Pi! This project is perfect for the holiday season or for learning how to create an engaging, interactive display. Whether you're a maker, coder, or holiday enthusiast, this project combines creativity, coding, and technology to spread some festive joy!

Supplies

20241218_184200_NGeUV0mXEX.jpg

Hardware

  1. Raspberry Pi (e.g., Raspberry Pi Zero 2 W - older version should also work)
  2. Touchscreen display (e.g., Pimoroni HyperPixel or any HDMI touchscreen)
  3. Power supply for Raspberry Pi
  4. MicroSD card (16GB or larger)
  5. USB keyboard/mouse (for initial setup)
  6. Optional: Case for Raspberry Pi


Software

  1. Raspberry Pi OS
  2. Required libraries: libsdl2-dev, libsdl2-image-dev, libevdev-dev
  3. Git for cloning the repository

Set Up Your Raspberry Pi

Before anything, we need to install the Raspberry PI

Install Raspberry Pi OS:

  1. Download and flash Raspberry Pi OS to your microSD card.
  2. We don't need the full version with desktop. The Lite version is enough.
  3. Insert the card into the Raspberry Pi and boot it up.

Update the System

sudo apt-get update && sudo apt-get upgrade -y

Install Dependencies

sudo apt-get install libsdl2-dev libsdl2-image-dev libevdev-dev


Clone the Project

After having the Raspberry PI ready, use Git to clone the project to your Raspberry Pi.

git clone git@github.com:feiticeir0/ChristmasSnowGlobe.git

Enter the directory

cd ChristmasSnowGlobe


Add Background Images

christmas4.jpg
christmas3.jpg
christmas2.jpg
christmas1.jpg

Our project slides through images, so we may add more images to the project.

Add your .jpg background images to the images directory in the project folder. For example:

ChristmasSnowGlobe/
├── images/
│ ├── background1.jpg
│ ├── background2.jpg
│ └── background3.jpg
└── christmasSnowGlobe


Build and Run

Let's now build the program.

Inside the ChristmasSnowGlobe directory:

make

After making it, let's run it and see if it works

./snow_simulation -w 720 -h 720 -e /dev/input/event0
  1. Replace 720 with your display's resolution.
  2. Replace /dev/input/event0 with your touchscreen's input device.

Switch Backgrounds:

  1. Tap the screen to change backgrounds dynamically.

To view the help message:

./christmasSnowGlobe --help


Touch

You probably need to add your user (or the user running this program) to the input group. Check with your distro for the correct group.

sudo usermod -aG input $USER


Autostart

1.jpg
2.jpg
3.jpg

To autostart the program when the Raspberry PI starts or reboots, we can use our.basrc file.

Just configure the Raspberry PI OS to auto login to cli:

1. System Options > S5 Boot / Auto Login > B2 Console Autologin

Next, edit your .basrc to execute the program once you login.

vi .bashrc

And place the following line at the end of the file:

cd ChristmasSnowGlobe;./christmasSnowGlobe -h 720 -w 720 -e /dev/input/event0 &

Save and exit.

NOTE: We need to execute it inside the directory because of the images path. Something to change in a next release.

Reboot your RPi and see if it works.


If using Pimoroni's Hyperpixel Touch 4.0 screen

Using Pimoroni's Hyperpixel Square 4.0, sometimes the touch device changes between reboots. So, it may not always be event0.

I've created a script to find the correct touch device for the Pimoroni's Hyperpixel Touch 4.0. This way, when invoking the Christmas Snow Globe, I always have the right device.

To use the script to find the device, the correct invocation is:

cd ChristmasSnowGlobe;./christmasSnowGlobe -h 720 -w 720 -e /dev/input/$(./findPimoronisTouchDevice.sh) &

The script findPimoronisTouchDevice.sh returns the correct event device for the touch screen.

Customization

Adding Your Own Backgrounds

  1. Replace or add .jpeg images in the images directory.
  2. The program automatically detects all .jpg files in the folder.


Adjusting Snowflake Properties

You can modify the following aspects in the code:

  1. Number of Snowflakes:
#define NUM_SNOWFLAKES 80
  1. Wind Speed and Acceleration:
float wind_speed = 0.0f;
float wind_acceleration = 0.02f;
  1. Snowflake Speed and Size:
snowflake->size = rand() % 3 + 2;
snowflake->speed = (float)(rand() % 300 + 200) / 100.0f;



Scaling for Larger Displays

Modify the screen resolution via the command-line arguments or adjust the default values in the code:

int width = 1024;
int height = 768;


Code Explanation

1. Initialization

  1. The program initializes SDL2, SDL2_image, and libevdev for rendering graphics and handling touch input.
  2. Command-line arguments allow setting screen width, height, and the touchscreen device path.
./snow_simulation -w 720 -h 720 -e /dev/input/event0
  1. If you're using the Pimoroni's Hyperpixel touch 4.0 screen, every time the Raspberry PI boots, the touch device can change. This invalidates an automatic execution.
  2. I developed a script that will detect the touch device. This way, I can always pass the correct device to the program.


2. Snowflake Simulation

  1. Each snowflake is represented by a struct containing its position, size, and speed.
  2. Snowflakes are updated dynamically to simulate falling with wind effects.
typedef struct {
int x, y, size;
float speed;
} Snowflake;


3. Dynamic Backgrounds

  1. Background images are loaded from the images directory using SDL2_image.
  2. Touch events cycle through the loaded backgrounds.
int load_textures(SDL_Renderer *renderer, const char *directory, SDL_Texture ***textures);


4. Touchscreen Integration

  1. The program uses libevdev to read raw input events from the touchscreen device.
  2. The EV_ABS and EV_SYN events detect touch positions and release states.
while (libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, &ev) == 0) {
if (ev.type == EV_ABS) {
if (!is_touch_active) {
current_background = (current_background + 1) % num_backgrounds;
is_touch_active = 1;
}
}
}



The code is available on my Github account

Touch Device

To find out your touch device, you can install evtest and use it to find the device:

sudo apt install evtest

Run the software

sudo evtest

It should list some devices and one of the them is your touch device.Here's an example using Pimoroni's HyperPixel 4.0 Square

evtest

No device specified, trying to scan all of /dev/input/event*
Not running as root, no devices may be available.
Available devices:
/dev/input/event0: vc4-hdmi
/dev/input/event1: 11-0048 EP0110M09
/dev/input/event2: vc4-hdmi HDMI Jack
Select the device event number [0-2]:

Select one, touch it and see if outputs text.

If you see text, that's the one you should use.

Conclusion

Exit the application

To exit the program, we have to kill it.

In a terminal, execute the following script to kill the snow wonderland:

kill -9 $(pidof christmasSnowGlobe)


Future Improvements

Here are some improvements that I'm hoping to make:

  1. Dynamic Background Scaling: Automatically scale backgrounds to fit the screen resolution.
  2. Audio Integration: Add festive music or sound effects.
  3. Enhanced Visual Effects: Introduce twinkling lights or a dynamic weather system.
  4. Exit Cleanly: Find a way to exit the software without having to kill it.


Conclusion

I hope you enjoy creating and customizing your own Interactive Winter Wonderland!

Add your family photos or favorite holiday scenes to make it uniquely yours and create a cozy, festive atmosphere for everyone to enjoy.

Have fun, and let your creativity snowball into the perfect holiday project! ❄️