10 years, 11 months ago.

Dynamically reconfiguring BusOut and BusInOut

I want to reconfigure some pins on a KL25Z dynamically based on the status of some other inputs I'm testing. The scenario is I've built a matrix keyboard scanner the connects to a musical keyboard and and sends Midi messages over USB. That works OK but I want to re-configure the scanning matrix based upon reding a set of links so I can re-configure the board in order to allow for different keyboard wiring without needing to adjust the BusOut and BusInOut statements and re-compile.

I can re-confiure BusOut or BusInOut within the body of the program but it doesn't work if I put the code within conditional statements such as switch or if?

Any ideas? I am a MBED / C/C++ novice.

2 Answers

10 years, 11 months ago.

With reconfiguring you mean that you want your BusInOut to have different pin definitions? And I guess you try to make a new BusInOut object in an if statement? The compiler doesn't understand that :)

You can get it to work by defining already a pointer to a BusInOut object, and then changing the type of BusInOut object it points too. This way the compiler already has the pointer it can do its magic on, independent of where it points too exactly.

Code example (for regular mbed, but just pin definitions):

#include "mbed.h"

BusInOut *bus;

int main() {
    int i = 0;
    while(1) {
         delete bus;
         if (i==5)
            bus = new BusInOut(p10, p11, p12);
         else
            bus = new BusInOut(p13, p14, p15);
         
         bus->write(i);
         
         i++;
    }
}

Some important things to take into account:

Make sure the 'bus' pointer points to an actual BusInOut object (ie: the new part) before any function is used on it. Otherwise your KL25Z will go cry in a corner (I guess it will crash). Aditionally after it is deleted and before a new BusInOut object is assigned to it same story.

Every call to 'new' must also have a call to 'delete' (unless that specific object has to be used forever). If you don't do that you just created your first memory leak ;). (Well I guess it is your first one).

And that's why in general it is easier not to create objects this way, you can run into all kind of nasty memory stuff. But for what you want I think you need it.

(The example compiles, I didn't check functionality).

10 years, 11 months ago.

Thanks for the help it's step forward in my education.

I've tried what you suggested but it looks like I've got something wrong

my code is:

#include "mbed.h"

BusOut *col;
BusInOut *row;

int main()
{

    int i=1;
    delete row;
    delete col;
    if (i==1) {
        col = new BusOut  (PTC7, PTC0, PTC3, PTC4,  PTC5,  PTC6,  PTC10, PTC11 );
        row = new BusInOut(PTA1, PTA2, PTD4, PTA12, PTA4,  PTA5,  PTC8,  PTC9  );
    } else {
        col = new BusOut  (PTC11, PTC6, PTC4, PTC0,  PTC10,  PTC5,  PTC3, PTC7 );
        row = new BusInOut(PTC9,  PTC8, PTA5, PTA4,  PTA12,  PTD4,  PTA2, PTA1 );
    }
    col->write(i);
    row->write(i);

    row.mode(PullUp);

    while (1) {
    }
}

I get the error at line 22:

expression must have a class type

You have a dot still there. A dot is used to call a function on an object, an arrow (->) is used to call a function when you have a pointer to an object.

posted by Erik - 20 May 2013