Hello,
I want to use a master enum to be used with other enums. The compiler won't accept that.
Is there anyway to do that ?
This is the code I want to design:
// master button index
typedef enum : uint8_t {} FUNCTION_BUTTON_INDEX_T;
// electrical monitor and control
typedef enum : uint8_t {NO_BUTTON, CURRENT_READING, VOLTAGE_READING,
POWER_READING} ELECTRICAL_BUTTON_INDEX_T;
// motor control modes
typedef enum : uint8_t {NO_BUTTON, SERVO_MOTOR_CONTROL, DC_MOTOR_CONTROL,
STEPPER_MOTOR_CONTROL} MOTOR_BUTTON_INDEX_T;
As I want to use the master button index in one master struct for function buttons:
// function_button
struct function_button{
uint16_t x;
uint16_t y;
uint16_t w;
uint16_t h;
uint16_t button_color;
uint16_t text_color;
char text[20];
FUNCTION_BUTTON_INDEX_T button_number;
};
In case I want to do this:
function_button button1;
button1.button_number = SERVO_MOTOR_CONTROL;
Then of course that's an error.
I thought of using a void *FUNCTION_BUTTON_INDEX_T ;
but that will require me to typecast every line after that.
Any suggestions ?
J-M-L
March 3, 2023, 8:51am
2
An enum is a type so you would end up with a kind of dual type in what you want to do if I got that right and There is no inheritance with enums
1 Like
typedef enum : uint8_t {} FUNCTION_BUTTON_INDEX_T;
Please explain in English what this line of code does
1 Like
The best I could do is to typecast it, this one has no error. But I'm searching for a solution not to use typecast as much as I can.
function_button b1;
b1.button_number = (FUNCTION_BUTTON_INDEX_T)CURRENT_READING;
I also think that I may not use enums, and instead go with defines and then use uint8_t
.
OK, I think I solved the problem, I shouldn't use a master enum, and just use a variable inside the struct; like this:
This:
// function_button
struct function_button{
uint16_t x;
uint16_t y;
uint16_t w;
uint16_t h;
uint16_t button_color;
uint16_t text_color;
char text[20];
FUNCTION_BUTTON_INDEX_T button_number;
};
Should be this:
// function_button
struct function_button{
uint16_t x;
uint16_t y;
uint16_t w;
uint16_t h;
uint16_t button_color;
uint16_t text_color;
char text[20];
uint8_t button_number;
};
Then I can assign it like this:
function_button b_current = {40, 60, 150, 30, TFT_BLUE, TFT_WHITE, "CURRENT READING", CURRENT_READING};
function_button b_voltage = {40, 90, 150, 30, TFT_BLUE, TFT_WHITE, "VOLTAGE READING", VOLTAGE_READING};
And it compiled without errors.
I thought I cold use the name of the enum as a master type to hold the data for other enums.
J-M-L
March 3, 2023, 9:19am
9
You might want to consider inheritance and pure virtual functions
for example
Run IoT and embedded projects in your browser: ESP32, Arduino, Pi Pico, and more. No installation required!
class Button {
protected:
uint16_t x;
uint16_t y;
uint16_t w;
uint16_t h;
uint16_t button_color;
uint16_t text_color;
const char* text;
public:
Button(const char * lbl) : x(0), y(0), w(10), h(12), button_color(0), text_color(0xFFFF), text(lbl) {}
virtual void action() = 0; // Pure virtual
};
class ElectricalButton : public Button {
public:
ElectricalButton(const char * lbl) : Button(lbl) {}
void action() {
Serial.print("ElectricalButton action : ");
Serial.println(text);
}
};
class MotorButton : public Button {
public:
MotorButton(const char * lbl) : Button(lbl) {}
void action() {
Serial.print("MotorButton action : ");
Serial.println(text);
}
};
MotorButton leftMotorButton("left engine");
MotorButton rightMotorButton("right engine");
ElectricalButton elecButton("power");
Button* myButtons[] = {&leftMotorButton, &rightMotorButton, &elecButton};
void setup() {
Serial.begin(115200); Serial.println();
for (auto && b : myButtons) b->action();
}
void loop() {}
Serial Monitor (@ 115200 bauds) will show
MotorButton action : left engine
MotorButton action : right engine
ElectricalButton action : power
➜ every Button has an action function and its behavior depends on the subclass but yet everything was stored in an array of pointers which types are from the base class.
1 Like
OK, thank you so much for this wonderful piece of code. This is completely different that my simple coding.
I stopped at the pure virtual functions before months and didn't come back to carry on that topic, but this should really put a good application advantage in my project
system
Closed
August 30, 2023, 11:49am
11
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.