Programming advice

Status
Not open for further replies.

sixeight

Well-known member
I need some programming advice for my VController project. The current code can be found here: https://github.com/sixeight7/VController_v2

The VController is a midi controller that can control three other devices at the moment, but as more people are showing interest I would like to add more devices.

I would like to make it easier to add code for a new device.

At the moment I find I have to update my code on almost all pages to achieve this. This is because a new device will need to "connect" with all parts of my code, like buttons, displays, midi messages, etc.

Right now I have in many places code that looks like this:
Code:
void OnProgramChange(byte channel, byte program)
{
  check_PC_in_GP10(channel, program);
  check_PC_in_GR55(channel, program);
  check_PC_in_VG99(channel, program);
}

To add a new device I will have to add a line to this part of the code, but also in 20 other places. Is there a smarter way to do this?

I have looked at object-oriented programming, but I cannot create one type of object that can serve for all the devices, because these devices are totally different and need big portions of unique code.

But maybe there is a smart way to organize and build my code. I welcome your ideas.
 
Last edited:
Well it is hard to know what we are dealing with here .... but here is a suggestion ...

if you code up a bunch of presets for your VG99, GP10, and GR55 and create pointers to them, and then group the function pointers into arrays, you could use an enum of the presets to choose the funtion (or just loop through them all)

so, in pseudo-code:

Code:
GP10CheckPC() {blah;}
GP1111CheckPC() {blah;}
GR55CheckPC() {blah;}
GR8888CheckPC() {blah;}


fptrA = GP10CheckPC; // I know the syntax is wrong ...its pseudo-code...
fptrB = GP1111CheckPC;
fptrC = GR55CheckPC;
fptrD = GR8888CheckPC;

Arrayfptr [4] {fptrA, fptrB, fptrC, fptrD};

enum preset {GP10, GP1111, GR55, GR8888}; //order has to match order in Arrayfptr

preset PRESET=GR8888;

void OnProgramChange(byte channel, byte program)
{
Arrayfptr[PRESET] (channel, program);
}

makes it easy to add presets unique code that is then used 'wherever" ... you don't have to change the code in your midi callback at all.
 
Status
Not open for further replies.
Back
Top