Calling Class Functions in a setup() Function

07 Jun 2011

Hi,

I'm trying to get familiar with the mbed, and I'm wondering why I can't put the calls to functions like TextLCD() and DigitalOut() in a setup() function that is called in main() and prototyped before main(). The program compiles fine if the lines associated with these functions are placed before main() or even inside main() but before reference to the variables that are associated in the DigitalOut or TextLCD functions.

Thank you!

(The code below gives a compiler error 20: the identifiers for mledn are undefined)

#include "mbed.h"
#include "TextLCD.h"

#define WAIT_time 0.04
#define D() wait(WAIT_time)

void setup();

int main(void)
{
    setup();
    while(1)
    {
        mled0 = 1;D();mled1 = 1;D();mled2 = 1;D();        
        mled3 = 1;mled0=0;D();
        mled1 = 0;D();mled2 = 0;D();mled3 = 0;D();D();D();
        
        mled3 = 1;D();mled2 = 1;D();mled1 = 1;D();        
        mled0 = 1;mled3=0;D();
        mled2 = 0;D();mled1 = 0;D();mled0 = 0;D();D();D();    
// print message to the LCD
    lcd.cls();
    lcd.locate(0, 0);
    lcd.printf("Hello World");
    lcd.locate(0, 1);
    lcd.printf("mbed NXP LPC1768");
    }
   return 0;
}

void setup()
{
    // set up LCD pins and LED pins
    TextLCD lcd(p24, p26, p27, p28, p29, p30);

    DigitalOut mled0(LED1);
    DigitalOut mled1(LED2);
    DigitalOut mled2(LED3);
    DigitalOut mled3(LED4);
}
07 Jun 2011

You are making mledn an object local to setup().

Normally the:

DigitalOut mled0(LED1);

Line is not included inside a function but just listed at the beginning of the code.

I tried:

DigitalOut mled0;

at the beginning but that didn't work.

07 Jun 2011

The particular class methods that you are trying to call in your setup() function are actually the constructors. These need to be called when the objects are defined and the definition sets the scope of the objects. In your code, you are defining the objects within the setup() function so they aren't visible from your main() function. These object will actually only exist while you are inside of the setup() function and will be destroyed when you exit the function.

Your best solution is probably to just define these objects in your main() function and not try to abstract away their initialization into another function. A more object oriented approach might be to put your main application logic into its own object, make class members of these pin/lcd objects, and then initialize them in your application object's constructor. Your main() function would then construct your application object and call a method named something like process() to actually enter the main loop. You could also just maintain pointers to the pin/lcd objects in main() and have setup() dynamically allocate and initialize the pin/lcd objects but I think this is more work. I think that just moving the setup() functionality into main() or making them globals as Clark has indicated is easier.

08 Jun 2011

Thanks, guys!

08 Jun 2011

I was annoyed with this also but just did it the way that works and went on..haha.