![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
SBart-SSD341-HW9.2
Fork of kl46z_stop_watch_v2 by
main.cpp@2:375f524a171d, 2016-10-19 (annotated)
- Committer:
- sbart
- Date:
- Wed Oct 19 05:03:14 2016 +0000
- Revision:
- 2:375f524a171d
- Parent:
- 1:0d13b6d7907f
SBart-HW-9.2
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 |
sbart | 2:375f524a171d | 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 | |
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 | } |
sbart | 2:375f524a171d | 50 | |
scohennm | 0:04499bc54bee | 51 | int main(void) { |
scohennm | 1:0d13b6d7907f | 52 | int i; |
scohennm | 0:04499bc54bee | 53 | char lcdData[LCDCHARLEN]; |
scohennm | 0:04499bc54bee | 54 | PwmOut gled(LED_GREEN); |
scohennm | 0:04499bc54bee | 55 | PwmOut rled(LED_RED); |
scohennm | 0:04499bc54bee | 56 | pc.printf(PROGNAME); |
scohennm | 1:0d13b6d7907f | 57 | float secondsCount = 0.0; |
scohennm | 1:0d13b6d7907f | 58 | int minutesCount; // for displaying mininuts |
scohennm | 1:0d13b6d7907f | 59 | int seconds; // |
scohennm | 1:0d13b6d7907f | 60 | int fifthSeconds; |
scohennm | 1:0d13b6d7907f | 61 | bool statetoggle = false; //was in stopped state. |
scohennm | 1:0d13b6d7907f | 62 | |
scohennm | 1:0d13b6d7907f | 63 | initialize_global_vars(); |
scohennm | 1:0d13b6d7907f | 64 | showTitle(); |
scohennm | 0:04499bc54bee | 65 | |
scohennm | 0:04499bc54bee | 66 | while (true) { |
sbart | 2:375f524a171d | 67 | bool rbut_pressed = false; |
sbart | 2:375f524a171d | 68 | bool lbut_pressed = false; |
sbart | 2:375f524a171d | 69 | |
scohennm | 1:0d13b6d7907f | 70 | if (ButtonTimer > BUTTONTIME){ |
sbart | 2:375f524a171d | 71 | if(LBUT) { |
sbart | 2:375f524a171d | 72 | displayState = TIMINGSTATE; |
sbart | 2:375f524a171d | 73 | lbut_pressed = true; |
sbart | 2:375f524a171d | 74 | } |
sbart | 2:375f524a171d | 75 | else if(RBUT) { |
sbart | 2:375f524a171d | 76 | displayState = STOPPEDSTATE; |
sbart | 2:375f524a171d | 77 | rbut_pressed = true; |
sbart | 2:375f524a171d | 78 | } |
sbart | 2:375f524a171d | 79 | |
sbart | 2:375f524a171d | 80 | if(lbut_pressed == true && rbut_pressed == true) { |
sbart | 2:375f524a171d | 81 | displayState = RESETTINGSTATE; |
sbart | 2:375f524a171d | 82 | } |
sbart | 2:375f524a171d | 83 | |
scohennm | 1:0d13b6d7907f | 84 | ButtonTimer.reset(); |
scohennm | 1:0d13b6d7907f | 85 | switch (displayState){ |
sbart | 2:375f524a171d | 86 | //this goes away |
scohennm | 1:0d13b6d7907f | 87 | case STOPPEDSTATE : { |
scohennm | 1:0d13b6d7907f | 88 | rled = 0.0; |
scohennm | 1:0d13b6d7907f | 89 | gled = 1.0; |
scohennm | 1:0d13b6d7907f | 90 | break; |
scohennm | 1:0d13b6d7907f | 91 | } |
scohennm | 1:0d13b6d7907f | 92 | case RESETTINGSTATE: |
sbart | 2:375f524a171d | 93 | secondsCount = 0; |
sbart | 2:375f524a171d | 94 | displayState = STOPPEDSTATE; |
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 : { |
sbart | 2:375f524a171d | 101 | secondsCount = secondsCount + BUTTONTIME; |
scohennm | 1:0d13b6d7907f | 102 | rled = 1.0; |
scohennm | 1:0d13b6d7907f | 103 | gled = 0.0; |
scohennm | 1:0d13b6d7907f | 104 | break; |
scohennm | 1:0d13b6d7907f | 105 | } |
scohennm | 1:0d13b6d7907f | 106 | } |
scohennm | 1:0d13b6d7907f | 107 | |
scohennm | 1:0d13b6d7907f | 108 | // Parse the seconds |
scohennm | 1:0d13b6d7907f | 109 | seconds = (int)secondsCount; // make seconds "mask" |
scohennm | 1:0d13b6d7907f | 110 | fifthSeconds = (int)((secondsCount - (float)seconds) * 10); // the 0.2 seconds |
scohennm | 1:0d13b6d7907f | 111 | minutesCount = seconds / FULLMINUTE; |
scohennm | 1:0d13b6d7907f | 112 | seconds = seconds % FULLMINUTE; |
scohennm | 1:0d13b6d7907f | 113 | sprintf (lcdData,"%1d.%02d.%1d",minutesCount,seconds,fifthSeconds); |
scohennm | 1:0d13b6d7907f | 114 | LCDMess(lcdData); |
scohennm | 1:0d13b6d7907f | 115 | }// end Button timer |
scohennm | 0:04499bc54bee | 116 | } |
scohennm | 0:04499bc54bee | 117 | } |