I'm creating an FSM in python (it's a step sequencer and sample pad based on a Raspberry Pi 2).
Right now there are two states and the third is the Menu. This is handled by a class System which handles all the states. The Menu state has to edit the attributes of the other states, so I passed the other states to its constructor.
class State(object): def buttonPress(self, numberButton): raise NotImplementedError class StepSequencer(State): def buttonPress(self, numberButton): ... class SamplePad(State): def buttonPress(self, numberButton): ... class Menu(State): def __init__(self,stepSequencer,samplePad): self.stepsequencer = stepSequencer self.samplepad = samplePad def buttonPress(self, numberButton): ... def setMenuItem(self, currentMenuItem): self.currentMenuItem = currentMenuItem class MenuItem(object): def __init__(self, text): self.text = text class System(object): def __init__(self): self.stepsequencer = StepSequencer() self.samplepad = SamplePad() self.menu = Menu(self.stepsequencer, self.samplepad) def setState(self,state): self.state = state def buttonPress(self, numberButton): self.state.buttonPress(numberButton)
I can't figure out how to create the structure for the menu. I thought of creating a class MenuItem for every menu item so the Menu state has all these objects and can change the current MenuItem, however, there are some things that I cannot overcome:
how can I declare all the menu items and pass them to the Menu state to create dynamically the structure of the menu?
having this menu:
Step Sequencer settings: Set samples Set volume Sample Pad setting: Set samples Set volume
for example, if I want to have a slider that sets the volume, do I have to create another state to handle this? This state can be another state of the System or it must be a substate of the Menu state?
can I execute the code of each state using this logic? :
while(system.state = system.stepsequencer): ... while(system.state = system.menu): ...
The state is changed by a listener in another thread. This seems a very unorthodox way of handling states but it seems to work. If this is effective, how can I handle the Menu substates?