1

I am modifying a pedal pi (link here). I split the input program from the output program. For simplicity right now, that all it does. The standard out from my input program is being fed to the standard in of my output program using a pipe and it prints the value to the screen:

sudo ./input | sudo ./output

I have to use sudo because the bcm2835 requires me to be in root. My problem is,that there is like a 3 second delay between when I strum the strings on the guitar, and when the output program prints the values. When I just print the values from the input program, there is no delay, So I am guessing that the delay is caused by the pipe. I researched it, and some forums said I could use sudo unbuffer ./input | sudo ./output but using that still has the same delay. Is there a setting I have to tweak to get rid of this delay?

Input code:

#include <stdio.h>
#include <bcm2835.h>
#include <inttypes.h>

uint32_t read_timer=0;
uint32_t input_signal=0;

int main(int argc, char **argv)
{
    if (!bcm2835_init())
    {printf("bcm2835_init failed. Are you running as root??\n"); return 1;}

    if (!bcm2835_spi_begin())
    {printf("bcm2835_spi_begin failed. Are you running as root??\n"); return 1;}

    bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);      // The default
    bcm2835_spi_setDataMode(BCM2835_SPI_MODE0);                   // The default
    bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_64);    // 4MHz clock with _64 
    bcm2835_spi_chipSelect(BCM2835_SPI_CS0);                      // The default
    bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW);      // the default

    uint8_t mosi[10] = { 0x01, 0x00, 0x00 }; //12 bit ADC read channel 0. 
    uint8_t miso[10] = { 0 };

    while(1)
    { 
        bcm2835_spi_transfernb(mosi, miso, 3);
        input_signal = miso[2] + ((miso[1] & 0x0F) << 8); 
        printf("%d \n", input_signal);
    }

    bcm2835_spi_end();
    bcm2835_close();
    return 0;
}

Output code:

#include <stdio.h>
#include <bcm2835.h>
#include <inttypes.h>


uint32_t input_signal=0;

int main(int argc, char **argv)
{

    // Start the BCM2835 Library to access GPIO.
    if (!bcm2835_init())
    {printf("bcm2835_init failed. Are you running as root??\n"); return 1;}


    // Start the SPI BUS.
    if (!bcm2835_spi_begin())
    {printf("bcm2835_spi_begin failed. Are you running as root??\n"); return 1;}

    bcm2835_gpio_fsel(18,BCM2835_GPIO_FSEL_ALT5 ); //PWM0 signal on GPIO18    
    bcm2835_gpio_fsel(13,BCM2835_GPIO_FSEL_ALT0 ); //PWM1 signal on GPIO13    
    bcm2835_pwm_set_clock(2);                      // Max clk frequency (19.2MHz/2 = 9.6MHz)
    bcm2835_pwm_set_mode(0,1 , 1);                 //channel 0, markspace mode, PWM enabled. 
    bcm2835_pwm_set_range(0,64);                   //channel 0, 64 is max range (6bits): 9.6MHz/64=150KHz PWM freq.
    bcm2835_pwm_set_mode(1, 1, 1);                 //channel 1, markspace mode, PWM enabled.
    bcm2835_pwm_set_range(1,64);                   //channel 0, 64 is max range (6bits): 9.6MHz/64=150KHz PWM freq.

    while(1)
    {   
        scanf("%d", &input_signal);
        printf("%" PRIu32 "\n", input_signal);
        bcm2835_pwm_set_data(1,input_signal & 0x3F);
        bcm2835_pwm_set_data(0,input_signal >> 6);
    }
    //close all and exit
    bcm2835_spi_end();
    bcm2835_close();
    return 0;
}
9
  • On the website I can see only one example with INPUT and OUTPUT in the same loop and the same application. Also, the loop contain a delay of 0.25s before next check. Please edit your question with your modified code.
    – Ephemeral
    Jan 5, 2020 at 8:07
  • When I just print the values , function used for that ?
    – Ephemeral
    Jan 5, 2020 at 8:22
  • What do you mean by and when the output program prints the values ? values printed into the terminal with your printf function exactly , not a 'sound lag' ?
    – Ephemeral
    Jan 5, 2020 at 19:38
  • @Ephemeral I am printing the digital value of the guitar sound. I strum a string, then it takes about 3 seconds for the digital value of that strumming to be printed out. Jan 5, 2020 at 19:56
  • 1
    @Ephemeral There is no delay, also if I use just the input program and print the values, then there is no delay Jan 6, 2020 at 14:56

1 Answer 1

2

In your code Output code you have not define

bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_64); // 4MHz clock with _64

, you not divide the clock. Per default the clock freq. is 250MHz, so you receive with 250MHz clock and you send with 64Mhz... which would explain why it would work when a single program is used and the frequency is the same.

Try to divide the clock also on the read program, maybe the lag come when the clock frequency change ?

Sorry, maybe I should have thought about that first.

(I don't know if only one definition is sufficient)

7
  • I haven't tested this just yet, due to me having to add the code for the input, but how would this work when I want to insert an effect between the input and output. Previously I would just do ./input | ./effect | ./output. will that still work? Jan 7, 2020 at 2:24
  • Yes, you can change with your own code. Start ./output then start ./input : input contain the printf of your input input_signal = miso[2] + ((miso[1] & 0x0F) << 8); and will be display in the output prog. Replace my for loop by your while loop
    – Ephemeral
    Jan 7, 2020 at 5:33
  • @AubreyChampagne, see my edited answer (but I have not try the code)
    – Ephemeral
    Jan 7, 2020 at 5:56
  • There is still that delay. Do you need a video of what is happening to help you out? Jan 7, 2020 at 15:04
  • 1
    here is a link to the vid youtube.com/watch?v=weWowH7e0E8&feature=youtu.be. for testing purposes I am just sending it straight power. The wire I unplugged is the signal in. As you can see it takes a while for the signal to drop down. Jan 7, 2020 at 18:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.