HW 7.1 Attempt to use button timers to check for button status instead of interrupts
Fork of slider_diatonic_v1 by
slider_diatonic_v1.cpp@4:922d6f8fe95f, 2015-02-25 (annotated)
- Committer:
- bomalley
- Date:
- Wed Feb 25 18:28:52 2015 +0000
- Revision:
- 4:922d6f8fe95f
- Parent:
- 3:f68e9cdfaf2d
HW 7.1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
scohennm | 2:7f347d6a6422 | 1 | #include "mbed.h" |
scohennm | 2:7f347d6a6422 | 2 | #include "SLCD.h" |
scohennm | 2:7f347d6a6422 | 3 | #include "TSISensor.h" |
scohennm | 2:7f347d6a6422 | 4 | |
scohennm | 2:7f347d6a6422 | 5 | |
scohennm | 2:7f347d6a6422 | 6 | #define CHANNELON 0 |
scohennm | 2:7f347d6a6422 | 7 | #define CHANNELOFF 1 |
scohennm | 2:7f347d6a6422 | 8 | #define LCDLEN 10 |
scohennm | 3:f68e9cdfaf2d | 9 | #define DATATIME 100 //milli seccnds |
scohennm | 2:7f347d6a6422 | 10 | //LCD messages |
scohennm | 2:7f347d6a6422 | 11 | |
scohennm | 2:7f347d6a6422 | 12 | |
scohennm | 2:7f347d6a6422 | 13 | |
scohennm | 2:7f347d6a6422 | 14 | // Operating parameters |
scohennm | 2:7f347d6a6422 | 15 | #define SIDETONE 700.0 |
scohennm | 2:7f347d6a6422 | 16 | #define TONEMIN 200.0 |
scohennm | 2:7f347d6a6422 | 17 | #define TONEINT 800.00 // So tone max is 1000 |
scohennm | 2:7f347d6a6422 | 18 | #define TONEON 0.50 |
scohennm | 2:7f347d6a6422 | 19 | #define TONEOFF 0.0 |
scohennm | 2:7f347d6a6422 | 20 | #define SPEEDAST 0 |
scohennm | 2:7f347d6a6422 | 21 | #define TONEAST 1 |
scohennm | 3:f68e9cdfaf2d | 22 | #define NUMTONES 10 |
scohennm | 3:f68e9cdfaf2d | 23 | #define LEDPERIOD 0.001 |
scohennm | 3:f68e9cdfaf2d | 24 | //#define PRINTDEBUG |
scohennm | 2:7f347d6a6422 | 25 | |
bomalley | 4:922d6f8fe95f | 26 | #define NUMOCTS 1 |
bomalley | 4:922d6f8fe95f | 27 | //Button --> |
bomalley | 4:922d6f8fe95f | 28 | #define BUTOFF 1 |
bomalley | 4:922d6f8fe95f | 29 | #define BUTON 0 |
bomalley | 4:922d6f8fe95f | 30 | #define BUTTON PTC3 |
bomalley | 4:922d6f8fe95f | 31 | #define PUSHTIME 1000 //wait to check for button press |
bomalley | 4:922d6f8fe95f | 32 | |
scohennm | 3:f68e9cdfaf2d | 33 | Serial pc(USBTX, USBRX); |
scohennm | 3:f68e9cdfaf2d | 34 | |
scohennm | 3:f68e9cdfaf2d | 35 | float diatonicScale[NUMTONES] = {246.94, 261.63,293.66,329.63,349.23,392.00,440.00,493.88,523.25,587.33}; |
scohennm | 2:7f347d6a6422 | 36 | SLCD slcd; //define LCD display |
scohennm | 2:7f347d6a6422 | 37 | |
scohennm | 2:7f347d6a6422 | 38 | TSISensor tsiScaling; // Capacitive sensor/slider |
scohennm | 2:7f347d6a6422 | 39 | |
scohennm | 3:f68e9cdfaf2d | 40 | |
scohennm | 2:7f347d6a6422 | 41 | PwmOut led(LED_RED); |
scohennm | 2:7f347d6a6422 | 42 | DigitalOut outPin(PTC9); //J1-16 |
scohennm | 2:7f347d6a6422 | 43 | PwmOut soundOut(PTA13); |
scohennm | 3:f68e9cdfaf2d | 44 | Timer dataTimer; |
scohennm | 2:7f347d6a6422 | 45 | // Global scalars |
scohennm | 2:7f347d6a6422 | 46 | char lcdData[LCDLEN]; |
scohennm | 2:7f347d6a6422 | 47 | |
scohennm | 2:7f347d6a6422 | 48 | float tonePeriod; |
scohennm | 2:7f347d6a6422 | 49 | float toneFreq = SIDETONE; |
scohennm | 2:7f347d6a6422 | 50 | |
bomalley | 4:922d6f8fe95f | 51 | //Timers--> |
bomalley | 4:922d6f8fe95f | 52 | Timer buttonTimer;//checks for button state change after set amout of time. |
scohennm | 2:7f347d6a6422 | 53 | |
bomalley | 4:922d6f8fe95f | 54 | //buttons --> |
bomalley | 4:922d6f8fe95f | 55 | |
bomalley | 4:922d6f8fe95f | 56 | int LButtonState = BUTOFF; //Set the button state to off |
bomalley | 4:922d6f8fe95f | 57 | DigitalIn LftButton(BUTTON); //PTC3 |
bomalley | 4:922d6f8fe95f | 58 | |
bomalley | 4:922d6f8fe95f | 59 | //Octave changer --> |
bomalley | 4:922d6f8fe95f | 60 | int octaveState = NUMOCTS; //Not sure what value this should be |
scohennm | 2:7f347d6a6422 | 61 | |
scohennm | 2:7f347d6a6422 | 62 | void LCDMessNoDwell(char *lMess){ |
scohennm | 2:7f347d6a6422 | 63 | slcd.Home(); |
scohennm | 2:7f347d6a6422 | 64 | slcd.clear(); |
scohennm | 2:7f347d6a6422 | 65 | slcd.printf(lMess); |
scohennm | 2:7f347d6a6422 | 66 | } |
scohennm | 2:7f347d6a6422 | 67 | |
scohennm | 3:f68e9cdfaf2d | 68 | void diatonicAdjust( float scaling) { |
scohennm | 2:7f347d6a6422 | 69 | int tempInt; |
scohennm | 3:f68e9cdfaf2d | 70 | int scaleIndex; |
scohennm | 3:f68e9cdfaf2d | 71 | static int oldScaleIndex = 0; |
scohennm | 3:f68e9cdfaf2d | 72 | /* There appears to be a set up time for setting the PWM time period |
scohennm | 3:f68e9cdfaf2d | 73 | only do a nes set up if the indext changes. |
scohennm | 3:f68e9cdfaf2d | 74 | */ |
scohennm | 3:f68e9cdfaf2d | 75 | scaleIndex = (int)(NUMTONES * scaling); |
scohennm | 3:f68e9cdfaf2d | 76 | if (scaleIndex != oldScaleIndex) { |
scohennm | 3:f68e9cdfaf2d | 77 | toneFreq = diatonicScale[scaleIndex]; |
scohennm | 3:f68e9cdfaf2d | 78 | tonePeriod = 1.0/toneFreq; |
scohennm | 3:f68e9cdfaf2d | 79 | soundOut.period(tonePeriod); // adjusting period |
scohennm | 3:f68e9cdfaf2d | 80 | soundOut.write(TONEON); // there is a setup time for both period and DF |
scohennm | 3:f68e9cdfaf2d | 81 | oldScaleIndex = scaleIndex; |
scohennm | 3:f68e9cdfaf2d | 82 | } else { |
scohennm | 3:f68e9cdfaf2d | 83 | return; |
scohennm | 3:f68e9cdfaf2d | 84 | } |
scohennm | 3:f68e9cdfaf2d | 85 | #ifdef PRINTDEBUG |
scohennm | 3:f68e9cdfaf2d | 86 | pc.printf(" %f,%d\n\r",scaling, scaleIndex ); |
scohennm | 3:f68e9cdfaf2d | 87 | #endif |
scohennm | 2:7f347d6a6422 | 88 | tempInt = (int)toneFreq; |
scohennm | 2:7f347d6a6422 | 89 | sprintf (lcdData,"%4d",tempInt); |
scohennm | 2:7f347d6a6422 | 90 | LCDMessNoDwell(lcdData); |
scohennm | 2:7f347d6a6422 | 91 | return; |
scohennm | 2:7f347d6a6422 | 92 | } |
scohennm | 2:7f347d6a6422 | 93 | void lightAdjust( float scaling) { // Control brightness of LED |
scohennm | 2:7f347d6a6422 | 94 | float tempDutyFactor; |
scohennm | 2:7f347d6a6422 | 95 | |
scohennm | 3:f68e9cdfaf2d | 96 | tempDutyFactor = 1.0 - scaling; //LED is a sinking connection // anode is held at 5V |
scohennm | 3:f68e9cdfaf2d | 97 | led.write(tempDutyFactor); //adjusting duty factor |
scohennm | 2:7f347d6a6422 | 98 | return; |
scohennm | 2:7f347d6a6422 | 99 | } |
scohennm | 3:f68e9cdfaf2d | 100 | |
scohennm | 2:7f347d6a6422 | 101 | int main(){ |
scohennm | 2:7f347d6a6422 | 102 | int tempInt; |
scohennm | 2:7f347d6a6422 | 103 | float tempValue; |
scohennm | 3:f68e9cdfaf2d | 104 | |
scohennm | 2:7f347d6a6422 | 105 | tonePeriod = 1.0/toneFreq; |
scohennm | 2:7f347d6a6422 | 106 | soundOut.period(tonePeriod); |
scohennm | 3:f68e9cdfaf2d | 107 | led.period(tonePeriod); |
scohennm | 3:f68e9cdfaf2d | 108 | led.write(CHANNELOFF); |
scohennm | 3:f68e9cdfaf2d | 109 | outPin.write(CHANNELOFF); |
scohennm | 2:7f347d6a6422 | 110 | |
scohennm | 2:7f347d6a6422 | 111 | tempInt = (int)toneFreq; |
scohennm | 2:7f347d6a6422 | 112 | sprintf (lcdData,"%4d",tempInt); |
scohennm | 2:7f347d6a6422 | 113 | LCDMessNoDwell(lcdData); |
scohennm | 3:f68e9cdfaf2d | 114 | // Start data timer |
scohennm | 3:f68e9cdfaf2d | 115 | dataTimer.start(); |
scohennm | 3:f68e9cdfaf2d | 116 | dataTimer.reset(); |
bomalley | 4:922d6f8fe95f | 117 | buttonTimer.start(); //checks for button state change after set amout of time. |
bomalley | 4:922d6f8fe95f | 118 | buttonTimer.reset(); |
bomalley | 4:922d6f8fe95f | 119 | |
scohennm | 2:7f347d6a6422 | 120 | while (true) { |
bomalley | 4:922d6f8fe95f | 121 | |
bomalley | 4:922d6f8fe95f | 122 | if(buttonTimer.read_ms() > PUSHTIME) { |
bomalley | 4:922d6f8fe95f | 123 | //check button first |
bomalley | 4:922d6f8fe95f | 124 | LButtonState = !LftButton; //button is off or on |
bomalley | 4:922d6f8fe95f | 125 | if(LButtonState) { |
bomalley | 4:922d6f8fe95f | 126 | octaveState++; |
bomalley | 4:922d6f8fe95f | 127 | octaveState = octaveState % NUMOCTS; |
bomalley | 4:922d6f8fe95f | 128 | } |
bomalley | 4:922d6f8fe95f | 129 | } |
bomalley | 4:922d6f8fe95f | 130 | |
scohennm | 3:f68e9cdfaf2d | 131 | if (dataTimer.read_ms() > DATATIME){ // check to see if enough time has passed |
scohennm | 3:f68e9cdfaf2d | 132 | // to read the touch pad |
scohennm | 3:f68e9cdfaf2d | 133 | tempValue = tsiScaling.readPercentage(); |
scohennm | 3:f68e9cdfaf2d | 134 | if(tempValue > 0) { |
scohennm | 3:f68e9cdfaf2d | 135 | // soundOut.write(TONEON); // set duty factor to 50% |
scohennm | 3:f68e9cdfaf2d | 136 | diatonicAdjust(tempValue); |
scohennm | 3:f68e9cdfaf2d | 137 | lightAdjust(tempValue); |
scohennm | 3:f68e9cdfaf2d | 138 | } else { |
scohennm | 3:f68e9cdfaf2d | 139 | soundOut.write(TONEOFF); // set dutyfactor to 0% |
bomalley | 4:922d6f8fe95f | 140 | sprintf(lcdData, "%4d", octaveState + 1); |
bomalley | 4:922d6f8fe95f | 141 | LCDMessNoDwell("lcdData"); |
scohennm | 3:f68e9cdfaf2d | 142 | } |
scohennm | 3:f68e9cdfaf2d | 143 | dataTimer.reset(); |
scohennm | 3:f68e9cdfaf2d | 144 | } |
scohennm | 2:7f347d6a6422 | 145 | } // while forever |
scohennm | 2:7f347d6a6422 | 146 | }// end main |