Arduino Screen Controller for Raspberry Pi Part 1 Hot Keys

A few months ago I setup a web browser based dashboard running on a Raspberry Pi, displaying weather, time and transit information.  It worked out well, but it’s success revealed another problem.  Turning the screen on and off multiple times a day was getting old.

So I did what any technically inclined person would do.  I decided to create a device to turn the screen on and off by responding to dramatic changes in ambient light.  The device would also provide a manual power toggle and button to refresh the browser.

This is part 1 of 3, describing how to create a script to turn on and off the screen and hot keys to call this script.

Although intended to be used with a Raspberry Pi, the screen controller can be used to emulate a USB keyboard and send any desired keystrokes.

Assumptions

  • Using Rasperry Pi Model B rev 2
  • Monitor using HDMI port
  • Raspbian wheezy
  • X windows running with lxde
  • USB keyboard for testing

Links

Screen Control Commands

The Raspberry Pi has a utility called tvservice that controls the video hardware.  Progammatically turning off the screen turned out to be quite simple, but turning it back on turned out to be more complex.

To turn back on, calling tvservice must be followed by calls to fbset and xrefresh.  In fact, two fbset calls are required because fbset will not do anything if it thinks nothing has changed.  Specifically, it is necessary to ‘bounce’ the color depth to 16 and back to 32.  It appears that fbset is not aware that tvservice has turned off the video hardware and apparently reset the color depth.

NOTE: I have changed the default color depth for my Raspberry Pi to 32 bit, for details see Set Color Depth in Raspberry Pi Full Screen Browser.  If you have not made this change, try setting the depth to 8 and then 16, this should have a similar effect.

Test these commands out on your Raspberry Pi before continuing to make sure they work.  They can be safely run from an ssh connection.

Screen Control Shell Script

The screen control commands were wrapped in a shell script which logs requests and is called by they hot keys.  State is stored by the Arduino device.

This shell script should be created by the default user and in a place the user can access it.  For example if the default user pi is used, it can be in /home/pi.

Finally, make the file executable.

 Hot Keys

Next step is to create a hot key combination to turn on and off the screen, relying on sending the F5 key to refresh the dashboard in the web browser.

The hot keys are added to the lxde configuration file which is the default Raspbian window manager.  The hot key combinations are:

Command + 0 to turn off
Command + 1 to turn on

The Apple Command Key and Windows Key can be used interchangeably.

Edit the lxde-rc.xml config file:

– Add to the <keyboard> section:

Restart the window manager by rebooting.

After reboot test the script, plug in a usb keyboard and type Command + 0 to turn off and Command + 1 to turn on.

Comments are closed.