I am working on a project for an STM32-based device that has multiple peripherals connected to it. For the most simple version of my problem, these peripherals are one:
- stepper motor that is connected to a driver, which is driven by the GPIO pins of the STM32
- touch-screen display that communicates with the STM32 via UART
The display is used to display and manipulate the settings that the stepper is running on, like speed, slow-down characteristic (none/gradual), distance to reverse direction, etc.
So far I have written all my code in C, but when I want to expand the peripherals (for example: with another instance of a stepper motor, which is required) it would make a mess. So far I had used volatile global variables for maintaining all the settings for the stepper, and everything works, but I feel like it would be best to instead have an object for the stepper motor and maintain all the variables it uses to track its current status parameters as object variables. So I set out to rewrite the code to C++ and write a classes for a stepper motor, which went well so far, but now I’m running into an issue.
The current structure of my program:
- main.cpp: here all objects are instantiated, timers and UART are initialized, and FreeRTOS stuff
- display.hpp: header file for display objects
- display.cpp: display object code
- stepper.hpp: header file for stepper motor objects
- stepper.cpp: stepper motor code
The display both uses and manipulates data that is stored in the stepper object. Before, when I was using C, I could simply include the stepper.h file in the display.h file and vice versa, and they could call eachothers functions, like:
display.c:
int display_process_integer_data(uint8_t *buf) // buf = message stream received from display over UART
int retval = 0;
switch (buf[5]) // buf[5] defines the type of data is received from the display, i.e. start/stop, change speed etc.
{
case 0x00: // Start/stop
if (buf[8] == 0) { stepper_stop(); break; }
if (buf[8] == 1) { stepper_start(); break; }
case 0x01: // Change speed
if ( stepper_set_speed( buf[7]*256 + buf[8] ) ) { retval = -1; break; }
break;
default: // No valid case
retval = -1;
break;
}
return retval;
Ideally I would now change this to this:
display.cpp:
int Display::process_integer_data(uint8_t *buf)
int retval = 0;
switch (buf[5])
{
case 0x00: // Start/stop
if (buf[8] == 0) { motor1.start(); break; }
if (buf[8] == 1) { motor1.stop(); break; }
case 0x01: // Change speed
if ( motor1.setSpeed( buf[7]*256 + buf[8] ) ) { retval = -1; break; }
break;
default: // No valid case
retval = -1;
break;
}
return retval;
But this is not possible. Now that the stepper is an instantiated object in main.cpp, I can no longer call this function as this object doesn’t exist in this scope.
Now usually I’d say ‘make the function give back the data to main.cpp and then do the rest of the work there’, but there’s more information that this function does than could fit in one return value: The function both figures out the type of instruction that is received (stored in buf[5]) and the data contained in that instruction (stored in buf[7] and buf[8] as an int16). So that is not possible (I think?).
In other situations I got around this by making an object a variable inside the Display class, like:
display.hpp:
class Display
{
protected:
Stepper __motor = Stepper(uint8_t instantiating_var);
};
But this is more a hack than a proper solution I think, and it would only work if the display only interacts with __motor. I also need the Stepper object to call functions in the Display object, so this would not work. like:
stepper.cpp:
int Stepper::init()
{
while (0 != display.inform_speed(__speed)) {}
while (0 != display.inform_startstop(__running_FLAG)) {}
while (0 != display.inform_slowdown_mode(__mode)) {}
__initialized_FLAG = 1;
return 0;
}
(above may not be the best example, but I can imagine that alarm states detected by the stepper motor, such as running into an end-stop, will need to be reported to the display. I haven’t programmed this yet)
So my question ultimately is: What is the right way to have two objects in main.cpp have access to other object methods? I feel like I’m missing a very obvious solution to this problem but I can’t wrap my head around it atm.
Joppe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.