41
CMPE-013/L: “C” Programmin riel Hugh Elkaim – Spring 2013 CMPE-013/L Software Engineering Gabriel Hugh Elkaim Spring 2013

CMPE-013/L Software Engineering

  • Upload
    renata

  • View
    68

  • Download
    0

Embed Size (px)

DESCRIPTION

CMPE-013/L Software Engineering. Gabriel Hugh Elkaim Spring 2013. Modular C Code. The three uses of static. Encapsulation (1.2). Encapsulation (2.2). What goes into a header file?. What shouldn’t be in a header file?. Where do you #include the header file?. Programming Style Issues. - PowerPoint PPT Presentation

Citation preview

Page 1: CMPE-013/L Software Engineering

CMPE-013/L: “C” ProgrammingGabriel Hugh Elkaim – Spring 2013

CMPE-013/L

Software Engineering

Gabriel Hugh ElkaimSpring 2013

Page 2: CMPE-013/L Software Engineering

CMPE-013/L: “C” ProgrammingGabriel Hugh Elkaim – Spring 2013

Modular C Code

Page 3: CMPE-013/L Software Engineering

CMPE-013/L: “C” ProgrammingGabriel Hugh Elkaim – Spring 2013

The three uses of static

Page 4: CMPE-013/L Software Engineering

CMPE-013/L: “C” ProgrammingGabriel Hugh Elkaim – Spring 2013

FunctionA() FunctionB()

int foo char bar

FunctionC()

Encapsulation (1.2)

Page 5: CMPE-013/L Software Engineering

CMPE-013/L: “C” ProgrammingGabriel Hugh Elkaim – Spring 2013

FunctionA() FunctionB()

int foo char bar

FunctionC()

Encapsulation (2.2)

Page 6: CMPE-013/L Software Engineering

CMPE-013/L: “C” ProgrammingGabriel Hugh Elkaim – Spring 2013

What goes into a header file?

Page 7: CMPE-013/L Software Engineering

CMPE-013/L: “C” ProgrammingGabriel Hugh Elkaim – Spring 2013

What shouldn’t be in a header file?

Page 8: CMPE-013/L Software Engineering

CMPE-013/L: “C” ProgrammingGabriel Hugh Elkaim – Spring 2013

Where do you #include the header file?

Page 9: CMPE-013/L Software Engineering

CMPE-013/L: “C” ProgrammingGabriel Hugh Elkaim – Spring 2013

Programming Style IssuesLayout within a module

One "True" Indentation Style

Use of White Space

Use of Comments Naming Conventions

Module Level Comment#includes

library /module headersPublic header for this module

Private Constant DefinitionsPrivate Macro DefinitonsPrivate Type DefinitionsPrivate VariablesPrivate Function PrototypesCode

while (1) { MSES_HandleEvents(); }

void putKey(SERVICE_PARAM){ putchar( GET_SHARED_BYTE() ); putchar('.');}

if (NewTime() && ((GetTime() % 1050) == 0)) return 1; else return 0;

Programming Style Issues

Page 10: CMPE-013/L Software Engineering

CMPE-013/L: “C” ProgrammingGabriel Hugh Elkaim – Spring 2013

Module Design by Interface Specification

• View– The module provides Services to the rest of the

code• Design Activities

– Specify the services• Describe Functionality

– Name the Services– Design the implementation

Page 11: CMPE-013/L Software Engineering

CMPE-013/L: “C” ProgrammingGabriel Hugh Elkaim – Spring 2013

Application Programming Interfaces (API) for the ME218c Master modules

Module: Communications to UI on PC

To avoid hanging up the master during the transmission or reception of messages, this module should implement buffered, interrupt driven transmit and receive. The communications routines for this module will need to be interrupt driven because the UI may send its message at any time.

Char InitializeUICommunications(void);Do whatever hardware and software initialization necessary to prepare for communications with the UI on SC1.

Void TellUINewUserReady(void);Should send the message to the UI that a new iButton has been inserted and read.

Unsigned char IsNameReady(void);Should check to see if a new name is ready from the UI. Return TRUE if a new name is ready, FALSE otherwise.

Unsigned char GetNewName( unsigned char NameSpace[])Should copy the name gotten from the UI into the array NameSpace. The copy operation should copy no more than 16 characters, including the terminating NULL. Should return TRUE if there was a new name ready, FALSE otherwise.

A Real Example

Page 12: CMPE-013/L Software Engineering

CMPE-013/L: “C” ProgrammingGabriel Hugh Elkaim – Spring 2013

Design the interfaces to modules

• Design interface for:– Driving the platform– Gathering Sensor data

• Produce– Public Interface specification– What are the details that are being hidden?

Page 13: CMPE-013/L Software Engineering

CMPE-013/L: “C” ProgrammingGabriel Hugh Elkaim – Spring 2013

Hierarchical State Machines

• Harel Statecharts

Page 14: CMPE-013/L Software Engineering

CMPE-013/L: “C” ProgrammingGabriel Hugh Elkaim – Spring 2013

Beaco n

A

10'Tape45 d e g .

Page 15: CMPE-013/L Software Engineering

CMPE-013/L: “C” ProgrammingGabriel Hugh Elkaim – Spring 2013

F ind in gTape II

F ind ingTape I

TrackingTape

S t o p

E n t e r

F o u n d T

F o u n d T a p e

F o u n d T a p e

A Possible Top-Level State Diagram

Page 16: CMPE-013/L Software Engineering

CMPE-013/L: “C” ProgrammingGabriel Hugh Elkaim – Spring 2013

Work out State Diagrams to Implement Finding Tape I

Page 17: CMPE-013/L Software Engineering

CMPE-013/L: “C” ProgrammingGabriel Hugh Elkaim – Spring 2013

Acquiring

Drivingto

TapeStop

Enter

Beacon Acquired

Hit Tape

Finding Tape

Finding Tape

Page 18: CMPE-013/L Software Engineering

CMPE-013/L: “C” ProgrammingGabriel Hugh Elkaim – Spring 2013

TurningCW

TurningCCW

Acquired

EntryLeft Sensor Peaks

Set Slow Speed,Reverse

L R

Acquiring

Stop Turning, Post Beacon Aquired

Acquiring

Page 19: CMPE-013/L Software Engineering

CMPE-013/L: “C” ProgrammingGabriel Hugh Elkaim – Spring 2013

Implementing Hierarchical State Machines

• What do you need?

Page 20: CMPE-013/L Software Engineering

CMPE-013/L: “C” ProgrammingGabriel Hugh Elkaim – Spring 2013

If current state is state oneExecute During function for state oneIf an event is active

If event is event oneExecute action function for state one :

event oneDecide what the next state will be

EndifIf event is event two

Execute action function for state one : event two

Decide what the next state will beEndif

Repeat the block above as required for each of the possible events affecting this state.

If next state is different from current stateExecute exit function for state oneExecute entry function for new stateModify state variable to reflect the new

stateEndif

Endif Return from state machine function

State Machine Function Template

Page 21: CMPE-013/L Software Engineering

CMPE-013/L: “C” ProgrammingGabriel Hugh Elkaim – Spring 2013

/**************************************************************************** Module d:\me218b\Lectures\Lecture 29\SMTemplate.c

Description This is a template file for implementing state machines.

Notes

History When Who What/Why -------------- --- -------- 02/18/99 10:19 jec built template from MasterMachine.c 02/14/99 10:34 jec Began Coding****************************************************************************//*----------------------------- Include Files -----------------------------*//* include header files for this state machine as well as any machines at the next lower level in the hierarchy that are sub-machines to this machine*/

/*----------------------------- Module Defines ----------------------------*/// define constants for the states and event for this machine

/*---------------------------- Module Functions ---------------------------*//* prototypes for private functions for this machine, things like entry & exit functions. *//*---------------------------- Module Variables ---------------------------*/// everybody needs a state variable, you may need others as wellstatic unsigned char CurrentState;

Page 22: CMPE-013/L Software Engineering

CMPE-013/L: “C” ProgrammingGabriel Hugh Elkaim – Spring 2013

void RunStateMachine(unsigned char CurrentEvent ){ unsigned char NextState = CurrentState;

switch ( CurrentState ) { case STATE_ONE : // If current state is state one DuringStateOne(); //Execute During function for state one if ( CurrentEvent != NO_EVENT ) //If an event is active { switch ( CurrentEvent) { case EVENT_ONE : //If event is event one // Execute action function for state one : event one NextState = STATE_TWO;//Decide what the next state will be break; } // If next state is different from current state if ( NextState != CurrentState) { // Execute exit function for current state // Execute entry function for new state CurrentState = NextState; //Modify state variable } } return;

Page 23: CMPE-013/L Software Engineering

CMPE-013/L: “C” ProgrammingGabriel Hugh Elkaim – Spring 2013

/**************************************************************************** Function StartStateMachine

Parameters None

Returns None

Description Does any required initialization for this state machine Notes

Author J. Edward Carryer, 2/18/99, 10:38AM****************************************************************************/void StartStateMachine ( void ){ CurrentState = ENTRY_STATE;// call the (optional) entry function for the ENTRY_STATE// any other initialization necessary to re-start the state machine}

Page 24: CMPE-013/L Software Engineering

CMPE-013/L: “C” ProgrammingGabriel Hugh Elkaim – Spring 2013

Questions?

Page 25: CMPE-013/L Software Engineering

CMPE-013/L: “C” ProgrammingGabriel Hugh Elkaim – Spring 2013

Page 26: CMPE-013/L Software Engineering

CMPE-013/L: “C” ProgrammingGabriel Hugh Elkaim – Spring 2013

Page 27: CMPE-013/L Software Engineering

CMPE-013/L: “C” ProgrammingGabriel Hugh Elkaim – Spring 2013

Page 28: CMPE-013/L Software Engineering

CMPE-013/L: “C” ProgrammingGabriel Hugh Elkaim – Spring 2013

Page 29: CMPE-013/L Software Engineering

CMPE-013/L: “C” ProgrammingGabriel Hugh Elkaim – Spring 2013

Page 30: CMPE-013/L Software Engineering

CMPE-013/L: “C” ProgrammingGabriel Hugh Elkaim – Spring 2013

Page 31: CMPE-013/L Software Engineering

CMPE-013/L: “C” ProgrammingGabriel Hugh Elkaim – Spring 2013

Page 32: CMPE-013/L Software Engineering

CMPE-013/L: “C” ProgrammingGabriel Hugh Elkaim – Spring 2013

Page 33: CMPE-013/L Software Engineering

CMPE-013/L: “C” ProgrammingGabriel Hugh Elkaim – Spring 2013

Page 34: CMPE-013/L Software Engineering

CMPE-013/L: “C” ProgrammingGabriel Hugh Elkaim – Spring 2013

Page 35: CMPE-013/L Software Engineering

CMPE-013/L: “C” ProgrammingGabriel Hugh Elkaim – Spring 2013

Page 36: CMPE-013/L Software Engineering

CMPE-013/L: “C” ProgrammingGabriel Hugh Elkaim – Spring 2013

Page 37: CMPE-013/L Software Engineering

CMPE-013/L: “C” ProgrammingGabriel Hugh Elkaim – Spring 2013

Page 38: CMPE-013/L Software Engineering

CMPE-013/L: “C” ProgrammingGabriel Hugh Elkaim – Spring 2013

Page 39: CMPE-013/L Software Engineering

CMPE-013/L: “C” ProgrammingGabriel Hugh Elkaim – Spring 2013

Page 40: CMPE-013/L Software Engineering

CMPE-013/L: “C” ProgrammingGabriel Hugh Elkaim – Spring 2013

Page 41: CMPE-013/L Software Engineering

CMPE-013/L: “C” ProgrammingGabriel Hugh Elkaim – Spring 2013