05.11.2014 Views

AN: Capstone Dive Computer Example - Quantum Leaps

AN: Capstone Dive Computer Example - Quantum Leaps

AN: Capstone Dive Computer Example - Quantum Leaps

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Application Note:<br />

<strong>Capstone</strong> <strong>Dive</strong> <strong>Computer</strong> <strong>Example</strong><br />

www.state-machine.com<br />

(1) The <strong>Capstone</strong> active object starts in the “surfaced” state.<br />

(2) The Alarm active object starts in the “silent” state.<br />

(3) The system clock tick ISR posts the HEARTBEAT event to the <strong>Capstone</strong> state machine every ½<br />

second.<br />

(4) Upon receiving the HEARTBEAT event, <strong>Capstone</strong> triggers ADC conversion to find out the ascent<br />

rate.<br />

(5) When the ADC finishes the conversion, the ADC ISR posts the ASCENT_RATE_ADC(raw) event<br />

to <strong>Capstone</strong>. The event parameter raw contains the raw, 10-bit ADC conversion value.<br />

(6) The system clock tick ISR also performs debouncing of the buttons B1 and B2 (see Figure 3).<br />

This ISR publishes event B1_DOWN when button B1 is depressed and B2_DOWN when button<br />

B2 is depressed.<br />

(7) Similarly, the system clock publishes event B1_UP when button B1 is released and B2_UP when<br />

button B2 is released.<br />

(8) <strong>Capstone</strong> ignores positive ascent rates in the “surfaced” state. However, a negative ascent rate<br />

(diving) triggers the transition to the “diving” state.<br />

(9) In the “diving” state <strong>Capstone</strong> tests for alarm conditions. If any of the alarm condition arises,<br />

the <strong>Capstone</strong> active object posts an ALARM_REQUEST(alarm_type) event to the Alarm state<br />

machine. The event parameter alarm_type conatins the enumerated type of the alarm.<br />

(10) The ALARM_REQUEST(alarm_type) event triggers transition to “playing” in the Alarm active<br />

object.<br />

(11) The Alarm active object changes the pitch of the speaker based on the TIMEOUT event produced<br />

by the system clock tick ISR.<br />

(12) When the <strong>Capstone</strong> active object determines that the alarm should be silenced, it sends the<br />

ALARM_SILENCE(alarm_type) to the Alarm active object. It is up to the Alarm active object to<br />

handle the priorities of the requested and silence alarms, as the alarms can overlap.<br />

2.3 Step 3: Signals, Events, and Active Objects<br />

Sequence diagrams, like Figure 4, help you discover events exchanged among active objects. The<br />

choice of signals and event parameters is perhaps the most important design decision in any<br />

event-driven system. The events affect the other main application components: events and state<br />

machines of the active objects.<br />

In QP, signals are typically enumerated constants and events with parameters are structures derived<br />

from the QEvent base structure. Listing 1 shows signals and events used in the <strong>Capstone</strong> application.<br />

The sample code for the non-preemptive kernel is located in the Solutions\<strong>Dive</strong><strong>Computer</strong><br />

directory.<br />

NOTE: This section describes the platform-independent code of the application. This code is actually<br />

identical in both versions.<br />

#ifndef capstone_h<br />

#define capstone_h<br />

(1) enum <strong>Capstone</strong>Signals {<br />

Copyright © <strong>Quantum</strong> <strong>Leaps</strong>, LLC. All Rights Reserved.<br />

9 of 29

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!