I'm building a microcontroller project which is programmed in MicroPython, and I'm struggling to find a sensible way to extend my code to support multiple operating modes.
Currently it looks like this: I have a base hardware class which instantiates all the different hardware interfaces, and loads information from a configuration file, and an application class which does the useful work - they're separated primarily to stop them getting too unweildy.
class HardwareClass():
def __init__(self):
self.display = self.initialise_display()
self.sensor = self.initialise_sensor()
self.config = self.load_config_from_sd()
def ...
class ApplicationClass(HardwareClass):
def run(self):
while (True)
# do useful work
pass
app = ApplicationClass()
app.run()
What I'd like to do is have modes for testing and normal operation which are selected by a value in the configuration file.
My first thought was to simply write a different ApplicationClass for each mode - that means they can re-use all the hardware interfaces that were initialised at boot, and each will have a different run() method.
However, as I can't just do this:
import HardwareClass
class TestModeClass(HardwareClass):
def run(self):
# do testing
class RunModeClass(HardwareClass):
def run(self):
while (True)
# do useful work
because I don't know which class I'll need until the config loading is done.
I could define run() for each mode separately and import them at runtime, but I've seen that has issues, as well as making it difficult for the IDE to know whether I'm referencing valid class members (ie. the different bits of hardware) in each of the different run methods (or functions).
Any suggestions for how you'd arrange an application like this?
ApplicationClassand override a small number of methods but keep the rest of the functionality?