Tone Demo for PWM library use slider for control on KL46Z

Dependencies:   SLCD TSI mbed

Fork of keyer_test_v3 by Stanley Cohen

Committer:
scohennm
Date:
Fri Dec 19 16:34:51 2014 +0000
Revision:
1:6f0197189fcc
Parent:
0:6c16ec0e7b9d
Iambic keyer for Morse code with iambic paddle. Added a side tone using PWM @50% DF changing the cycle period to make a 700 Hz tone.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
scohennm 0:6c16ec0e7b9d 1 #include "mbed.h"
scohennm 0:6c16ec0e7b9d 2 #include "SLCD.h"
scohennm 0:6c16ec0e7b9d 3 #include "TSISensor.h"
scohennm 0:6c16ec0e7b9d 4
scohennm 0:6c16ec0e7b9d 5 #define BASESPEED 1.200
scohennm 0:6c16ec0e7b9d 6 #define DOT 1
scohennm 0:6c16ec0e7b9d 7 #define DASH 3
scohennm 0:6c16ec0e7b9d 8 #define INTERCHAR 3
scohennm 0:6c16ec0e7b9d 9 #define CHANNELON 0
scohennm 0:6c16ec0e7b9d 10 #define CHANNELOFF 1
scohennm 0:6c16ec0e7b9d 11 #define LCDLEN 10
scohennm 0:6c16ec0e7b9d 12 #define DOTMESS "DIT"
scohennm 0:6c16ec0e7b9d 13 #define DASHMESS "DAH"
scohennm 0:6c16ec0e7b9d 14 #define IAMMESS "IAMB"
scohennm 0:6c16ec0e7b9d 15 #define NULLMESS "111" // LCD idle message
scohennm 0:6c16ec0e7b9d 16 #define FIXEDWPM 13
scohennm 1:6f0197189fcc 17 #define LOWSPEED 5
scohennm 0:6c16ec0e7b9d 18 #define SPPEDINT 25.0 // This gives a max speed of 30 - 5 + 25
scohennm 0:6c16ec0e7b9d 19
scohennm 0:6c16ec0e7b9d 20 #define STATECOEF 2
scohennm 0:6c16ec0e7b9d 21 #define IDLE 0
scohennm 0:6c16ec0e7b9d 22 #define DIT 1
scohennm 0:6c16ec0e7b9d 23 #define DAH 2
scohennm 0:6c16ec0e7b9d 24 #define IAM 3
scohennm 1:6f0197189fcc 25 #define SIDETONE 0.0014 //seconds
scohennm 1:6f0197189fcc 26 #define TONEON 0.50
scohennm 1:6f0197189fcc 27 #define TONEOFF 0.0
scohennm 0:6c16ec0e7b9d 28
scohennm 0:6c16ec0e7b9d 29 SLCD slcd; //define LCD display
scohennm 0:6c16ec0e7b9d 30 // keyer test
scohennm 0:6c16ec0e7b9d 31 TSISensor tsiScaling; // Capacitive sensor/slider
scohennm 0:6c16ec0e7b9d 32 DigitalIn RtButton(PTC12);
scohennm 0:6c16ec0e7b9d 33 DigitalIn LftButton(PTC3);
scohennm 0:6c16ec0e7b9d 34 DigitalIn DahPaddle(PTD7);
scohennm 0:6c16ec0e7b9d 35 DigitalIn DitPaddle(PTD6);
scohennm 0:6c16ec0e7b9d 36 DigitalOut led(LED_RED);
scohennm 0:6c16ec0e7b9d 37 DigitalOut outPin(PTC9); //J1-16
scohennm 1:6f0197189fcc 38 PwmOut soundOut(PTC9);
scohennm 0:6c16ec0e7b9d 39 int KeyState = IDLE;
scohennm 0:6c16ec0e7b9d 40
scohennm 0:6c16ec0e7b9d 41
scohennm 0:6c16ec0e7b9d 42 void LCDMessNoDwell(char *lMess){
scohennm 0:6c16ec0e7b9d 43 slcd.Home();
scohennm 0:6c16ec0e7b9d 44 slcd.clear();
scohennm 0:6c16ec0e7b9d 45 slcd.printf(lMess);
scohennm 0:6c16ec0e7b9d 46 }
scohennm 0:6c16ec0e7b9d 47
scohennm 0:6c16ec0e7b9d 48 void makeDit (float dotLen,DigitalOut outChannel, DigitalOut cloneChannel) {
scohennm 0:6c16ec0e7b9d 49 outChannel.write(CHANNELON);
scohennm 0:6c16ec0e7b9d 50 cloneChannel.write(CHANNELON);
scohennm 1:6f0197189fcc 51 soundOut.write(TONEON);
scohennm 0:6c16ec0e7b9d 52 wait(dotLen);
scohennm 0:6c16ec0e7b9d 53 outChannel.write(CHANNELOFF);
scohennm 0:6c16ec0e7b9d 54 cloneChannel.write(CHANNELOFF);
scohennm 1:6f0197189fcc 55 soundOut.write(TONEOFF);
scohennm 0:6c16ec0e7b9d 56 wait(float(DOT)*dotLen);
scohennm 0:6c16ec0e7b9d 57 return;
scohennm 0:6c16ec0e7b9d 58 }
scohennm 0:6c16ec0e7b9d 59
scohennm 0:6c16ec0e7b9d 60 void makeDah (float dotLen,DigitalOut outChannel, DigitalOut cloneChannel) {
scohennm 0:6c16ec0e7b9d 61 outChannel.write(CHANNELON);
scohennm 0:6c16ec0e7b9d 62 cloneChannel.write(CHANNELON);
scohennm 1:6f0197189fcc 63 soundOut.write(TONEON);
scohennm 0:6c16ec0e7b9d 64 wait(float(DASH)*dotLen);
scohennm 0:6c16ec0e7b9d 65 outChannel.write(CHANNELOFF);
scohennm 0:6c16ec0e7b9d 66 cloneChannel.write(CHANNELOFF);
scohennm 1:6f0197189fcc 67 soundOut.write(TONEOFF);
scohennm 0:6c16ec0e7b9d 68 wait(float(DOT)*dotLen);
scohennm 0:6c16ec0e7b9d 69 return;
scohennm 0:6c16ec0e7b9d 70 }
scohennm 0:6c16ec0e7b9d 71
scohennm 0:6c16ec0e7b9d 72
scohennm 0:6c16ec0e7b9d 73 int main(){
scohennm 0:6c16ec0e7b9d 74 int wpm = FIXEDWPM;
scohennm 0:6c16ec0e7b9d 75 float elementLen; // period in seconds
scohennm 0:6c16ec0e7b9d 76 int RButtonState;
scohennm 0:6c16ec0e7b9d 77 int LButtonState;
scohennm 0:6c16ec0e7b9d 78 int DahPaddleState;
scohennm 0:6c16ec0e7b9d 79 int DitPaddleState;
scohennm 0:6c16ec0e7b9d 80 char lcdData[LCDLEN];
scohennm 0:6c16ec0e7b9d 81 float tempValue;
scohennm 0:6c16ec0e7b9d 82 int lastKeyHit = DIT;
scohennm 0:6c16ec0e7b9d 83
scohennm 1:6f0197189fcc 84 soundOut.period(SIDETONE);
scohennm 1:6f0197189fcc 85
scohennm 0:6c16ec0e7b9d 86
scohennm 0:6c16ec0e7b9d 87 elementLen = BASESPEED / (float) wpm;
scohennm 0:6c16ec0e7b9d 88
scohennm 0:6c16ec0e7b9d 89 led.write(CHANNELOFF);
scohennm 0:6c16ec0e7b9d 90 outPin.write(CHANNELOFF);
scohennm 0:6c16ec0e7b9d 91 sprintf (lcdData,"%d",wpm);
scohennm 0:6c16ec0e7b9d 92 LCDMessNoDwell(lcdData);
scohennm 0:6c16ec0e7b9d 93
scohennm 0:6c16ec0e7b9d 94
scohennm 0:6c16ec0e7b9d 95 while (true) {
scohennm 0:6c16ec0e7b9d 96 /*
scohennm 0:6c16ec0e7b9d 97 RButtonState = !RtButton.read(); // button is pulled up so false is when button is pushed it's inverted to avoid confusion downstream
scohennm 0:6c16ec0e7b9d 98 if (RButtonState){
scohennm 0:6c16ec0e7b9d 99 KeyState = DAH;
scohennm 0:6c16ec0e7b9d 100 }
scohennm 0:6c16ec0e7b9d 101 LButtonState = !LftButton.read();
scohennm 0:6c16ec0e7b9d 102 if (LButtonState) {
scohennm 0:6c16ec0e7b9d 103 KeyState = DIT;
scohennm 0:6c16ec0e7b9d 104 }
scohennm 0:6c16ec0e7b9d 105 */
scohennm 0:6c16ec0e7b9d 106 RButtonState = !RtButton.read();
scohennm 0:6c16ec0e7b9d 107 LButtonState = !LftButton.read();
scohennm 0:6c16ec0e7b9d 108 DahPaddleState =!DahPaddle.read();
scohennm 0:6c16ec0e7b9d 109 DitPaddleState = !DitPaddle.read();
scohennm 0:6c16ec0e7b9d 110 KeyState = LButtonState + STATECOEF*RButtonState;
scohennm 0:6c16ec0e7b9d 111 KeyState = KeyState + DitPaddleState + STATECOEF*DahPaddleState;
scohennm 0:6c16ec0e7b9d 112
scohennm 0:6c16ec0e7b9d 113 switch (KeyState) {
scohennm 0:6c16ec0e7b9d 114 case DAH:{
scohennm 0:6c16ec0e7b9d 115 LCDMessNoDwell(DASHMESS);
scohennm 0:6c16ec0e7b9d 116 makeDah(elementLen,outPin,led);
scohennm 0:6c16ec0e7b9d 117 KeyState = IDLE;
scohennm 0:6c16ec0e7b9d 118 lastKeyHit = DAH;
scohennm 0:6c16ec0e7b9d 119 sprintf (lcdData,"%d",wpm);
scohennm 0:6c16ec0e7b9d 120 LCDMessNoDwell(lcdData);
scohennm 0:6c16ec0e7b9d 121 break;
scohennm 0:6c16ec0e7b9d 122 }
scohennm 0:6c16ec0e7b9d 123 case DIT:{
scohennm 0:6c16ec0e7b9d 124 LCDMessNoDwell(DOTMESS);
scohennm 0:6c16ec0e7b9d 125 makeDit(elementLen,outPin,led);
scohennm 0:6c16ec0e7b9d 126 KeyState = IDLE;
scohennm 0:6c16ec0e7b9d 127 lastKeyHit = DIT;
scohennm 0:6c16ec0e7b9d 128 sprintf (lcdData,"%d",wpm);
scohennm 0:6c16ec0e7b9d 129 LCDMessNoDwell(lcdData);
scohennm 0:6c16ec0e7b9d 130 break;
scohennm 0:6c16ec0e7b9d 131 }
scohennm 0:6c16ec0e7b9d 132 case IAM:{
scohennm 0:6c16ec0e7b9d 133 LCDMessNoDwell(IAMMESS);
scohennm 0:6c16ec0e7b9d 134 if (lastKeyHit == DAH){;
scohennm 0:6c16ec0e7b9d 135 makeDit(elementLen,outPin,led);
scohennm 0:6c16ec0e7b9d 136 makeDah( elementLen,outPin,led);
scohennm 0:6c16ec0e7b9d 137 lastKeyHit = DAH;
scohennm 0:6c16ec0e7b9d 138 } else {
scohennm 0:6c16ec0e7b9d 139 makeDah(elementLen,outPin,led);
scohennm 0:6c16ec0e7b9d 140 makeDit(elementLen,outPin,led);
scohennm 0:6c16ec0e7b9d 141 lastKeyHit = DIT;
scohennm 0:6c16ec0e7b9d 142 }
scohennm 0:6c16ec0e7b9d 143 KeyState = IDLE;
scohennm 0:6c16ec0e7b9d 144 sprintf (lcdData,"%d",wpm);
scohennm 0:6c16ec0e7b9d 145 LCDMessNoDwell(lcdData);
scohennm 0:6c16ec0e7b9d 146 break;
scohennm 0:6c16ec0e7b9d 147 }
scohennm 0:6c16ec0e7b9d 148 case IDLE:{
scohennm 0:6c16ec0e7b9d 149 tempValue = tsiScaling.readPercentage();
scohennm 0:6c16ec0e7b9d 150 if(tempValue > 0) {
scohennm 0:6c16ec0e7b9d 151 wpm = LOWSPEED + int(tempValue * SPPEDINT);
scohennm 0:6c16ec0e7b9d 152 elementLen = BASESPEED / (float) wpm;
scohennm 0:6c16ec0e7b9d 153 sprintf (lcdData,"%d",wpm);
scohennm 0:6c16ec0e7b9d 154 LCDMessNoDwell(lcdData);
scohennm 0:6c16ec0e7b9d 155 }
scohennm 0:6c16ec0e7b9d 156 break;
scohennm 0:6c16ec0e7b9d 157 }
scohennm 0:6c16ec0e7b9d 158 }
scohennm 0:6c16ec0e7b9d 159
scohennm 0:6c16ec0e7b9d 160 } // while forever
scohennm 0:6c16ec0e7b9d 161 }// end main