Simple button and slider command to PC program, adapted from the KL46Z button with interrupt and slider made by Stan
Fork of blink_kl46z_but_interrupt_slider by
main.cpp@6:a109f45fae66, 2015-09-27 (annotated)
- Committer:
- scohennm
- Date:
- Sun Sep 27 23:28:00 2015 +0000
- Revision:
- 6:a109f45fae66
- Parent:
- 5:dc506f422393
- Child:
- 7:8f64ad5334ca
button interrupt with LED's and slider/LCD display demo
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 |
scohennm | 3:7e9670be412e | 13 | #define BLINKTIME 0.3 // in seconds |
scohennm | 6:a109f45fae66 | 14 | #define DATATIME 50 //milli seccnds |
scohennm | 6:a109f45fae66 | 15 | #define LCDLEN 10 |
scohennm | 6:a109f45fae66 | 16 | #define PROGNAME "blink_kl46z_buttton interrupt slider\r\n" |
scohennm | 3:7e9670be412e | 17 | |
scohennm | 6:a109f45fae66 | 18 | // Cap slider interface |
scohennm | 6:a109f45fae66 | 19 | SLCD slcd; //define LCD display |
scohennm | 6:a109f45fae66 | 20 | |
scohennm | 6:a109f45fae66 | 21 | TSISensor tsiScaling; // Capacitive sensor/slider |
scohennm | 6:a109f45fae66 | 22 | Timer dataTimer; |
scohennm | 0:e23fffd4b9a7 | 23 | |
scohennm | 3:7e9670be412e | 24 | // Timer to elliminate wait() function |
scohennm | 6:a109f45fae66 | 25 | // Timer LEDTimer; // for blinking LEDs |
scohennm | 6:a109f45fae66 | 26 | Ticker Blinker; |
scohennm | 1:2688f68df85d | 27 | |
scohennm | 6:a109f45fae66 | 28 | //Timer ButtonTimer; // no longer needed |
scohennm | 6:a109f45fae66 | 29 | // Button interrrupts |
scohennm | 6:a109f45fae66 | 30 | InterruptIn rtButton(RBUT); // Big change. |
scohennm | 6:a109f45fae66 | 31 | InterruptIn lfButton(LBUT); |
scohennm | 3:7e9670be412e | 32 | |
scohennm | 6:a109f45fae66 | 33 | char lcdData[LCDLEN]; |
scohennm | 6:a109f45fae66 | 34 | bool ledState = LEDON; |
scohennm | 6:a109f45fae66 | 35 | int currentLED = 0; // used in LEDBliker interrupt |
scohennm | 6:a109f45fae66 | 36 | |
scohennm | 6:a109f45fae66 | 37 | // DigitalIn buttons[NUMBUTS] = {RBUT, LBUT}; This array paradigm will not work right now. |
scohennm | 1:2688f68df85d | 38 | DigitalOut LEDs[NUMBUTS] = {LED_GREEN, LED_RED}; |
scohennm | 1:2688f68df85d | 39 | Serial pc(USBTX, USBRX);// set up USB as communicationis to Host PC via USB connectons |
scohennm | 0:e23fffd4b9a7 | 40 | |
scohennm | 3:7e9670be412e | 41 | void allLEDsOff(){ |
scohennm | 3:7e9670be412e | 42 | int i; |
scohennm | 3:7e9670be412e | 43 | for (i=0; i<NUMBUTS; i++){ |
scohennm | 3:7e9670be412e | 44 | LEDs[i] = LEDOFF; |
scohennm | 3:7e9670be412e | 45 | } |
scohennm | 3:7e9670be412e | 46 | } |
scohennm | 3:7e9670be412e | 47 | |
scohennm | 6:a109f45fae66 | 48 | // Interrupt service routines |
scohennm | 6:a109f45fae66 | 49 | |
scohennm | 6:a109f45fae66 | 50 | void rtButtonPressed(){ |
scohennm | 6:a109f45fae66 | 51 | allLEDsOff(); |
scohennm | 6:a109f45fae66 | 52 | currentLED = RBUTINDEX; |
scohennm | 6:a109f45fae66 | 53 | } |
scohennm | 6:a109f45fae66 | 54 | |
scohennm | 6:a109f45fae66 | 55 | void lfButtonPressed(){ |
scohennm | 6:a109f45fae66 | 56 | allLEDsOff(); |
scohennm | 6:a109f45fae66 | 57 | currentLED = LBUTINDEX; |
scohennm | 6:a109f45fae66 | 58 | } |
scohennm | 6:a109f45fae66 | 59 | |
scohennm | 6:a109f45fae66 | 60 | void LEDBlinker(){ |
scohennm | 6:a109f45fae66 | 61 | ledState = !ledState; // Flip the general state |
scohennm | 6:a109f45fae66 | 62 | LEDs[currentLED] = ledState; |
scohennm | 6:a109f45fae66 | 63 | } |
scohennm | 6:a109f45fae66 | 64 | |
scohennm | 6:a109f45fae66 | 65 | // End interrupt routines |
scohennm | 6:a109f45fae66 | 66 | |
scohennm | 6:a109f45fae66 | 67 | // Regular routines |
scohennm | 6:a109f45fae66 | 68 | void LCDMessNoDwell(char *lMess){ |
scohennm | 6:a109f45fae66 | 69 | slcd.Home(); |
scohennm | 6:a109f45fae66 | 70 | slcd.clear(); |
scohennm | 6:a109f45fae66 | 71 | slcd.printf(lMess); |
scohennm | 6:a109f45fae66 | 72 | } |
scohennm | 6:a109f45fae66 | 73 | |
scohennm | 2:b49e5adf84df | 74 | |
scohennm | 3:7e9670be412e | 75 | void initialize_global_vars(){ |
scohennm | 3:7e9670be412e | 76 | pc.printf(PROGNAME); |
scohennm | 3:7e9670be412e | 77 | // set up DAQ timers |
scohennm | 3:7e9670be412e | 78 | allLEDsOff(); |
scohennm | 3:7e9670be412e | 79 | } |
scohennm | 1:2688f68df85d | 80 | // -------------------------------- |
scohennm | 3:7e9670be412e | 81 | int main() { |
scohennm | 6:a109f45fae66 | 82 | float sliderValue=0.0; |
scohennm | 6:a109f45fae66 | 83 | // int currentLED = 0; needs to be gobal for interrupt routine |
scohennm | 2:b49e5adf84df | 84 | |
scohennm | 3:7e9670be412e | 85 | initialize_global_vars(); //keep things organized |
scohennm | 3:7e9670be412e | 86 | LEDs[currentLED] = LEDON; |
scohennm | 6:a109f45fae66 | 87 | dataTimer.start(); |
scohennm | 6:a109f45fae66 | 88 | dataTimer.reset(); |
scohennm | 6:a109f45fae66 | 89 | sprintf (lcdData,"%4.3f",0.0); |
scohennm | 6:a109f45fae66 | 90 | LCDMessNoDwell(lcdData); |
scohennm | 6:a109f45fae66 | 91 | |
scohennm | 6:a109f45fae66 | 92 | // Interrupt routine setups |
scohennm | 6:a109f45fae66 | 93 | Blinker.attach(&LEDBlinker, BLINKTIME); |
scohennm | 6:a109f45fae66 | 94 | rtButton.fall(&rtButtonPressed); |
scohennm | 6:a109f45fae66 | 95 | lfButton.fall(&lfButtonPressed); |
scohennm | 6:a109f45fae66 | 96 | |
scohennm | 3:7e9670be412e | 97 | // End of setup |
scohennm | 3:7e9670be412e | 98 | |
scohennm | 0:e23fffd4b9a7 | 99 | while(true) { |
scohennm | 6:a109f45fae66 | 100 | // All in the interrupts |
scohennm | 6:a109f45fae66 | 101 | |
scohennm | 3:7e9670be412e | 102 | // Do other things here between times of reading and flashing |
scohennm | 6:a109f45fae66 | 103 | // OK |
scohennm | 6:a109f45fae66 | 104 | sliderValue = tsiScaling.readPercentage(); |
scohennm | 6:a109f45fae66 | 105 | if (dataTimer.read_ms() > DATATIME){ |
scohennm | 6:a109f45fae66 | 106 | if(sliderValue > 0.0) { |
scohennm | 6:a109f45fae66 | 107 | sprintf (lcdData,"%4.3f",sliderValue); // Just to amke things user readable |
scohennm | 6:a109f45fae66 | 108 | LCDMessNoDwell(lcdData); |
scohennm | 6:a109f45fae66 | 109 | } |
scohennm | 6:a109f45fae66 | 110 | dataTimer.reset(); |
scohennm | 6:a109f45fae66 | 111 | } |
scohennm | 6:a109f45fae66 | 112 | |
scohennm | 0:e23fffd4b9a7 | 113 | } |
scohennm | 0:e23fffd4b9a7 | 114 | } |