MBed on class pointer issue

05 Aug 2011

SOVLED: - Problem is the Create Mechanism is incorrect.

Thank you for any help you can give in advance.

The problem is my code crashes the MBed on the "Crashes here" line.

I am attempting to dynamically allocate the ports on the chip. I am having trouble on the dynamic allocation and assigning it to a on class pointer.

The use of LED_Two works correctly with the pointer defined and then allocated a port and it allows it work correctly. However allocating it to LED_One it then causes the MBed to hang even though they are defined the same.

Is it something to do with how I am declaring the pointer?

File: main.c

#include "mbed.h"

#include "LED_Light.h"

DigitalOut myled(LED1);

int main() {

    LED_Light *LED = 0;
    
    while(1) {
        myled = 1;
        wait(0.2);
        myled = 0;
        wait(0.2);
        
        LED = LED -> Create();
        LED -> Initalise(LED3);
        wait(10);
    }
}

File: LED_Light.h

#include "mbed.h"

#ifndef LED_LIGHT
#define LED_LIGHT

class LED_Light {

private:

    //-- Definition of the private LED Lights
    
    DigitalOut *LED_One;
    
    //-- Constructor
    LED_Light();
    
    //-- Destructor
    ~LED_Light();

public:

    LED_Light *Create();
    
    void Initalise(PinName LED_One_Pin);
};

#endif

File: LED_Light.cpp

#include "LED_Light.h"

Serial pc(USBTX, USBRX);

PinName Port[4] = {P1_18, P1_20, P1_21, P1_23};

//---------------------------------------------------------
//-- Name: Create
//--
//-- Function: Standard Create Mechanism
//--
//---------------------------------------------------------

LED_Light *LED_Light::Create(){

    LED_Light *The_LED = 0;
    
    if (this == 0){
        The_LED = new LED_Light();
    }
    
    return The_LED;
}

void LED_Light::Initalise(PinName LED_One_Pin){

    DigitalOut *LED_Two;
    pc.printf("\n\r");

    pc.printf("Creating LED Two\n\r");
    LED_Two = new DigitalOut (Port[2]);
    pc.printf("Setting LED Two On\n\r");
    LED_Two -> write(1);
    pc.printf("End\n\r");

    pc.printf("Creating LED One\n\r");
    LED_One = new DigitalOut (Port[1]);  //-- Crashes Here
    pc.printf("Setting LED One On\n\r");
    LED_One -> write(1);
    pc.printf("End\n\r");
}

LED_Light::LED_Light(){ };