Tl;Dr
Given a language with no polymorphism how to you elegantly modify the behavior of a leaf in a tree data structure.
Context
I'm programming an application in C for a microcontroller and I'm wondering what would be the best way to design flexible GUI-less menus.
Basically inputs may come from 2 different sources: The built-in buttons on the board used by the user and a Uart port used by the production team to calibrate the device before shipping.
I already have a mechanism using a thread to listen to inputs (from both the buttons and the serial port) who post them to a crude parser. The parser then takes action depending on the currently registered callback for the received input. It also knows if the menu must be printed on the LCD screen or through the serial port.
I'm also using a k-ary tree to represent the hierarchy between sub-menus and different options. Since the menus are not defined yet it'll allow me to simply add / remove nodes when my supervisor makes his mind on the supported features.
Now when the currently displayed menu is not a leaf, the behavior is quite simply. Left and Right moves between siblings, Back selects the parent and Enter selects the first child.
If its a leaf, Enter activates / deactivates modifications, Left and Right increment / decrement the value and Back cancels the moficiation.
Other concerns
- Am I over-engeenering stuff that could be done with a bunch of switch-cases?
- Are there ressources giving example of clean GUI-less menus when you don't know where the inputs come from, the actions you will have to take, the menus you must offer and where to display those menus?
- Since there is basically 2 options (leaf or not) should I just do everything with if/elses and move on?