Explores the behavior of wait and ticker functions.

Dependencies:   mbed

Committer:
CSTritt
Date:
Fri Oct 27 13:58:40 2017 +0000
Revision:
1:7ae3a9b9b2b0
Parent:
0:1917e5873a6e
Modified comments to indicate improper printf use.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
CSTritt 0:1917e5873a6e 1 /*
CSTritt 0:1917e5873a6e 2 Project: WaitTest
CSTritt 0:1917e5873a6e 3 File: main.cpp
CSTritt 0:1917e5873a6e 4 Modified by by: Dr. C. S. Tritt
CSTritt 0:1917e5873a6e 5 Last revision on: 9/27/17 (v. 1.0)
CSTritt 0:1917e5873a6e 6
CSTritt 0:1917e5873a6e 7 Demonstrate/confirm wait function behaivor
CSTritt 0:1917e5873a6e 8 */
CSTritt 0:1917e5873a6e 9 #include "mbed.h"
CSTritt 0:1917e5873a6e 10
CSTritt 0:1917e5873a6e 11 Ticker myTicker; // My ticker object. Used to generate output.
CSTritt 0:1917e5873a6e 12
CSTritt 0:1917e5873a6e 13 // Declare my ticker callback function. Sends text to serial.
CSTritt 0:1917e5873a6e 14 void tick_Callback(void);
CSTritt 0:1917e5873a6e 15
CSTritt 0:1917e5873a6e 16 int main() {
CSTritt 0:1917e5873a6e 17 // Setup to call to_Callback once a second.
CSTritt 0:1917e5873a6e 18 myTicker.attach(&tick_Callback, 1.0);
CSTritt 0:1917e5873a6e 19 wait(0.5); // Offset main loop calls by half a second.
CSTritt 0:1917e5873a6e 20 int mainCount = 0; // Main loop counter.
CSTritt 0:1917e5873a6e 21 while (true) {
CSTritt 0:1917e5873a6e 22 // Tested a wait of 0. It was not taken as infinity.
CSTritt 0:1917e5873a6e 23 wait(1.0); // Cycle this loop once a second.
CSTritt 0:1917e5873a6e 24 mainCount++; // Increment mainCount.
CSTritt 0:1917e5873a6e 25 if (mainCount == 11) mainCount = 1; // Reset every 10 times.
CSTritt 0:1917e5873a6e 26 printf("In main loop. Count = %d.\n", mainCount);
CSTritt 0:1917e5873a6e 27 }
CSTritt 0:1917e5873a6e 28 }
CSTritt 0:1917e5873a6e 29
CSTritt 0:1917e5873a6e 30 // Define my ticker callback function. Sends text to serial.
CSTritt 0:1917e5873a6e 31 void tick_Callback(void) {
CSTritt 0:1917e5873a6e 32 static int cbCount = 0; // Initialized on load. Remembered between calls.
CSTritt 0:1917e5873a6e 33 cbCount++; // Increment call back counter.
CSTritt 0:1917e5873a6e 34 if (cbCount == 11) cbCount = 1; // Reset every 10 times.
CSTritt 1:7ae3a9b9b2b0 35 // Note: printf should not be used in interrupt style callbacks!
CSTritt 0:1917e5873a6e 36 printf("In ticker callback loop. Count = %d.\n", cbCount);
CSTritt 0:1917e5873a6e 37 }