AnnaLouise Martinez
/
kl46z_stop_watch_v2
AMart_SSD341_hw9.2
Fork of kl46z_stop_watch_v2 by
main.cpp@2:de155cad0909, 2016-10-19 (annotated)
- Committer:
- annalou
- Date:
- Wed Oct 19 04:36:35 2016 +0000
- Revision:
- 2:de155cad0909
- Parent:
- 1:0d13b6d7907f
AMartSSD341_hw9.2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
scohennm | 0:04499bc54bee | 1 | #include "mbed.h" |
annalou | 2:de155cad0909 | 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 |
annalou | 2:de155cad0909 | 12 | #define RESETTINGSTATE 2 |
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 | |
annalou | 2:de155cad0909 | 32 | void initialize_global_vars() |
annalou | 2:de155cad0909 | 33 | { |
scohennm | 1:0d13b6d7907f | 34 | pc.printf(PROGNAME); |
scohennm | 1:0d13b6d7907f | 35 | // set up DAQ timer |
scohennm | 1:0d13b6d7907f | 36 | // set up DAQ timers |
scohennm | 1:0d13b6d7907f | 37 | ButtonTimer.start(); |
annalou | 2:de155cad0909 | 38 | ButtonTimer.reset(); |
annalou | 2:de155cad0909 | 39 | } |
scohennm | 0:04499bc54bee | 40 | |
annalou | 2:de155cad0909 | 41 | void LCDMess(char *lMess) |
annalou | 2:de155cad0909 | 42 | { |
annalou | 2:de155cad0909 | 43 | slcd.Home(); |
annalou | 2:de155cad0909 | 44 | slcd.clear(); |
annalou | 2:de155cad0909 | 45 | slcd.printf(lMess); |
scohennm | 0:04499bc54bee | 46 | } |
annalou | 2:de155cad0909 | 47 | void showTitle() |
annalou | 2:de155cad0909 | 48 | { |
scohennm | 1:0d13b6d7907f | 49 | LCDMess(LCDTITLE); |
scohennm | 1:0d13b6d7907f | 50 | wait(TITLEWAIT); |
scohennm | 1:0d13b6d7907f | 51 | return; |
scohennm | 1:0d13b6d7907f | 52 | } |
annalou | 2:de155cad0909 | 53 | int main(void) |
annalou | 2:de155cad0909 | 54 | { |
scohennm | 1:0d13b6d7907f | 55 | int i; |
scohennm | 0:04499bc54bee | 56 | char lcdData[LCDCHARLEN]; |
scohennm | 0:04499bc54bee | 57 | PwmOut gled(LED_GREEN); |
scohennm | 0:04499bc54bee | 58 | PwmOut rled(LED_RED); |
scohennm | 0:04499bc54bee | 59 | pc.printf(PROGNAME); |
scohennm | 1:0d13b6d7907f | 60 | float secondsCount = 0.0; |
scohennm | 1:0d13b6d7907f | 61 | int minutesCount; // for displaying mininuts |
scohennm | 1:0d13b6d7907f | 62 | int seconds; // |
scohennm | 1:0d13b6d7907f | 63 | int fifthSeconds; |
scohennm | 1:0d13b6d7907f | 64 | bool statetoggle = false; //was in stopped state. |
annalou | 2:de155cad0909 | 65 | |
scohennm | 1:0d13b6d7907f | 66 | initialize_global_vars(); |
scohennm | 1:0d13b6d7907f | 67 | showTitle(); |
scohennm | 0:04499bc54bee | 68 | |
annalou | 2:de155cad0909 | 69 | while (true) { |
annalou | 2:de155cad0909 | 70 | if (ButtonTimer > BUTTONTIME) { |
annalou | 2:de155cad0909 | 71 | for (i=0; i<NUMBUTS; i++) { // index will be 0 or 1 |
scohennm | 1:0d13b6d7907f | 72 | if(!buttons[i]) { // a buttton is pressed |
annalou | 2:de155cad0909 | 73 | displayState = i; |
scohennm | 1:0d13b6d7907f | 74 | } // if ! buttons |
annalou | 2:de155cad0909 | 75 | else{ |
annalou | 2:de155cad0909 | 76 | if(!buttons[i]){ |
annalou | 2:de155cad0909 | 77 | { |
annalou | 2:de155cad0909 | 78 | if(!buttons[i + 1]){ |
annalou | 2:de155cad0909 | 79 | displayState = 2; |
annalou | 2:de155cad0909 | 80 | } |
annalou | 2:de155cad0909 | 81 | if(!buttons[i-1]){ |
annalou | 2:de155cad0909 | 82 | displayState = 2; |
annalou | 2:de155cad0909 | 83 | } } |
annalou | 2:de155cad0909 | 84 | } |
annalou | 2:de155cad0909 | 85 | } |
scohennm | 1:0d13b6d7907f | 86 | }// for loop to look at buttons |
annalou | 2:de155cad0909 | 87 | |
scohennm | 1:0d13b6d7907f | 88 | switch (displayState){ |
scohennm | 1:0d13b6d7907f | 89 | case RESETTINGSTATE: |
scohennm | 1:0d13b6d7907f | 90 | if (!statetoggle){ |
scohennm | 1:0d13b6d7907f | 91 | secondsCount = 0; |
scohennm | 1:0d13b6d7907f | 92 | displayState = TIMINGSTATE; |
scohennm | 1:0d13b6d7907f | 93 | } |
scohennm | 1:0d13b6d7907f | 94 | rled = 0.0; |
scohennm | 1:0d13b6d7907f | 95 | gled = 1.0; |
scohennm | 1:0d13b6d7907f | 96 | break; |
scohennm | 1:0d13b6d7907f | 97 | case TIMINGSTATE : { |
annalou | 2:de155cad0909 | 98 | statetoggle = !statetoggle; |
scohennm | 1:0d13b6d7907f | 99 | if(statetoggle){ |
annalou | 2:de155cad0909 | 100 | secondsCount = secondsCount + BUTTONTIME; |
annalou | 2:de155cad0909 | 101 | } |
scohennm | 1:0d13b6d7907f | 102 | rled = 1.0; |
scohennm | 1:0d13b6d7907f | 103 | gled = 0.0; |
scohennm | 1:0d13b6d7907f | 104 | break; |
scohennm | 1:0d13b6d7907f | 105 | } |
annalou | 2:de155cad0909 | 106 | case STOPPEDSTATE : { |
annalou | 2:de155cad0909 | 107 | |
annalou | 2:de155cad0909 | 108 | displayState = RESETTINGSTATE; |
annalou | 2:de155cad0909 | 109 | |
annalou | 2:de155cad0909 | 110 | rled = 0.0; |
annalou | 2:de155cad0909 | 111 | gled = 1.0; |
annalou | 2:de155cad0909 | 112 | break; |
annalou | 2:de155cad0909 | 113 | } |
scohennm | 1:0d13b6d7907f | 114 | } |
annalou | 2:de155cad0909 | 115 | ButtonTimer.reset(); |
scohennm | 1:0d13b6d7907f | 116 | // Parse the seconds |
scohennm | 1:0d13b6d7907f | 117 | seconds = (int)secondsCount; // make seconds "mask" |
scohennm | 1:0d13b6d7907f | 118 | fifthSeconds = (int)((secondsCount - (float)seconds) * 10); // the 0.2 seconds |
scohennm | 1:0d13b6d7907f | 119 | minutesCount = seconds / FULLMINUTE; |
annalou | 2:de155cad0909 | 120 | seconds = seconds % FULLMINUTE; |
annalou | 2:de155cad0909 | 121 | sprintf (lcdData,"\r\n%1d.%02d.%1d \r\n",minutesCount,seconds,fifthSeconds); |
annalou | 2:de155cad0909 | 122 | pc.printf (lcdData,"\r\n%1d.%02d.%1d","\r\n",minutesCount,seconds,fifthSeconds); |
annalou | 2:de155cad0909 | 123 | LCDMess(lcdData); |
scohennm | 1:0d13b6d7907f | 124 | }// end Button timer |
scohennm | 0:04499bc54bee | 125 | } |
annalou | 2:de155cad0909 | 126 | } |