After installing the Adafruit PiTFT display to my Raspberry-Pi, I wanted to use the buttons on the panel. I searched high and low for examples of this, but couldn't find what I was looking for. Ultimately I want to use one switch to port the display back and forth between the HDMI monitor and the TFT display and another switch to switch the wireless from a LAN configuration to an ad-hoc configuration. I found a nice article that teaches the basics of using switches connected to the GPIO header here. https://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/robot/buttons_and_switches/ This was really helpful in developing the script I wound up building.
I'm still having trouble figuring out how to call the framebuffer and network using a script, so I chose to takle something a little simpler, just to prove the concept of using the buttons to execute Pi commands. The TFT has a backlight that can be turned on and off, and there's a program called OctoPiPanel that can pretty easily be called by a script, so these are what I chose to control. I programmed the third switch to simply output a line of text to the terminal indicating that the switch had been pressed and what state the toggle was in.
I found a reference at https://github.com/jonaslorander/OctoPiPanel that showed me how to run OctoPiPanel. Once I had it installed I had to get the Octoprint API key and copy it to the OctoPiPanel.cfg file. Then I had to make OctoPiPanel executable. From that point, I can launch OctoPiPanel from a command line which I will later tie to one of the switches.
The next step was to create a python script that monitors the buttons. it uses RPi.GPIO to map the switches. I added some logic to create a two state state engine, so I can essentially treat each switch as an on-off switch even though they are only momentary. I mapped one switch to start and stop OctoPiPanel and another switch to toggle the LCD backlight. Finally, I modified the startup script /etc/rc.local to call my monitoring script.
Now I can toggle the backlight and run OctoPiPanel at the press of a button.
I published my work on GitHub here: https://github.com/TJEmsley/PiTFT-Button-Monitor
I'm still having trouble figuring out how to call the framebuffer and network using a script, so I chose to takle something a little simpler, just to prove the concept of using the buttons to execute Pi commands. The TFT has a backlight that can be turned on and off, and there's a program called OctoPiPanel that can pretty easily be called by a script, so these are what I chose to control. I programmed the third switch to simply output a line of text to the terminal indicating that the switch had been pressed and what state the toggle was in.
I found a reference at https://github.com/jonaslorander/OctoPiPanel that showed me how to run OctoPiPanel. Once I had it installed I had to get the Octoprint API key and copy it to the OctoPiPanel.cfg file. Then I had to make OctoPiPanel executable. From that point, I can launch OctoPiPanel from a command line which I will later tie to one of the switches.
The next step was to create a python script that monitors the buttons. it uses RPi.GPIO to map the switches. I added some logic to create a two state state engine, so I can essentially treat each switch as an on-off switch even though they are only momentary. I mapped one switch to start and stop OctoPiPanel and another switch to toggle the LCD backlight. Finally, I modified the startup script /etc/rc.local to call my monitoring script.
Now I can toggle the backlight and run OctoPiPanel at the press of a button.
I published my work on GitHub here: https://github.com/TJEmsley/PiTFT-Button-Monitor