CALPOLY MECHATRONICS
Documentation for all Mechatronics Labs
lab3.simonSays Class Reference

A class that plays simon says with the user using an LED and a button. More...

Public Member Functions

def __init__ (self, pwm, startState=0)
 A constructor that initializes all data stored in a simonSays object. More...
 
def runStateMachine (self)
 A function that runs through the state machine logic of the simonSays object once.
 
def generateLevel (self, length, maxDurration)
 A function that creates the pattern for the user to replicate. More...
 
def displayLevel (self, level, tick, tempo=1)
 A function that flashes the LED in a randomly generated pattern. More...
 
def listenForLevel (self, level, tick, tolerance=0.25, tempo=1)
 A function that compares the timing of the user buttom presses to the previously displayed pattern. More...
 
def onButtonChangeFCN (self, IRQ_src)
 A function that sets buttonChange to True when the blue button state changes and negates the current button state. More...
 
def transitionStates (self, trans, st, customMessage="", checkBounce=False)
 A function that preforms all repetative tasks durring a state change. More...
 

Public Attributes

 currentLevel
 
 highScore
 The user's highest score since startup.
 
 currentScore
 The user's current score.
 
 levelNumber
 The current level number the user is on.
 
 tempo
 The speed of the pattern, in seconds/beat.
 
 levelLength
 The length of the levels, in number of flashes.
 
 maxFlashLength
 The max durration, in beats, of a flash or rest.
 
 buttonState
 The current state of the button (True = pressed)
 
 state
 The current state of the finite state machine.
 
 lastTransition
 
 buttonPress
 Wether or not the button state has changed (True = pressed or released).
 
 buttonRelease
 
 pinC13
 
 ButtonChangeInt
 
 pwm
 

Static Public Attributes

dictionary stateMessages
 dictionary of messages to display at each state change More...
 
list incLengthAt = [4, 6, 10, 15, 20]
 List of level numbers to increase the level length at.
 
list incTempoAt = [2, 3, 5, 9, 11, 13, 14, 15, 16, 17, 18, 19, 20]
 List of level numbers to increase the speed (tempo) at.
 
list incmaxFlashLengthAt = [2, 5, 12, 20]
 List of level numbers to increase the max flash durration at.
 

Detailed Description

A class that plays simon says with the user using an LED and a button.

The simonSays class randomly generates a sting of LED flashes of varying length. Then, it compares the durration of button presses by the user to the pattern, and if they are sufficiently close, congratulates the user and generates another random sequence for them to replicate.

Constructor & Destructor Documentation

◆ __init__()

def lab3.simonSays.__init__ (   self,
  pwm,
  startState = 0 
)

A constructor that initializes all data stored in a simonSays object.

Parameters
pwmThe pwm object for the simonSays object to output to, usually used to control an LED brightness.
startStatethe startup state for the simonSays object to initialize in.

Member Function Documentation

◆ displayLevel()

def lab3.simonSays.displayLevel (   self,
  level,
  tick,
  tempo = 1 
)

A function that flashes the LED in a randomly generated pattern.

Parameters
levelA 1D list of durrations to turn the LED on and off in a pattern
tickThe current system time in ms
tempothe speed at which to display the pattern (1 = 1 time unit is 1 sec, 2 = 1 time unit is 2 sec etc.)
Returns
The duty cycle of the LED to turn it on or off based on the generated pattern of flashed

◆ generateLevel()

def lab3.simonSays.generateLevel (   self,
  length,
  maxDurration 
)

A function that creates the pattern for the user to replicate.

Parameters

◆ listenForLevel()

def lab3.simonSays.listenForLevel (   self,
  level,
  tick,
  tolerance = 0.25,
  tempo = 1 
)

A function that compares the timing of the user buttom presses to the previously displayed pattern.

Parameters
levelA 1D list of durrations to compare to the user button presses.
tickThe current system time in ms.
toleranceThe maximum allowable difference between the time the user pressed the button and the desired time.
tempoThe time durration of one beat.

◆ onButtonChangeFCN()

def lab3.simonSays.onButtonChangeFCN (   self,
  IRQ_src 
)

A function that sets buttonChange to True when the blue button state changes and negates the current button state.

Parameters
IRQ_srcThe pin number that triggered the inturrupt.

◆ transitionStates()

def lab3.simonSays.transitionStates (   self,
  trans,
  st,
  customMessage = "",
  checkBounce = False 
)

A function that preforms all repetative tasks durring a state change.

Parameters
transA boolean representing wether to transition or not (True = transition now).
stThe state to transition to.
customMessageA string to print in addition to the standard state message.
checkBounceA boolean to check if the button pressed time was unreasonably short to check elminate button bounce. This is used to prevent the state from changing until the button is both pressed and released.

Member Data Documentation

◆ stateMessages

dictionary lab3.simonSays.stateMessages
static
Initial value:
= {
0 : "Welcome to Simon Says! The Nucleo will flash a pattern on using the LED."
+ "Then, repeate the pattern by pressing the blue button. If you get it"
+ "right you'll move to the next level, if you get it wrong you loose!",
1 : "Press the blue button to play!",
2 : "Playing Sequence. Pay close attention!",
3 : "Repeat the pattern by pressing the blue button",
4 : "Continue Pattern",
5 : "Press the blue button to proceed to the next level."
}

dictionary of messages to display at each state change


The documentation for this class was generated from the following file: