Initial published version.

Committer:
CSTritt
Date:
Thu Sep 23 03:03:59 2021 +0000
Revision:
109:744c6cb711bf
Parent:
108:eee3167b25b4
Child:
110:8170476d91a6
Initial version.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
CSTritt 107:61b9c99a4e27 1 /*
CSTritt 109:744c6cb711bf 2 Project: Button Tests
CSTritt 108:eee3167b25b4 3 File: main.cpp
CSTritt 108:eee3167b25b4 4
CSTritt 109:744c6cb711bf 5 This program demonstrates User Button behavior.
CSTritt 108:eee3167b25b4 6
CSTritt 108:eee3167b25b4 7 Written by: Dr. C. S. Tritt
CSTritt 109:744c6cb711bf 8 Created: 9/22/21 (v. 1.0)
CSTritt 107:61b9c99a4e27 9 */
Jonathan Austin 0:2757d7abb7d9 10 #include "mbed.h"
CSTritt 108:eee3167b25b4 11
CSTritt 109:744c6cb711bf 12 DigitalIn uB(USER_BUTTON); // Active low! Normally 1 go to 0 when pressed.
CSTritt 108:eee3167b25b4 13
CSTritt 108:eee3167b25b4 14 int main() {
CSTritt 109:744c6cb711bf 15 // Enter main loop. Sequential calls to the button like this is generally
CSTritt 109:744c6cb711bf 16 // not good practice, but I want to use the DigitalIn object in these
CSTritt 109:744c6cb711bf 17 // contexts to illustrate my points. See while loop below for a better
CSTritt 109:744c6cb711bf 18 // (more common and realistic) approach.
CSTritt 108:eee3167b25b4 19 while(true) {
CSTritt 109:744c6cb711bf 20 // Use the button in some printf calls.
CSTritt 109:744c6cb711bf 21 printf("\nuB (w/o cast) = %d.\n", uB); // Bad! Warning! Wrong!
CSTritt 109:744c6cb711bf 22 printf("uB (with int cast) = %d.\n", static_cast<int>(uB));
CSTritt 109:744c6cb711bf 23 // I found a reference that claimed there is a %b (or B) speicifer for
CSTritt 109:744c6cb711bf 24 // type bool, but it doesn't work in mbed.
CSTritt 109:744c6cb711bf 25 printf("uB (with bool cast) = %d.\n", static_cast<bool>(uB));
CSTritt 109:744c6cb711bf 26 printf("uB.read = %d.\n", uB.read());
CSTritt 109:744c6cb711bf 27
CSTritt 109:744c6cb711bf 28 // Use the button in some if constructs. No need to compare. No need to
CSTritt 109:744c6cb711bf 29 // use .read().
CSTritt 109:744c6cb711bf 30 if (uB)
CSTritt 109:744c6cb711bf 31 printf("In if (uB).\n");
CSTritt 109:744c6cb711bf 32 if (uB.read())
CSTritt 109:744c6cb711bf 33 printf("In if (uB.read()).\n");
CSTritt 109:744c6cb711bf 34 if (!uB)
CSTritt 109:744c6cb711bf 35 printf("In if (!uB).\n");
CSTritt 109:744c6cb711bf 36 if (!uB.read())
CSTritt 109:744c6cb711bf 37 printf("In if (!uB.read()).\n");
CSTritt 109:744c6cb711bf 38 ThisThread::sleep_for(500); // For 0.5 seconds after some output.
CSTritt 109:744c6cb711bf 39
CSTritt 109:744c6cb711bf 40 // Best practice is to get and save the button state and use until you
CSTritt 109:744c6cb711bf 41 // are done with your response to it.
CSTritt 109:744c6cb711bf 42 int count = 5; // Count for loop below.
CSTritt 109:744c6cb711bf 43 bool uBstate = uB; // Get the buttons state. Save it and use it.
CSTritt 109:744c6cb711bf 44 // uB could change states between the if below and the following while.
CSTritt 109:744c6cb711bf 45 // This would result in an extra line of output without the contents of
CSTritt 109:744c6cb711bf 46 // the while being output. That is what the use of uBstate prevents.
CSTritt 109:744c6cb711bf 47 if (!uBstate) printf("\n"); // Print extra line before while output.
CSTritt 109:744c6cb711bf 48 while (!uBstate && count >= 1) {
CSTritt 109:744c6cb711bf 49 // Count down from 5 to 1.
CSTritt 109:744c6cb711bf 50 // Do something here with uBstate false & assuming uB is false.
CSTritt 109:744c6cb711bf 51 // Decrement after use!
CSTritt 109:744c6cb711bf 52 printf("In while loop. Count = % d.\n", count--);
CSTritt 109:744c6cb711bf 53 // Now determine if state has changed.
CSTritt 109:744c6cb711bf 54 if (uB != uBstate) {
CSTritt 109:744c6cb711bf 55 printf("The button changed while in the loop. Exiting.\n");
CSTritt 109:744c6cb711bf 56 // Using a while and setting exit conditions is generally better
CSTritt 109:744c6cb711bf 57 // than using a break. Strive for one way in and one way out in
CSTritt 109:744c6cb711bf 58 // all your code for more reliability and easier maintenance.
CSTritt 109:744c6cb711bf 59 // But there are exceptions to this rule. That is why break (and
CSTritt 109:744c6cb711bf 60 // continue exist.
CSTritt 109:744c6cb711bf 61 uBstate = !uBstate; // Update uBstate to exit loop.
CSTritt 109:744c6cb711bf 62 } //end of if.
CSTritt 109:744c6cb711bf 63 ThisThread::sleep_for(500); // For 0.5 seconds in while.
CSTritt 109:744c6cb711bf 64 } // end of !uBstate while.
CSTritt 109:744c6cb711bf 65 printf("End of main while.\n\n");
CSTritt 109:744c6cb711bf 66 } // end of main while.
CSTritt 109:744c6cb711bf 67 } // end of main function.