From ba744f5b7ae7de21ed61fe74edcc5a85e3ba74e8 Mon Sep 17 00:00:00 2001 From: Rob French Date: Sun, 31 Jan 2021 22:46:43 -0600 Subject: [PATCH] Miscellaneous fixes on the integration branch. Next up on hardware: swap the FWD and REV PWR lines (or switch them in software, duh...). Then in software--verify that S-Meter, FWD/REV PWR, and VSWR signals are working correctly. --- TeensyDSP/DSP.cpp | 55 +++++++++++++++---------------- TeensyDSP/Debug.h | 2 +- TeensyDSP/Nextion.cpp | 8 ++--- TeensyDSP/Nextion.h | 8 ++--- TeensyDSP/Sensors.h | 29 ++++++++++++---- TeensyDSP/TR.cpp | 10 +++--- TeensyDSP/TR.h | 73 ++++++++++++++++------------------------- TeensyDSP/TeensyDSP.ino | 60 +++++++++++++++++++++++++++++---- 8 files changed, 144 insertions(+), 101 deletions(-) diff --git a/TeensyDSP/DSP.cpp b/TeensyDSP/DSP.cpp index 9d29f06..306b644 100644 --- a/TeensyDSP/DSP.cpp +++ b/TeensyDSP/DSP.cpp @@ -22,30 +22,28 @@ UBitxDSP DSP; //static struct { - // GUItool: begin automatically generated code - AudioInputI2S lineIn; //xy=137,220 - AudioInputUSB usbIn; //xy=142,326 - AudioMixer4 rxAudio; //xy=394,134 - AudioMixer4 txAudio; //xy=398,322 - AudioOutputAnalog spkrOut; //xy=774,104 - AudioOutputUSB usbOut; //xy=774,138 - AudioOutputI2S lineOut; //xy=776,236 - AudioConnection patchCord1(lineIn, 0, rxAudio, 0); - AudioConnection patchCord2(lineIn, 1, txAudio, 0); - AudioConnection patchCord3(usbIn, 0, txAudio, 1); - AudioConnection patchCord4(usbIn, 1, txAudio, 2); - AudioConnection patchCord5(rxAudio, spkrOut); - AudioConnection patchCord6(rxAudio, 0, lineOut, 0); - AudioConnection patchCord7(rxAudio, 0, usbOut, 0); - AudioConnection patchCord8(rxAudio, 0, usbOut, 1); - AudioConnection patchCord9(txAudio, 0, lineOut, 1); - AudioControlSGTL5000 audioCtrl; //xy=403,463 - // GUItool: end automatically generated code - +// GUItool: begin automatically generated code +AudioInputI2S lineIn; //xy=385.9371643066406,1001.9600830078125 +AudioInputUSB usbIn; //xy=390.9371643066406,1107.9600830078125 +AudioMixer4 rxAudio; //xy=642.9371643066406,915.9600830078125 +AudioMixer4 txAudio; //xy=646.9371643066406,1103.9600830078125 +AudioOutputUSB usbOut; //xy=1022.9371643066406,919.9600830078125 +AudioOutputI2S lineOut; //xy=1024.9371643066406,1017.9600830078125 +AudioConnection patchCord1(lineIn, 0, rxAudio, 0); +AudioConnection patchCord2(lineIn, 1, txAudio, 0); +AudioConnection patchCord3(usbIn, 0, txAudio, 1); +AudioConnection patchCord4(usbIn, 1, txAudio, 2); +AudioConnection patchCord5(rxAudio, 0, lineOut, 0); +AudioConnection patchCord6(rxAudio, 0, usbOut, 0); +AudioConnection patchCord7(rxAudio, 0, usbOut, 1); +AudioConnection patchCord8(txAudio, 0, lineOut, 1); +AudioControlSGTL5000 audioCtrl; //xy=651.9371643066406,1244.9600830078125 +// GUItool: end automatically generated code + //} audio; void UBitxDSP::begin() { - AudioMemory(20); + AudioMemory(16); audioCtrl.enable(); audioCtrl.volume(0.0); // headphone volume... audioCtrl.muteHeadphone(); // ...not used by UBitxDSP @@ -66,8 +64,8 @@ void UBitxDSP::begin() { // Rig (Line) Input (RX) audioCtrl.inputSelect(AUDIO_INPUT_LINEIN); audioCtrl.unmuteLineout(); - audioCtrl.lineInLevel(5, 5); // RX, TX - audioCtrl.lineOutLevel(29, 29); //RX, TX + audioCtrl.lineInLevel(9, 5); // RX, TX + audioCtrl.lineOutLevel(29, 31); //RX, TX // Mic Input (TX) audioCtrl.micGain(0); // TODO: set value @@ -78,9 +76,6 @@ void UBitxDSP::begin() { // SETUP THE AUDIO OUTPUTS - // Speaker Output (RX) - spkrOut.analogReference(INTERNAL); - // Line Output (RX) // USB Output (RX) @@ -96,6 +91,7 @@ void UBitxDSP::end() { void UBitxDSP::rx() { // mute all tx audio + audioCtrl.micGain(0); for (int i = 0; i < 4; i++) { txAudio.gain(i, 0.0); } @@ -110,9 +106,10 @@ void UBitxDSP::txMicIn() { rxAudio.gain(RX_AUDIO_CH, 0.0); // restore the tx mic audio audioCtrl.inputSelect(AUDIO_INPUT_MIC); + audioCtrl.micGain(12); for (int i = 0; i < 4; i++) { if (i == TX_MIC_IN_CH) - txAudio.gain(i, 1.0); + txAudio.gain(i, 0.1); else txAudio.gain(i, 0.0); } @@ -125,7 +122,7 @@ void UBitxDSP::txLineIn() { audioCtrl.inputSelect(AUDIO_INPUT_LINEIN); for (int i = 0; i < 4; i++) { if (i == TX_LINE_IN_CH) - txAudio.gain(i, 1.0); + txAudio.gain(i, 0.1); else txAudio.gain(i, 0.0); } @@ -138,7 +135,7 @@ void UBitxDSP::txUSBIn() { audioCtrl.inputSelect(AUDIO_INPUT_LINEIN); for (int i = 0; i < 4; i++) { if (i == TX_USB_IN_CH1 || i == TX_USB_IN_CH2) - txAudio.gain(i, 1.0); + txAudio.gain(i, 0.1); else txAudio.gain(i, 0.0); } diff --git a/TeensyDSP/Debug.h b/TeensyDSP/Debug.h index 31a94c8..aa18b3d 100644 --- a/TeensyDSP/Debug.h +++ b/TeensyDSP/Debug.h @@ -7,7 +7,7 @@ #define DBGPRINT(MSG) do { Serial.print("DBG: "); Serial.print(MSG); } while (0) #define DBGPRINTLN(MSG) do { Serial.print("DBG: "); Serial.println(MSG); } while (0) #define DBGNEWLINE() do { Serial.println(); } while (0) -#define DBGCMD(CMD) do { Serial.println("DBG: "); Serial.println(#CMD); CMD; } while (0) +#define DBGCMD(CMD) do { Serial.print("DBG: "); Serial.println(#CMD); CMD; } while (0) #else #define DBGPRINT(MSG) do {} while (0) #define DBGPRINTLN(MSG) do {} while (0) diff --git a/TeensyDSP/Nextion.cpp b/TeensyDSP/Nextion.cpp index 2c4d85e..8f3f2d7 100644 --- a/TeensyDSP/Nextion.cpp +++ b/TeensyDSP/Nextion.cpp @@ -30,10 +30,10 @@ float calcVSWR = 0.0; float L_calcVSWR = 0.0; byte scaledVSWR = 0; byte L_scaledVSWR = 0; -int fwdPower = 0; -int L_fwdPower = 0; -int revPower = 0; -int L_revPower = 0; +float fwdPower = 0; +float L_fwdPower = 0; +float revPower = 0; +float L_revPower = 0; //Control must have prefix 'v' or 's' diff --git a/TeensyDSP/Nextion.h b/TeensyDSP/Nextion.h index 8d02d77..4cfe185 100644 --- a/TeensyDSP/Nextion.h +++ b/TeensyDSP/Nextion.h @@ -107,11 +107,11 @@ extern float L_calcVSWR; extern byte scaledVSWR; extern byte L_scaledVSWR; -extern int fwdPower; -extern int L_fwdPower; +extern float fwdPower; +extern float L_fwdPower; -extern int revPower; -extern int L_revPower; +extern float revPower; +extern float L_revPower; void sendHeader(char varType, char varIndex); void sendCommandUL(char varIndex, unsigned long sendValue); diff --git a/TeensyDSP/Sensors.h b/TeensyDSP/Sensors.h index 4f0cd52..387d0b8 100644 --- a/TeensyDSP/Sensors.h +++ b/TeensyDSP/Sensors.h @@ -8,31 +8,39 @@ /**********************************************************************/ #ifndef UBITX_SENSORS_S_METER_PIN -#define UBITX_SENSORS_S_METER_PIN 27 +#define UBITX_SENSORS_S_METER_PIN 28 #endif #ifndef UBITX_SENSORS_FWD_PWR_PIN -#define UBITX_SENSORS_FWD_PWR_PIN 20 +#define UBITX_SENSORS_FWD_PWR_PIN 26 #endif #ifndef UBITX_SENSORS_REV_PWR_PIN -#define UBITX_SENSORS_REV_PWR_PIN 28 +#define UBITX_SENSORS_REV_PWR_PIN 20 #endif #ifndef UBITX_SENSORS_SUPPLY_PIN #define UBITX_SENSORS_SUPPLY_PIN 21 #endif +#ifndef UBITX_SENSORS_SPARE1_PIN +#define UBITX_SENSORS_SPARE1_PIN 27 +#endif + +#ifndef UBITX_SENSORS_SPARE2_PIN +#define UBITX_SENSORS_SPARE2_PIN 31 +#endif + #ifndef UBITX_SENSORS_AVG_SAMPLES #define UBITX_SENSORS_AVG_SAMPLES 16 #endif #ifndef UBITX_SENSORS_S_METER_R1 -#define UBITX_SENSORS_S_METER_R1 22000.0 +#define UBITX_SENSORS_S_METER_R1 0.0 #endif #ifndef UBITX_SENSORS_S_METER_R2 -#define UBITX_SENSORS_S_METER_R2 33000.0 +#define UBITX_SENSORS_S_METER_R2 1.0 #endif #ifndef UBITX_SENSORS_FWD_PWR_R1 @@ -101,6 +109,8 @@ const int uBitxSensorsSMeterPin = UBITX_SENSORS_S_METER_PIN; const int uBitxSensorsFwdPwrPin = UBITX_SENSORS_FWD_PWR_PIN; const int uBitxSensorsRevPwrPin = UBITX_SENSORS_REV_PWR_PIN; const int uBitxSensorsSupplyPin = UBITX_SENSORS_SUPPLY_PIN; +const int uBitxSensorsSpare1Pin = UBITX_SENSORS_SPARE1_PIN; +const int uBitxSensorsSpare2Pin = UBITX_SENSORS_SPARE2_PIN; const int uBitxSensorsAvgSamples = UBITX_SENSORS_AVG_SAMPLES; const float uBitxSensorsSMeterR1 = UBITX_SENSORS_S_METER_R1; const float uBitxSensorsSMeterR2 = UBITX_SENSORS_S_METER_R2; @@ -165,6 +175,7 @@ class TrailingAverage { int last = (current - 1) % N; average -= data[last]; current = (current + 1) % N; + data[current] = val; average += data[current] / divisor; } @@ -200,12 +211,16 @@ class UBitxSensors { sMeterPin(uBitxSensorsSMeterPin), fwdPwrPin(uBitxSensorsFwdPwrPin), revPwrPin(uBitxSensorsRevPwrPin), - supplyPin(uBitxSensorsSupplyPin) + supplyPin(uBitxSensorsSupplyPin), + spare1Pin(uBitxSensorsSpare1Pin), + spare2Pin(uBitxSensorsSpare2Pin) { pinMode(sMeterPin, INPUT); // analog pinMode(fwdPwrPin, INPUT); // analog pinMode(revPwrPin, INPUT); // analog pinMode(supplyPin, INPUT); // analog + pinMode(spare1Pin, INPUT); // analog + pinMode(spare2Pin, INPUT); // analog } /*! @@ -328,6 +343,8 @@ class UBitxSensors { int fwdPwrPin; int revPwrPin; int supplyPin; + int spare1Pin; + int spare2Pin; // Buffers for averages TrailingAverage sMeter; diff --git a/TeensyDSP/TR.cpp b/TeensyDSP/TR.cpp index 5ed1e1c..0be76fa 100644 --- a/TeensyDSP/TR.cpp +++ b/TeensyDSP/TR.cpp @@ -17,22 +17,22 @@ void UBitxTR::update(bool cw) { // release) will result in exitting transmit... except for VOX // and CAT which can only function as a release if it was enabled. if (pttReleased() || keyReleased() || - (voxEnabled && voxDeactivated()) || - (catEnabled && catDeactivated())) { + (voxEnable && voxDeactivated()) || + (catEnable && catDeactivated())) { // first, stop transmitting; then, setup RX audio DBGCMD( setRX() ); DBGCMD( dsp.rx() ); } } else { - if ((pttEnabled && pttPressed()) || (voxEnabled && voxActivated())) { + if ((pttEnable && pttPressed()) || (voxEnable && voxActivated())) { // first, setup TX audio; then, start transmitting (from Mic) DBGCMD( dsp.txMicIn() ); DBGCMD( setTX() ); - } else if (keyEnabled && keyPressed()) { + } else if (keyEnable && keyPressed()) { // first, setup TX audio; then, start transmitting (from Line In) DBGCMD( dsp.txLineIn() ); DBGCMD( setTX() ); - } else if (catEnabled && catActivated()) { + } else if (catEnable && catActivated()) { // first, setup TX audio; then, start transmitting (USB) DBGCMD( dsp.txUSBIn() ); DBGCMD( setTX() ); diff --git a/TeensyDSP/TR.h b/TeensyDSP/TR.h index 6b4345f..5ebc813 100644 --- a/TeensyDSP/TR.h +++ b/TeensyDSP/TR.h @@ -36,18 +36,33 @@ class UBitxTR { DBGCMD( disableVOX() ); DBGCMD( enableKey() ); DBGCMD( enableCAT() ); + + DBGCMD( setRX() ); } - inline void enablePTT() { pttEnabled = true; } - inline void enableVOX() { voxEnabled = true; } - inline void enableKey() { keyEnabled = true; } - inline void enableCAT() { catEnabled = true; } - inline void disablePTT() { pttEnabled = false; } - inline void disableVOX() { voxEnabled = false; } - inline void disableKey() { keyEnabled = false; } - inline void disableCAT() { catEnabled = false; } - + inline void enablePTT() { pttEnable = true; } + inline void enableVOX() { voxEnable = true; } + inline void enableKey() { keyEnable = true; } + inline void enableCAT() { catEnable = true; } + inline void disablePTT() { pttEnable = false; } + inline void disableVOX() { voxEnable = false; } + inline void disableKey() { keyEnable = false; } + inline void disableCAT() { catEnable = false; } + inline bool pttEnabled() { return pttEnable; } + inline bool voxEnabled() { return voxEnable; } + inline bool keyEnabled() { return keyEnable; } + inline bool catEnabled() { return catEnable; } + + inline bool pttPressed() { return ptt.fell(); } + inline bool pttReleased() { return ptt.rose(); } + inline bool voxActivated() { return (L_voxActive != voxActive) && L_voxActive; } + inline bool voxDeactivated() { return (L_voxActive != voxActive) && voxActive; } + inline bool keyPressed() { return (L_keyDown != keyDown) && L_keyDown; } + inline bool keyReleased() { return (L_keyDown != keyDown) && keyDown; } + inline bool catActivated() { return (L_catActive != catActive) && L_catActive; } + inline bool catDeactivated() { return (L_catActive != catActive) && catActive; } + inline void catTX() { L_catActive = catActive; catActive = true; @@ -94,48 +109,16 @@ class UBitxTR { ptt.update(); } - inline bool pttPressed() { - return ptt.fell(); - } - - inline bool pttReleased() { - return ptt.rose(); - } - inline void updateVOX() { L_voxActive = voxActive; voxActive = (digitalRead(voxPin) == LOW); } - inline bool voxActivated() { - return (L_voxActive != voxActive) && L_voxActive; - } - - inline bool voxDeactivated() { - return (L_voxActive != voxActive) && voxActive; - } - inline void updateKey() { L_keyDown = keyDown; keyDown = (digitalRead(keyPin) == LOW); } - inline bool keyPressed() { - return (L_keyDown != keyDown) && L_keyDown; - } - - inline bool keyReleased() { - return (L_keyDown != keyDown) && keyDown; - } - - inline bool catActivated() { - return (L_catActive != catActive) && L_catActive; - } - - inline bool catDeactivated() { - return (L_catActive != catActive) && catActive; - } - UBitxDSP& dsp; Bounce ptt; @@ -147,10 +130,10 @@ class UBitxTR { bool isTX = false; - bool pttEnabled = false; - bool voxEnabled = false; - bool keyEnabled = false; - bool catEnabled = false; + bool pttEnable = false; + bool voxEnable = false; + bool keyEnable = false; + bool catEnable = false; bool voxActive = false; bool L_voxActive = false; diff --git a/TeensyDSP/TeensyDSP.ino b/TeensyDSP/TeensyDSP.ino index 31c5f68..833c3c0 100644 --- a/TeensyDSP/TeensyDSP.ino +++ b/TeensyDSP/TeensyDSP.ino @@ -16,6 +16,11 @@ //const uint8_t responseFooter[4]={'"', 0xFF, 0xFF, 0xFF}; //const char hexCodes[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', }; +#ifdef DEBUG +int i2cCmdCounter[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; +int i2cRespCounter[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; +#endif + unsigned long SAMPLE_INTERVAL = 0; int i2cCommand = 0; @@ -308,6 +313,13 @@ void sendMeterData(uint8_t isSend) void setup() { +#ifdef DEBUG + Serial.begin(38400); +#endif + + DBGCMD( DSP.begin() ); + DBGCMD( TR.begin() ); + // load configuration EEPROM.get(EEPROM_DSPTYPE, DSPType); if (DSPType > 5) @@ -350,9 +362,6 @@ void setup() SAMPLE_INTERVAL = round(1000000 * (1.0 / SAMPLE_FREQUENCY)); //calculateCoeff(cwDecodeHz); //Set 750Hz //9 * 50 + 300 = 750Hz //Serial1.println("Start..."); - - DBGCMD( DSP.begin() ); - DBGCMD( TR.begin() ); } /*! @@ -374,6 +383,9 @@ void i2cReceiveEvent(size_t numBytes) if (0x50 <= readCommand && readCommand <= 0x59) { + #ifdef DEBUG + i2cCmdCounter[readCommand - 0x50]++; + #endif i2cCommand = readCommand; } } @@ -393,21 +405,33 @@ void i2cRequestEvent(void) case I2CMETER_CALCS: // Returns an already-calculated S-meter value. Wire1.write(scaledSMeter); + #ifdef DEBUG + i2cRespCounter[i2cCommand - 0x50]++; + #endif break; case I2CMETER_UNCALCS: // Returns a raw signal strength value. Wire1.write(Sensors.sMeterUnscaled() >> 2); // divided by 4... do we want this? + #ifdef DEBUG + i2cRespCounter[i2cCommand - 0x50]++; + #endif break; case I2CMETER_CALCP: // Returns a raw forward power value. - Wire1.write(fwdPower); + Wire1.write(int(fwdPower * 100.0)); + #ifdef DEBUG + i2cRespCounter[i2cCommand - 0x50]++; + #endif break; case I2CMETER_CALCR: // Returns a raw reverse power value. - Wire1.write(revPower); + Wire1.write(int(revPower * 100.0)); + #ifdef DEBUG + i2cRespCounter[i2cCommand - 0x50]++; + #endif break; default: @@ -477,6 +501,7 @@ void loop() // For debugging, output some debug info every 1.0" (40 frames @ 40 Hz). frameCounter++; if (frameCounter % 40 == 0) { + Serial.println("======================================================================"); Serial.print("DBG: Frame: "); Serial.print(frameCounter); if (isTX) { @@ -501,6 +526,27 @@ void loop() Serial.print(fwdPower, 2); Serial.print(", REV PWR: "); Serial.println(revPower, 2); + Serial.print("Audio Memory: "); + Serial.print(AudioMemoryUsage()); + Serial.print(","); + Serial.println(AudioMemoryUsageMax()); + Serial.println("----------------------------------------------------------------------"); + Serial.print("Enabled/Active: PTT: "); + Serial.print(TR.pttEnabled() ? "Y" : "N"); Serial.print("/"); Serial.print(TR.pttPressed() ? "Y" : "N"); + Serial.print(", VOX: "); + Serial.print(TR.voxEnabled() ? "Y" : "N"); Serial.print("/"); Serial.print(TR.voxActivated() ? "Y" : "N"); + Serial.print(", Key: "); + Serial.print(TR.keyEnabled() ? "Y" : "N"); Serial.print("/"); Serial.print(TR.keyPressed() ? "Y" : "N"); + Serial.print(", CAT: "); + Serial.print(TR.catEnabled() ? "Y" : "N"); Serial.print("/"); Serial.print(TR.catActivated() ? "Y" : "N"); + Serial.println(); + Serial.print("I2C Command/Response: "); + for (int i = 0x50; i <= 0x59; i++) { + Serial.print(i, HEX); Serial.print(": "); + Serial.print(i2cCmdCounter[i - 0x50]); Serial.print("/"); + Serial.print(i2cRespCounter[i - 0x50]); Serial.print(", "); + } + Serial.println(); } #endif @@ -521,14 +567,14 @@ void loop() // Send forward power. if (L_fwdPower != fwdPower) { L_fwdPower = fwdPower; - sendCommandL('m', fwdPower * 100); // watts x 100? + sendCommandL('m', int(fwdPower * 100.0)); // watts x 100? sendCommand1Num('m', 2); } // Send reverse power. //if (L_revPower != revPower) { // L_revPower = revPower; - // sendCommandL('m', revPower * 100); // watts x 100? + // sendCommandL('m', int(revPower * 100.0)); // watts x 100? // sendCommand1Num('m', 2); //}