![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
KBrat-SSD541-HW-9.2
Fork of kl46z_stop_watch_v2 by
main.cpp@2:1d77439751bf, 2016-10-19 (annotated)
- Committer:
- tisbrat
- Date:
- Wed Oct 19 06:42:00 2016 +0000
- Revision:
- 2:1d77439751bf
- Parent:
- 1:0d13b6d7907f
KBrat-SSD541-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 |
tisbrat | 2:1d77439751bf | 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 |
tisbrat | 2:1d77439751bf | 17 | #define PROGNAME "KBrat-SSD541-HW-9.2" |
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 | void initialize_global_vars(){ |
scohennm | 1:0d13b6d7907f | 31 | pc.printf(PROGNAME); |
scohennm | 1:0d13b6d7907f | 32 | // set up DAQ timer |
scohennm | 1:0d13b6d7907f | 33 | // set up DAQ timers |
scohennm | 1:0d13b6d7907f | 34 | ButtonTimer.start(); |
scohennm | 1:0d13b6d7907f | 35 | ButtonTimer.reset(); |
scohennm | 1:0d13b6d7907f | 36 | } |
scohennm | 0:04499bc54bee | 37 | |
scohennm | 0:04499bc54bee | 38 | void LCDMess(char *lMess){ |
scohennm | 0:04499bc54bee | 39 | slcd.Home(); |
scohennm | 0:04499bc54bee | 40 | slcd.clear(); |
scohennm | 0:04499bc54bee | 41 | slcd.printf(lMess); |
scohennm | 0:04499bc54bee | 42 | } |
scohennm | 1:0d13b6d7907f | 43 | void showTitle(){ |
scohennm | 1:0d13b6d7907f | 44 | LCDMess(LCDTITLE); |
scohennm | 1:0d13b6d7907f | 45 | wait(TITLEWAIT); |
scohennm | 1:0d13b6d7907f | 46 | return; |
scohennm | 1:0d13b6d7907f | 47 | } |
scohennm | 0:04499bc54bee | 48 | int main(void) { |
scohennm | 1:0d13b6d7907f | 49 | int i; |
scohennm | 0:04499bc54bee | 50 | char lcdData[LCDCHARLEN]; |
scohennm | 0:04499bc54bee | 51 | PwmOut gled(LED_GREEN); |
scohennm | 0:04499bc54bee | 52 | PwmOut rled(LED_RED); |
scohennm | 0:04499bc54bee | 53 | pc.printf(PROGNAME); |
scohennm | 1:0d13b6d7907f | 54 | float secondsCount = 0.0; |
scohennm | 1:0d13b6d7907f | 55 | int minutesCount; // for displaying mininuts |
scohennm | 1:0d13b6d7907f | 56 | int seconds; // |
scohennm | 1:0d13b6d7907f | 57 | int fifthSeconds; |
tisbrat | 2:1d77439751bf | 58 | //bool statetoggle = false; //was in stopped state. |
scohennm | 1:0d13b6d7907f | 59 | |
scohennm | 1:0d13b6d7907f | 60 | initialize_global_vars(); |
scohennm | 1:0d13b6d7907f | 61 | showTitle(); |
scohennm | 0:04499bc54bee | 62 | |
scohennm | 0:04499bc54bee | 63 | while (true) { |
scohennm | 1:0d13b6d7907f | 64 | if (ButtonTimer > BUTTONTIME){ |
tisbrat | 2:1d77439751bf | 65 | for (i=0; i<NUMBUTS; i++){ // index will be 0 or 1 |
tisbrat | 2:1d77439751bf | 66 | if(!buttons[i]) { // a button is pressed |
scohennm | 1:0d13b6d7907f | 67 | displayState = i; |
tisbrat | 2:1d77439751bf | 68 | |
tisbrat | 2:1d77439751bf | 69 | |
tisbrat | 2:1d77439751bf | 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 | } |
tisbrat | 2:1d77439751bf | 78 | }*/ |
tisbrat | 2:1d77439751bf | 79 | }else if(!buttons[0] && !buttons[1]){ |
tisbrat | 2:1d77439751bf | 80 | switch (displayState){ |
tisbrat | 2:1d77439751bf | 81 | case RESETTINGSTATE:{ |
tisbrat | 2:1d77439751bf | 82 | /*secondsCount = 0; |
tisbrat | 2:1d77439751bf | 83 | displayState = TIMINGSTATE; |
tisbrat | 2:1d77439751bf | 84 | |
tisbrat | 2:1d77439751bf | 85 | rled = 0.0; |
tisbrat | 2:1d77439751bf | 86 | gled = 1.0;*/ |
tisbrat | 2:1d77439751bf | 87 | |
tisbrat | 2:1d77439751bf | 88 | break; |
tisbrat | 2:1d77439751bf | 89 | } |
tisbrat | 2:1d77439751bf | 90 | } |
tisbrat | 2:1d77439751bf | 91 | } // if ! buttons |
scohennm | 1:0d13b6d7907f | 92 | }// for loop to look at buttons |
scohennm | 1:0d13b6d7907f | 93 | ButtonTimer.reset(); |
tisbrat | 2:1d77439751bf | 94 | |
scohennm | 1:0d13b6d7907f | 95 | switch (displayState){ |
tisbrat | 2:1d77439751bf | 96 | |
tisbrat | 2:1d77439751bf | 97 | case STOPPEDSTATE : { //state 0 |
scohennm | 1:0d13b6d7907f | 98 | rled = 0.0; |
scohennm | 1:0d13b6d7907f | 99 | gled = 1.0; |
scohennm | 1:0d13b6d7907f | 100 | break; |
scohennm | 1:0d13b6d7907f | 101 | } |
tisbrat | 2:1d77439751bf | 102 | |
tisbrat | 2:1d77439751bf | 103 | case TIMINGSTATE : { //state 1 |
tisbrat | 2:1d77439751bf | 104 | secondsCount = secondsCount + BUTTONTIME; |
tisbrat | 2:1d77439751bf | 105 | rled = 1.0; |
tisbrat | 2:1d77439751bf | 106 | gled = 0.0; |
tisbrat | 2:1d77439751bf | 107 | |
tisbrat | 2:1d77439751bf | 108 | break; |
tisbrat | 2:1d77439751bf | 109 | } |
tisbrat | 2:1d77439751bf | 110 | |
tisbrat | 2:1d77439751bf | 111 | case RESETTINGSTATE: {//state 2 |
tisbrat | 2:1d77439751bf | 112 | secondsCount = 0; |
tisbrat | 2:1d77439751bf | 113 | displayState = TIMINGSTATE; |
tisbrat | 2:1d77439751bf | 114 | |
tisbrat | 2:1d77439751bf | 115 | rled = 0.0; |
tisbrat | 2:1d77439751bf | 116 | gled = 0.0; |
tisbrat | 2:1d77439751bf | 117 | break; |
tisbrat | 2:1d77439751bf | 118 | |
tisbrat | 2:1d77439751bf | 119 | } |
tisbrat | 2:1d77439751bf | 120 | |
tisbrat | 2:1d77439751bf | 121 | /*case TIMINGSTATE : { //state 1 |
tisbrat | 2:1d77439751bf | 122 | if(statetoggle){ |
tisbrat | 2:1d77439751bf | 123 | secondsCount = secondsCount + BUTTONTIME; |
tisbrat | 2:1d77439751bf | 124 | } |
tisbrat | 2:1d77439751bf | 125 | rled = 1.0; |
tisbrat | 2:1d77439751bf | 126 | gled = 0.0; |
tisbrat | 2:1d77439751bf | 127 | |
tisbrat | 2:1d77439751bf | 128 | break; |
tisbrat | 2:1d77439751bf | 129 | |
tisbrat | 2:1d77439751bf | 130 | case RESETTINGSTATE: //state 2 |
tisbrat | 2:1d77439751bf | 131 | if (!statetoggle){ |
scohennm | 1:0d13b6d7907f | 132 | secondsCount = 0; |
scohennm | 1:0d13b6d7907f | 133 | displayState = TIMINGSTATE; |
scohennm | 1:0d13b6d7907f | 134 | } |
scohennm | 1:0d13b6d7907f | 135 | rled = 0.0; |
scohennm | 1:0d13b6d7907f | 136 | gled = 1.0; |
scohennm | 1:0d13b6d7907f | 137 | break; |
scohennm | 1:0d13b6d7907f | 138 | |
tisbrat | 2:1d77439751bf | 139 | }*/ |
scohennm | 1:0d13b6d7907f | 140 | } |
scohennm | 1:0d13b6d7907f | 141 | |
scohennm | 1:0d13b6d7907f | 142 | // Parse the seconds |
scohennm | 1:0d13b6d7907f | 143 | seconds = (int)secondsCount; // make seconds "mask" |
scohennm | 1:0d13b6d7907f | 144 | fifthSeconds = (int)((secondsCount - (float)seconds) * 10); // the 0.2 seconds |
scohennm | 1:0d13b6d7907f | 145 | minutesCount = seconds / FULLMINUTE; |
scohennm | 1:0d13b6d7907f | 146 | seconds = seconds % FULLMINUTE; |
scohennm | 1:0d13b6d7907f | 147 | sprintf (lcdData,"%1d.%02d.%1d",minutesCount,seconds,fifthSeconds); |
scohennm | 1:0d13b6d7907f | 148 | LCDMess(lcdData); |
scohennm | 1:0d13b6d7907f | 149 | }// end Button timer |
scohennm | 0:04499bc54bee | 150 | } |
scohennm | 0:04499bc54bee | 151 | } |