Keisha Brathwaite
/
KBrat-SSD541-HW-9_2
KBrat-SSD541-HW-9.2
Fork of kl46z_stop_watch_v2 by
main.cpp@1:0d13b6d7907f, 2016-10-12 (annotated)
- Committer:
- scohennm
- Date:
- Wed Oct 12 15:55:44 2016 +0000
- Revision:
- 1:0d13b6d7907f
- Parent:
- 0:04499bc54bee
- Child:
- 2:1d77439751bf
Stop watch to demo mod buttons and timer class.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
scohennm | 0:04499bc54bee | 1 | #include "mbed.h" |
scohennm | 1:0d13b6d7907f | 2 | #include <math.h> |
scohennm | 0:04499bc54bee | 3 | #include "SLCD.h" |
scohennm | 1:0d13b6d7907f | 4 | |
scohennm | 1:0d13b6d7907f | 5 | #define LEDON false |
scohennm | 1:0d13b6d7907f | 6 | #define LEDOFF true |
scohennm | 1:0d13b6d7907f | 7 | #define NUMBUTS 2 |
scohennm | 1:0d13b6d7907f | 8 | #define LBUT PTC12 // port addresses for buttons |
scohennm | 1:0d13b6d7907f | 9 | #define RBUT PTC3 |
scohennm | 1:0d13b6d7907f | 10 | #define STOPPEDSTATE 0 |
scohennm | 1:0d13b6d7907f | 11 | #define TIMINGSTATE 1 |
scohennm | 1:0d13b6d7907f | 12 | #define RESETTINGSTATE 0 |
scohennm | 1:0d13b6d7907f | 13 | #define PRINTDELTA 0.01 |
scohennm | 0:04499bc54bee | 14 | #define LCDCHARLEN 10 |
scohennm | 1:0d13b6d7907f | 15 | #define BUTTONTIME 0.2 |
scohennm | 1:0d13b6d7907f | 16 | #define FULLMINUTE 60 //seconds |
scohennm | 1:0d13b6d7907f | 17 | #define PROGNAME "kl46z_stop_watch_v1\n\r" |
scohennm | 1:0d13b6d7907f | 18 | #define LCDTITLE "STPW" |
scohennm | 1:0d13b6d7907f | 19 | #define TITLEWAIT 2.0 |
scohennm | 1:0d13b6d7907f | 20 | |
scohennm | 1:0d13b6d7907f | 21 | //#define PRINTDBUG // uncomment if printing to serial port desired |
scohennm | 0:04499bc54bee | 22 | |
scohennm | 0:04499bc54bee | 23 | SLCD slcd; //define LCD display |
scohennm | 0:04499bc54bee | 24 | Serial pc(USBTX, USBRX); |
scohennm | 0:04499bc54bee | 25 | |
scohennm | 1:0d13b6d7907f | 26 | Timer ButtonTimer; // for reading button states |
scohennm | 1:0d13b6d7907f | 27 | DigitalIn buttons[NUMBUTS] = {RBUT, LBUT}; |
scohennm | 1:0d13b6d7907f | 28 | int displayState = STOPPEDSTATE; |
scohennm | 1:0d13b6d7907f | 29 | |
scohennm | 1:0d13b6d7907f | 30 | |
scohennm | 1:0d13b6d7907f | 31 | |
scohennm | 1:0d13b6d7907f | 32 | void initialize_global_vars(){ |
scohennm | 1:0d13b6d7907f | 33 | pc.printf(PROGNAME); |
scohennm | 1:0d13b6d7907f | 34 | // set up DAQ timer |
scohennm | 1:0d13b6d7907f | 35 | // set up DAQ timers |
scohennm | 1:0d13b6d7907f | 36 | ButtonTimer.start(); |
scohennm | 1:0d13b6d7907f | 37 | ButtonTimer.reset(); |
scohennm | 1:0d13b6d7907f | 38 | } |
scohennm | 0:04499bc54bee | 39 | |
scohennm | 0:04499bc54bee | 40 | void LCDMess(char *lMess){ |
scohennm | 0:04499bc54bee | 41 | slcd.Home(); |
scohennm | 0:04499bc54bee | 42 | slcd.clear(); |
scohennm | 0:04499bc54bee | 43 | slcd.printf(lMess); |
scohennm | 0:04499bc54bee | 44 | } |
scohennm | 1:0d13b6d7907f | 45 | void showTitle(){ |
scohennm | 1:0d13b6d7907f | 46 | LCDMess(LCDTITLE); |
scohennm | 1:0d13b6d7907f | 47 | wait(TITLEWAIT); |
scohennm | 1:0d13b6d7907f | 48 | return; |
scohennm | 1:0d13b6d7907f | 49 | } |
scohennm | 0:04499bc54bee | 50 | int main(void) { |
scohennm | 1:0d13b6d7907f | 51 | int i; |
scohennm | 0:04499bc54bee | 52 | char lcdData[LCDCHARLEN]; |
scohennm | 0:04499bc54bee | 53 | PwmOut gled(LED_GREEN); |
scohennm | 0:04499bc54bee | 54 | PwmOut rled(LED_RED); |
scohennm | 0:04499bc54bee | 55 | pc.printf(PROGNAME); |
scohennm | 1:0d13b6d7907f | 56 | float secondsCount = 0.0; |
scohennm | 1:0d13b6d7907f | 57 | int minutesCount; // for displaying mininuts |
scohennm | 1:0d13b6d7907f | 58 | int seconds; // |
scohennm | 1:0d13b6d7907f | 59 | int fifthSeconds; |
scohennm | 1:0d13b6d7907f | 60 | bool statetoggle = false; //was in stopped state. |
scohennm | 1:0d13b6d7907f | 61 | |
scohennm | 1:0d13b6d7907f | 62 | initialize_global_vars(); |
scohennm | 1:0d13b6d7907f | 63 | showTitle(); |
scohennm | 0:04499bc54bee | 64 | |
scohennm | 0:04499bc54bee | 65 | while (true) { |
scohennm | 1:0d13b6d7907f | 66 | if (ButtonTimer > BUTTONTIME){ |
scohennm | 1:0d13b6d7907f | 67 | for (i=0; i<NUMBUTS; i++){ // index will be 0 or 1 |
scohennm | 1:0d13b6d7907f | 68 | if(!buttons[i]) { // a buttton is pressed |
scohennm | 1:0d13b6d7907f | 69 | displayState = i; |
scohennm | 1:0d13b6d7907f | 70 | switch (displayState){ // this keeps things generic |
scohennm | 1:0d13b6d7907f | 71 | case TIMINGSTATE: { |
scohennm | 1:0d13b6d7907f | 72 | statetoggle = !statetoggle; |
scohennm | 1:0d13b6d7907f | 73 | break; |
scohennm | 1:0d13b6d7907f | 74 | } |
scohennm | 1:0d13b6d7907f | 75 | case RESETTINGSTATE :{ |
scohennm | 1:0d13b6d7907f | 76 | break; |
scohennm | 1:0d13b6d7907f | 77 | } |
scohennm | 1:0d13b6d7907f | 78 | } |
scohennm | 1:0d13b6d7907f | 79 | } // if ! buttons |
scohennm | 1:0d13b6d7907f | 80 | }// for loop to look at buttons |
scohennm | 1:0d13b6d7907f | 81 | ButtonTimer.reset(); |
scohennm | 1:0d13b6d7907f | 82 | switch (displayState){ |
scohennm | 1:0d13b6d7907f | 83 | /* this goes away |
scohennm | 1:0d13b6d7907f | 84 | case STOPPEDSTATE : { |
scohennm | 1:0d13b6d7907f | 85 | rled = 0.0; |
scohennm | 1:0d13b6d7907f | 86 | gled = 1.0; |
scohennm | 1:0d13b6d7907f | 87 | break; |
scohennm | 1:0d13b6d7907f | 88 | } |
scohennm | 1:0d13b6d7907f | 89 | */ |
scohennm | 1:0d13b6d7907f | 90 | case RESETTINGSTATE: |
scohennm | 1:0d13b6d7907f | 91 | if (!statetoggle){ |
scohennm | 1:0d13b6d7907f | 92 | secondsCount = 0; |
scohennm | 1:0d13b6d7907f | 93 | displayState = TIMINGSTATE; |
scohennm | 1:0d13b6d7907f | 94 | } |
scohennm | 1:0d13b6d7907f | 95 | rled = 0.0; |
scohennm | 1:0d13b6d7907f | 96 | gled = 1.0; |
scohennm | 1:0d13b6d7907f | 97 | break; |
scohennm | 1:0d13b6d7907f | 98 | |
scohennm | 1:0d13b6d7907f | 99 | |
scohennm | 1:0d13b6d7907f | 100 | case TIMINGSTATE : { |
scohennm | 1:0d13b6d7907f | 101 | if(statetoggle){ |
scohennm | 1:0d13b6d7907f | 102 | secondsCount = secondsCount + BUTTONTIME; |
scohennm | 1:0d13b6d7907f | 103 | } |
scohennm | 1:0d13b6d7907f | 104 | rled = 1.0; |
scohennm | 1:0d13b6d7907f | 105 | gled = 0.0; |
scohennm | 1:0d13b6d7907f | 106 | break; |
scohennm | 1:0d13b6d7907f | 107 | } |
scohennm | 1:0d13b6d7907f | 108 | } |
scohennm | 1:0d13b6d7907f | 109 | |
scohennm | 1:0d13b6d7907f | 110 | // Parse the seconds |
scohennm | 1:0d13b6d7907f | 111 | seconds = (int)secondsCount; // make seconds "mask" |
scohennm | 1:0d13b6d7907f | 112 | fifthSeconds = (int)((secondsCount - (float)seconds) * 10); // the 0.2 seconds |
scohennm | 1:0d13b6d7907f | 113 | minutesCount = seconds / FULLMINUTE; |
scohennm | 1:0d13b6d7907f | 114 | seconds = seconds % FULLMINUTE; |
scohennm | 1:0d13b6d7907f | 115 | sprintf (lcdData,"%1d.%02d.%1d",minutesCount,seconds,fifthSeconds); |
scohennm | 1:0d13b6d7907f | 116 | LCDMess(lcdData); |
scohennm | 1:0d13b6d7907f | 117 | }// end Button timer |
scohennm | 0:04499bc54bee | 118 | } |
scohennm | 0:04499bc54bee | 119 | } |