Tone Demo for PWM library use slider for control on KL46Z
Fork of keyer_test_v3 by
slider_tone_v1.cpp@2:7f347d6a6422, 2015-02-14 (annotated)
- Committer:
- scohennm
- Date:
- Sat Feb 14 23:03:06 2015 +0000
- Revision:
- 2:7f347d6a6422
Tone Demo for PWM library use slider for control on KL46Z
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 | 2:7f347d6a6422 | 9 | #define DATATIME 0.1 |
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 | 2:7f347d6a6422 | 22 | |
scohennm | 2:7f347d6a6422 | 23 | SLCD slcd; //define LCD display |
scohennm | 2:7f347d6a6422 | 24 | |
scohennm | 2:7f347d6a6422 | 25 | TSISensor tsiScaling; // Capacitive sensor/slider |
scohennm | 2:7f347d6a6422 | 26 | |
scohennm | 2:7f347d6a6422 | 27 | PwmOut led(LED_RED); |
scohennm | 2:7f347d6a6422 | 28 | DigitalOut outPin(PTC9); //J1-16 |
scohennm | 2:7f347d6a6422 | 29 | PwmOut soundOut(PTA13); |
scohennm | 2:7f347d6a6422 | 30 | // Global scalars |
scohennm | 2:7f347d6a6422 | 31 | char lcdData[LCDLEN]; |
scohennm | 2:7f347d6a6422 | 32 | |
scohennm | 2:7f347d6a6422 | 33 | float tonePeriod; |
scohennm | 2:7f347d6a6422 | 34 | float toneFreq = SIDETONE; |
scohennm | 2:7f347d6a6422 | 35 | |
scohennm | 2:7f347d6a6422 | 36 | |
scohennm | 2:7f347d6a6422 | 37 | |
scohennm | 2:7f347d6a6422 | 38 | void LCDMessNoDwell(char *lMess){ |
scohennm | 2:7f347d6a6422 | 39 | slcd.Home(); |
scohennm | 2:7f347d6a6422 | 40 | slcd.clear(); |
scohennm | 2:7f347d6a6422 | 41 | slcd.printf(lMess); |
scohennm | 2:7f347d6a6422 | 42 | } |
scohennm | 2:7f347d6a6422 | 43 | |
scohennm | 2:7f347d6a6422 | 44 | void toneAdjust( float scaling) { |
scohennm | 2:7f347d6a6422 | 45 | int tempInt; |
scohennm | 2:7f347d6a6422 | 46 | |
scohennm | 2:7f347d6a6422 | 47 | toneFreq = TONEMIN + scaling * TONEINT; |
scohennm | 2:7f347d6a6422 | 48 | tonePeriod = 1.0/toneFreq; |
scohennm | 2:7f347d6a6422 | 49 | soundOut.period(tonePeriod); // adusting period |
scohennm | 2:7f347d6a6422 | 50 | tempInt = (int)toneFreq; |
scohennm | 2:7f347d6a6422 | 51 | sprintf (lcdData,"%4d",tempInt); |
scohennm | 2:7f347d6a6422 | 52 | LCDMessNoDwell(lcdData); |
scohennm | 2:7f347d6a6422 | 53 | return; |
scohennm | 2:7f347d6a6422 | 54 | } |
scohennm | 2:7f347d6a6422 | 55 | void lightAdjust( float scaling) { // Control brightness of LED |
scohennm | 2:7f347d6a6422 | 56 | float tempDutyFactor; |
scohennm | 2:7f347d6a6422 | 57 | |
scohennm | 2:7f347d6a6422 | 58 | tempDutyFactor = 1.0 - scaling; //LED is a sinking connection |
scohennm | 2:7f347d6a6422 | 59 | // anode is held at 5V |
scohennm | 2:7f347d6a6422 | 60 | led.write(tempDutyFactor); //sdjusting duty factor |
scohennm | 2:7f347d6a6422 | 61 | return; |
scohennm | 2:7f347d6a6422 | 62 | } |
scohennm | 2:7f347d6a6422 | 63 | int main(){ |
scohennm | 2:7f347d6a6422 | 64 | int tempInt; |
scohennm | 2:7f347d6a6422 | 65 | float tempValue; |
scohennm | 2:7f347d6a6422 | 66 | tonePeriod = 1.0/toneFreq; |
scohennm | 2:7f347d6a6422 | 67 | soundOut.period(tonePeriod); |
scohennm | 2:7f347d6a6422 | 68 | |
scohennm | 2:7f347d6a6422 | 69 | |
scohennm | 2:7f347d6a6422 | 70 | |
scohennm | 2:7f347d6a6422 | 71 | led.write(CHANNELON); |
scohennm | 2:7f347d6a6422 | 72 | outPin.write(CHANNELOFF); |
scohennm | 2:7f347d6a6422 | 73 | tempInt = (int)toneFreq; |
scohennm | 2:7f347d6a6422 | 74 | sprintf (lcdData,"%4d",tempInt); |
scohennm | 2:7f347d6a6422 | 75 | LCDMessNoDwell(lcdData); |
scohennm | 2:7f347d6a6422 | 76 | wait(DATATIME); |
scohennm | 2:7f347d6a6422 | 77 | while (true) { |
scohennm | 2:7f347d6a6422 | 78 | |
scohennm | 2:7f347d6a6422 | 79 | tempValue = tsiScaling.readPercentage(); |
scohennm | 2:7f347d6a6422 | 80 | if(tempValue > 0) { |
scohennm | 2:7f347d6a6422 | 81 | soundOut.write(TONEON); // set duty factor to 505 |
scohennm | 2:7f347d6a6422 | 82 | toneAdjust( tempValue); |
scohennm | 2:7f347d6a6422 | 83 | lightAdjust(tempValue); |
scohennm | 2:7f347d6a6422 | 84 | } else { |
scohennm | 2:7f347d6a6422 | 85 | soundOut.write(TONEOFF); // set dutyfactor to 0% |
scohennm | 2:7f347d6a6422 | 86 | LCDMessNoDwell("SOFF"); |
scohennm | 2:7f347d6a6422 | 87 | } |
scohennm | 2:7f347d6a6422 | 88 | wait(DATATIME); |
scohennm | 2:7f347d6a6422 | 89 | } // while forever |
scohennm | 2:7f347d6a6422 | 90 | }// end main |