Creating a state machine with changing frames - is it possible?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Creating a state machine with changing frames - is it possible?

Karina Coug
Alright, I'm about to rip my hair out. I'm working to make a simple application on my Raspberry Pi that changes screens with each change in state (I'm using a state machine). Is doing this even possible?

For example: User sees starting screen, pushes button which places them in state machine. Each state in the state machine has an exit, such as "button pushed," or "input received from Raspberry pi", and then it goes to a new state with a new screen.

Here's what I have:
-----------------------------------------------------------------------------------------------------------------------------------
#state.py

class State(object):
    def __init__(self):
        print('Processing current state:', str(self))
        
    def on_event(self, event):
        pass
    
    def __repr__(self):
        return self.__str__()
    
    def __str__(self):
        return self.__class__.__name__
-----------------------------------------------------------------------------------------------------------------------------------
#my_states.py

from state import State
import time
import wx
import RPi.GPIO as GPIO

BUTTON = 40
PHOTO_TRANSISTOR = 37
RELAY = 38
LED = 12

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(BUTTON, GPIO.IN)
GPIO.setup(PHOTO_TRANSISTOR, GPIO.IN)
GPIO.setup(RELAY, GPIO.OUT)
GPIO.setup(LED, GPIO.OUT)
GPIO.output(RELAY, GPIO.LOW)
GPIO.output(LED, GPIO.HIGH)

class WelcomeState(State):
    def on_event(self,event):
        print(event)
        png = wx.Image('2 insert card.png', wx.BITMAP_TYPE_ANY).ConvertToBitmap()  -----This is where my problem is
        wx.StaticBitmap(self, -1, png, (10,5), (png.GetWidth(), png.GetHeight()))
        if (GPIO.input(BUTTON) == True):
            return InsertCardState()
        else:
            return WelcomeState()                                                                                       -------Also trying to figure out how to simply stay in the state until input is received, doing this keeps looping it and it gets stuck
    
class InsertCardState(State):
    def on_event(self, event):
        print(event)
        if (GPIO.input(BUTTON) == True):
            return FillState()
        return InsertCardState()  
    
class FillState(State):
    def on_event(self, event):
        print(event)
        runWaterPump()
        return ThankYouState()
    
class ThankYouState(State):
    def on_event(self, event):
        print(event)
        return WelcomeState()
        
def runWaterPump():
    runTime = 10
    time.sleep(runTime)
    GPIO.output(RELAY, GPIO.HIGH)
-----------------------------------------------------------------------------------------------------------------------------------
#state_machine.py
from my_states import WelcomeState

class StateMachine(object):
    
    def __init__(self):
        self.state = WelcomeState()
        
    def on_event(self, event):
        self.state = self.state.on_event(event)
-----------------------------------------------------------------------------------------------------------------------------------
#main.py
#!/usr/bin/env python
import wx
import images
import time
import threading
from threading import Thread
from multiprocessing.pool import ThreadPool
from state_machine import StateMachine
from state import State
from wx.lib.pubsub import pub

        
class Main(wx.Frame):
    def __init__(self, parent, title):
        super(Main, self).__init__(parent, title=title, size=(300,250))
        self.Move((0,0))
        self.Maximize()
        self.Show()
        self.bitmap1 = wx.StaticBitmap(self, -1, wx.Bitmap('1 welcome.png'), (0,0))
        self.welcome_button = wx.Button(self, label="WELCOME")
        self.Bind(wx.EVT_BUTTON, self.startSystem, self.welcome_button)
        
    def startSystem(self,event):
        thread = Thread(target = runSystem, args = (self,))
        thread.start()
        
def runSystem(arg):
    print('runSystem')
    global systemEvent
    machine = StateMachine()
    true_on = True
    while true_on:
        machine.on_event(machine)                                                          ----This main code all works, it just gets stuck once it gets to the first state
     

if __name__ == '__main__':
    
    app = wx.App()
    Main(None, title='Tide Detergent Station')
    app.MainLoop()
        

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Creating a state machine with changing frames - is it possible?

Adrian Hill
Consider using a wx.Notebook with multiple pages to display different "screens" - it is simple to switch between notebook pages.

Also, consider the pigpio library on the Raspberry Pi - it's included with recent Raspbian versions and is more convenient than
RPi.GPIO.

Yes, it is all possible.


On Friday, April 6, 2018 at 9:23:39 AM UTC-6, Karina Coug wrote:
Alright, I'm about to rip my hair out. I'm working to make a simple application on my Raspberry Pi that changes screens with each change in state (I'm using a state machine). Is doing this even possible?

For example: User sees starting screen, pushes button which places them in state machine. Each state in the state machine has an exit, such as "button pushed," or "input received from Raspberry pi", and then it goes to a new state with a new screen.

Here's what I have:
-----------------------------------------------------------------------------------------------------------------------------------
#state.py

class State(object):
    def __init__(self):
        print('Processing current state:', str(self))
        
    def on_event(self, event):
        pass
    
    def __repr__(self):
        return self.__str__()
    
    def __str__(self):
        return self.__class__.__name__
-----------------------------------------------------------------------------------------------------------------------------------
#my_states.py

from state import State
import time
import wx
import RPi.GPIO as GPIO

BUTTON = 40
PHOTO_TRANSISTOR = 37
RELAY = 38
LED = 12

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(BUTTON, <a href="http://gpio.in/" style="color:rgb(17,85,204)" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fgpio.in%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHUPZVRgvpnfScb0AtMs76P0hkLXg&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fgpio.in%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHUPZVRgvpnfScb0AtMs76P0hkLXg&#39;;return true;">GPIO.IN)
GPIO.setup(PHOTO_TRANSISTOR, <a href="http://gpio.in/" style="color:rgb(17,85,204)" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fgpio.in%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHUPZVRgvpnfScb0AtMs76P0hkLXg&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fgpio.in%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHUPZVRgvpnfScb0AtMs76P0hkLXg&#39;;return true;">GPIO.IN)
GPIO.setup(RELAY, GPIO.OUT)
GPIO.setup(LED, GPIO.OUT)
GPIO.output(RELAY, GPIO.LOW)
GPIO.output(LED, GPIO.HIGH)

class WelcomeState(State):
    def on_event(self,event):
        print(event)
        png = wx.Image('2 insert card.png', wx.BITMAP_TYPE_ANY).ConvertToBitmap()  -----This is where my problem is
        wx.StaticBitmap(self, -1, png, (10,5), (png.GetWidth(), png.GetHeight()))
        if (GPIO.input(BUTTON) == True):
            return InsertCardState()
        else:
            return WelcomeState()                                                                                       -------Also trying to figure out how to simply stay in the state until input is received, doing this keeps looping it and it gets stuck
    
class InsertCardState(State):
    def on_event(self, event):
        print(event)
        if (GPIO.input(BUTTON) == True):
            return FillState()
        return InsertCardState()  
    
class FillState(State):
    def on_event(self, event):
        print(event)
        runWaterPump()
        return ThankYouState()
    
class ThankYouState(State):
    def on_event(self, event):
        print(event)
        return WelcomeState()
        
def runWaterPump():
    runTime = 10
    time.sleep(runTime)
    GPIO.output(RELAY, GPIO.HIGH)
-----------------------------------------------------------------------------------------------------------------------------------
#state_machine.py
from my_states import WelcomeState

class StateMachine(object):
    
    def __init__(self):
        self.state = WelcomeState()
        
    def on_event(self, event):
        self.state = self.state.on_event(event)
-----------------------------------------------------------------------------------------------------------------------------------
#main.py
#!/usr/bin/env python
import wx
import images
import time
import threading
from threading import Thread
from multiprocessing.pool import ThreadPool
from state_machine import StateMachine
from state import State
from wx.lib.pubsub import pub

        
class Main(wx.Frame):
    def __init__(self, parent, title):
        super(Main, self).__init__(parent, title=title, size=(300,250))
        self.Move((0,0))
        self.Maximize()
        self.Show()
        self.bitmap1 = wx.StaticBitmap(self, -1, wx.Bitmap('1 welcome.png'), (0,0))
        self.welcome_button = wx.Button(self, label="WELCOME")
        self.Bind(wx.EVT_BUTTON, self.startSystem, self.welcome_button)
        
    def startSystem(self,event):
        thread = Thread(target = runSystem, args = (self,))
        thread.start()
        
def runSystem(arg):
    print('runSystem')
    global systemEvent
    machine = StateMachine()
    true_on = True
    while true_on:
        machine.on_event(machine)                                                          ----This main code all works, it just gets stuck once it gets to the first state
     

if __name__ == '__main__':
    
    app = wx.App()
    Main(None, title='Tide Detergent Station')
    app.MainLoop()
        

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.