250 likes | 463 Views
Event-Driven Programming and State Machines. Here. Traditional Program Structures. Often characterized by: Input from keyboard or data file Begins, performs operations, then ends (linear program) Decisions made don’t change fundamental program structure Output to monitor or data file.
E N D
Often characterized by: Input from keyboard or data file Begins, performs operations, then ends (linear program) Decisions made don’t change fundamental program structure Output to monitor or data file Traditional Program Structure
often asynchronous “simultaneous” inputs & outputs sequences unknowable, re-orderable no “end” or “exit” sensors (switches, light sensors, voltages, etc.) timers user inputs (keypad, push-buttons) update a display move something switch something on or off in general CHANGE SOMETHING PHYSICAL Programming Embedded Systems Characteristics: Inputs: Outputs:
Conceptual framework An excellent method for implementing Event-driven programming & state machines Emphasizes design first Gets you to use structures that: 1) make it clear how to define the low-level functions 2) make debugging code simpler The Events and Services Framework
RULE #1: Recognize that tasks break down ONLY into two fundamental classes: a. Event Detectors b. Services Events and Services Framework
CORROLARY TO RULE #1: Keep Event-Detector and Service routines as short as possible. Must implement “Non-Blocking” routines Events and Services Framework
Complete program structure: Initialize hardware and software Continually test for Events Round-robin scanning “Non-blocking” code Perform Service(s) when Event detected “Non-blocking” code Repeat Events and Services Framework
Description of an abstract Machine At any given time, it can only be in one of a fixed number of discrete states The next state in a progression depends on inputs and the current state Idealized, instantaneous transitions from one state to the next State Machines
Useful tool for describing the behavior of Event-Driven Programming tasks. Allows you to explore the behavior of the program before you implement anything. Naturally fits with the Event-Driven Programming. State Machines
Optional Pumping (Qual) Event1 Action2 Spraying State Diagram Conventions Event1 Action1 (!Qual)
None Right 1 Right Open 2 Right 3 Right Example: Smart Combination Lock Combination = 1-1-8
Example: Smart Combination Lock Combination = 1-1-8
SES - Software Events and Services Initialize SES by calling: SES_Init(); Event-Checking Functions prototyped with the parameter EVENT_PARAMMyEventChecker(EVENT_PARAM) return unsigned char = 0 if event not detectedreturn unsigned char 0 if event detected to pass data from the Event-Checking Function to its Service Function, use SET_SHARED_BYTE_TO(foo); or SET_SHARED_WORD_TO(foo); Data passed between functions must be static Service Function prototyped with the parameter: SERVICE_PARAMMyServiceFunction(SERVICE_PARAM) no return value to read the data passed from the Event-Checking Function, use GET_SHARED_BYTE() if it’s 8-bit data, or GET_SHARED_WORD() if it’s 16-bit data.
SES - Software Events and Services Register each Event Function and Service Functions in pairs: SES_Register(MyEventChecker, MyServiceFunction); Start the process by calling SES_HandleEvents() in an infinite loop. while(1) { SES_HandleEvents(); }
TIMER: 8 timers available to you (0-7) Initialize timer functionality by calling the function: TMR_Init() Initialize a timer by calling the function: TMR_InitTimer(0,TIME_INTERVAL); TIME_INTERVAL = number of timer ticks (1 tick = 4.1ms) Check to see if the timer has expired by calling: TMR_IsTimerExpired(timer number); Clear the timer flags by calling: TMR_ClearTimerExpired(timer number); Timer Library
ROACHLIB: You need to initialize the functions by calling RoachInit(); Functions available for controlling the motors (see documentation for full details): LeftMtrSpeed(x); RightMtrSpeed(x); x is a number from -10 (reverse) to 10 (forward) Functions available for checking the bumpers: uchar ReadBumpers(); Function available for reading the light level: uchar LightLevel(); Roach Library
Pseudo-Code (PDL)PDL = Program Design Language • Pseudo-code is written in ENGLISH • Doesn’t use the syntax of any particular programming language • It is a written, low-level exploration of an implementation of an algorithm. • It can form the first level of comments for your code