Keisha Brathwaite
/
KBrat-SSD541-Midterm-Q2a-1
KBrat-SSD541-Midterm-Q2a
Fork of KBrat-SSD541-Midterm-Q2a by
main.cpp@1:44dcf262c7dd, 2016-10-01 (annotated)
- Committer:
- scohennm
- Date:
- Sat Oct 01 22:10:10 2016 +0000
- Revision:
- 1:44dcf262c7dd
- Parent:
- 0:04499bc54bee
- Child:
- 2:8b60d4eb7388
Basis program for midterm Q2 AAA Fall 2016
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
scohennm | 0:04499bc54bee | 1 | #include "mbed.h" |
scohennm | 1:44dcf262c7dd | 2 | #include <math.h> |
scohennm | 0:04499bc54bee | 3 | #include "TSISensor.h" |
scohennm | 0:04499bc54bee | 4 | #include "SLCD.h" |
scohennm | 1:44dcf262c7dd | 5 | |
scohennm | 1:44dcf262c7dd | 6 | #define LEDON false |
scohennm | 1:44dcf262c7dd | 7 | #define LEDOFF true |
scohennm | 1:44dcf262c7dd | 8 | #define NUMBUTS 2 |
scohennm | 1:44dcf262c7dd | 9 | #define LBUT PTC12 // port addresses for buttons |
scohennm | 1:44dcf262c7dd | 10 | #define RBUT PTC3 |
scohennm | 1:44dcf262c7dd | 11 | #define ARGUMENTSTATE 0 |
scohennm | 1:44dcf262c7dd | 12 | #define ANSWERSTATE 1 |
scohennm | 1:44dcf262c7dd | 13 | #define TSILIMIT 0.01 |
scohennm | 1:44dcf262c7dd | 14 | #define PRINTDELTA 0.01 |
scohennm | 0:04499bc54bee | 15 | #define LCDCHARLEN 10 |
scohennm | 0:04499bc54bee | 16 | #define DATAINTERVAL 0.1 |
scohennm | 1:44dcf262c7dd | 17 | #define BUTTONTIME 0.1 |
scohennm | 1:44dcf262c7dd | 18 | #define PROGNAME "kl46z_slider_mid_v1\n\r" |
scohennm | 0:04499bc54bee | 19 | |
scohennm | 0:04499bc54bee | 20 | SLCD slcd; //define LCD display |
scohennm | 0:04499bc54bee | 21 | Serial pc(USBTX, USBRX); |
scohennm | 0:04499bc54bee | 22 | |
scohennm | 1:44dcf262c7dd | 23 | Timer dataTimer; |
scohennm | 1:44dcf262c7dd | 24 | Timer ButtonTimer; // for reading button states |
scohennm | 1:44dcf262c7dd | 25 | DigitalIn buttons[NUMBUTS] = {RBUT, LBUT}; |
scohennm | 0:04499bc54bee | 26 | float tsidata; |
scohennm | 1:44dcf262c7dd | 27 | int displayState; |
scohennm | 1:44dcf262c7dd | 28 | |
scohennm | 1:44dcf262c7dd | 29 | void initialize_global_vars(){ |
scohennm | 1:44dcf262c7dd | 30 | pc.printf(PROGNAME); |
scohennm | 1:44dcf262c7dd | 31 | // set up DAQ timers |
scohennm | 1:44dcf262c7dd | 32 | ButtonTimer.start(); |
scohennm | 1:44dcf262c7dd | 33 | ButtonTimer.reset(); |
scohennm | 1:44dcf262c7dd | 34 | dataTimer.start(); |
scohennm | 1:44dcf262c7dd | 35 | dataTimer.reset(); |
scohennm | 1:44dcf262c7dd | 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 | 0:04499bc54bee | 43 | |
scohennm | 0:04499bc54bee | 44 | int main(void) { |
scohennm | 1:44dcf262c7dd | 45 | int i; |
scohennm | 0:04499bc54bee | 46 | char lcdData[LCDCHARLEN]; |
scohennm | 1:44dcf262c7dd | 47 | float lastTouch = 0.0; |
scohennm | 1:44dcf262c7dd | 48 | TSISensor tsi; |
scohennm | 1:44dcf262c7dd | 49 | float tempTSI; |
scohennm | 0:04499bc54bee | 50 | PwmOut gled(LED_GREEN); |
scohennm | 0:04499bc54bee | 51 | PwmOut rled(LED_RED); |
scohennm | 1:44dcf262c7dd | 52 | |
scohennm | 1:44dcf262c7dd | 53 | initialize_global_vars(); |
scohennm | 0:04499bc54bee | 54 | |
scohennm | 0:04499bc54bee | 55 | while (true) { |
scohennm | 1:44dcf262c7dd | 56 | if (ButtonTimer > BUTTONTIME){ |
scohennm | 1:44dcf262c7dd | 57 | for (i=0; i<NUMBUTS; i++){ // index will be 0 or 1 |
scohennm | 1:44dcf262c7dd | 58 | if(!buttons[i]) { |
scohennm | 1:44dcf262c7dd | 59 | displayState = i; |
scohennm | 1:44dcf262c7dd | 60 | // do something here. |
scohennm | 1:44dcf262c7dd | 61 | |
scohennm | 1:44dcf262c7dd | 62 | } // if ! buttons |
scohennm | 1:44dcf262c7dd | 63 | }// for loop to look at buttons |
scohennm | 1:44dcf262c7dd | 64 | ButtonTimer.reset(); |
scohennm | 1:44dcf262c7dd | 65 | sprintf (lcdData,"%0.4f",tsidata); |
scohennm | 1:44dcf262c7dd | 66 | LCDMess(lcdData); |
scohennm | 0:04499bc54bee | 67 | rled = 0.0; |
scohennm | 1:44dcf262c7dd | 68 | gled = 1.0; |
scohennm | 1:44dcf262c7dd | 69 | |
scohennm | 0:04499bc54bee | 70 | } |
scohennm | 1:44dcf262c7dd | 71 | if(dataTimer.read() > DATAINTERVAL){ |
scohennm | 1:44dcf262c7dd | 72 | dataTimer.reset(); |
scohennm | 1:44dcf262c7dd | 73 | tempTSI = tsi.readPercentage(); |
scohennm | 1:44dcf262c7dd | 74 | if (tempTSI > TSILIMIT){ |
scohennm | 1:44dcf262c7dd | 75 | tsidata = tempTSI; |
scohennm | 1:44dcf262c7dd | 76 | if (fabs(tsidata - lastTouch)> PRINTDELTA){ |
scohennm | 1:44dcf262c7dd | 77 | pc.printf("Position %0.4f\n\r", tsidata); |
scohennm | 1:44dcf262c7dd | 78 | } |
scohennm | 1:44dcf262c7dd | 79 | } |
scohennm | 1:44dcf262c7dd | 80 | lastTouch=tsidata; |
scohennm | 1:44dcf262c7dd | 81 | } |
scohennm | 0:04499bc54bee | 82 | } |
scohennm | 0:04499bc54bee | 83 | } |