Tone Demo for PWM library use slider for control on KL46Z
Fork of keyer_test_v3 by
Revision 2:7f347d6a6422, committed 2015-02-14
- Comitter:
- scohennm
- Date:
- Sat Feb 14 23:03:06 2015 +0000
- Parent:
- 1:6f0197189fcc
- Commit message:
- Tone Demo for PWM library use slider for control on KL46Z
Changed in this revision
keyer_test_v3.cpp | Show diff for this revision Revisions of this file |
slider_tone_v1.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/keyer_test_v3.cpp Fri Dec 19 16:34:51 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,161 +0,0 @@ -#include "mbed.h" -#include "SLCD.h" -#include "TSISensor.h" - -#define BASESPEED 1.200 -#define DOT 1 -#define DASH 3 -#define INTERCHAR 3 -#define CHANNELON 0 -#define CHANNELOFF 1 -#define LCDLEN 10 -#define DOTMESS "DIT" -#define DASHMESS "DAH" -#define IAMMESS "IAMB" -#define NULLMESS "111" // LCD idle message -#define FIXEDWPM 13 -#define LOWSPEED 5 -#define SPPEDINT 25.0 // This gives a max speed of 30 - 5 + 25 - -#define STATECOEF 2 -#define IDLE 0 -#define DIT 1 -#define DAH 2 -#define IAM 3 -#define SIDETONE 0.0014 //seconds -#define TONEON 0.50 -#define TONEOFF 0.0 - -SLCD slcd; //define LCD display -// keyer test -TSISensor tsiScaling; // Capacitive sensor/slider -DigitalIn RtButton(PTC12); -DigitalIn LftButton(PTC3); -DigitalIn DahPaddle(PTD7); -DigitalIn DitPaddle(PTD6); -DigitalOut led(LED_RED); -DigitalOut outPin(PTC9); //J1-16 -PwmOut soundOut(PTC9); -int KeyState = IDLE; - - -void LCDMessNoDwell(char *lMess){ - slcd.Home(); - slcd.clear(); - slcd.printf(lMess); -} - -void makeDit (float dotLen,DigitalOut outChannel, DigitalOut cloneChannel) { - outChannel.write(CHANNELON); - cloneChannel.write(CHANNELON); - soundOut.write(TONEON); - wait(dotLen); - outChannel.write(CHANNELOFF); - cloneChannel.write(CHANNELOFF); - soundOut.write(TONEOFF); - wait(float(DOT)*dotLen); - return; -} - -void makeDah (float dotLen,DigitalOut outChannel, DigitalOut cloneChannel) { - outChannel.write(CHANNELON); - cloneChannel.write(CHANNELON); - soundOut.write(TONEON); - wait(float(DASH)*dotLen); - outChannel.write(CHANNELOFF); - cloneChannel.write(CHANNELOFF); - soundOut.write(TONEOFF); - wait(float(DOT)*dotLen); - return; -} - - -int main(){ - int wpm = FIXEDWPM; - float elementLen; // period in seconds - int RButtonState; - int LButtonState; - int DahPaddleState; - int DitPaddleState; - char lcdData[LCDLEN]; - float tempValue; - int lastKeyHit = DIT; - - soundOut.period(SIDETONE); - - - elementLen = BASESPEED / (float) wpm; - - led.write(CHANNELOFF); - outPin.write(CHANNELOFF); - sprintf (lcdData,"%d",wpm); - LCDMessNoDwell(lcdData); - - - while (true) { - /* - RButtonState = !RtButton.read(); // button is pulled up so false is when button is pushed it's inverted to avoid confusion downstream - if (RButtonState){ - KeyState = DAH; - } - LButtonState = !LftButton.read(); - if (LButtonState) { - KeyState = DIT; - } - */ - RButtonState = !RtButton.read(); - LButtonState = !LftButton.read(); - DahPaddleState =!DahPaddle.read(); - DitPaddleState = !DitPaddle.read(); - KeyState = LButtonState + STATECOEF*RButtonState; - KeyState = KeyState + DitPaddleState + STATECOEF*DahPaddleState; - - switch (KeyState) { - case DAH:{ - LCDMessNoDwell(DASHMESS); - makeDah(elementLen,outPin,led); - KeyState = IDLE; - lastKeyHit = DAH; - sprintf (lcdData,"%d",wpm); - LCDMessNoDwell(lcdData); - break; - } - case DIT:{ - LCDMessNoDwell(DOTMESS); - makeDit(elementLen,outPin,led); - KeyState = IDLE; - lastKeyHit = DIT; - sprintf (lcdData,"%d",wpm); - LCDMessNoDwell(lcdData); - break; - } - case IAM:{ - LCDMessNoDwell(IAMMESS); - if (lastKeyHit == DAH){; - makeDit(elementLen,outPin,led); - makeDah( elementLen,outPin,led); - lastKeyHit = DAH; - } else { - makeDah(elementLen,outPin,led); - makeDit(elementLen,outPin,led); - lastKeyHit = DIT; - } - KeyState = IDLE; - sprintf (lcdData,"%d",wpm); - LCDMessNoDwell(lcdData); - break; - } - case IDLE:{ - tempValue = tsiScaling.readPercentage(); - if(tempValue > 0) { - wpm = LOWSPEED + int(tempValue * SPPEDINT); - elementLen = BASESPEED / (float) wpm; - sprintf (lcdData,"%d",wpm); - LCDMessNoDwell(lcdData); - } - break; - } - } - - } // while forever -}// end main \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/slider_tone_v1.cpp Sat Feb 14 23:03:06 2015 +0000 @@ -0,0 +1,90 @@ +#include "mbed.h" +#include "SLCD.h" +#include "TSISensor.h" + + +#define CHANNELON 0 +#define CHANNELOFF 1 +#define LCDLEN 10 +#define DATATIME 0.1 +//LCD messages + + + +// Operating parameters +#define SIDETONE 700.0 +#define TONEMIN 200.0 +#define TONEINT 800.00 // So tone max is 1000 +#define TONEON 0.50 +#define TONEOFF 0.0 +#define SPEEDAST 0 +#define TONEAST 1 + +SLCD slcd; //define LCD display + +TSISensor tsiScaling; // Capacitive sensor/slider + +PwmOut led(LED_RED); +DigitalOut outPin(PTC9); //J1-16 +PwmOut soundOut(PTA13); +// Global scalars +char lcdData[LCDLEN]; + +float tonePeriod; +float toneFreq = SIDETONE; + + + +void LCDMessNoDwell(char *lMess){ + slcd.Home(); + slcd.clear(); + slcd.printf(lMess); +} + +void toneAdjust( float scaling) { + int tempInt; + + toneFreq = TONEMIN + scaling * TONEINT; + tonePeriod = 1.0/toneFreq; + soundOut.period(tonePeriod); // adusting period + tempInt = (int)toneFreq; + sprintf (lcdData,"%4d",tempInt); + LCDMessNoDwell(lcdData); + return; +} +void lightAdjust( float scaling) { // Control brightness of LED + float tempDutyFactor; + + tempDutyFactor = 1.0 - scaling; //LED is a sinking connection + // anode is held at 5V + led.write(tempDutyFactor); //sdjusting duty factor + return; +} +int main(){ + int tempInt; + float tempValue; + tonePeriod = 1.0/toneFreq; + soundOut.period(tonePeriod); + + + + led.write(CHANNELON); + outPin.write(CHANNELOFF); + tempInt = (int)toneFreq; + sprintf (lcdData,"%4d",tempInt); + LCDMessNoDwell(lcdData); + wait(DATATIME); + while (true) { + + tempValue = tsiScaling.readPercentage(); + if(tempValue > 0) { + soundOut.write(TONEON); // set duty factor to 505 + toneAdjust( tempValue); + lightAdjust(tempValue); + } else { + soundOut.write(TONEOFF); // set dutyfactor to 0% + LCDMessNoDwell("SOFF"); + } + wait(DATATIME); + } // while forever +}// end main \ No newline at end of file