KLUJA-SSD-341-Hw-9.2

Dependencies:   SLCD mbed

Fork of kl46z_stop_watch_v2 by Stanley Cohen

Committer:
kennylujan42
Date:
Wed Oct 19 06:27:05 2016 +0000
Revision:
2:5ad6ad8c563a
Parent:
1:0d13b6d7907f
KLUJA-SSD-341-Hw-9.2

Who changed what in which revision?

UserRevisionLine numberNew 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
kennylujan42 2:5ad6ad8c563a 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 }
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
kennylujan42 2:5ad6ad8c563a 62 int RButtonState;
kennylujan42 2:5ad6ad8c563a 63 int LButtonState;
kennylujan42 2:5ad6ad8c563a 64
kennylujan42 2:5ad6ad8c563a 65
scohennm 1:0d13b6d7907f 66 initialize_global_vars();
scohennm 1:0d13b6d7907f 67 showTitle();
scohennm 0:04499bc54bee 68
scohennm 0:04499bc54bee 69 while (true) {
kennylujan42 2:5ad6ad8c563a 70
kennylujan42 2:5ad6ad8c563a 71 LButtonState = !buttons[1].read();
kennylujan42 2:5ad6ad8c563a 72 RButtonState = !buttons[0].read();
kennylujan42 2:5ad6ad8c563a 73
kennylujan42 2:5ad6ad8c563a 74
scohennm 1:0d13b6d7907f 75 if (ButtonTimer > BUTTONTIME){
scohennm 1:0d13b6d7907f 76 for (i=0; i<NUMBUTS; i++){ // index will be 0 or 1
kennylujan42 2:5ad6ad8c563a 77
kennylujan42 2:5ad6ad8c563a 78 if(LButtonState && RButtonState) { //a butttons is pressed
kennylujan42 2:5ad6ad8c563a 79 displayState = RESETTINGSTATE;
kennylujan42 2:5ad6ad8c563a 80 statetoggle = !statetoggle;
kennylujan42 2:5ad6ad8c563a 81
kennylujan42 2:5ad6ad8c563a 82 break;
kennylujan42 2:5ad6ad8c563a 83 }
kennylujan42 2:5ad6ad8c563a 84 else if(LButtonState){
kennylujan42 2:5ad6ad8c563a 85 displayState = TIMINGSTATE;
kennylujan42 2:5ad6ad8c563a 86 statetoggle = !statetoggle;
kennylujan42 2:5ad6ad8c563a 87 break;
kennylujan42 2:5ad6ad8c563a 88 }
kennylujan42 2:5ad6ad8c563a 89 else if(RButtonState){
kennylujan42 2:5ad6ad8c563a 90 displayState = STOPPEDSTATE;
kennylujan42 2:5ad6ad8c563a 91 break;
kennylujan42 2:5ad6ad8c563a 92 }
kennylujan42 2:5ad6ad8c563a 93 else{
kennylujan42 2:5ad6ad8c563a 94 ButtonTimer.reset();
scohennm 1:0d13b6d7907f 95 break;
kennylujan42 2:5ad6ad8c563a 96 }
kennylujan42 2:5ad6ad8c563a 97 }
kennylujan42 2:5ad6ad8c563a 98
kennylujan42 2:5ad6ad8c563a 99
scohennm 1:0d13b6d7907f 100 ButtonTimer.reset();
scohennm 1:0d13b6d7907f 101 switch (displayState){
scohennm 1:0d13b6d7907f 102 case STOPPEDSTATE : {
scohennm 1:0d13b6d7907f 103 rled = 0.0;
scohennm 1:0d13b6d7907f 104 gled = 1.0;
scohennm 1:0d13b6d7907f 105 break;
scohennm 1:0d13b6d7907f 106 }
kennylujan42 2:5ad6ad8c563a 107
scohennm 1:0d13b6d7907f 108 case RESETTINGSTATE:
kennylujan42 2:5ad6ad8c563a 109 if (statetoggle){
scohennm 1:0d13b6d7907f 110 secondsCount = 0;
scohennm 1:0d13b6d7907f 111 displayState = TIMINGSTATE;
scohennm 1:0d13b6d7907f 112 }
scohennm 1:0d13b6d7907f 113 rled = 0.0;
kennylujan42 2:5ad6ad8c563a 114 gled = 0.0;
scohennm 1:0d13b6d7907f 115 break;
scohennm 1:0d13b6d7907f 116
scohennm 1:0d13b6d7907f 117
scohennm 1:0d13b6d7907f 118 case TIMINGSTATE : {
scohennm 1:0d13b6d7907f 119 if(statetoggle){
scohennm 1:0d13b6d7907f 120 secondsCount = secondsCount + BUTTONTIME;
scohennm 1:0d13b6d7907f 121 }
scohennm 1:0d13b6d7907f 122 rled = 1.0;
scohennm 1:0d13b6d7907f 123 gled = 0.0;
scohennm 1:0d13b6d7907f 124 break;
scohennm 1:0d13b6d7907f 125 }
scohennm 1:0d13b6d7907f 126 }
scohennm 1:0d13b6d7907f 127
scohennm 1:0d13b6d7907f 128 // Parse the seconds
scohennm 1:0d13b6d7907f 129 seconds = (int)secondsCount; // make seconds "mask"
scohennm 1:0d13b6d7907f 130 fifthSeconds = (int)((secondsCount - (float)seconds) * 10); // the 0.2 seconds
scohennm 1:0d13b6d7907f 131 minutesCount = seconds / FULLMINUTE;
scohennm 1:0d13b6d7907f 132 seconds = seconds % FULLMINUTE;
scohennm 1:0d13b6d7907f 133 sprintf (lcdData,"%1d.%02d.%1d",minutesCount,seconds,fifthSeconds);
scohennm 1:0d13b6d7907f 134 LCDMess(lcdData);
scohennm 1:0d13b6d7907f 135 }// end Button timer
scohennm 0:04499bc54bee 136 }
kennylujan42 2:5ad6ad8c563a 137 }
kennylujan42 2:5ad6ad8c563a 138