Ryan McLaughlin | Mechatronics Portfolio
game.game Class Reference

Public Member Functions

def __init__ (self, unit, tol, setLength=5, maxTime=7000000, debugFlag=False)
 Constructor for class game.py More...
 
def run (self)
 Finite state machine for the Nucleo Says game. More...
 
def transitionTo (self, newState)
 Method for transitioning states. More...
 
def scoringUpdate (self, newState)
 Method for scorekeeping during gameplay. More...
 
def onButtonPressFCN (self, IRQ_src)
 Callback function for the button press. More...
 
def ledBlink (self, currentMorse, ledElapsedTime, blinkIndex)
 Method to blink the LED according to the morse pattern. More...
 
def countdownDisplay (self, countdownElapsedTime)
 Method to display a countdown. More...
 
def morseGenerator (self, currentSet, newRound, setLength, debugFlag)
 Method for generating the morse code pattern for the round. More...
 
def reset (self, stateReset=False, setReset=False, roundReset=False, blinkReset=False, elementReset=False, ledReset=False, countdownReset=False, haveReleasedReset=False)
 Method to reset common attributes. More...
 

Public Attributes

 debugFlag
 Debugging flag. More...
 
 unit
 Timewise defintion of one unit. More...
 
 tol
 Tolerance on user input. More...
 
 setLength
 Number of sets per round.
 
 pinC13
 Pin attribute for blue user button. More...
 
 pinA5
 Pin attribute for LED. More...
 
 tim2
 Pin attribute for pyb timer. More...
 
 t2ch1
 Channel attribute for pyb timer.
 
 ButtonInt
 Callback interrupt attribute. More...
 
 blinkIndex
 Counter variable for what LED should be doing. More...
 
 state
 Finite state machine needs a state variable to regularly update as state transitions occur.
 
 round
 Round counter attribute. More...
 
 currentSet
 Keeps track of the current set of the game. More...
 
 elementNum
 Morse code element counter. More...
 
 buttonPress
 Define the button as False (released)
 
 haveReleased
 Attribute to see if button has been released. More...
 
 newRound
 Attribute to see if new round should start. More...
 
 currentTime
 Current time through the entire program.
 
 buttonTime
 Time in microseconds for when the button has been triggered.
 
 countdown
 Defines the length of the countdown. More...
 
 countdownElapsedTime
 Timer counter for the countdown. More...
 
 maxTime
 Define the maximum time before program times out. More...
 
 nucleoScore
 The nucleo's combined score.
 
 userScore
 The user's combined score.
 
 zeroTime
 Refrence point for LED blinking.
 
 currentMorse
 Morse code sequence for current set.
 
 ledElapsedTime
 Time since LED pattern started.
 
 elapsedDownTime
 Time that the button has been pressed for.
 
 restartTime
 Timestamp for user incorrect input.
 
 value
 Morse code element equivalent translated from user input.
 
 winTime
 Timestamp for user winning a round.
 
 elapsedUpTime
 Time that the button has been released for.
 
 timeSinceRestart
 Elapsed time since incorrect input detected.
 
 timeSinceWon
 Elapsed time since user won a round.
 
 pattern
 Full morse code sequence generated at the beginning of a round.
 
 thisPattern
 Current morse code sequence, pre-proccessing.
 

Static Public Attributes

dictionary CODE
 Morse code dictionary. More...
 
int S0_INIT = 0
 basic init state
 
int S1_COUNTDOWN = 1
 Countdown state before blinking LED.
 
int S2_BLINK_LED = 2
 Blink n = 1 to n = i letter.
 
int S3_READY_FOR_INPUT = 3
 Waiting state after pattern is blinked.
 
int S4_BUTTON_PRESSED = 4
 Counting time that button has been pressed.
 
int S5_PROC_BUTTON_PRESSED = 5
 Post-processing state for button press.
 
int S6_BUTTON_RELEASED = 6
 Transition state between button presses.
 
int S7_PROC_BUTTON_RELEASED = 7
 Post process button released time.
 
int S8_INCORRECT_INPUT = 8
 User inputted incorrectly, show score/ask to play again.
 
int S9_END_GAME = 9
 End game if user doesn't want to play.
 
int S10_USER_WON = 10
 User has won the set, display game information.
 

Constructor & Destructor Documentation

◆ __init__()

def game.game.__init__ (   self,
  unit,
  tol,
  setLength = 5,
  maxTime = 7000000,
  debugFlag = False 
)

Constructor for class game.py

Class constructor definition allows us to define an instance of the class from within the main script, and allows for easy adjustment of certain game-defining parameters, such as the number of sets per round, the max time before the game times out, and others outlined below in variable documentation.

Member Function Documentation

◆ countdownDisplay()

def game.game.countdownDisplay (   self,
  countdownElapsedTime 
)

Method to display a countdown.

Before blinking the new LED pattern, a countdown will be displayed to prep the user for the incoming signal. This method will display a number (between 3 and 1) until one second has elapsed, in which it will increment to the next lower number until it gets to 1, where it will transition to blinking the LED state. This promotes the use of non-blocking code, while having a similar effect on gameplay as a time.sleep() variation.

◆ ledBlink()

def game.game.ledBlink (   self,
  currentMorse,
  ledElapsedTime,
  blinkIndex 
)

Method to blink the LED according to the morse pattern.

The LED will blink based on time without using blocking code. That is, it will be turned on while under a threshold of either a dot or dash amount of time. When finished displaying either a dot or a dash, it will find the next element of the current morse code pattern and display that, incrementing until the pattern has been fully displayed

◆ morseGenerator()

def game.game.morseGenerator (   self,
  currentSet,
  newRound,
  setLength,
  debugFlag 
)

Method for generating the morse code pattern for the round.

If starting a new round, an entirely new pattern will be randomly selected from a library of letters and corresponding morse code patterns. As a round progresses, morseGenerator will be called again, each time refrencing a larger portion of the pre-defined sequence. Only at the start of a new round will the sequence be refreshed, promoting more efficient code.

◆ onButtonPressFCN()

def game.game.onButtonPressFCN (   self,
  IRQ_src 
)

Callback function for the button press.

When the button is pressed, the onButtonPressFCN function will be called. In the constructor, buttonPress is set to False, such that when it is pressed, it switches to True. Likewise, since the button is set to detect both rising and falling edges, the button returns to False when released. Based on use of the not operator.

◆ reset()

def game.game.reset (   self,
  stateReset = False,
  setReset = False,
  roundReset = False,
  blinkReset = False,
  elementReset = False,
  ledReset = False,
  countdownReset = False,
  haveReleasedReset = False 
)

Method to reset common attributes.

Since certain attributes are commonly reset (such as counters), it was more efficient to write a method to reset them more efficiently. Depending on which attributes are named as inputs to the method, those specific ones will be reset to whatever value is intended. This way, we do not need to keep track of which variables reset to 0, 1, etc.

◆ run()

def game.game.run (   self)

Finite state machine for the Nucleo Says game.

following the flow of the state transition diagram, this method controls the finite state machine for the Nucleo Says game. For full documentation on the states and their corresponding transition conditions, please refer to the state transition diagram provided in the project description.

◆ scoringUpdate()

def game.game.scoringUpdate (   self,
  newState 
)

Method for scorekeeping during gameplay.

When transitioning between certain states, the Nucleo or user score will be incremented to keep tally of who is winning. This method takes in the state being transitioned to, and interprets what the outcome of the round accordingly was.

◆ transitionTo()

def game.game.transitionTo (   self,
  newState 
)

Method for transitioning states.

The method will reassign the self.state variable when directed to transition.

Member Data Documentation

◆ blinkIndex

game.game.blinkIndex

Counter variable for what LED should be doing.

blinkIndex is used by the ledBlink method to determine how much of the morse code sequence has already been ouputted through the LED

◆ ButtonInt

game.game.ButtonInt

Callback interrupt attribute.

When a rising or falling edge is detected by the button, the interupt will be triggered.

◆ CODE

dictionary game.game.CODE
static
Initial value:
= {'A': '._-', 'B': '-_._._.', 'C': '-_._-_.',
'D': '-_._.', 'E': '.', 'F': '._._-_.',
'G': '-_-_.', 'H': '._._._.', 'I': '._.',
'J': '._-_-_-', 'K': '-_._-', 'L': '._-_._.',
'M': '-_-', 'N': '-_.', 'O': '-_-_-',
'P': '._-_-_.', 'Q': '-_-_._-', 'R': '._-_.',
'S': '._._.', 'T': '-', 'U': '._._-',
'V': '._._._-', 'W': '._-_-', 'X': '-_._._-',
'Y': '-_._-_-', 'Z': '-_-_._.'
}

Morse code dictionary.

◆ countdown

game.game.countdown

Defines the length of the countdown.

The countdown will be defined as counting from 3 to 0

◆ countdownElapsedTime

game.game.countdownElapsedTime

Timer counter for the countdown.

This counter will keep track of the elapsed time in order determine how long to display a specific values (between 3 and 0)

◆ currentSet

game.game.currentSet

Keeps track of the current set of the game.

Set counter attribute.

Similar to the round counter, there are a fixed number of sets to cycle through in each round.

◆ debugFlag

game.game.debugFlag

Debugging flag.

When code is in the design and testing phase, we want a simple way of showing more print statements in the terminal for debugging

◆ elementNum

game.game.elementNum

Morse code element counter.

To compare user input with LED expected sequence, elementNum defines which element within the morse code sequence is currently being inputted.

◆ haveReleased

game.game.haveReleased

Attribute to see if button has been released.

Will be utilized to see if the button has been released before being pressed again

◆ maxTime

game.game.maxTime

Define the maximum time before program times out.

The program will time out if the user doesn't respond within the maxTime attribute

◆ newRound

game.game.newRound

Attribute to see if new round should start.

Will be set to True when user has beat Nucleo for an entire round

◆ pinA5

game.game.pinA5

Pin attribute for LED.

Similar to pinC13, but for the output LED.

◆ pinC13

game.game.pinC13

Pin attribute for blue user button.

The blue button on the board serves as the main user input interface, and therefore needs a pin assignment.

◆ round

game.game.round

Round counter attribute.

Counter variable for the current element.

Game begins on the first round every time

Increments through the number of elements within the pattern, which would include dots, dashes, and spaces

Game begins on round 1, and every time a win or loss occurs a round has been completed.

◆ tim2

game.game.tim2

Pin attribute for pyb timer.

To use pulse width modulation of the LED, we need a timer.

◆ tol

game.game.tol

Tolerance on user input.

Since this game is being played by human users, it is expected that some error is inherent in replicating the LED pattern, therefore a bi-lateral tolerance is defined for user inputs.

◆ unit

game.game.unit

Timewise defintion of one unit.

In using morse code standards for LED pattern creation, letters, spaces, and other elements are all based off the definition of one unit.


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