SinelaboreRT Header Logo

SinelaboreRT

It's better when it's simple!

User Tools

Site Tools


Sidebar

UML-Tool specific Intro

Language Backends

Getting started

How-To

Examples

Designers Toolbox

There are better ways to model state machines than using spread sheets!

In the past different μC manufacturers have published application notes about the benefit of using state machines for the design of embedded software. An example is the application note SLAA402 from Texas Instruments (TI). It suggests to generate source code based on a spread sheet table. Nowadays several affordable UML modeling tools are available supporting the efficient design of state machines in a graphical way. SinelaboreRT generates production quality source code from state diagrams created with many different UML tools. Give it a try!

Latest Changes

wiki:backends:python_lang

Generating Python Code

To generate Python code call the code generator with the command line flag ’-l python’.

The generator generates just a Python class which implements the complete state machine. This has the benefit that your Python project does not become bloated with all kinds of helper classes. If required an optional base class can be specified in the configuration file. To get started the microwave oven project from the introduction section is is available in the examples folder. It is a fully running example. A GUI realized with Tkinker allows you to send events to the state machine and observe the state machine reaction.

Example Code:

''' Command line options: -l PYTHON -p ssc -o oven oven.xml  '''
''' This file is generated from oven.xml - do not edit manually '''
''' Generated on: Tue Jan 01 19:08:22 CET 2019 / version 3.7.3'''
 
 
from oven_base import *
 
class oven(oven_base):
 
	def __init__(self, errorhandler=None):
 
		''' Set state vars to default states '''
		self.stateVar = "Super" # set init state of top state 
		self.stateVarSuper = "Idle" # set init state of Super
 
 
		''' call on entry code of default states '''
		self.radiator.ovenOff();
 
 
 
		self.events = {
			'evPwrLow',
			'evTimeout',
			'evDec',
			'evDoorOpen',
			'evDoorClosed',
			'evPwrHigh',
			'evInc',
			'OVEN_NO_MSG'
		}
 
		self.eventConsumed=False
 
		if errorhandler is None:
			self.ehlr=self.errorhandler
		else:
			self.ehlr=errorhandler
 
 
	def errorhandler(self, msg):
		print ("Error:"  + msg)
 
 
	def processEvent(self,  event):
 
		self.eventConsumed=False
 
 
 
		# action code
		# just a comment
 
 
 
 
		if self.stateVar == "Super":
 
 
 
			if self.stateVarSuper == "Completed":
				if event=="evDoorOpen":
					''' Transition from Completed to Idle'''
					self.eventConsumed=True
 
					''' OnEntry code of state Idle'''
					self.radiator.ovenOff();
 
					''' adjust state variables '''
					self.stateVarSuper = "Idle";
				else:
					pass  #Intentionally left blank
 
 
 
			elif self.stateVarSuper == "Cooking":
				if event=="evDoorOpen":
					''' Transition from Cooking to CookingPause'''
					self.eventConsumed=True
 
					''' Action code for transition '''
					self.radiator.ovenOff();
					self.tm.pause();
 
 
					''' adjust state variables '''
					self.stateVarSuper = "CookingPause";
				elif event=="evTimeout":
					''' Transition from Cooking to Completed'''
					self.eventConsumed=True
 
					''' Action code for transition '''
					self.radiator.ovenOff();
					self.tm.clear();
 
 
					''' adjust state variables '''
					self.stateVarSuper = "Completed";
				else:
					pass  #Intentionally left blank
 
 
 
			elif self.stateVarSuper == "CookingPause":
				if event=="evDoorClosed":
					''' Transition from CookingPause to Cooking'''
					self.eventConsumed=True
 
					''' Action code for transition '''
					self.tm.cont();
 
					''' OnEntry code of state Cooking'''
					self.radiator.ovenOn();
 
					''' adjust state variables '''
					self.stateVarSuper = "Cooking";
				else:
					pass  #Intentionally left blank
 
 
 
			elif self.stateVarSuper == "Idle":
				if event=="evDoorClosed":
					if self.tm.preset()>0:
						''' Transition from Idle to Cooking'''
						self.eventConsumed=True
 
						''' Action code for transition '''
						self.tm.start();
 
						''' OnEntry code of state Cooking'''
						self.radiator.ovenOn();
 
						''' adjust state variables '''
						self.stateVarSuper = "Cooking";
					else:
						pass  #Intentionally left blank
 
				else:
					pass  #Intentionally left blank
 
 
 
 
 
			''' Check if event was already processed '''
			if self.eventConsumed !=True:
 
				if event=="evDec":
					''' Transition from Super to Super'''
					self.eventConsumed=True
 
					''' Action code for transition '''
					self.tm.dec();
 
					self.stateVar = "Super";''' entry chain '''
					if self.stateVarSuper== "Cooking":
						self.radiator.ovenOn();
 
					elif self.stateVarSuper== "Idle":
						self.radiator.ovenOff();
 
					###
 
				elif event=="evInc":
					''' Transition from Super to Super'''
					self.eventConsumed=True
 
					''' Action code for transition '''
					self.tm.inc();
 
					self.stateVar = "Super";''' entry chain '''
					if self.stateVarSuper== "Cooking":
						self.radiator.ovenOn();
 
					elif self.stateVarSuper== "Idle":
						self.radiator.ovenOff();
 
					###
 
				elif event=="evPwrHigh":
					''' Transition from Super to Super'''
					self.eventConsumed=True
 
					''' Action code for transition '''
					self.radiator.ovenSetPwrHigh();
 
					self.stateVar = "Super";''' entry chain '''
					if self.stateVarSuper== "Cooking":
						self.radiator.ovenOn();
 
					elif self.stateVarSuper== "Idle":
						self.radiator.ovenOff();
 
					###
 
				elif event=="evPwrLow":
					''' Transition from Super to Super'''
					self.eventConsumed=True
 
					''' Action code for transition '''
					self.radiator.ovenSetPwrLow();
 
					self.stateVar = "Super";''' entry chain '''
					if self.stateVarSuper== "Cooking":
						self.radiator.ovenOn();
 
					elif self.stateVarSuper== "Idle":
						self.radiator.ovenOff();
 
					###
 
				else:
					pass  #Intentionally left blank
 
 
 
 
 
		return self.eventConsumed
This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
wiki/backends/python_lang.txt · Last modified: 2020/02/15 13:00 by pmueller