vendredi 3 janvier 2020

Design pattern for hardware slots that need different class/object instantiated and is determined on boot

This is a C++ question for an embedded custom board I am designing a program for. We will be running freeRTOS if that helps. The board has 3 physical slots/headers. 1 slot/header has 20 pins and will accept 1 of 3 specific boards, but in the future there may be more boards. The other two slots/headers have 18 pins and can each take 1 of 8 boards, but in the future there may be more boards.

The boards are generally communicated to through SPI but sometimes they have to be bit banged with manual calls to GPIOs(depends what board is installed). The communication scheme can not be standardized (not that I can think of) because of backwards compatibility issues. The boards that go in these slots have to be able to be plugged in to other, different, main boards if end customers choose.

I have an SPI class that brings up the base hardware SPI. I have a slot class that accepts a pointer to the SPI object and includes some reset pins and some test functions to determine what board is plugged into what slot. I instantiate 3 of these slot objects, one for each physical slot/header.

After determining what each slot contains I am now to the point I need to design/create/code up classes for each of the boards but I am running into an architectural issue, at least thats what it seems like to me. As this is an embedded system I would like to avoid the use of the keyword "new" but this is not set in stone.

My thinking is that, it would be wasteful on space to instantiate a copy of every board and then only use the 3 that were detected.

In my head I am thinking that my slot class maybe should have some sort of void pointer that can be set to point at the object that gets instantiated after we determine what board it is. In this case I am pretty sure I would have to use "new".

I know in C there is the concept of "union" where you create some sort of custom type that can be any one of some handful of types. When you create copies of this type it allocates enough memory for the largest member of the union. Can you do this in C++ and put classes as members of the union and then later somehow make it to be of the type of class you determined the slot to contain?

Any other design ideas?

Any other information that needs added for someone to get a better idea of the problem?

Thanks,

Aucun commentaire:

Enregistrer un commentaire