Introduction
I work with raspberries PI GPIO. So far, I have written code in the same way as in C, using functions to group sections of code.
My work went so far that I am happy that everything works, but now everything is starting to get confused, and therefore I would like to move on to an object-oriented approach.
Problem
Here is the problem I am facing.
At the moment, I have a class that represents my "device". (The hardware I built that is connected to the GPIO port.) The hardware has 2 separate partitions. One section is the "input" section, and the other is the "exit".
To help you better understand this, the input section is an ADC. (An analog-to-digital converter.) This is a device that converts an analog signal into a 10-bit binary representation. (If you are not familiar with electronics.)
The output section is simply a transistor that turns a set of LEDs on and off.
I wanted to have a class that represented the ADC Board, and one that represented the LED board, because they are two conceptually different devices, because they are not connected to each other.
This causes a problem, since the GPIO pins must be set to certain values before their modes are set. By values I mean "HIGH" or "LOW", 1 or 0. By modes, I mean "INPUT" or "OUTPUT". This sounds strange, but basically, the ADC will desynchronize if the control lines are not set to their correct LOGIC HIGH and LOGIC LOW values before it is turned on. (This is a very strange device, it does not turn on until it is told, even if it is connected to a power source (VCC or VDD 5.0V). One of the control lines sends a signal to turn on the device.
To accomplish the above, make sure the GPIO pins are initially in INPUT mode. To “make ADC work correctly,” we first set the data values (HIGH / LOW) that must be present on the pins before they are changed to OUTPUT mode. Thus, when the mode changes from INPUT to OUTPUT, data with the correct values is present, and we will not violate the ADC.
My initial idea was to have a constructor for the ADC, which first sets the values for the output, and then changes the required outputs from INPUT mode to OUTPUT mode. But this forces us to build the ADC Board class to the LED Board class.
This can be fixed with both constructors executing the same code to set the output modes, but this seems like a bad idea because we call two bits of code twice - not a very elegant solution.
Another solution is to have a GPIOPort class that combines input and output devices together, but that is also not very elegant, and it would be difficult to change if we ever added a second, identical LED board. (For instance.)
What I think I want, but I'm not sure ...
I think I want another class that represents GPIOPort itself. (In my opinion, is this an abstract idea?) Then I think that I want the "class inside the class" to represent the ADC Board and the "class in the class" to represent the LED Board. I don’t remember what this method is called, but usually the "outer class" looks like a wrapper with a pointer to an object of the type "inner class" and the create method and destroy method. The outer class does something like pointer = new type; in the create method and delete pointer in the destroy method. This allows you to call the constructor if necessary, and the class destructor will be called if necessary.
The fact is that the constructor of the GPIOPort class handles the order in which these objects are created, which hides everything from main (). Basically, a programmer just does something like GPIOPort myGPIOPort; and this handles everything you need, so you don’t need to include 20 lines of code in main () to set up data for output contacts, which is the only other solution. (What I did not mention above).
Questions
So my first question is that this method is known? I thought this was called a wrapper class, but my understanding is a wrapper class for using such fundamental types as double and int . (And adding methods like clear() or reset() or something like that.) Is this what I really want to do, or is there a better method? (I guess this boils down to "how do I fix my problem.")
My second question is that, as far as I remember, I have to use some of the methods (destructor?) Of the virtual methods, but I can’t remember why. (Or maybe I'm not doing this, and I'm just embarrassed.)
My third question is: are there any examples of this that I can use to help myself understand this, or, alternatively, where I can improve my understanding. (Resources).
Thanks, obviously this is a pretty long question. I tried to include as much information as possible to help explain the situation. If you want to clarify, I will try to improve what I said.
Edit: More device information
Data must be sent to the GPIO pins before their mode is changed from input to output.
The GPIO pins look like all zeros, since the pins have push-pull resistors and they are still set as inputs. Data that was sent is not displayed until its mode is changed.
Then the contacts are output. (Or some of them anyway.) Now the data that was sent is displayed on the contacts.
If the pins are set to output mode before sending data, we cannot prevent the ADC from turning on, since the data pin that controls the ADC can be turned on to HIGH. It can be set to LOW, but there is no way to tell its status is undefined until we tell GPIO what values we want before setting the output mode. Fortunately, we can guarantee that all contacts will be in input mode.