When Pepper is built into the Eurorack and not connected to an IDE it is obviously impossible to restart a patch or the Bela by pressing the various onboard buttons (Bela etc).
How can i
- restart a patch
- restart Bela
- restart the BBB & Bela
- •loop• through patches
using Pepper buttons, eventually even back and forth?

I am sorry if this has been asked before, my search here turned up all kinds of half-related things it seems.
I am working in PD/libpd.
Thank you in advance.

not sure what the difference between these two is:

  • restart Bela
  • restart the BBB & Bela

these should be doable:

  • restart a patch
  • •loop• through patches

At the moment there are two different behaviours implemented for patches running at boot:
1- single patch in a loop
2- multiple patches in a loop

In both cases the only control that is exposed to the user is Button. By pressing the button in case 1- the patch restarts, by pressing it in case 2-, it moves on to the next patch.
Another thing can be achieved with the button:
3- when hold-pressing the Button, you turn off Bela.

Now, on Pepper none of the front panel buttons are connected to the Button on the cape. However, the behaviour associated with the Button can be assigned to a different digital pin, so you could repurpose one of the buttons on the Pepper front panel to behave as the Button. This requires a few steps (assuming a recent Bela core code).

  • identify the GPIO number for the pin you would like to use. This is done cross-checking the Pepper schematic, the BeagleBone pinouts in here and here and the Pepper pins from here. This gives the following numbers (if I am not wrong):
Button | GPIO_NUMBER | BELA_PD
     0 |          89 | 26
     1 |          87 | 25
     2 |          88 | 24
     3 |          86 | 23

- create a file on the board: /root/.bela/belaconfig. In this file place the following (replacing GPIO_NUMBER with the number above:

CL=--stop-button-pin GPIO_NUMBER

This should make it so that when you press that button the current program stops. This by itself is enough to implement behaviours 1 and 2 above.
- To move the hold -press behaviour to the same pin, you have to edit /lib/systemd/system/bela_button.service . There, at this line

ExecStart=/usr/bin/stdbuf -oL -eL /usr/local/bin/bela-cape-btn --pin 115 --hold /opt/Bela/bela_button_hold.sh --delay 20 --monitor-click 0

you should change 115 to GPIO_NUMBER. If you want to change the action triggered by hold-pressing the button, you can either place an executable file at /opt/Bela/local/bela_button_hold.sh or replace /opt/Bela/bela_button_hold.sh above with a path to your executable.

I think the above should work, though you may have to modify initialise_digitals.pd in your projects to avoid affecting the corresponding digital input: remove the corresponding in BELA_PD from the "Initialise the buttons" section, also avoid using the corresponding signals from the [buttons] abstraction.

eventually even back and forth?

This could be fairly complicated. You may need two instances of bela-cape-btn running and somehow share state, or even roll your own version of it which monitors two pins and keeps track of what is the prev/next patch and start it upon button press.

    19 days later

    giuliomoro Okay. I created belaconfig, changed the GPIO to 86 (Peppers right-most button) and edited the line /lib/systemd/system/bela_button.service accordingly. Unfortunately the complete board turns off now always after approx 20 secs. I assume, that this is because of the entry saying "delay 20" in bela_button.service. I had changed this to "delay 200" first, which left it on for a while. Stupidly i now haven't enough tim to sh into th board and change the value back to much longer than 20.
    Can i get around reinstalling the whole system now somehow?
    Starting from RAM instead of the SC card?
    And apart from that: The button hasn't worked yet, i assume. I had set "delay" to "200" because i assumed this could make a pressed button active only after a min. 200 milliseconds long-press, but this had no ffect until the board switched off after some mins (200 seconds i would guess).
    Thank you already for more insights.

    P.S.: i did edit initialise_digitals.pd accordingly.
    P.S.2: Starting from internal RAM helps. The IDE starts alo, unit stays powered on. So i guess i have to reverse the edits on the SD card first. So i need to learn how to access the SD card, when i had only inserted it later. Possible?

    Right, so I am looking at the schematics again and I think the issue is that button pins are in a pull-down configuration instead of pull-up as the button on the cape itself. This means that as far as the bela-cape-btn program is concerned, the button is pressed when it is not actually pressed and viceverse. So, first off, to get this to work again you should be able to boot the board while hold pressing the button. Keep it pressed until you manage to ssh in and run systemctl stop bela_button. Then you should be able to release the button and the board should stay up. If holding the button is complicated you can disconnect your Bela from Pepper and run a jumper wire from 3v3 (P9.03) to the pin (P8.27).

    Once you re-acquire control of the board, edit /lib/systemd/system/bela_button.service adding --pressed 1 to the argument list. This will tell it that the button is pressed when a 1 is read (default is opposite, which is causing the current behaviour).

    I think this should work as far as the systemd service is concerned. However, the Bela program will fail to start saying "button pressed ... exit". I don't have a command line option / setting to fix that yet. For now you should make a manual change to /root/Bela/core/PRU.cpp: the line that reads if(stopButton.read() == 0){ should become if(stopButton.read() == 1){. If this works, I can add a command-line option for it that can be stored in belaconfig in a couple of weeks from now.

      PS: I fixed the Pd pin number in the above table (was 34, it actually is 23)

        giuliomoro Yes i had used 23 already, assumed a typo. Which of the three buttons do i need to hold? Reset, Power or Bela button? - if i hold the Bela button th unit dosn't reach any state that i can ssh into -

        EDIT trying jumper wire

        the rightmost button on the front panel: I understand that that is the one you have assigned to bela-cape-btn after editing the .service file?

          giuliomoro Jumper wire helps. But "/root/Bela/core/PRU.cpp" contains no line for the stopButton. I went through some of the /include files but couldn't find that line either.

          giuliomoro and yes, the rightmost button on Pepper's front panel. If i could wish for sthg i would love to be able for this button only to fire "next patch" in loop mode, when i have pressd it for a minimum of a second. Just slipping this in while we're at it. Thank you for walking me through.

          hmmm maybe you have an oldish version of the codebase. It looked like this before:

                                 if(belaCapeButton.read() == 0){

          again, make it == 1 instead.

          Actually now that I think of it you should probably update your Bela code anyhow, or the whole belaconfig stuff may not work.

            giuliomoro You were right, it was "if(stopButton.read()..." - changed value to 1. Have to reattach to Pepper to see what it does, bare with me...

            After the stopButton.read change in PRU.cpp IDE starts up, but fails to run, in single patch mode i get this:

            Nov 03 17:17:22 bela stdbuf[969]: Button pressed, quitting
            Nov 03 17:17:22 bela stdbuf[969]: make: warning: Clock skew detected. Your build may be incomplete.
            Nov 03 17:17:22 bela stdbuf[969]: make: Leaving directory '/root/Bela'
            Nov 03 17:17:23 bela systemd[1]: bela_startup.service: Service hold-off time over, scheduling restart.
            Nov 03 17:17:23 bela systemd[1]: Stopped Run Bela at boot.
            Nov 03 17:17:23 bela systemd[1]: bela_startup.service: Start request repeated too quickly.
            Nov 03 17:17:23 bela systemd[1]: Failed to start Run Bela at boot.
            Nov 03 17:17:23 bela systemd[1]: bela_startup.service: Unit entered failed state.
            Nov 03 17:17:23 bela systemd[1]: bela_startup.service: Failed with result 'start-limit-hit'.

            Ay. i guess i will return everything to initial state now.

            Bela version is v0.3.8b btw

            Does it run when you run the project from the IDE?

              giuliomoro No, the messages above came from the IDE. It also doesn't matter, if i choose single patch or loop mode. The essage "bela stdbuf[969]: Button pressed, quitting" appears regardless.

              yeah but what if you start a program from the IDE? That's the first step we need to get to work before the rest falls into place.In particular, I would like to make sure that PRU.cpp got rebuilt after your changes. You could do rm /root/Bela/build/core/PRU.o before starting a project and that should force a rebuild of PRU.cpp.

              I did start from the IDE, too. rm /root/Bela/build/core/PRU.o gives me a "no such file or directory"

              and when you start the program from the IDE it should show that it rebuilds PRU.cpp, is that the case?

              Okay IDE is single patch mode is doing something

              Building PRU.cpp...
              ...done
              Linking...
              ...done
              Enabling Bela at startup in a loop...
              project set to run on boot succesfully

              Running several different patches then each say

              Button pressed, quitting
              ...done
              Enabling Bela at startup in a loop...
              Bela stopped
              project set to run on boot succesfully

              All in IDE.