Wayne Chin
/
UIMockup
Got quadrature decoders and mode switch working
Diff: main.cpp
- Revision:
- 1:fb60f76e33ee
- Parent:
- 0:7ae28d2c1e3e
diff -r 7ae28d2c1e3e -r fb60f76e33ee main.cpp --- a/main.cpp Fri Oct 22 20:41:49 2010 +0000 +++ b/main.cpp Mon Oct 25 20:48:04 2010 +0000 @@ -4,6 +4,9 @@ // Version 0.1 // Created by Wayne Chin // October 22, 2010 +// Version 0.2 +// Added amps/gas mode selections; confirmed quad decoder operation. +// October 25, 2010 #include "mbed.h" @@ -11,18 +14,25 @@ #define FALSE 0 #define ON 0 // LED drives are inverted #define OFF 1 // LED drives are inverted -#define MAXAMPS200 200 -#define MINAMPSET 25 -#define MAXGASPRESS 99 +#define MAXAMPS200 200 // Max amps display +#define MINAMPSET 25 // Min amps display +#define MAXGASPRESS 99 // Max gas display +#define STARTGASPRESS 75 #define MINGASPRESS 0 - -//#define AMPSMODE // ***Comment this line to compile for gas control mode*** +#define AMPSMODE 0 +#define GASMODE 1 +#define BUTTONTIMEOUT 1000 // Mode change timeout in milliseconds // Global variables int quadnew, quadold; // Quadrature decoding int quadcount; // Integer counter for quadrature encoder output +int gascount, ampscount; // Separately keep track of gas and amps int select, gasselect; // Keep track of mode/gas selection LEDs int quadswitchnew, quadswitchold; // Debounce pushbutton states +int mode; // Keep track of AMPS or GAS pressure display modes +int modechanged; // Keep track of entry into mode change + +Serial pc(USBTX, USBRX); // tx, rx // mbed LEDs DigitalOut led1(LED1); @@ -66,20 +76,19 @@ // Interrupt Ticker getswitches; // Periodic function call used to sample switches +Timer modetimer; // Used for press-and-hold function // Take integer input and drive 3 digit 7-segment display and toggle among gas select LEDs void LEDout(int number) { int hundreds, tens, ones; -#ifdef AMPSMODE - if (number > 999) // Throw out numbers greater than 999 -#else - if (number > 99) // Throw out numbers greater than 99 -#endif + if (mode == AMPSMODE && number > 999) // Throw out numbers greater than 999 + return; + else if (mode == GASMODE && number > 99) // Throw out numbers greater than 99 return; else if (number < 0) // Throw out numbers less than 0 return; - else + else // Display number { // Extract hundreds digit hundreds = number - (number / 100); @@ -89,243 +98,81 @@ // Extract ones digit ones = number - hundreds - tens; -#ifdef AMPSMODE - switch (hundreds) + if (mode == AMPSMODE) { - case 0: - { - LED1a = ON; - LED1b = ON; - LED1c = ON; - LED1d = ON; - LED1e = ON; - LED1f = ON; - LED1g = OFF; - } - break; - case 1: - { - LED1a = OFF; - LED1b = ON; - LED1c = ON; - LED1d = OFF; - LED1e = OFF; - LED1f = OFF; - LED1g = OFF; - } - break; - case 2: - { - LED1a = ON; - LED1b = ON; - LED1c = OFF; - LED1d = ON; - LED1e = ON; - LED1f = OFF; - LED1g = ON; - } - break; - case 3: - { - LED1a = ON; - LED1b = ON; - LED1c = ON; - LED1d = ON; - LED1e = OFF; - LED1f = OFF; - LED1g = ON; - } - break; - case 4: - { - LED1a = OFF; - LED1b = ON; - LED1c = ON; - LED1d = OFF; - LED1e = OFF; - LED1f = ON; - LED1g = ON; - } - break; - case 5: - { - LED1a = ON; - LED1b = OFF; - LED1c = ON; - LED1d = ON; - LED1e = OFF; - LED1f = ON; - LED1g = ON; - } - break; - case 6: - { - LED1a = ON; - LED1b = OFF; - LED1c = ON; - LED1d = ON; - LED1e = ON; - LED1f = ON; - LED1g = ON; - } - break; - case 7: - { - LED1a = ON; - LED1b = ON; - LED1c = ON; - LED1d = OFF; - LED1e = OFF; - LED1f = OFF; - LED1g = OFF; - } - break; - case 8: - { - LED1a = ON; - LED1b = ON; - LED1c = ON; - LED1d = ON; - LED1e = ON; - LED1f = ON; - LED1g = ON; - } - break; - case 9: - { - LED1a = ON; - LED1b = ON; - LED1c = ON; - LED1d = ON; - LED1e = OFF; - LED1f = ON; - LED1g = ON; - } - break; - default: - break; // Do nothing - } // switch (hundreds) -#else - // Turn off hundreds digit in gas mode - LED1a = OFF; - LED1b = OFF; - LED1c = OFF; - LED1d = OFF; - LED1e = OFF; - LED1f = OFF; - LED1g = OFF; -#endif + switch (hundreds) + { + case 0: { + LED1a = ON; LED1b = ON; LED1c = ON; LED1d = ON; LED1e = ON; LED1f = ON; LED1g = OFF; } + break; + case 1: { + LED1a = OFF; LED1b = ON; LED1c = ON; LED1d = OFF; LED1e = OFF; LED1f = OFF; LED1g = OFF; } + break; + case 2: { + LED1a = ON; LED1b = ON; LED1c = OFF; LED1d = ON; LED1e = ON; LED1f = OFF; LED1g = ON; } + break; + case 3: { + LED1a = ON; LED1b = ON; LED1c = ON; LED1d = ON; LED1e = OFF; LED1f = OFF; LED1g = ON; } + break; + case 4: { + LED1a = OFF; LED1b = ON; LED1c = ON; LED1d = OFF; LED1e = OFF; LED1f = ON; LED1g = ON; } + break; + case 5: { + LED1a = ON; LED1b = OFF; LED1c = ON; LED1d = ON; LED1e = OFF; LED1f = ON; LED1g = ON; } + break; + case 6: { + LED1a = ON; LED1b = OFF; LED1c = ON; LED1d = ON; LED1e = ON; LED1f = ON; LED1g = ON; } + break; + case 7: { + LED1a = ON; LED1b = ON; LED1c = ON; LED1d = OFF; LED1e = OFF; LED1f = OFF; LED1g = OFF; } + break; + case 8: { + LED1a = ON; LED1b = ON; LED1c = ON; LED1d = ON; LED1e = ON; LED1f = ON; LED1g = ON; } + break; + case 9: { + LED1a = ON; LED1b = ON; LED1c = ON; LED1d = ON; LED1e = OFF; LED1f = ON; LED1g = ON; } + break; + default: + break; // Do nothing + } // switch (hundreds) + } + else // (mode == GASMODE) + { + // Turn off hundreds digit in gas mode + LED1a = OFF; LED1b = OFF; LED1c = OFF; LED1d = OFF; LED1e = OFF; LED1f = OFF; LED1g = OFF; + } + switch (tens) { - case 0: - { - LED2a = ON; - LED2b = ON; - LED2c = ON; - LED2d = ON; - LED2e = ON; - LED2f = ON; - LED2g = OFF; - } + case 0: { + LED2a = ON; LED2b = ON; LED2c = ON; LED2d = ON; LED2e = ON; LED2f = ON; LED2g = OFF; } break; - case 1: - { - LED2a = OFF; - LED2b = ON; - LED2c = ON; - LED2d = OFF; - LED2e = OFF; - LED2f = OFF; - LED2g = OFF; - } + case 1: { + LED2a = OFF; LED2b = ON; LED2c = ON; LED2d = OFF; LED2e = OFF; LED2f = OFF; LED2g = OFF; } break; - case 2: - { - LED2a = ON; - LED2b = ON; - LED2c = OFF; - LED2d = ON; - LED2e = ON; - LED2f = OFF; - LED2g = ON; - } + case 2: { + LED2a = ON; LED2b = ON; LED2c = OFF; LED2d = ON; LED2e = ON; LED2f = OFF; LED2g = ON; } break; - case 3: - { - LED2a = ON; - LED2b = ON; - LED2c = ON; - LED2d = ON; - LED2e = OFF; - LED2f = OFF; - LED2g = ON; - } + case 3: { + LED2a = ON; LED2b = ON; LED2c = ON; LED2d = ON; LED2e = OFF; LED2f = OFF; LED2g = ON; } break; - case 4: - { - LED2a = OFF; - LED2b = ON; - LED2c = ON; - LED2d = OFF; - LED2e = OFF; - LED2f = ON; - LED2g = ON; - } + case 4: { + LED2a = OFF; LED2b = ON; LED2c = ON; LED2d = OFF; LED2e = OFF; LED2f = ON; LED2g = ON; } break; - case 5: - { - LED2a = ON; - LED2b = OFF; - LED2c = ON; - LED2d = ON; - LED2e = OFF; - LED2f = ON; - LED2g = ON; - } + case 5: { + LED2a = ON; LED2b = OFF; LED2c = ON; LED2d = ON; LED2e = OFF; LED2f = ON; LED2g = ON; } break; - case 6: - { - LED2a = ON; - LED2b = OFF; - LED2c = ON; - LED2d = ON; - LED2e = ON; - LED2f = ON; - LED2g = ON; - } + case 6: { + LED2a = ON; LED2b = OFF; LED2c = ON; LED2d = ON; LED2e = ON; LED2f = ON; LED2g = ON; } break; - case 7: - { - LED2a = ON; - LED2b = ON; - LED2c = ON; - LED2d = OFF; - LED2e = OFF; - LED2f = OFF; - LED2g = OFF; - } + case 7: { + LED2a = ON; LED2b = ON; LED2c = ON; LED2d = OFF; LED2e = OFF; LED2f = OFF; LED2g = OFF; } break; - case 8: - { - LED2a = ON; - LED2b = ON; - LED2c = ON; - LED2d = ON; - LED2e = ON; - LED2f = ON; - LED2g = ON; - } + case 8: { + LED2a = ON; LED2b = ON; LED2c = ON; LED2d = ON; LED2e = ON; LED2f = ON; LED2g = ON; } break; - case 9: - { - LED2a = ON; - LED2b = ON; - LED2c = ON; - LED2d = ON; - LED2e = OFF; - LED2f = ON; - LED2g = ON; - } + case 9: { + LED2a = ON; LED2b = ON; LED2c = ON; LED2d = ON; LED2e = OFF; LED2f = ON; LED2g = ON; } break; default: break; // Do nothing @@ -333,121 +180,40 @@ switch (ones) { - case 0: - { - LED3a = ON; - LED3b = ON; - LED3c = ON; - LED3d = ON; - LED3e = ON; - LED3f = ON; - LED3g = OFF; - } + case 0: { + LED3a = ON; LED3b = ON; LED3c = ON; LED3d = ON; LED3e = ON; LED3f = ON; LED3g = OFF; } break; - case 1: - { - LED3a = OFF; - LED3b = ON; - LED3c = ON; - LED3d = OFF; - LED3e = OFF; - LED3f = OFF; - LED3g = OFF; - } + case 1: { + LED3a = OFF; LED3b = ON; LED3c = ON; LED3d = OFF; LED3e = OFF; LED3f = OFF; LED3g = OFF; } break; - case 2: - { - LED3a = ON; - LED3b = ON; - LED3c = OFF; - LED3d = ON; - LED3e = ON; - LED3f = OFF; - LED3g = ON; - } + case 2: { + LED3a = ON; LED3b = ON; LED3c = OFF; LED3d = ON; LED3e = ON; LED3f = OFF; LED3g = ON; } break; - case 3: - { - LED3a = ON; - LED3b = ON; - LED3c = ON; - LED3d = ON; - LED3e = OFF; - LED3f = OFF; - LED3g = ON; - } + case 3: { + LED3a = ON; LED3b = ON; LED3c = ON; LED3d = ON; LED3e = OFF; LED3f = OFF; LED3g = ON; } break; - case 4: - { - LED3a = OFF; - LED3b = ON; - LED3c = ON; - LED3d = OFF; - LED3e = OFF; - LED3f = ON; - LED3g = ON; - } + case 4: { + LED3a = OFF; LED3b = ON; LED3c = ON; LED3d = OFF; LED3e = OFF; LED3f = ON; LED3g = ON; } break; - case 5: - { - LED3a = ON; - LED3b = OFF; - LED3c = ON; - LED3d = ON; - LED3e = OFF; - LED3f = ON; - LED3g = ON; - } + case 5: { + LED3a = ON; LED3b = OFF; LED3c = ON; LED3d = ON; LED3e = OFF; LED3f = ON; LED3g = ON; } break; - case 6: - { - LED3a = ON; - LED3b = OFF; - LED3c = ON; - LED3d = ON; - LED3e = ON; - LED3f = ON; - LED3g = ON; - } + case 6: { + LED3a = ON; LED3b = OFF; LED3c = ON; LED3d = ON; LED3e = ON; LED3f = ON; LED3g = ON; } break; - case 7: - { - LED3a = ON; - LED3b = ON; - LED3c = ON; - LED3d = OFF; - LED3e = OFF; - LED3f = OFF; - LED3g = OFF; - } + case 7: { + LED3a = ON; LED3b = ON; LED3c = ON; LED3d = OFF; LED3e = OFF; LED3f = OFF; LED3g = OFF; } break; - case 8: - { - LED3a = ON; - LED3b = ON; - LED3c = ON; - LED3d = ON; - LED3e = ON; - LED3f = ON; - LED3g = ON; - } + case 8: { + LED3a = ON; LED3b = ON; LED3c = ON; LED3d = ON; LED3e = ON; LED3f = ON; LED3g = ON; } break; - case 9: - { - LED3a = ON; - LED3b = ON; - LED3c = ON; - LED3d = ON; - LED3e = OFF; - LED3f = ON; - LED3g = ON; - } + case 9: { + LED3a = ON; LED3b = ON; LED3c = ON; LED3d = ON; LED3e = OFF; LED3f = ON; LED3g = ON; } break; default: break; // Do nothing } // switch (ones) - - } // if + } // if Display number } // LEDout() // Read quadrature encoder switch inputs from I/O port. @@ -469,73 +235,61 @@ // Quadrature counter-clockwise states: 00 -> 01 -> 11 -> 10 void sampleswitches() { -#ifdef AMPSMODE - led1 = !led1; -#endif + int quadcountold = quadcount; // Change gas select state only when button is depressed quadswitchnew = quadsw; - if (quadswitchnew == ON && quadswitchold == OFF) // Button pushed down + if (quadswitchnew == OFF && quadswitchold == ON) // Button pushed down, then released { - select++; - if (select > 2) // Only 2 LEDs enabled for UIMockup Rev 1 board - select = 1; -#ifndef AMPSMODE - gasselect++; - if (gasselect > 4) // Use mbed LEDs - gasselect = 1; -#endif + if (modetimer.read_ms() <= BUTTONTIMEOUT) + { + if (mode == AMPSMODE) + { + select++; + if (select > 3) // Cycle through 2 UIMockup LEDs + select = 2; + } + else // (mode == GASMODE) + { + gasselect++; + if (gasselect > 4) // Cycle through 4 mbed LEDs + gasselect = 1; + } + } + modetimer.stop(); + modetimer.reset(); + quadswitchold = quadswitchnew; + modechanged = FALSE; + } + else if (quadswitchnew == ON && quadswitchold == ON) // Button held down + { + if (modetimer.read_ms() == 0) + modetimer.start(); + else if (modetimer.read_ms() > BUTTONTIMEOUT) // Look for mode change + { + if (modechanged == FALSE) + { + if (mode == AMPSMODE) + { + mode = GASMODE; + pc.printf("Gas mode\n\r"); + } + else + { + mode = AMPSMODE; + pc.printf("Amps mode\n\r"); + } + modetimer.stop(); + modechanged = TRUE; + } + } quadswitchold = quadswitchnew; } - else if (quadswitchnew == ON && quadswitchold == ON) // Button held down - ; // Do nothing, since transition happened already + else if (quadswitchnew == ON) // Button depressed for the first time + quadswitchold = quadswitchnew; // Do nothing else if (quadswitchnew == OFF) // Button released quadswitchold = quadswitchnew; // Do nothing - // Update LEDs - if (select == 1) - { - LEDg2 = ON; - LEDg3 = OFF; - } - else // select == 2 - { - LEDg2 = OFF; - LEDg3 = ON; - } -#ifndef AMPSMODE - // Update mbed LEDs to simulate gas selection - if (gasselect == 1) - { - led1 = 1; - led2 = 0; - led3 = 0; - led4 = 0; - } - else if (gasselect == 2) - { - led1 = 0; - led2 = 1; - led3 = 0; - led4 = 0; - } - else if (gasselect == 3) - { - led1 = 0; - led2 = 0; - led3 = 1; - led4 = 0; - } - else // gasselect == 4 - { - led1 = 0; - led2 = 0; - led3 = 0; - led4 = 1; - } -#endif - - // Update 7-segment display in AMPs mode - quadold = quadnew; // Store current quad state + // Update 7-segment display quadnew = getquad(); // Get new quad state switch (quadold) { @@ -604,68 +358,102 @@ } // switch break; } // switch (quadold) - - #ifdef AMPSMODE - if (quadcount > MAXAMPS200) - quadcount = MAXAMPS200; // Clip at high end - else if (quadcount < MINAMPSET) - quadcount = MINAMPSET; // Clip at low end - #else - if (quadcount > MAXGASPRESS) - quadcount = MAXGASPRESS; // Clip at high end - else if (quadcount < MINGASPRESS) - quadcount = MINGASPRESS; // Clip at low end - #endif - - LEDout(quadcount); + quadold = quadnew; // Store current quad state -#ifdef AMPSMODE - led1 = !led1; -#endif + if (mode == AMPSMODE) + { + ampscount += (quadcountold - quadcount); + if (ampscount > MAXAMPS200) + ampscount = MAXAMPS200; // Clip at high end + else if (ampscount < MINAMPSET) + ampscount = MINAMPSET; // Clip at low end + if (select == 2) + { + LEDg2 = ON; LEDg3 = OFF; + } + else // select == 3 + { + LEDg2 = OFF; LEDg3 = ON; + } + switch (ampscount % 4) // Get 2 LSBs and display on mbed LEDs + { + case 0: + led1 = 1; led2 = 0; led3 = 0; led4 = 0; + break; + case 1: + led1 = 0; led2 = 1; led3 = 0; led4 = 0; + break; + case 2: + led1 = 0; led2 = 0; led3 = 1; led4 = 0; + break; + default: + led1 = 0; led2 = 0; led3 = 0; led4 = 1; + break; + } // switch + LEDout(ampscount); + //if (quadcount != quadcountold) pc.printf("%d amps\n\r", ampscount); + } + else // (mode == GASMODE) + { + gascount += (quadcountold - quadcount); + if (gascount > MAXGASPRESS) + gascount = MAXGASPRESS; // Clip at high end + else if (gascount < MINGASPRESS) + gascount = MINGASPRESS; // Clip at low end + // Update mbed LEDs to simulate gas selection + switch (gasselect) + { + case 1: { + led1 = 1; led2 = 0; led3 = 0; led4 = 0; } + break; + case 2: { + led1 = 0; led2 = 1; led3 = 0; led4 = 0; + LEDg2 = ON; LEDg3 = OFF; } + break; + case 3: { + led1 = 0; led2 = 0; led3 = 1; led4 = 0; + LEDg2 = OFF; LEDg3 = ON; } + break; + default: { // gasselect == 4 + led1 = 0; led2 = 0; led3 = 0; led4 = 1; } + break; + } + LEDout(gascount); + //if (quadcount != quadcountold) pc.printf("%d psi\n\r", gascount); + } // if (mode == GASMODE) } // sampleswitches() int main() { + pc.baud(19200); + pc.printf("\n\rConnected to mBed...\r\n"); + /****** Program Starts Here *******/ // Initialize variables quadnew = getquad(); // Get initial quadrature state - quadold = quadnew; - quadswitchold = OFF; - #ifdef AMPSMODE - quadcount = MAXAMPS200; - #else - quadcount = MAXGASPRESS; - #endif - select = 1; // Default mode/gas selection + quadold = quadnew; // Initialize state variables + quadswitchold = OFF; // Initialize state variables + mode = AMPSMODE; // Power up in AMPS selection mode + modetimer.stop(); + modetimer.reset(); + modechanged = FALSE; // Initialize state variables + select = 1; // Default discrete LED selection gasselect = 1; + quada.mode(PullUp); // Enable pullup resistors on switch inputs + quadb.mode(PullUp); + quadsw.mode(PullUp); + + ampscount = MAXAMPS200; + gascount = STARTGASPRESS; // Turn off all LEDs first, and let sampleswitches update everything - LED1a = OFF; - LED1b = OFF; - LED1c = OFF; - LED1d = OFF; - LED1e = OFF; - LED1f = OFF; - LED1g = OFF; - LED2a = OFF; - LED2b = OFF; - LED2c = OFF; - LED2d = OFF; - LED2e = OFF; - LED2f = OFF; - LED2g = OFF; - LED3a = OFF; - LED3b = OFF; - LED3c = OFF; - LED3d = OFF; - LED3e = OFF; - LED3f = OFF; - LED3g = OFF; - LEDg2 = OFF; - LEDg3 = OFF; + LED1a = OFF; LED1b = OFF; LED1c = OFF; LED1d = OFF; LED1e = OFF; LED1f = OFF; LED1g = OFF; + LED2a = OFF; LED2b = OFF; LED2c = OFF; LED2d = OFF; LED2e = OFF; LED2f = OFF; LED2g = OFF; + LED3a = OFF; LED3b = OFF; LED3c = OFF; LED3d = OFF; LED3e = OFF; LED3f = OFF; LED3g = OFF; + LEDg2 = OFF; LEDg3 = OFF; // Set up interrupt call - getswitches.attach_us(&sampleswitches, 25000); // setup getswitches to call sampleswitches every 25 ms + getswitches.attach_us(&sampleswitches, 10000); // setup getswitches to call sampleswitches every 10 ms while (1) {