In my first post on easy application development on the Librem 5 I discussed how to turn a simple shell script that takes a screenshot into a full graphical app with only a few extra lines of code. In this post I will follow up with an even simpler application that took about twenty minutes to write with much of that time involved in reading documentation.

My Bright Idea

The interesting thing about smart phones is how many other devices they have replaced beyond a regular phone. For instance, there used to be a market for small, pocket-sized digital cameras, but now many people just use the cameras on their smart phones. While some people still do keep a pocket flashlight with them, many people just use the light on their smart phone.

I realized that a flashlight app would be another great way to showcase just how easy it is to develop applications for the Librem 5. As applications go the requirements are pretty simple: you need a button to turn on the light, a button to turn off the light, and a button to close the app.

The first step was for me to find out how to control the light from the command line. After talking to our hardware team I got the series of commands. First you need to enable that LED so it can receive commands:

sudo bash -c 'echo 255 > /sys/class/leds/flash_en/brightness'

Once the LED is enabled, you can control the light by setting a particular register to a particular value using the i2cset command.

To turn the light on:

sudo i2cset -y 1 0x53 0x10 0x1a

To turn the light off:

sudo i2cset -y 1 0x53 0x10 0x18

After testing that the commands worked on the command line, I started looking up how to build the application using the same yad command-line tool I used to build the screenshot application. In that application I used the --form option to build a stacked series of text input fields. It turns out that you can use the same feature to build a series of buttons that can execute commands when pressed.

The flashlight app turned out to be even shorter than my screenshot app with all the heavy lifting being done by a single yad command:

#!/bin/bash # Enable the LED so we can poll its state sudo bash -c 'echo 255 > /sys/class/leds/flash_en/brightness' # Display the GUI yad --title flashlight --form \ --field='Flashlight ON!switch-on-symbolic':fbtn "sudo i2cset -y 1 0x53 0x10 0x1a" \ --field='Flashlight OFF!switch-off-symbolic':fbtn "sudo i2cset -y 1 0x53 0x10 0x18" \ --button='Close!gtk-cancel':1 # Disable the LED now that we're done sudo bash -c 'echo 0 > /sys/class/leds/flash_en/brightness'

The --field arguments allow you to define different UI elements in your app and in this case I started with the text that would be on each button followed by the name of a system icon to use for that button (separated by a ! symbol). Then I told yad to make these fields buttons with the :fbtn addition to the end. If you follow this type of field with a command, it will execute that command when the button is pressed so I just added my i2cset commands. Finally I added a standard button to close the application.

I saved the program in /home/purism/bin/flashlight and made it executable with chmod a+x /home/purism/bin/flashlight . The result is this application:

I then created a /home/purism/.local/share/applications/flashlight.desktop file so I could have an icon show up on the desktop:

[Desktop Entry] Name=Flashlight Type=Application Icon=system-shutdown Exec=flashlight Categories=Utility;

That’s it! I’m finding yad to be a very useful way to create simple GUI programs. I imagine I’ll reuse this script’s ability to trigger command-line commands with buttons in future programs.