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:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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?
What operating system are you using?

If you're on an embedded system then check if you can directly write to the hardware.

@jlb I am on an embedded system. The kernel running the program is old (2.6.35.14) and the board is a Mx5 with a Digi SoM overlay. I was under the impression that writing to the value directory was writing directly to the hardware, I will look into it but maybe you can enlighten me as to the difference between what is happening here and writing directly to the hardware.
Last edited on
I am on an embedded system. The kernel running the program is old (2.6.35.14) and the board is a Mx5 with a Digi SoM overlay.

Who manufactured the hardware? What type of processor? What type of device drivers are available? Etc? Etc?

I have no idea what "Mx5" refers to, or what a "Digi SoM overlay" is or means.

Sorry for the in-house office lingo. The processor is an MCIMX51 and it's sitting on a Digi ConnectiCore Wi-i.MX51 board. Digi still sells it:

https://www.digi.com/products/embedded-systems/system-on-modules/connectcore-wi-mx51
You may be better off trying to find a message board specific to the processor start by checking the manufacturers website.

Read the specs of the new amp. Maybe it needs some delay between functions.

It might also be that the sleep causes something that's occurring in parallel to complete. In that case you'd have to do something to synchronize the functions.

Finally, you could reduce the code in both systems:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
const char *const amplifierGPIO = "/sys/class/gpio/gpio107/value";

    void writeToAmp(const char *str)
    {
      std::ofstream amp(amplifierGPIO);
      if (amp.is_open())
      {
        amp << str;
        amp.close();
      }
    }

    void amplifierOn() { writeToAmp("1"); }
    void amplifierOff() {writeToAmp("0"); }


Do you really want to open and close the stream each time? It might be more efficient to leave it open. Just be sure to flush it after each write.
Registered users can post here. Sign in or register to post.