Wayne Chin
/
UIMockup
Got quadrature decoders and mode switch working
main.cpp@1:fb60f76e33ee, 2010-10-25 (annotated)
- Committer:
- wac
- Date:
- Mon Oct 25 20:48:04 2010 +0000
- Revision:
- 1:fb60f76e33ee
- Parent:
- 0:7ae28d2c1e3e
0.2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wac | 0:7ae28d2c1e3e | 1 | // 9430 User Interface Mockup |
wac | 0:7ae28d2c1e3e | 2 | // |
wac | 0:7ae28d2c1e3e | 3 | // Read quadrature encoder and drive LEDs |
wac | 0:7ae28d2c1e3e | 4 | // Version 0.1 |
wac | 0:7ae28d2c1e3e | 5 | // Created by Wayne Chin |
wac | 0:7ae28d2c1e3e | 6 | // October 22, 2010 |
wac | 1:fb60f76e33ee | 7 | // Version 0.2 |
wac | 1:fb60f76e33ee | 8 | // Added amps/gas mode selections; confirmed quad decoder operation. |
wac | 1:fb60f76e33ee | 9 | // October 25, 2010 |
wac | 0:7ae28d2c1e3e | 10 | |
wac | 0:7ae28d2c1e3e | 11 | #include "mbed.h" |
wac | 0:7ae28d2c1e3e | 12 | |
wac | 0:7ae28d2c1e3e | 13 | #define TRUE 1 |
wac | 0:7ae28d2c1e3e | 14 | #define FALSE 0 |
wac | 0:7ae28d2c1e3e | 15 | #define ON 0 // LED drives are inverted |
wac | 0:7ae28d2c1e3e | 16 | #define OFF 1 // LED drives are inverted |
wac | 1:fb60f76e33ee | 17 | #define MAXAMPS200 200 // Max amps display |
wac | 1:fb60f76e33ee | 18 | #define MINAMPSET 25 // Min amps display |
wac | 1:fb60f76e33ee | 19 | #define MAXGASPRESS 99 // Max gas display |
wac | 1:fb60f76e33ee | 20 | #define STARTGASPRESS 75 |
wac | 0:7ae28d2c1e3e | 21 | #define MINGASPRESS 0 |
wac | 1:fb60f76e33ee | 22 | #define AMPSMODE 0 |
wac | 1:fb60f76e33ee | 23 | #define GASMODE 1 |
wac | 1:fb60f76e33ee | 24 | #define BUTTONTIMEOUT 1000 // Mode change timeout in milliseconds |
wac | 0:7ae28d2c1e3e | 25 | |
wac | 0:7ae28d2c1e3e | 26 | // Global variables |
wac | 0:7ae28d2c1e3e | 27 | int quadnew, quadold; // Quadrature decoding |
wac | 0:7ae28d2c1e3e | 28 | int quadcount; // Integer counter for quadrature encoder output |
wac | 1:fb60f76e33ee | 29 | int gascount, ampscount; // Separately keep track of gas and amps |
wac | 0:7ae28d2c1e3e | 30 | int select, gasselect; // Keep track of mode/gas selection LEDs |
wac | 0:7ae28d2c1e3e | 31 | int quadswitchnew, quadswitchold; // Debounce pushbutton states |
wac | 1:fb60f76e33ee | 32 | int mode; // Keep track of AMPS or GAS pressure display modes |
wac | 1:fb60f76e33ee | 33 | int modechanged; // Keep track of entry into mode change |
wac | 1:fb60f76e33ee | 34 | |
wac | 1:fb60f76e33ee | 35 | Serial pc(USBTX, USBRX); // tx, rx |
wac | 0:7ae28d2c1e3e | 36 | |
wac | 0:7ae28d2c1e3e | 37 | // mbed LEDs |
wac | 0:7ae28d2c1e3e | 38 | DigitalOut led1(LED1); |
wac | 0:7ae28d2c1e3e | 39 | DigitalOut led2(LED2); |
wac | 0:7ae28d2c1e3e | 40 | DigitalOut led3(LED3); |
wac | 0:7ae28d2c1e3e | 41 | DigitalOut led4(LED4); |
wac | 0:7ae28d2c1e3e | 42 | |
wac | 0:7ae28d2c1e3e | 43 | // Quadrature encoder inputs |
wac | 0:7ae28d2c1e3e | 44 | DigitalIn quada(p21); |
wac | 0:7ae28d2c1e3e | 45 | DigitalIn quadb(p22); |
wac | 0:7ae28d2c1e3e | 46 | DigitalIn quadsw(p30); |
wac | 0:7ae28d2c1e3e | 47 | |
wac | 0:7ae28d2c1e3e | 48 | // 7-segment display LEDs |
wac | 0:7ae28d2c1e3e | 49 | DigitalOut LED1a(p23); // Hundreds digit |
wac | 0:7ae28d2c1e3e | 50 | DigitalOut LED1b(p24); |
wac | 0:7ae28d2c1e3e | 51 | DigitalOut LED1c(p25); |
wac | 0:7ae28d2c1e3e | 52 | DigitalOut LED1d(p26); |
wac | 0:7ae28d2c1e3e | 53 | DigitalOut LED1e(p27); |
wac | 0:7ae28d2c1e3e | 54 | DigitalOut LED1f(p28); |
wac | 0:7ae28d2c1e3e | 55 | DigitalOut LED1g(p29); |
wac | 0:7ae28d2c1e3e | 56 | DigitalOut LED2a(p13); // Tens digit |
wac | 0:7ae28d2c1e3e | 57 | DigitalOut LED2b(p14); |
wac | 0:7ae28d2c1e3e | 58 | DigitalOut LED2c(p15); |
wac | 0:7ae28d2c1e3e | 59 | DigitalOut LED2d(p16); |
wac | 0:7ae28d2c1e3e | 60 | DigitalOut LED2e(p17); |
wac | 0:7ae28d2c1e3e | 61 | DigitalOut LED2f(p18); |
wac | 0:7ae28d2c1e3e | 62 | DigitalOut LED2g(p19); |
wac | 0:7ae28d2c1e3e | 63 | DigitalOut LED3a(p5); // Ones digit |
wac | 0:7ae28d2c1e3e | 64 | DigitalOut LED3b(p6); |
wac | 0:7ae28d2c1e3e | 65 | DigitalOut LED3c(p7); |
wac | 0:7ae28d2c1e3e | 66 | DigitalOut LED3d(p8); |
wac | 0:7ae28d2c1e3e | 67 | DigitalOut LED3e(p9); |
wac | 0:7ae28d2c1e3e | 68 | DigitalOut LED3f(p10); |
wac | 0:7ae28d2c1e3e | 69 | DigitalOut LED3g(p11); |
wac | 0:7ae28d2c1e3e | 70 | |
wac | 0:7ae28d2c1e3e | 71 | // Gas selection LEDs |
wac | 0:7ae28d2c1e3e | 72 | //DigitalOut LEDg1(p20); // Not wired up |
wac | 0:7ae28d2c1e3e | 73 | DigitalOut LEDg2(p20); |
wac | 0:7ae28d2c1e3e | 74 | DigitalOut LEDg3(p12); |
wac | 0:7ae28d2c1e3e | 75 | //DigitalOut LEDg4(p20); // Not wired up |
wac | 0:7ae28d2c1e3e | 76 | |
wac | 0:7ae28d2c1e3e | 77 | // Interrupt |
wac | 0:7ae28d2c1e3e | 78 | Ticker getswitches; // Periodic function call used to sample switches |
wac | 1:fb60f76e33ee | 79 | Timer modetimer; // Used for press-and-hold function |
wac | 0:7ae28d2c1e3e | 80 | |
wac | 0:7ae28d2c1e3e | 81 | // Take integer input and drive 3 digit 7-segment display and toggle among gas select LEDs |
wac | 0:7ae28d2c1e3e | 82 | void LEDout(int number) |
wac | 0:7ae28d2c1e3e | 83 | { |
wac | 0:7ae28d2c1e3e | 84 | int hundreds, tens, ones; |
wac | 1:fb60f76e33ee | 85 | if (mode == AMPSMODE && number > 999) // Throw out numbers greater than 999 |
wac | 1:fb60f76e33ee | 86 | return; |
wac | 1:fb60f76e33ee | 87 | else if (mode == GASMODE && number > 99) // Throw out numbers greater than 99 |
wac | 0:7ae28d2c1e3e | 88 | return; |
wac | 0:7ae28d2c1e3e | 89 | else if (number < 0) // Throw out numbers less than 0 |
wac | 0:7ae28d2c1e3e | 90 | return; |
wac | 1:fb60f76e33ee | 91 | else // Display number |
wac | 0:7ae28d2c1e3e | 92 | { |
wac | 0:7ae28d2c1e3e | 93 | // Extract hundreds digit |
wac | 0:7ae28d2c1e3e | 94 | hundreds = number - (number / 100); |
wac | 0:7ae28d2c1e3e | 95 | // Extract tens digit |
wac | 0:7ae28d2c1e3e | 96 | tens = number - hundreds; |
wac | 0:7ae28d2c1e3e | 97 | tens = tens - (tens / 10); |
wac | 0:7ae28d2c1e3e | 98 | // Extract ones digit |
wac | 0:7ae28d2c1e3e | 99 | ones = number - hundreds - tens; |
wac | 0:7ae28d2c1e3e | 100 | |
wac | 1:fb60f76e33ee | 101 | if (mode == AMPSMODE) |
wac | 0:7ae28d2c1e3e | 102 | { |
wac | 1:fb60f76e33ee | 103 | switch (hundreds) |
wac | 1:fb60f76e33ee | 104 | { |
wac | 1:fb60f76e33ee | 105 | case 0: { |
wac | 1:fb60f76e33ee | 106 | LED1a = ON; LED1b = ON; LED1c = ON; LED1d = ON; LED1e = ON; LED1f = ON; LED1g = OFF; } |
wac | 1:fb60f76e33ee | 107 | break; |
wac | 1:fb60f76e33ee | 108 | case 1: { |
wac | 1:fb60f76e33ee | 109 | LED1a = OFF; LED1b = ON; LED1c = ON; LED1d = OFF; LED1e = OFF; LED1f = OFF; LED1g = OFF; } |
wac | 1:fb60f76e33ee | 110 | break; |
wac | 1:fb60f76e33ee | 111 | case 2: { |
wac | 1:fb60f76e33ee | 112 | LED1a = ON; LED1b = ON; LED1c = OFF; LED1d = ON; LED1e = ON; LED1f = OFF; LED1g = ON; } |
wac | 1:fb60f76e33ee | 113 | break; |
wac | 1:fb60f76e33ee | 114 | case 3: { |
wac | 1:fb60f76e33ee | 115 | LED1a = ON; LED1b = ON; LED1c = ON; LED1d = ON; LED1e = OFF; LED1f = OFF; LED1g = ON; } |
wac | 1:fb60f76e33ee | 116 | break; |
wac | 1:fb60f76e33ee | 117 | case 4: { |
wac | 1:fb60f76e33ee | 118 | LED1a = OFF; LED1b = ON; LED1c = ON; LED1d = OFF; LED1e = OFF; LED1f = ON; LED1g = ON; } |
wac | 1:fb60f76e33ee | 119 | break; |
wac | 1:fb60f76e33ee | 120 | case 5: { |
wac | 1:fb60f76e33ee | 121 | LED1a = ON; LED1b = OFF; LED1c = ON; LED1d = ON; LED1e = OFF; LED1f = ON; LED1g = ON; } |
wac | 1:fb60f76e33ee | 122 | break; |
wac | 1:fb60f76e33ee | 123 | case 6: { |
wac | 1:fb60f76e33ee | 124 | LED1a = ON; LED1b = OFF; LED1c = ON; LED1d = ON; LED1e = ON; LED1f = ON; LED1g = ON; } |
wac | 1:fb60f76e33ee | 125 | break; |
wac | 1:fb60f76e33ee | 126 | case 7: { |
wac | 1:fb60f76e33ee | 127 | LED1a = ON; LED1b = ON; LED1c = ON; LED1d = OFF; LED1e = OFF; LED1f = OFF; LED1g = OFF; } |
wac | 1:fb60f76e33ee | 128 | break; |
wac | 1:fb60f76e33ee | 129 | case 8: { |
wac | 1:fb60f76e33ee | 130 | LED1a = ON; LED1b = ON; LED1c = ON; LED1d = ON; LED1e = ON; LED1f = ON; LED1g = ON; } |
wac | 1:fb60f76e33ee | 131 | break; |
wac | 1:fb60f76e33ee | 132 | case 9: { |
wac | 1:fb60f76e33ee | 133 | LED1a = ON; LED1b = ON; LED1c = ON; LED1d = ON; LED1e = OFF; LED1f = ON; LED1g = ON; } |
wac | 1:fb60f76e33ee | 134 | break; |
wac | 1:fb60f76e33ee | 135 | default: |
wac | 1:fb60f76e33ee | 136 | break; // Do nothing |
wac | 1:fb60f76e33ee | 137 | } // switch (hundreds) |
wac | 1:fb60f76e33ee | 138 | } |
wac | 1:fb60f76e33ee | 139 | else // (mode == GASMODE) |
wac | 1:fb60f76e33ee | 140 | { |
wac | 1:fb60f76e33ee | 141 | // Turn off hundreds digit in gas mode |
wac | 1:fb60f76e33ee | 142 | LED1a = OFF; LED1b = OFF; LED1c = OFF; LED1d = OFF; LED1e = OFF; LED1f = OFF; LED1g = OFF; |
wac | 1:fb60f76e33ee | 143 | } |
wac | 1:fb60f76e33ee | 144 | |
wac | 0:7ae28d2c1e3e | 145 | switch (tens) |
wac | 0:7ae28d2c1e3e | 146 | { |
wac | 1:fb60f76e33ee | 147 | case 0: { |
wac | 1:fb60f76e33ee | 148 | LED2a = ON; LED2b = ON; LED2c = ON; LED2d = ON; LED2e = ON; LED2f = ON; LED2g = OFF; } |
wac | 0:7ae28d2c1e3e | 149 | break; |
wac | 1:fb60f76e33ee | 150 | case 1: { |
wac | 1:fb60f76e33ee | 151 | LED2a = OFF; LED2b = ON; LED2c = ON; LED2d = OFF; LED2e = OFF; LED2f = OFF; LED2g = OFF; } |
wac | 0:7ae28d2c1e3e | 152 | break; |
wac | 1:fb60f76e33ee | 153 | case 2: { |
wac | 1:fb60f76e33ee | 154 | LED2a = ON; LED2b = ON; LED2c = OFF; LED2d = ON; LED2e = ON; LED2f = OFF; LED2g = ON; } |
wac | 0:7ae28d2c1e3e | 155 | break; |
wac | 1:fb60f76e33ee | 156 | case 3: { |
wac | 1:fb60f76e33ee | 157 | LED2a = ON; LED2b = ON; LED2c = ON; LED2d = ON; LED2e = OFF; LED2f = OFF; LED2g = ON; } |
wac | 0:7ae28d2c1e3e | 158 | break; |
wac | 1:fb60f76e33ee | 159 | case 4: { |
wac | 1:fb60f76e33ee | 160 | LED2a = OFF; LED2b = ON; LED2c = ON; LED2d = OFF; LED2e = OFF; LED2f = ON; LED2g = ON; } |
wac | 0:7ae28d2c1e3e | 161 | break; |
wac | 1:fb60f76e33ee | 162 | case 5: { |
wac | 1:fb60f76e33ee | 163 | LED2a = ON; LED2b = OFF; LED2c = ON; LED2d = ON; LED2e = OFF; LED2f = ON; LED2g = ON; } |
wac | 0:7ae28d2c1e3e | 164 | break; |
wac | 1:fb60f76e33ee | 165 | case 6: { |
wac | 1:fb60f76e33ee | 166 | LED2a = ON; LED2b = OFF; LED2c = ON; LED2d = ON; LED2e = ON; LED2f = ON; LED2g = ON; } |
wac | 0:7ae28d2c1e3e | 167 | break; |
wac | 1:fb60f76e33ee | 168 | case 7: { |
wac | 1:fb60f76e33ee | 169 | LED2a = ON; LED2b = ON; LED2c = ON; LED2d = OFF; LED2e = OFF; LED2f = OFF; LED2g = OFF; } |
wac | 0:7ae28d2c1e3e | 170 | break; |
wac | 1:fb60f76e33ee | 171 | case 8: { |
wac | 1:fb60f76e33ee | 172 | LED2a = ON; LED2b = ON; LED2c = ON; LED2d = ON; LED2e = ON; LED2f = ON; LED2g = ON; } |
wac | 0:7ae28d2c1e3e | 173 | break; |
wac | 1:fb60f76e33ee | 174 | case 9: { |
wac | 1:fb60f76e33ee | 175 | LED2a = ON; LED2b = ON; LED2c = ON; LED2d = ON; LED2e = OFF; LED2f = ON; LED2g = ON; } |
wac | 0:7ae28d2c1e3e | 176 | break; |
wac | 0:7ae28d2c1e3e | 177 | default: |
wac | 0:7ae28d2c1e3e | 178 | break; // Do nothing |
wac | 0:7ae28d2c1e3e | 179 | } // switch (tens) |
wac | 0:7ae28d2c1e3e | 180 | |
wac | 0:7ae28d2c1e3e | 181 | switch (ones) |
wac | 0:7ae28d2c1e3e | 182 | { |
wac | 1:fb60f76e33ee | 183 | case 0: { |
wac | 1:fb60f76e33ee | 184 | LED3a = ON; LED3b = ON; LED3c = ON; LED3d = ON; LED3e = ON; LED3f = ON; LED3g = OFF; } |
wac | 0:7ae28d2c1e3e | 185 | break; |
wac | 1:fb60f76e33ee | 186 | case 1: { |
wac | 1:fb60f76e33ee | 187 | LED3a = OFF; LED3b = ON; LED3c = ON; LED3d = OFF; LED3e = OFF; LED3f = OFF; LED3g = OFF; } |
wac | 0:7ae28d2c1e3e | 188 | break; |
wac | 1:fb60f76e33ee | 189 | case 2: { |
wac | 1:fb60f76e33ee | 190 | LED3a = ON; LED3b = ON; LED3c = OFF; LED3d = ON; LED3e = ON; LED3f = OFF; LED3g = ON; } |
wac | 0:7ae28d2c1e3e | 191 | break; |
wac | 1:fb60f76e33ee | 192 | case 3: { |
wac | 1:fb60f76e33ee | 193 | LED3a = ON; LED3b = ON; LED3c = ON; LED3d = ON; LED3e = OFF; LED3f = OFF; LED3g = ON; } |
wac | 0:7ae28d2c1e3e | 194 | break; |
wac | 1:fb60f76e33ee | 195 | case 4: { |
wac | 1:fb60f76e33ee | 196 | LED3a = OFF; LED3b = ON; LED3c = ON; LED3d = OFF; LED3e = OFF; LED3f = ON; LED3g = ON; } |
wac | 0:7ae28d2c1e3e | 197 | break; |
wac | 1:fb60f76e33ee | 198 | case 5: { |
wac | 1:fb60f76e33ee | 199 | LED3a = ON; LED3b = OFF; LED3c = ON; LED3d = ON; LED3e = OFF; LED3f = ON; LED3g = ON; } |
wac | 0:7ae28d2c1e3e | 200 | break; |
wac | 1:fb60f76e33ee | 201 | case 6: { |
wac | 1:fb60f76e33ee | 202 | LED3a = ON; LED3b = OFF; LED3c = ON; LED3d = ON; LED3e = ON; LED3f = ON; LED3g = ON; } |
wac | 0:7ae28d2c1e3e | 203 | break; |
wac | 1:fb60f76e33ee | 204 | case 7: { |
wac | 1:fb60f76e33ee | 205 | LED3a = ON; LED3b = ON; LED3c = ON; LED3d = OFF; LED3e = OFF; LED3f = OFF; LED3g = OFF; } |
wac | 0:7ae28d2c1e3e | 206 | break; |
wac | 1:fb60f76e33ee | 207 | case 8: { |
wac | 1:fb60f76e33ee | 208 | LED3a = ON; LED3b = ON; LED3c = ON; LED3d = ON; LED3e = ON; LED3f = ON; LED3g = ON; } |
wac | 0:7ae28d2c1e3e | 209 | break; |
wac | 1:fb60f76e33ee | 210 | case 9: { |
wac | 1:fb60f76e33ee | 211 | LED3a = ON; LED3b = ON; LED3c = ON; LED3d = ON; LED3e = OFF; LED3f = ON; LED3g = ON; } |
wac | 0:7ae28d2c1e3e | 212 | break; |
wac | 0:7ae28d2c1e3e | 213 | default: |
wac | 0:7ae28d2c1e3e | 214 | break; // Do nothing |
wac | 0:7ae28d2c1e3e | 215 | } // switch (ones) |
wac | 1:fb60f76e33ee | 216 | } // if Display number |
wac | 0:7ae28d2c1e3e | 217 | } // LEDout() |
wac | 0:7ae28d2c1e3e | 218 | |
wac | 0:7ae28d2c1e3e | 219 | // Read quadrature encoder switch inputs from I/O port. |
wac | 0:7ae28d2c1e3e | 220 | // Return 2-bit value with (quada,quadb). |
wac | 0:7ae28d2c1e3e | 221 | int getquad() |
wac | 0:7ae28d2c1e3e | 222 | { |
wac | 0:7ae28d2c1e3e | 223 | if (quada == 0 && quadb == 0) // (0,0) |
wac | 0:7ae28d2c1e3e | 224 | return(0); |
wac | 0:7ae28d2c1e3e | 225 | else if (quada == 1 && quadb == 0) // Clockwise from (0,0) |
wac | 0:7ae28d2c1e3e | 226 | return(2); |
wac | 0:7ae28d2c1e3e | 227 | else if (quada == 1 && quadb == 1) // Clockwise from (1,0) |
wac | 0:7ae28d2c1e3e | 228 | return(3); |
wac | 0:7ae28d2c1e3e | 229 | else // Clockwise from (1,1) |
wac | 0:7ae28d2c1e3e | 230 | return(1); |
wac | 0:7ae28d2c1e3e | 231 | } // getquad() |
wac | 0:7ae28d2c1e3e | 232 | |
wac | 0:7ae28d2c1e3e | 233 | // Read quadrature encoder, update counter variable, and update LEDs |
wac | 0:7ae28d2c1e3e | 234 | // Quadrature clockwise states: 00 -> 10 -> 11 -> 01 |
wac | 0:7ae28d2c1e3e | 235 | // Quadrature counter-clockwise states: 00 -> 01 -> 11 -> 10 |
wac | 0:7ae28d2c1e3e | 236 | void sampleswitches() |
wac | 0:7ae28d2c1e3e | 237 | { |
wac | 1:fb60f76e33ee | 238 | int quadcountold = quadcount; |
wac | 0:7ae28d2c1e3e | 239 | // Change gas select state only when button is depressed |
wac | 0:7ae28d2c1e3e | 240 | quadswitchnew = quadsw; |
wac | 1:fb60f76e33ee | 241 | if (quadswitchnew == OFF && quadswitchold == ON) // Button pushed down, then released |
wac | 0:7ae28d2c1e3e | 242 | { |
wac | 1:fb60f76e33ee | 243 | if (modetimer.read_ms() <= BUTTONTIMEOUT) |
wac | 1:fb60f76e33ee | 244 | { |
wac | 1:fb60f76e33ee | 245 | if (mode == AMPSMODE) |
wac | 1:fb60f76e33ee | 246 | { |
wac | 1:fb60f76e33ee | 247 | select++; |
wac | 1:fb60f76e33ee | 248 | if (select > 3) // Cycle through 2 UIMockup LEDs |
wac | 1:fb60f76e33ee | 249 | select = 2; |
wac | 1:fb60f76e33ee | 250 | } |
wac | 1:fb60f76e33ee | 251 | else // (mode == GASMODE) |
wac | 1:fb60f76e33ee | 252 | { |
wac | 1:fb60f76e33ee | 253 | gasselect++; |
wac | 1:fb60f76e33ee | 254 | if (gasselect > 4) // Cycle through 4 mbed LEDs |
wac | 1:fb60f76e33ee | 255 | gasselect = 1; |
wac | 1:fb60f76e33ee | 256 | } |
wac | 1:fb60f76e33ee | 257 | } |
wac | 1:fb60f76e33ee | 258 | modetimer.stop(); |
wac | 1:fb60f76e33ee | 259 | modetimer.reset(); |
wac | 1:fb60f76e33ee | 260 | quadswitchold = quadswitchnew; |
wac | 1:fb60f76e33ee | 261 | modechanged = FALSE; |
wac | 1:fb60f76e33ee | 262 | } |
wac | 1:fb60f76e33ee | 263 | else if (quadswitchnew == ON && quadswitchold == ON) // Button held down |
wac | 1:fb60f76e33ee | 264 | { |
wac | 1:fb60f76e33ee | 265 | if (modetimer.read_ms() == 0) |
wac | 1:fb60f76e33ee | 266 | modetimer.start(); |
wac | 1:fb60f76e33ee | 267 | else if (modetimer.read_ms() > BUTTONTIMEOUT) // Look for mode change |
wac | 1:fb60f76e33ee | 268 | { |
wac | 1:fb60f76e33ee | 269 | if (modechanged == FALSE) |
wac | 1:fb60f76e33ee | 270 | { |
wac | 1:fb60f76e33ee | 271 | if (mode == AMPSMODE) |
wac | 1:fb60f76e33ee | 272 | { |
wac | 1:fb60f76e33ee | 273 | mode = GASMODE; |
wac | 1:fb60f76e33ee | 274 | pc.printf("Gas mode\n\r"); |
wac | 1:fb60f76e33ee | 275 | } |
wac | 1:fb60f76e33ee | 276 | else |
wac | 1:fb60f76e33ee | 277 | { |
wac | 1:fb60f76e33ee | 278 | mode = AMPSMODE; |
wac | 1:fb60f76e33ee | 279 | pc.printf("Amps mode\n\r"); |
wac | 1:fb60f76e33ee | 280 | } |
wac | 1:fb60f76e33ee | 281 | modetimer.stop(); |
wac | 1:fb60f76e33ee | 282 | modechanged = TRUE; |
wac | 1:fb60f76e33ee | 283 | } |
wac | 1:fb60f76e33ee | 284 | } |
wac | 0:7ae28d2c1e3e | 285 | quadswitchold = quadswitchnew; |
wac | 0:7ae28d2c1e3e | 286 | } |
wac | 1:fb60f76e33ee | 287 | else if (quadswitchnew == ON) // Button depressed for the first time |
wac | 1:fb60f76e33ee | 288 | quadswitchold = quadswitchnew; // Do nothing |
wac | 0:7ae28d2c1e3e | 289 | else if (quadswitchnew == OFF) // Button released |
wac | 0:7ae28d2c1e3e | 290 | quadswitchold = quadswitchnew; // Do nothing |
wac | 0:7ae28d2c1e3e | 291 | |
wac | 1:fb60f76e33ee | 292 | // Update 7-segment display |
wac | 0:7ae28d2c1e3e | 293 | quadnew = getquad(); // Get new quad state |
wac | 0:7ae28d2c1e3e | 294 | switch (quadold) |
wac | 0:7ae28d2c1e3e | 295 | { |
wac | 0:7ae28d2c1e3e | 296 | case 0: |
wac | 0:7ae28d2c1e3e | 297 | switch (quadnew) |
wac | 0:7ae28d2c1e3e | 298 | { |
wac | 0:7ae28d2c1e3e | 299 | case 0: |
wac | 0:7ae28d2c1e3e | 300 | break; |
wac | 0:7ae28d2c1e3e | 301 | case 2: |
wac | 0:7ae28d2c1e3e | 302 | quadcount++; |
wac | 0:7ae28d2c1e3e | 303 | break; |
wac | 0:7ae28d2c1e3e | 304 | case 1: |
wac | 0:7ae28d2c1e3e | 305 | quadcount--; |
wac | 0:7ae28d2c1e3e | 306 | break; |
wac | 0:7ae28d2c1e3e | 307 | case 3: |
wac | 0:7ae28d2c1e3e | 308 | ; // Missed a count, so do nothing |
wac | 0:7ae28d2c1e3e | 309 | break; |
wac | 0:7ae28d2c1e3e | 310 | } // switch |
wac | 0:7ae28d2c1e3e | 311 | break; |
wac | 0:7ae28d2c1e3e | 312 | case 1: |
wac | 0:7ae28d2c1e3e | 313 | switch (quadnew) |
wac | 0:7ae28d2c1e3e | 314 | { |
wac | 0:7ae28d2c1e3e | 315 | case 1: |
wac | 0:7ae28d2c1e3e | 316 | break; |
wac | 0:7ae28d2c1e3e | 317 | case 0: |
wac | 0:7ae28d2c1e3e | 318 | quadcount++; |
wac | 0:7ae28d2c1e3e | 319 | break; |
wac | 0:7ae28d2c1e3e | 320 | case 3: |
wac | 0:7ae28d2c1e3e | 321 | quadcount--; |
wac | 0:7ae28d2c1e3e | 322 | break; |
wac | 0:7ae28d2c1e3e | 323 | case 2: |
wac | 0:7ae28d2c1e3e | 324 | ; // Missed a count, so do nothing |
wac | 0:7ae28d2c1e3e | 325 | break; |
wac | 0:7ae28d2c1e3e | 326 | } //switch |
wac | 0:7ae28d2c1e3e | 327 | break; |
wac | 0:7ae28d2c1e3e | 328 | case 2: |
wac | 0:7ae28d2c1e3e | 329 | switch (quadnew) |
wac | 0:7ae28d2c1e3e | 330 | { |
wac | 0:7ae28d2c1e3e | 331 | case 2: |
wac | 0:7ae28d2c1e3e | 332 | break; |
wac | 0:7ae28d2c1e3e | 333 | case 3: |
wac | 0:7ae28d2c1e3e | 334 | quadcount++; |
wac | 0:7ae28d2c1e3e | 335 | break; |
wac | 0:7ae28d2c1e3e | 336 | case 0: |
wac | 0:7ae28d2c1e3e | 337 | quadcount--; |
wac | 0:7ae28d2c1e3e | 338 | break; |
wac | 0:7ae28d2c1e3e | 339 | case 1: |
wac | 0:7ae28d2c1e3e | 340 | ; // Missed a count, so do nothing |
wac | 0:7ae28d2c1e3e | 341 | break; |
wac | 0:7ae28d2c1e3e | 342 | } // switch |
wac | 0:7ae28d2c1e3e | 343 | break; |
wac | 0:7ae28d2c1e3e | 344 | case 3: |
wac | 0:7ae28d2c1e3e | 345 | switch (quadnew) |
wac | 0:7ae28d2c1e3e | 346 | { |
wac | 0:7ae28d2c1e3e | 347 | case 3: |
wac | 0:7ae28d2c1e3e | 348 | break; |
wac | 0:7ae28d2c1e3e | 349 | case 1: |
wac | 0:7ae28d2c1e3e | 350 | quadcount++; |
wac | 0:7ae28d2c1e3e | 351 | break; |
wac | 0:7ae28d2c1e3e | 352 | case 2: |
wac | 0:7ae28d2c1e3e | 353 | quadcount--; |
wac | 0:7ae28d2c1e3e | 354 | break; |
wac | 0:7ae28d2c1e3e | 355 | case 0: |
wac | 0:7ae28d2c1e3e | 356 | ; // Missed a count, so do nothing |
wac | 0:7ae28d2c1e3e | 357 | break; |
wac | 0:7ae28d2c1e3e | 358 | } // switch |
wac | 0:7ae28d2c1e3e | 359 | break; |
wac | 0:7ae28d2c1e3e | 360 | } // switch (quadold) |
wac | 1:fb60f76e33ee | 361 | quadold = quadnew; // Store current quad state |
wac | 0:7ae28d2c1e3e | 362 | |
wac | 1:fb60f76e33ee | 363 | if (mode == AMPSMODE) |
wac | 1:fb60f76e33ee | 364 | { |
wac | 1:fb60f76e33ee | 365 | ampscount += (quadcountold - quadcount); |
wac | 1:fb60f76e33ee | 366 | if (ampscount > MAXAMPS200) |
wac | 1:fb60f76e33ee | 367 | ampscount = MAXAMPS200; // Clip at high end |
wac | 1:fb60f76e33ee | 368 | else if (ampscount < MINAMPSET) |
wac | 1:fb60f76e33ee | 369 | ampscount = MINAMPSET; // Clip at low end |
wac | 1:fb60f76e33ee | 370 | if (select == 2) |
wac | 1:fb60f76e33ee | 371 | { |
wac | 1:fb60f76e33ee | 372 | LEDg2 = ON; LEDg3 = OFF; |
wac | 1:fb60f76e33ee | 373 | } |
wac | 1:fb60f76e33ee | 374 | else // select == 3 |
wac | 1:fb60f76e33ee | 375 | { |
wac | 1:fb60f76e33ee | 376 | LEDg2 = OFF; LEDg3 = ON; |
wac | 1:fb60f76e33ee | 377 | } |
wac | 1:fb60f76e33ee | 378 | switch (ampscount % 4) // Get 2 LSBs and display on mbed LEDs |
wac | 1:fb60f76e33ee | 379 | { |
wac | 1:fb60f76e33ee | 380 | case 0: |
wac | 1:fb60f76e33ee | 381 | led1 = 1; led2 = 0; led3 = 0; led4 = 0; |
wac | 1:fb60f76e33ee | 382 | break; |
wac | 1:fb60f76e33ee | 383 | case 1: |
wac | 1:fb60f76e33ee | 384 | led1 = 0; led2 = 1; led3 = 0; led4 = 0; |
wac | 1:fb60f76e33ee | 385 | break; |
wac | 1:fb60f76e33ee | 386 | case 2: |
wac | 1:fb60f76e33ee | 387 | led1 = 0; led2 = 0; led3 = 1; led4 = 0; |
wac | 1:fb60f76e33ee | 388 | break; |
wac | 1:fb60f76e33ee | 389 | default: |
wac | 1:fb60f76e33ee | 390 | led1 = 0; led2 = 0; led3 = 0; led4 = 1; |
wac | 1:fb60f76e33ee | 391 | break; |
wac | 1:fb60f76e33ee | 392 | } // switch |
wac | 1:fb60f76e33ee | 393 | LEDout(ampscount); |
wac | 1:fb60f76e33ee | 394 | //if (quadcount != quadcountold) pc.printf("%d amps\n\r", ampscount); |
wac | 1:fb60f76e33ee | 395 | } |
wac | 1:fb60f76e33ee | 396 | else // (mode == GASMODE) |
wac | 1:fb60f76e33ee | 397 | { |
wac | 1:fb60f76e33ee | 398 | gascount += (quadcountold - quadcount); |
wac | 1:fb60f76e33ee | 399 | if (gascount > MAXGASPRESS) |
wac | 1:fb60f76e33ee | 400 | gascount = MAXGASPRESS; // Clip at high end |
wac | 1:fb60f76e33ee | 401 | else if (gascount < MINGASPRESS) |
wac | 1:fb60f76e33ee | 402 | gascount = MINGASPRESS; // Clip at low end |
wac | 1:fb60f76e33ee | 403 | // Update mbed LEDs to simulate gas selection |
wac | 1:fb60f76e33ee | 404 | switch (gasselect) |
wac | 1:fb60f76e33ee | 405 | { |
wac | 1:fb60f76e33ee | 406 | case 1: { |
wac | 1:fb60f76e33ee | 407 | led1 = 1; led2 = 0; led3 = 0; led4 = 0; } |
wac | 1:fb60f76e33ee | 408 | break; |
wac | 1:fb60f76e33ee | 409 | case 2: { |
wac | 1:fb60f76e33ee | 410 | led1 = 0; led2 = 1; led3 = 0; led4 = 0; |
wac | 1:fb60f76e33ee | 411 | LEDg2 = ON; LEDg3 = OFF; } |
wac | 1:fb60f76e33ee | 412 | break; |
wac | 1:fb60f76e33ee | 413 | case 3: { |
wac | 1:fb60f76e33ee | 414 | led1 = 0; led2 = 0; led3 = 1; led4 = 0; |
wac | 1:fb60f76e33ee | 415 | LEDg2 = OFF; LEDg3 = ON; } |
wac | 1:fb60f76e33ee | 416 | break; |
wac | 1:fb60f76e33ee | 417 | default: { // gasselect == 4 |
wac | 1:fb60f76e33ee | 418 | led1 = 0; led2 = 0; led3 = 0; led4 = 1; } |
wac | 1:fb60f76e33ee | 419 | break; |
wac | 1:fb60f76e33ee | 420 | } |
wac | 1:fb60f76e33ee | 421 | LEDout(gascount); |
wac | 1:fb60f76e33ee | 422 | //if (quadcount != quadcountold) pc.printf("%d psi\n\r", gascount); |
wac | 1:fb60f76e33ee | 423 | } // if (mode == GASMODE) |
wac | 0:7ae28d2c1e3e | 424 | } // sampleswitches() |
wac | 0:7ae28d2c1e3e | 425 | |
wac | 0:7ae28d2c1e3e | 426 | int main() |
wac | 0:7ae28d2c1e3e | 427 | { |
wac | 1:fb60f76e33ee | 428 | pc.baud(19200); |
wac | 1:fb60f76e33ee | 429 | pc.printf("\n\rConnected to mBed...\r\n"); |
wac | 1:fb60f76e33ee | 430 | |
wac | 0:7ae28d2c1e3e | 431 | /****** Program Starts Here *******/ |
wac | 0:7ae28d2c1e3e | 432 | // Initialize variables |
wac | 0:7ae28d2c1e3e | 433 | quadnew = getquad(); // Get initial quadrature state |
wac | 1:fb60f76e33ee | 434 | quadold = quadnew; // Initialize state variables |
wac | 1:fb60f76e33ee | 435 | quadswitchold = OFF; // Initialize state variables |
wac | 1:fb60f76e33ee | 436 | mode = AMPSMODE; // Power up in AMPS selection mode |
wac | 1:fb60f76e33ee | 437 | modetimer.stop(); |
wac | 1:fb60f76e33ee | 438 | modetimer.reset(); |
wac | 1:fb60f76e33ee | 439 | modechanged = FALSE; // Initialize state variables |
wac | 1:fb60f76e33ee | 440 | select = 1; // Default discrete LED selection |
wac | 0:7ae28d2c1e3e | 441 | gasselect = 1; |
wac | 1:fb60f76e33ee | 442 | quada.mode(PullUp); // Enable pullup resistors on switch inputs |
wac | 1:fb60f76e33ee | 443 | quadb.mode(PullUp); |
wac | 1:fb60f76e33ee | 444 | quadsw.mode(PullUp); |
wac | 1:fb60f76e33ee | 445 | |
wac | 1:fb60f76e33ee | 446 | ampscount = MAXAMPS200; |
wac | 1:fb60f76e33ee | 447 | gascount = STARTGASPRESS; |
wac | 0:7ae28d2c1e3e | 448 | |
wac | 0:7ae28d2c1e3e | 449 | // Turn off all LEDs first, and let sampleswitches update everything |
wac | 1:fb60f76e33ee | 450 | LED1a = OFF; LED1b = OFF; LED1c = OFF; LED1d = OFF; LED1e = OFF; LED1f = OFF; LED1g = OFF; |
wac | 1:fb60f76e33ee | 451 | LED2a = OFF; LED2b = OFF; LED2c = OFF; LED2d = OFF; LED2e = OFF; LED2f = OFF; LED2g = OFF; |
wac | 1:fb60f76e33ee | 452 | LED3a = OFF; LED3b = OFF; LED3c = OFF; LED3d = OFF; LED3e = OFF; LED3f = OFF; LED3g = OFF; |
wac | 1:fb60f76e33ee | 453 | LEDg2 = OFF; LEDg3 = OFF; |
wac | 0:7ae28d2c1e3e | 454 | |
wac | 0:7ae28d2c1e3e | 455 | // Set up interrupt call |
wac | 1:fb60f76e33ee | 456 | getswitches.attach_us(&sampleswitches, 10000); // setup getswitches to call sampleswitches every 10 ms |
wac | 0:7ae28d2c1e3e | 457 | |
wac | 0:7ae28d2c1e3e | 458 | while (1) |
wac | 0:7ae28d2c1e3e | 459 | { |
wac | 0:7ae28d2c1e3e | 460 | }// while (1) |
wac | 0:7ae28d2c1e3e | 461 | |
wac | 0:7ae28d2c1e3e | 462 | } // main() |