Thank you for your donation!


Cloudsmith graciously provides open-source package management and distribution for our project.


Two functions on one button in Gpio buttons
#1
Greetings.

I have connected an oled display, everything works. But this display burns out pixels when working for a long time. I decided to use buttons in gpio buttons to give commands to turn the display off or on. But this requires two buttons - one to turn on and one to turn off. Is there any way to make it work on one button? Pressed - the command to turn off is sent. Pressed again - command to turn on. That is the trigger mode.

Thank you.

Sorry for my English - I write through a translator, I'm from Ukraine.
Reply
#2
What model display and how is it connected?
Enjoy the Music!
moodeaudio.org | Mastodon Feed | GitHub
Reply
#3
SSD1306 I2C display. Connected to pin 5 and 3 of the GPIO connector of the Raspberry Pi Zero 2W. MPD Oled program. It has commands "sudo systemctl stop mpd_oled" and "sudo systemctl start mpd_oled" to turn off and on the MPD Oled. When turned off, the display goes off. I am using in GPIO buttons two buttons. One turns off, the other turns on. It is not convenient and not rational. I would like to use only one button for on/off.
Reply
#4
Why not have a script that checks the status of the mpd_oled service ?

e.g:

Code:
if (systemctl is-active --quiet service); then
       echo -e "SERVICE is Running!"
else
       echo -e "SERVICE has Stopped status!"
fi

Change the echo to start/stop as appropriate.
Reply
#5
(07-05-2024, 09:00 PM)steve4star Wrote: Why not have a script that checks the status of the mpd_oled service ?

Change the echo to start/stop as appropriate.


Please forgive me, I don't understand programming in the Linux environment at all Sad
Could you please tell me how to do it, or point me in the right direction. 

Thanks
Reply
#6
(07-09-2024, 09:04 PM)Romanz Wrote:
(07-05-2024, 09:00 PM)steve4star Wrote: Why not have a script that checks the status of the mpd_oled service ?

Change the echo to start/stop as appropriate.


Please forgive me, I don't understand programming in the Linux environment at all Sad
Could you please tell me how to do it, or point me in the right direction. 

Thanks

Code:
#!/bin/bash

if (systemctl is-active --quiet mpd_oled); then
   sudo systemctl stop mpd_oled
else
   sudo systemctl start mpd_oled
fi

1. copy the contents in the code-block above into /home/<your username>/bin/mpd_oled_toggle.sh
2. make such file executable: chmod 0777 /home/<your username>/bin/mpd_oled_toggle.sh
3. in the command of the button (just one button, now), specify the whole path as it appears here above, instead of each of the sudo systemctl start/stop ...
Reply
#7
Thank you.
Moode 8.3.9
All done. On the first startup, the display goes blank. Restarting it doesn't turn it on. And the subsequent ones, too.
What is interesting, after the first application of the script, it does not start even in the terminal. I use the command sudo systemctl start mpd_oled - does not start.
If you look at the status with sudo systemctl status mpd_oled command - it says Stoped MPD Oled Display. Only rebooting helps.
Reply
#8
(07-10-2024, 04:08 PM)Romanz Wrote: Thank you.
Moode 8.3.9
All done. On the first startup, the display goes blank. Restarting it doesn't turn it on. And the subsequent ones, too.
What is interesting, after the first application of the script, it does not start even in the terminal. I use the command sudo systemctl start mpd_oled - does not start.
If you look at the status with sudo systemctl status mpd_oled command - it says Stoped MPD Oled Display. Only rebooting helps.

What does it mean "only rebooting helps"? Didn't you say that at startup it is OFF, and then it does not start, no matter what?
Reply
#9
The system boots up, the display appears on the oled display. If I go to terminal and enter the command "sudo systemctl stop mpd_oled" the display goes out. If I type "sudo systemctl start mpd_oled" at once, the display appears. If I press the button and run the script, the display goes out. The next press does not help. If I type the command "sudo systemctl start mpd_oled" in the terminal, there is no response either. Only rebooting moode helps.
Reply
#10
(07-11-2024, 08:29 AM)Romanz Wrote: The system boots up, the display appears on the oled display. If I go to terminal and enter the command "sudo systemctl stop mpd_oled" the display goes out. If I type "sudo systemctl start mpd_oled" at once, the display appears. If I press the button and run the script, the display goes out. The next press does not help. If I type the command "sudo systemctl start mpd_oled" in the terminal, there is no response either. Only rebooting moode helps.

Now it's clearer...
So:
1. at startup everything is OK
2. you can switch it OFF by pressing the button
3. you cannot, then, switch it back ON neither with the button, nor with the command.

Sounds strange... like the script somehow does not terminate, or there are some errors that prevent it to be run any further after the first invoke...

I would try the following, then:

1. add echo lines in the two IF branches, so to see which path is taken by the script, so that it looks like this:

Code:
#!/bin/bash

if (systemctl is-active --quiet mpd_oled); then
   echo "MPD_OLED is active, turning it OFF..."
   sudo systemctl stop mpd_oled
   echo "Done."
else
   echo "MPD_OLED is not active, turning it ON..."
   sudo systemctl start mpd_oled
   echo "Done."
fi

2. after startup, instead of pressing the buttons, just open a terminal, and call the script on the command-line, and report what you see as the output of the script.
Reply


Forum Jump: