Rahul Patle
/
read_mic
Voice_masher_audio read KL46z
read_microphont.cpp@1:e4d5bf9c63f5, 2015-04-28 (annotated)
- Committer:
- rahulpatle101
- Date:
- Tue Apr 28 02:01:54 2015 +0000
- Revision:
- 1:e4d5bf9c63f5
- Parent:
- 0:fe8967d28c75
try2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rahulpatle101 | 0:fe8967d28c75 | 1 | #include "mbed.h" |
rahulpatle101 | 0:fe8967d28c75 | 2 | #include "SLCD.h" |
rahulpatle101 | 0:fe8967d28c75 | 3 | #include "TSISensor.h" |
rahulpatle101 | 1:e4d5bf9c63f5 | 4 | #include <algorithm> // std::max |
rahulpatle101 | 1:e4d5bf9c63f5 | 5 | |
rahulpatle101 | 1:e4d5bf9c63f5 | 6 | |
rahulpatle101 | 0:fe8967d28c75 | 7 | #define CHANNELON 0 |
rahulpatle101 | 0:fe8967d28c75 | 8 | #define CHANNELOFF 1 |
rahulpatle101 | 0:fe8967d28c75 | 9 | #define LCDLEN 10 |
rahulpatle101 | 0:fe8967d28c75 | 10 | #define DATATIME 0.1 |
rahulpatle101 | 0:fe8967d28c75 | 11 | //LCD messages |
rahulpatle101 | 1:e4d5bf9c63f5 | 12 | |
rahulpatle101 | 1:e4d5bf9c63f5 | 13 | |
rahulpatle101 | 1:e4d5bf9c63f5 | 14 | |
rahulpatle101 | 0:fe8967d28c75 | 15 | // Operating parameters |
rahulpatle101 | 1:e4d5bf9c63f5 | 16 | #define SIDETONE 700.0 |
rahulpatle101 | 0:fe8967d28c75 | 17 | #define TONEMIN 200.0 |
rahulpatle101 | 0:fe8967d28c75 | 18 | #define TONEINT 800.00 // So tone max is 1000 |
rahulpatle101 | 0:fe8967d28c75 | 19 | #define TONEON 0.50 |
rahulpatle101 | 0:fe8967d28c75 | 20 | #define TONEOFF 0.0 |
rahulpatle101 | 0:fe8967d28c75 | 21 | #define SPEEDAST 0 |
rahulpatle101 | 0:fe8967d28c75 | 22 | #define TONEAST 1 |
rahulpatle101 | 1:e4d5bf9c63f5 | 23 | |
rahulpatle101 | 0:fe8967d28c75 | 24 | SLCD slcd; //define LCD display |
rahulpatle101 | 1:e4d5bf9c63f5 | 25 | |
rahulpatle101 | 0:fe8967d28c75 | 26 | TSISensor tsiScaling; // Capacitive sensor/slider |
rahulpatle101 | 1:e4d5bf9c63f5 | 27 | |
rahulpatle101 | 0:fe8967d28c75 | 28 | AnalogIn analogRand(PTB3); |
rahulpatle101 | 0:fe8967d28c75 | 29 | PwmOut led(LED_RED); |
rahulpatle101 | 0:fe8967d28c75 | 30 | DigitalOut outPin(PTC9); //J1-16 |
rahulpatle101 | 0:fe8967d28c75 | 31 | PwmOut soundOut(PTA13); |
rahulpatle101 | 0:fe8967d28c75 | 32 | Serial pc(USBTX, USBRX); |
rahulpatle101 | 1:e4d5bf9c63f5 | 33 | |
rahulpatle101 | 0:fe8967d28c75 | 34 | // Global scalars |
rahulpatle101 | 0:fe8967d28c75 | 35 | char lcdData[LCDLEN]; |
rahulpatle101 | 1:e4d5bf9c63f5 | 36 | |
rahulpatle101 | 0:fe8967d28c75 | 37 | float tonePeriod; |
rahulpatle101 | 0:fe8967d28c75 | 38 | float toneFreq = SIDETONE; |
rahulpatle101 | 1:e4d5bf9c63f5 | 39 | |
rahulpatle101 | 1:e4d5bf9c63f5 | 40 | |
rahulpatle101 | 1:e4d5bf9c63f5 | 41 | |
rahulpatle101 | 1:e4d5bf9c63f5 | 42 | void LCDMessNoDwell(char *lMess) |
rahulpatle101 | 1:e4d5bf9c63f5 | 43 | { |
rahulpatle101 | 1:e4d5bf9c63f5 | 44 | slcd.Home(); |
rahulpatle101 | 1:e4d5bf9c63f5 | 45 | slcd.clear(); |
rahulpatle101 | 1:e4d5bf9c63f5 | 46 | slcd.printf(lMess); |
rahulpatle101 | 1:e4d5bf9c63f5 | 47 | } |
rahulpatle101 | 1:e4d5bf9c63f5 | 48 | |
rahulpatle101 | 1:e4d5bf9c63f5 | 49 | void toneAdjust( float scaling) |
rahulpatle101 | 1:e4d5bf9c63f5 | 50 | { |
rahulpatle101 | 0:fe8967d28c75 | 51 | int tempInt; |
rahulpatle101 | 1:e4d5bf9c63f5 | 52 | |
rahulpatle101 | 0:fe8967d28c75 | 53 | toneFreq = TONEMIN + scaling * TONEINT; |
rahulpatle101 | 1:e4d5bf9c63f5 | 54 | tonePeriod = 1.0/toneFreq; |
rahulpatle101 | 0:fe8967d28c75 | 55 | soundOut.period(tonePeriod); // adusting period |
rahulpatle101 | 0:fe8967d28c75 | 56 | tempInt = (int)toneFreq; |
rahulpatle101 | 0:fe8967d28c75 | 57 | sprintf (lcdData,"%4d",tempInt); |
rahulpatle101 | 0:fe8967d28c75 | 58 | LCDMessNoDwell(lcdData); |
rahulpatle101 | 0:fe8967d28c75 | 59 | return; |
rahulpatle101 | 0:fe8967d28c75 | 60 | } |
rahulpatle101 | 1:e4d5bf9c63f5 | 61 | void lightAdjust( float scaling) // Control brightness of LED |
rahulpatle101 | 1:e4d5bf9c63f5 | 62 | { |
rahulpatle101 | 0:fe8967d28c75 | 63 | float tempDutyFactor; |
rahulpatle101 | 1:e4d5bf9c63f5 | 64 | tempDutyFactor = 1.0 - scaling; //LED is a sinking connection |
rahulpatle101 | 1:e4d5bf9c63f5 | 65 | // anode is held at 5V |
rahulpatle101 | 0:fe8967d28c75 | 66 | led.write(tempDutyFactor); //sdjusting duty factor |
rahulpatle101 | 0:fe8967d28c75 | 67 | return; |
rahulpatle101 | 0:fe8967d28c75 | 68 | } |
rahulpatle101 | 1:e4d5bf9c63f5 | 69 | int main() |
rahulpatle101 | 1:e4d5bf9c63f5 | 70 | { |
rahulpatle101 | 0:fe8967d28c75 | 71 | int tempInt; |
rahulpatle101 | 0:fe8967d28c75 | 72 | float tempValue; |
rahulpatle101 | 0:fe8967d28c75 | 73 | float analogValue; |
rahulpatle101 | 0:fe8967d28c75 | 74 | float mx = 0; |
rahulpatle101 | 0:fe8967d28c75 | 75 | float mn = 1; |
rahulpatle101 | 0:fe8967d28c75 | 76 | float previousDelta = 0; |
rahulpatle101 | 0:fe8967d28c75 | 77 | float delta = 0; |
rahulpatle101 | 1:e4d5bf9c63f5 | 78 | |
rahulpatle101 | 1:e4d5bf9c63f5 | 79 | tonePeriod = 1.0/toneFreq; |
rahulpatle101 | 0:fe8967d28c75 | 80 | soundOut.period(tonePeriod); |
rahulpatle101 | 1:e4d5bf9c63f5 | 81 | |
rahulpatle101 | 0:fe8967d28c75 | 82 | led.write(CHANNELON); |
rahulpatle101 | 0:fe8967d28c75 | 83 | outPin.write(CHANNELOFF); |
rahulpatle101 | 0:fe8967d28c75 | 84 | tempInt = (int)toneFreq; |
rahulpatle101 | 0:fe8967d28c75 | 85 | sprintf (lcdData,"%4d",tempInt); |
rahulpatle101 | 0:fe8967d28c75 | 86 | LCDMessNoDwell(lcdData); |
rahulpatle101 | 1:e4d5bf9c63f5 | 87 | wait(DATATIME); |
rahulpatle101 | 0:fe8967d28c75 | 88 | while (true) { |
rahulpatle101 | 1:e4d5bf9c63f5 | 89 | |
rahulpatle101 | 0:fe8967d28c75 | 90 | tempValue = tsiScaling.readPercentage(); |
rahulpatle101 | 1:e4d5bf9c63f5 | 91 | for (int i = 0; i < 100; ++i) { |
rahulpatle101 | 0:fe8967d28c75 | 92 | analogValue = analogRand.read(); |
rahulpatle101 | 1:e4d5bf9c63f5 | 93 | mx = max(mx, analogValue); |
rahulpatle101 | 1:e4d5bf9c63f5 | 94 | mn = min(mn, analogValue); |
rahulpatle101 | 1:e4d5bf9c63f5 | 95 | |
rahulpatle101 | 1:e4d5bf9c63f5 | 96 | |
rahulpatle101 | 1:e4d5bf9c63f5 | 97 | //mn = mn > analogValue ? analogValue : mn; |
rahulpatle101 | 0:fe8967d28c75 | 98 | // mx = mx < analogValue ? analogValue : mx; |
rahulpatle101 | 1:e4d5bf9c63f5 | 99 | //if (analogValue > mx){ |
rahulpatle101 | 1:e4d5bf9c63f5 | 100 | // mx = analogValue; |
rahulpatle101 | 1:e4d5bf9c63f5 | 101 | // } |
rahulpatle101 | 1:e4d5bf9c63f5 | 102 | // if (analogValue < mn){ |
rahulpatle101 | 1:e4d5bf9c63f5 | 103 | // mn = analogValue; |
rahulpatle101 | 1:e4d5bf9c63f5 | 104 | // } |
rahulpatle101 | 1:e4d5bf9c63f5 | 105 | |
rahulpatle101 | 0:fe8967d28c75 | 106 | } |
rahulpatle101 | 0:fe8967d28c75 | 107 | delta = mx - mn; |
rahulpatle101 | 1:e4d5bf9c63f5 | 108 | |
rahulpatle101 | 1:e4d5bf9c63f5 | 109 | pc.printf("%f %f %f %f \n", analogValue, mn, mx, delta); |
rahulpatle101 | 0:fe8967d28c75 | 110 | wait_ms(DATATIME); |
rahulpatle101 | 0:fe8967d28c75 | 111 | if(previousDelta != delta) { |
rahulpatle101 | 0:fe8967d28c75 | 112 | soundOut.write(TONEON); // set duty factor to 505 |
rahulpatle101 | 0:fe8967d28c75 | 113 | toneAdjust( analogValue); |
rahulpatle101 | 0:fe8967d28c75 | 114 | lightAdjust(analogValue); |
rahulpatle101 | 0:fe8967d28c75 | 115 | previousDelta = delta; |
rahulpatle101 | 1:e4d5bf9c63f5 | 116 | } else { |
rahulpatle101 | 0:fe8967d28c75 | 117 | soundOut.write(TONEOFF); // set dutyfactor to 0% |
rahulpatle101 | 0:fe8967d28c75 | 118 | LCDMessNoDwell("SOFF"); |
rahulpatle101 | 1:e4d5bf9c63f5 | 119 | } |
rahulpatle101 | 1:e4d5bf9c63f5 | 120 | wait(DATATIME); |
rahulpatle101 | 0:fe8967d28c75 | 121 | } // while forever |
rahulpatle101 | 0:fe8967d28c75 | 122 | }// end main |