C++, std:ofstream, and controlling an amplifiers mute function

Recently, I updated the unit I am running applications on with a new CODEC and amplifier; the older parts are EOL. There was a bug with the old setup. For some reason, even when a tone was not being generated, you could hear a noise emanating from the speaker when the LCD brightness was on a higher setting.

In order to counteract that problem the Amplifiers mute function (standby for old amp) was toggled on and off before and after a tone was produced:

const char *const amplifierGPIO = "/sys/class/gpio/gpio107/value";

    void amplifierOn()
    {
      std::ofstream amp(amplifierGPIO);
      if (amp.is_open())
      {
        amp << "1";
        amp.close();
      }
    }

    void amplifierOff()
    {
      std::ofstream amp(amplifierGPIO);
      if (amp.is_open())
      {
        amp << "0";
        amp.close();
      }
     }

The problem is that now in order to get the same functionality to work with the new amplifier I have to surround the ofstream in sleep statments:

const char *const amplifierGPIO = "/sys/class/gpio/gpio107/value";

    void amplifierOn()
    {
      sleep(1);
      std::ofstream amp(amplifierGPIO);
      if (amp.is_open())
      {
        amp << "1";
        amp.close();
      }
      sleep(1);
    }

    void amplifierOff()
    {
      sleep(1):
      std::ofstream amp(amplifierGPIO);
      if (amp.is_open())
      {
        amp << "0";
        amp.close();
      }
      sleep(1);
     }

1) Any ideas about why this might be the case? I do not believe it's the mute function on the amplifier itself because outside of the application that uses this ofstream (inside the boot loader) I can play a song and toggle it on and off with the mute function, with no delay needed.

2) Is there another way (other than ofstream) of toggling the GPIO pin 107 from within my application?

Have you considered doing a stack trace to hunt down the timing issue for this?

What system are you working on and what is the C++ compiler you are using, etc?

1 Like

@Neo I am working on it. The system is so old I am having to build a VM with Ubuntu 14.04 in order to properly build the kernel in and IDE that will allow me to debug. Hell, in order to build the kernel properly in general. I am also trying to troubleshoot gdb problems. The make file is telling my that the debugging symbols are making it into the build but gdb can't seem to find them. I will get back to you once I have all these 3'rd party tools working.