Shane Barton
/
SBart_kl46z_btn_slider_toSerial_noDecimal
SSD 447 - HW 1.1
Fork of kl46z_btn_slider_toSerial by
main.cpp@7:8f64ad5334ca, 2015-10-12 (annotated)
- Committer:
- sim1sgl
- Date:
- Mon Oct 12 03:19:22 2015 +0000
- Revision:
- 7:8f64ad5334ca
- Parent:
- 6:a109f45fae66
- Child:
- 8:f7063e4ba2c2
Simple button and slider command to PC program, adapted from the KL46Z button with interrupt made by Stan
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
scohennm | 0:e23fffd4b9a7 | 1 | #include "mbed.h" |
scohennm | 6:a109f45fae66 | 2 | #include "SLCD.h" |
scohennm | 6:a109f45fae66 | 3 | #include "TSISensor.h" |
scohennm | 6:a109f45fae66 | 4 | |
scohennm | 2:b49e5adf84df | 5 | |
scohennm | 0:e23fffd4b9a7 | 6 | #define LEDON false |
scohennm | 0:e23fffd4b9a7 | 7 | #define LEDOFF true |
scohennm | 1:2688f68df85d | 8 | #define NUMBUTS 2 |
scohennm | 6:a109f45fae66 | 9 | #define RBUTINDEX 0 |
scohennm | 6:a109f45fae66 | 10 | #define LBUTINDEX 1 |
scohennm | 3:7e9670be412e | 11 | #define LBUT PTC12 // port addresses for buttons |
scohennm | 1:2688f68df85d | 12 | #define RBUT PTC3 |
sim1sgl | 7:8f64ad5334ca | 13 | #define DATATIME 200 //milliseconds |
scohennm | 6:a109f45fae66 | 14 | #define LCDLEN 10 |
sim1sgl | 7:8f64ad5334ca | 15 | #define PROGNAME "kl46z_btn_slider_toSerial\r\n" |
scohennm | 3:7e9670be412e | 16 | |
scohennm | 6:a109f45fae66 | 17 | // Cap slider interface |
scohennm | 6:a109f45fae66 | 18 | SLCD slcd; //define LCD display |
scohennm | 6:a109f45fae66 | 19 | |
scohennm | 6:a109f45fae66 | 20 | TSISensor tsiScaling; // Capacitive sensor/slider |
scohennm | 6:a109f45fae66 | 21 | Timer dataTimer; |
scohennm | 0:e23fffd4b9a7 | 22 | |
scohennm | 6:a109f45fae66 | 23 | // Button interrrupts |
sim1sgl | 7:8f64ad5334ca | 24 | InterruptIn rtButton(RBUT); |
scohennm | 6:a109f45fae66 | 25 | InterruptIn lfButton(LBUT); |
scohennm | 3:7e9670be412e | 26 | |
scohennm | 6:a109f45fae66 | 27 | char lcdData[LCDLEN]; |
scohennm | 6:a109f45fae66 | 28 | |
sim1sgl | 7:8f64ad5334ca | 29 | Serial pc(USBTX, USBRX);// set up USB as communications to Host PC via USB connectons |
scohennm | 3:7e9670be412e | 30 | |
scohennm | 6:a109f45fae66 | 31 | // Interrupt service routines |
scohennm | 6:a109f45fae66 | 32 | void rtButtonPressed(){ |
sim1sgl | 7:8f64ad5334ca | 33 | pc.printf("button: right\r\n"); |
scohennm | 6:a109f45fae66 | 34 | } |
scohennm | 6:a109f45fae66 | 35 | |
scohennm | 6:a109f45fae66 | 36 | void lfButtonPressed(){ |
sim1sgl | 7:8f64ad5334ca | 37 | pc.printf("button: left \r\n"); |
scohennm | 6:a109f45fae66 | 38 | } |
scohennm | 6:a109f45fae66 | 39 | |
scohennm | 6:a109f45fae66 | 40 | // End interrupt routines |
scohennm | 6:a109f45fae66 | 41 | |
scohennm | 6:a109f45fae66 | 42 | // Regular routines |
scohennm | 6:a109f45fae66 | 43 | void LCDMessNoDwell(char *lMess){ |
scohennm | 6:a109f45fae66 | 44 | slcd.Home(); |
scohennm | 6:a109f45fae66 | 45 | slcd.clear(); |
scohennm | 6:a109f45fae66 | 46 | slcd.printf(lMess); |
scohennm | 6:a109f45fae66 | 47 | } |
scohennm | 6:a109f45fae66 | 48 | |
scohennm | 1:2688f68df85d | 49 | // -------------------------------- |
scohennm | 3:7e9670be412e | 50 | int main() { |
sim1sgl | 7:8f64ad5334ca | 51 | float sliderValue = 0.0; |
scohennm | 2:b49e5adf84df | 52 | |
sim1sgl | 7:8f64ad5334ca | 53 | pc.printf(PROGNAME); |
scohennm | 6:a109f45fae66 | 54 | dataTimer.start(); |
scohennm | 6:a109f45fae66 | 55 | dataTimer.reset(); |
scohennm | 6:a109f45fae66 | 56 | sprintf (lcdData,"%4.3f",0.0); |
scohennm | 6:a109f45fae66 | 57 | LCDMessNoDwell(lcdData); |
scohennm | 6:a109f45fae66 | 58 | |
scohennm | 6:a109f45fae66 | 59 | // Interrupt routine setups |
scohennm | 6:a109f45fae66 | 60 | rtButton.fall(&rtButtonPressed); |
scohennm | 6:a109f45fae66 | 61 | lfButton.fall(&lfButtonPressed); |
scohennm | 6:a109f45fae66 | 62 | |
scohennm | 3:7e9670be412e | 63 | // End of setup |
scohennm | 3:7e9670be412e | 64 | |
scohennm | 0:e23fffd4b9a7 | 65 | while(true) { |
sim1sgl | 7:8f64ad5334ca | 66 | // All the interrupts |
scohennm | 6:a109f45fae66 | 67 | |
scohennm | 6:a109f45fae66 | 68 | if (dataTimer.read_ms() > DATATIME){ |
sim1sgl | 7:8f64ad5334ca | 69 | // Read the slider value |
sim1sgl | 7:8f64ad5334ca | 70 | float newSliderValue = tsiScaling.readPercentage(); |
sim1sgl | 7:8f64ad5334ca | 71 | // Only do stuff with it if it's a new value or if it's not zero |
sim1sgl | 7:8f64ad5334ca | 72 | if(newSliderValue > 0.0 && newSliderValue != sliderValue) { |
sim1sgl | 7:8f64ad5334ca | 73 | sliderValue = newSliderValue; |
sim1sgl | 7:8f64ad5334ca | 74 | sprintf (lcdData,"%4.3f", sliderValue); // Just to make things user readable |
sim1sgl | 7:8f64ad5334ca | 75 | |
scohennm | 6:a109f45fae66 | 76 | LCDMessNoDwell(lcdData); |
sim1sgl | 7:8f64ad5334ca | 77 | pc.printf("slider: %4.3f\r\n", sliderValue); |
scohennm | 6:a109f45fae66 | 78 | } |
scohennm | 6:a109f45fae66 | 79 | dataTimer.reset(); |
scohennm | 6:a109f45fae66 | 80 | } |
scohennm | 6:a109f45fae66 | 81 | |
scohennm | 0:e23fffd4b9a7 | 82 | } |
scohennm | 0:e23fffd4b9a7 | 83 | } |