Hi!

I'm trying to make Bela run a Pure Data patch that writes to a text file and calls the '/root/Bela/scripts/halt_board.sh' script. It's working but I get underrun and mode switch detections - so I quess it's not smooth. Should I worry about the underrun and MSW? Is there an easy way to fix it?

I've previously succeeded both shutting down the board and writing to a txt-file at cleanup with C++:

system("/root/Bela/scripts/halt_board.sh");

float gCount = 0;
void logCount(const char* filename, float data) {
    std::ofstream outputFile(filename, std::ios::app);
    if (outputFile.is_open()) {
        outputFile << data << std::endl;
        outputFile.close();
    }
}

void cleanup(BelaContext *context, void *userData)
{
    logCount("log.txt", gLogCount);
}

The write message when sent to [textfile] writes to disk from the audio thread which causes dropouts.
I would also assume that it rewrites the file every time instead of appending to it. You could use something like this instead:

[echo $1 plays with a total play time of $2 seconds >> log.txt(
|
[s bela_system]

    the solution with bela_system will be more resource intensive as it will spawn a new bash process for each command, but if it's done not too often it may not be a big issue. Alternatively, a few lines of C++ should allow you to write to the file directly from the Bela process.

      giuliomoro

      Thank you very much!

      I'm not quite sure what you mean 🙂 I tried this and both times didn't write anything to the log.txt file:



      giuliomoro

      Thank you!

      The command only needs to be called once during runtime.

      So is it possible to run both a 'render.cpp' and a '_main.pd' on the board? Or is there another way to run C++ code?

      As previously mentioned this worked fine in another project:

      float gCount = 0;
      void logCount(const char* filename, float data) {
          std::ofstream outputFile(filename, std::ios::app);
          if (outputFile.is_open()) {
              outputFile << data << std::endl;
              outputFile.close();
          }
      }
      
      void cleanup(BelaContext *context, void *userData)
      {
          logCount("log.txt", gLogCount);
      }

      EDIT:

      I found the info about combining Pure Data and C++ and the 'custom-render' example in the IDE.

      But I'm having a hard time finding out how much of the provided C++ in this file, that I need to keep in order to pass a value from the PureData and call the above 'logCount' function on cleanup.

      I basically need a custom render.cpp that runs the script from render() after a counter surpasses a certain value:
      system("/root/Bela/scripts/halt_board.sh");

      And this to run at clean up with float(s) coming in from the _main.pd:

      float gCount = 0;
      void logCount(const char* filename, float data) {
          std::ofstream outputFile(filename, std::ios::app);
          if (outputFile.is_open()) {
              outputFile << data << std::endl;
              outputFile.close();
          }
      }
      
      void cleanup(BelaContext *context, void *userData)
      {
          logCount("log.txt", gLogCount);
      }

        I tried this and both times didn't write anything to the log.txt file:

        I have no idea how ggee/shell works, but when you send to [s bela_system] it should print to the console of the IDE the command it is executing. If it doesn't, it means your Bela core code needs updating, but as you previously mentioned you had it working, I'd assume that it should be working still?

        ThomasEg And this to run at clean up with float(s) coming in from the _main.pd:

        You could copy core/defaul_libpd_render.cpp to your project as render.cpp and add your code to cleanup(). Additionally you'd need an extra line libpd_bind("my_count"); in setup() (next to the other libpd_bind() calls) and then in Bela_floatHook() add something like:

                if(strcmp(source, "mycount")==0){
                        gCount = value;
                        return;
                }

        But hopefully you can do it all with bela_system without need to touch the C++

        Oh nice! So the [s bela_system] works as an inlet for the board's terminal? So I could also use that to run the halt script as well? I will try this later and return. If I need to update the core code should I just update the board?

        EDIT:

        I flashed the SD-card with the Bela Image and then tried to run this patch on the board. But nothing happened in the IDE console and nothing was written to the 'text.txt' file.

        You need the dev branch of the Bela core code as explained in the page you linked in the "update to the experimental release" section

          giuliomoro

          Nice! Thank you very much.

          Writing to the txt-file now works perfectly. I'm trying to call the '/root/Bela/scripts/halt_board.sh' script using [s bela_system]. But I get this error in the console: 'Host key verification failed.'


          Just call poweroff . That script is meant to be run from the host computer

            giuliomoro

            I last and quick question.. The command seems to combine with a previously called command? 🙂

            How do you clear the command line?

            EDIT: I found a solution using '&&' to call two commands. Once again, thank you.

            [ echo $1 plays with a total play time of $2 seconds >> log.txt && poweroff (
            |
            [s bela_system]

            Wouldn't the [text] object do the job?