Pin name as function argument?

14 Feb 2012

Hi all,

for my project I need three one-wire micro LANs. Trouble is, I can't figure out how to pass the pin name as an argument to my functions.

This is the reset function for a single run of DS18B20 which works ok:

unsigned char ow_reset() {
    unsigned char presence;
    DQ.output();
    DQ = 0;
    wait_us(480); // leave DQ low for 480 us
    DQ.input(); // allow line to return high
    wait_us(70); // wait for presence
    presence = DQ; // get presence signal
    wait_us(410); // wait for end of timeslot
    return(presence); // presence signal returned, 0=presence, 1 = no part detected
}

What I want is to pass the pin name to ow_reset, but my poor C skills won't let me. I've tried some variations found on this forum, including the examples in the 'Writing a library' thread without luck.

Any help on this is greatly appreciated.

15 Feb 2012

Here is an example I wrote one time to show someone how to dynamically associate a mbed object with a particular pin at runtime.

#include "mbed.h"

int main() 
{
    static const PinName PinNameArray[4] = { LED1, LED2, LED3, LED4 };
    int                  LEDNumber = -1;
    
    while (LEDNumber < 1 || LEDNumber > 4)
    {
        printf("\nWhich LED should I blink (1 - 4)? ");
        scanf("%d", &LEDNumber);
    }
    printf("\nBlinking LED%d...\n", LEDNumber);
    
    DigitalOut* pLED = new DigitalOut(PinNameArray[LEDNumber-1]);
    while(1) 
    {
        *pLED = 1;
        wait(0.2);
        *pLED = 0;
        wait(0.2);
    }
}

I think that in your case you might want to create three DS18B20 objects in your main program, one associated to the right pin for each device. Then you would pass in a reference to the object to you function to write to the device on a particular pin.

#include <mbed.h>

// Instantiate 3 DigitalOut objects, each associated to a different pin.
DigitalOut g_led1(LED1);
DigitalOut g_led2(LED2);
DigitalOut g_led3(LED3);

// This function has as a parameter, a reference to one of the above g_led* 
// objects to flip.
void FlipLED(DigitalOut& ledToFlip)
{
    ledToFlip = !ledToFlip;
}

int main() 
{
    for(;;)
    {
        wait(0.2f);
        FlipLED(g_led1);
        wait(0.2f);
        FlipLED(g_led2);
        wait(0.2f);
        FlipLED(g_led3);
    }
}
19 Feb 2012

If you code an class for your DS18B20 reading code, and pass the constructor the pin to read from/write to, that works. I've done similar for my matrix_HDSP ( only that requires a number of pins):

here's the .h file: /users/tonymudd/libraries/matrix_HDSP/lstlob/docs/matrixDisplay_8h_source.html

and it's used by:

    matrixDisplay *mDisplay;
    mDisplay = new matrixDisplay(0.5,  p19, p20, p10, p11, p12, p13, p14);

I've also done one for the DS18B20 too - which is based on code by Petras Saduikis, but copes with multiple sensors on the same pin. I'm not sure about how useful this is, i.e. when you've installed something with a handful of the sensors, it will be a right pain identifying them and then hardwiring their factory IDs into the code/configuration, much easier to have one-per-pin.

Tony.

20 Feb 2012

Thanks Adam and Tony!

It works!

08 May 2013