2018-08-03 22:23:20 -04:00
|
|
|
/*
|
2021-01-26 23:49:07 -05:00
|
|
|
FFT, CW Decode for uBITX
|
|
|
|
KD8CEC, Ian Lee
|
2019-04-11 09:37:24 -04:00
|
|
|
|
2021-01-26 23:49:07 -05:00
|
|
|
Version : 0.8
|
|
|
|
-----------------------------------------------------------------------
|
|
|
|
License : See fftfunctions.cpp for FFT and CW Decode.
|
2018-08-03 22:23:20 -04:00
|
|
|
**********************************************************************/
|
|
|
|
|
2021-01-26 23:22:02 -05:00
|
|
|
#include <ADC.h>
|
2021-01-21 10:44:06 -05:00
|
|
|
#include <i2c_t3.h> // using i2c_t3 library for multiple I2C busses
|
2018-08-03 22:23:20 -04:00
|
|
|
#include <EEPROM.h>
|
2021-01-21 00:26:38 -05:00
|
|
|
#include "TeensyDSP.h"
|
2018-08-03 22:23:20 -04:00
|
|
|
|
2021-01-26 23:22:02 -05:00
|
|
|
//const uint8_t responseHeader[11]={'p', 'm', '.', 's', 'p', '.', 't', 'x', 't', '=', '"'}; //for Spectrum from DSP
|
|
|
|
//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', };
|
2018-08-03 22:23:20 -04:00
|
|
|
|
2021-01-31 23:46:43 -05:00
|
|
|
#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
|
|
|
|
|
2018-08-03 22:23:20 -04:00
|
|
|
unsigned long SAMPLE_INTERVAL = 0;
|
|
|
|
|
2021-01-21 12:37:27 -05:00
|
|
|
int i2cCommand = 0;
|
2021-01-26 23:22:02 -05:00
|
|
|
//void calculateCoeff(uint8_t freqIndex);
|
|
|
|
uint8_t cwDecodeHz = 9;
|
|
|
|
int magnitudelimit_low = 30;
|
2018-08-03 22:23:20 -04:00
|
|
|
|
2021-01-21 10:44:06 -05:00
|
|
|
char forwardBuff[MAX_FORWARD_BUFF_LENGTH + 1];
|
2021-01-21 12:37:27 -05:00
|
|
|
static int nowBuffIndex = 0;
|
2018-08-03 22:23:20 -04:00
|
|
|
static char etxCount = 0;
|
|
|
|
static char nowSendingProtocol = 0;
|
|
|
|
|
|
|
|
uint8_t SMeterToUartSend = 0; //0 : Send, 1: Idle
|
|
|
|
uint8_t SMeterToUartIdleCount = 0;
|
|
|
|
#define SMeterToUartInterval 4
|
|
|
|
|
|
|
|
char DSPType = 1; //0 : Not Use, 1 : FFT, 2 : Morse Decoder, 3 : RTTY Decoder
|
|
|
|
char FFTToUartIdleCount = 0;
|
|
|
|
#define FFTToUartInterval 2
|
|
|
|
|
2021-01-21 10:44:06 -05:00
|
|
|
elapsedMillis sinceForward = 0;
|
2018-08-03 22:23:20 -04:00
|
|
|
uint8_t responseCommand = 0; //
|
2021-01-21 23:00:35 -05:00
|
|
|
bool isTX = false;
|
2021-01-26 23:22:02 -05:00
|
|
|
|
|
|
|
/**********************************************************************/
|
|
|
|
|
2021-01-21 12:37:27 -05:00
|
|
|
void responseConfig()
|
2018-08-03 22:23:20 -04:00
|
|
|
{
|
|
|
|
if (responseCommand == 2)
|
|
|
|
{
|
|
|
|
unsigned long returnValue = 0;
|
|
|
|
if (DSPType == 0)
|
|
|
|
{
|
|
|
|
returnValue = 94; //None
|
|
|
|
}
|
|
|
|
else if (DSPType == 1)
|
|
|
|
{
|
|
|
|
returnValue = 95; //Spectrum (FFT) mode
|
|
|
|
}
|
|
|
|
else if (DSPType == 2)
|
|
|
|
{
|
|
|
|
returnValue = 100 + cwDecodeHz;
|
|
|
|
}
|
2021-01-26 23:49:07 -05:00
|
|
|
|
2018-08-03 22:23:20 -04:00
|
|
|
returnValue = returnValue << 8;
|
|
|
|
returnValue = returnValue | (SMeterToUartSend & 0xFF);
|
|
|
|
returnValue = returnValue << 8;
|
|
|
|
uint8_t tmpValue = 0;
|
|
|
|
if (magnitudelimit_low > 255)
|
|
|
|
tmpValue = 255;
|
|
|
|
else if (magnitudelimit_low < 1)
|
|
|
|
tmpValue = 0;
|
2021-01-26 23:49:07 -05:00
|
|
|
else
|
2018-08-03 22:23:20 -04:00
|
|
|
tmpValue = magnitudelimit_low;
|
|
|
|
returnValue = returnValue | (tmpValue & 0xFF);
|
2021-01-26 23:49:07 -05:00
|
|
|
|
|
|
|
sendCommandUL('v', returnValue); //Return data
|
|
|
|
sendCommandUL('g', 0x6A); //Return data
|
2018-08-03 22:23:20 -04:00
|
|
|
}
|
|
|
|
responseCommand = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Result : if found .val=, 1 else 0
|
2021-01-21 12:37:27 -05:00
|
|
|
/*!
|
|
|
|
@brief Parse commands...
|
2021-01-26 23:49:07 -05:00
|
|
|
*/
|
|
|
|
|
2021-01-21 12:37:27 -05:00
|
|
|
char commandParser(int lastIndex)
|
2018-08-03 22:23:20 -04:00
|
|
|
{
|
2021-01-21 12:37:27 -05:00
|
|
|
//Analysing Forward data
|
2021-01-26 23:49:07 -05:00
|
|
|
//59 58 68 4A 1C 5F 6A E5 FF FF 73
|
2018-08-03 22:23:20 -04:00
|
|
|
//Find Loopback protocol
|
|
|
|
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
2021-01-26 23:49:07 -05:00
|
|
|
//70 6D 2E 76 76 2E 76 61 6C 3D 33 38 34 38 39 35 33 36 32 38 FF FF FF
|
2018-08-03 22:23:20 -04:00
|
|
|
//pm.vv.val=3848953628\xFF\xFF\xFF
|
|
|
|
//1234567890XXX
|
|
|
|
//
|
|
|
|
int startIndex = 0;
|
|
|
|
|
|
|
|
//Loop back command has 13 ~ 23
|
|
|
|
if (lastIndex < 13)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Protocol MAX Length : 22
|
|
|
|
if (lastIndex >= 22)
|
|
|
|
{
|
|
|
|
startIndex = lastIndex - 22;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
startIndex = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = lastIndex - 3; i >= startIndex + 7; i--)
|
|
|
|
{
|
|
|
|
//Find =
|
2021-01-26 23:49:07 -05:00
|
|
|
if (forwardBuff[i - 3] == 'v' && forwardBuff[i - 2] == 'a' && forwardBuff[i - 1] == 'l' && forwardBuff[i] == '=') //0x3D
|
2018-08-03 22:23:20 -04:00
|
|
|
{
|
2021-01-26 23:49:07 -05:00
|
|
|
uint8_t command1 = forwardBuff[i - 6]; //v
|
|
|
|
uint8_t command2 = forwardBuff[i - 5]; //v
|
|
|
|
// i-4 //.
|
2021-01-21 10:44:06 -05:00
|
|
|
forwardBuff[lastIndex - 2] = 0;
|
2021-01-26 23:49:07 -05:00
|
|
|
long commandVal = atol(&forwardBuff[i + 1]);
|
2021-01-21 12:37:27 -05:00
|
|
|
uint8_t *readBuff = (uint8_t *)&commandVal;
|
2021-01-26 23:49:07 -05:00
|
|
|
|
2018-08-03 22:23:20 -04:00
|
|
|
//Loop Back
|
|
|
|
if (command1 == 'v' && command2 == 'v')
|
|
|
|
{
|
2021-01-21 12:37:27 -05:00
|
|
|
int calcChecksum = readBuff[0] + readBuff[1] + readBuff[2];
|
2018-08-03 22:23:20 -04:00
|
|
|
calcChecksum = calcChecksum % 256;
|
|
|
|
|
|
|
|
//Correct Checksum and Receiver is DSP Moudle protocol v1.0
|
2021-01-21 12:37:27 -05:00
|
|
|
if (calcChecksum == readBuff[3] && readBuff[2] == 0x6A)
|
2018-08-03 22:23:20 -04:00
|
|
|
{
|
2021-01-21 12:37:27 -05:00
|
|
|
//Serial1.print("Correct Checksum Command : ");
|
|
|
|
//Serial1.println(readBuff[1]);
|
|
|
|
uint8_t cmd1 = readBuff[1];
|
2018-08-03 22:23:20 -04:00
|
|
|
if (cmd1 == 94)
|
|
|
|
{
|
|
|
|
DSPType = 0;
|
|
|
|
EEPROM.put(EEPROM_DSPTYPE, DSPType);
|
|
|
|
}
|
|
|
|
else if (cmd1 == 95)
|
|
|
|
{
|
2021-01-21 12:37:27 -05:00
|
|
|
//Serial1.println("Spectrum Mode");
|
2018-08-03 22:23:20 -04:00
|
|
|
DSPType = 1;
|
|
|
|
EEPROM.put(EEPROM_DSPTYPE, DSPType);
|
|
|
|
}
|
|
|
|
else if (cmd1 >= 100 && cmd1 <= 145)
|
|
|
|
{
|
|
|
|
cwDecodeHz = cmd1 - 100;
|
2021-01-26 23:22:02 -05:00
|
|
|
//calculateCoeff(cwDecodeHz);
|
2018-08-03 22:23:20 -04:00
|
|
|
DSPType = 2;
|
|
|
|
EEPROM.put(EEPROM_DSPTYPE, DSPType);
|
|
|
|
EEPROM.put(EEPROM_CW_FREQ, cwDecodeHz);
|
|
|
|
}
|
|
|
|
else if (cmd1 > 1 && cmd1 <= 5) //2~5 : Request Configuration
|
|
|
|
{
|
|
|
|
responseCommand = cmd1;
|
|
|
|
}
|
|
|
|
else if (cmd1 == 50 || cmd1 == 51) //Set Configuration
|
|
|
|
{
|
|
|
|
SMeterToUartSend = (cmd1 == 51);
|
|
|
|
EEPROM.put(EEPROM_SMETER_UART, SMeterToUartSend);
|
|
|
|
}
|
|
|
|
else if (cmd1 >= 146 && cmd1 <= 156 )
|
|
|
|
{
|
|
|
|
//Save Mode
|
|
|
|
magnitudelimit_low = (cmd1 - 146) * 10;
|
|
|
|
EEPROM.put(EEPROM_CW_MAG_LOW, magnitudelimit_low);
|
|
|
|
} //end of if
|
|
|
|
} //end of check Checksum
|
|
|
|
} //end of check Protocol (vv)
|
|
|
|
else if (command1 == 'c' && command2 == 't') //TX, RX
|
|
|
|
{
|
|
|
|
if (commandVal == 0) //RX
|
|
|
|
{
|
2021-01-21 23:00:35 -05:00
|
|
|
isTX = false;
|
2018-08-03 22:23:20 -04:00
|
|
|
SMeterToUartIdleCount = 0;
|
|
|
|
}
|
|
|
|
else if (commandVal == 1) //TX
|
|
|
|
{
|
2021-01-21 23:00:35 -05:00
|
|
|
isTX = true;
|
2018-08-03 22:23:20 -04:00
|
|
|
SMeterToUartIdleCount = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
} //end of check Protocol (.val)
|
|
|
|
} //end of for
|
|
|
|
|
|
|
|
//Not found Protocol (.val=
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-01-21 10:44:06 -05:00
|
|
|
|
|
|
|
|
2018-08-03 22:23:20 -04:00
|
|
|
//#define PROTOCOL_TIMEOUT = 100
|
2021-01-21 10:44:06 -05:00
|
|
|
|
|
|
|
/*!
|
|
|
|
@brief Forwards serial data from the RX line to the TX line.
|
2021-01-26 23:49:07 -05:00
|
|
|
*/
|
2021-01-21 10:44:06 -05:00
|
|
|
|
2021-01-21 12:37:27 -05:00
|
|
|
void forwardData(void)
|
2018-08-03 22:23:20 -04:00
|
|
|
{
|
2021-01-21 12:37:27 -05:00
|
|
|
char recvChar;
|
2018-08-03 22:23:20 -04:00
|
|
|
|
2021-01-21 12:37:27 -05:00
|
|
|
if (Serial1.available() > 0)
|
2018-08-03 22:23:20 -04:00
|
|
|
{
|
2021-01-21 12:37:27 -05:00
|
|
|
Serial1.flush();
|
2021-01-21 10:44:06 -05:00
|
|
|
|
|
|
|
// Check RX buffer for available data.
|
2021-01-21 12:37:27 -05:00
|
|
|
while (Serial1.available() > 0)
|
2018-08-03 22:23:20 -04:00
|
|
|
{
|
2021-01-21 12:37:27 -05:00
|
|
|
recvChar = char(Serial1.read());
|
2018-08-03 22:23:20 -04:00
|
|
|
|
2021-01-21 12:37:27 -05:00
|
|
|
forwardBuff[nowBuffIndex] = recvChar;
|
2018-08-03 22:23:20 -04:00
|
|
|
|
2021-01-21 10:44:06 -05:00
|
|
|
if (recvChar == 0xFF) // found ETX
|
2018-08-03 22:23:20 -04:00
|
|
|
{
|
2021-01-21 10:44:06 -05:00
|
|
|
etxCount++; // Nextion protocol, ETX: 0xFF, 0xFF, 0xFF
|
2018-08-03 22:23:20 -04:00
|
|
|
if (etxCount >= 3)
|
|
|
|
{
|
2021-01-21 10:44:06 -05:00
|
|
|
// Finished Protocol
|
2021-01-21 12:37:27 -05:00
|
|
|
if (commandParser(nowBuffIndex) == 1)
|
2018-08-03 22:23:20 -04:00
|
|
|
{
|
2021-01-21 10:44:06 -05:00
|
|
|
nowSendingProtocol = 0; // finished 1 set command
|
2018-08-03 22:23:20 -04:00
|
|
|
etxCount = 0;
|
|
|
|
nowBuffIndex = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-21 10:44:06 -05:00
|
|
|
nowSendingProtocol = 1; // sending data
|
2021-01-21 12:37:27 -05:00
|
|
|
etxCount = 0;
|
2018-08-03 22:23:20 -04:00
|
|
|
}
|
2021-01-21 10:44:06 -05:00
|
|
|
|
2021-01-21 12:37:27 -05:00
|
|
|
Serial1.write(recvChar);
|
2021-01-21 10:44:06 -05:00
|
|
|
sinceForward = 0;
|
2018-08-03 22:23:20 -04:00
|
|
|
nowBuffIndex++;
|
|
|
|
|
2021-01-21 10:44:06 -05:00
|
|
|
if (nowBuffIndex > MAX_FORWARD_BUFF_LENGTH - 2)
|
2018-08-03 22:23:20 -04:00
|
|
|
{
|
|
|
|
nowBuffIndex = 0;
|
|
|
|
}
|
2021-01-21 12:37:27 -05:00
|
|
|
}
|
2018-08-03 22:23:20 -04:00
|
|
|
|
2021-01-21 12:37:27 -05:00
|
|
|
Serial1.flush();
|
2021-01-21 10:44:06 -05:00
|
|
|
}
|
2018-08-03 22:23:20 -04:00
|
|
|
else
|
|
|
|
{
|
2021-01-26 23:49:07 -05:00
|
|
|
// check timeout
|
2018-08-03 22:23:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-26 23:22:02 -05:00
|
|
|
/**********************************************************************/
|
|
|
|
|
2021-01-21 23:00:35 -05:00
|
|
|
void sendMeterData(uint8_t isSend)
|
2018-08-03 22:23:20 -04:00
|
|
|
{
|
2021-01-26 23:22:02 -05:00
|
|
|
scaledSMeter = Sensors.sMeterScaled();
|
2018-08-03 22:23:20 -04:00
|
|
|
|
2021-01-26 23:49:07 -05:00
|
|
|
/*
|
|
|
|
1 : with noise (not use 0 ~ S3)
|
|
|
|
2 : -93 ~ -89
|
|
|
|
3 : -88 ~ -81
|
|
|
|
4 : -80 ~ -78
|
|
|
|
5 : -77 ~ -72
|
|
|
|
6 : -71 ~ -69
|
|
|
|
*/
|
2018-08-03 22:23:20 -04:00
|
|
|
|
|
|
|
if (isSend == 1)
|
|
|
|
{
|
|
|
|
if (L_scaledSMeter != scaledSMeter)
|
|
|
|
{
|
|
|
|
L_scaledSMeter = scaledSMeter;
|
2021-01-26 23:49:07 -05:00
|
|
|
sendCommand1Num(CMD_SMETER, L_scaledSMeter);
|
2018-08-03 22:23:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-26 23:22:02 -05:00
|
|
|
/**********************************************************************/
|
2018-08-03 22:23:20 -04:00
|
|
|
|
2021-01-26 23:22:02 -05:00
|
|
|
//void sendFFTData(void)
|
|
|
|
//{
|
|
|
|
// int readValue = 0;
|
|
|
|
// for (int i = 0; i < 11; i++)
|
|
|
|
// Serial1.write(responseHeader[i]);
|
|
|
|
//
|
|
|
|
// for(int i = 1; i < 64; i++)
|
|
|
|
// {
|
|
|
|
// readValue = (int)(FFTReal[i]);
|
|
|
|
// if (readValue < 0)
|
|
|
|
// {
|
|
|
|
// readValue = 0;
|
|
|
|
// }
|
|
|
|
// else if (readValue>255)
|
|
|
|
// {
|
|
|
|
// readValue=255;
|
|
|
|
// }
|
|
|
|
// Serial1.write(hexCodes[readValue >> 4]);
|
|
|
|
// Serial1.write(hexCodes[readValue & 0xf]);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// for (int i = 0; i < 4; i++)
|
|
|
|
// Serial1.write(responseFooter[i]);
|
|
|
|
//}
|
2018-08-03 22:23:20 -04:00
|
|
|
|
2021-01-26 23:49:07 -05:00
|
|
|
void setup()
|
2018-08-03 22:23:20 -04:00
|
|
|
{
|
2021-01-31 23:46:43 -05:00
|
|
|
#ifdef DEBUG
|
|
|
|
Serial.begin(38400);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
DBGCMD( DSP.begin() );
|
|
|
|
DBGCMD( TR.begin() );
|
|
|
|
|
2021-01-21 12:37:27 -05:00
|
|
|
// load configuration
|
2018-08-03 22:23:20 -04:00
|
|
|
EEPROM.get(EEPROM_DSPTYPE, DSPType);
|
|
|
|
if (DSPType > 5)
|
|
|
|
{
|
|
|
|
DSPType = 1;
|
|
|
|
}
|
|
|
|
|
2021-01-21 12:37:27 -05:00
|
|
|
// signal meter
|
2018-08-03 22:23:20 -04:00
|
|
|
EEPROM.get(EEPROM_SMETER_UART, SMeterToUartSend);
|
|
|
|
if (SMeterToUartSend > 2)
|
|
|
|
{
|
|
|
|
SMeterToUartSend = 1;
|
|
|
|
}
|
2021-01-26 23:49:07 -05:00
|
|
|
|
2021-01-21 12:37:27 -05:00
|
|
|
// something with CW decoding...
|
2018-08-03 22:23:20 -04:00
|
|
|
EEPROM.get(EEPROM_CW_FREQ, cwDecodeHz);
|
|
|
|
if (cwDecodeHz > 40 || cwDecodeHz < 1)
|
|
|
|
{
|
|
|
|
cwDecodeHz = 9;
|
|
|
|
}
|
|
|
|
|
2021-01-21 12:37:27 -05:00
|
|
|
// EEPROM_CW_MAG_LOW
|
2018-08-03 22:23:20 -04:00
|
|
|
EEPROM.get(EEPROM_CW_MAG_LOW, magnitudelimit_low);
|
|
|
|
if (magnitudelimit_low > 1000 || magnitudelimit_low < 1)
|
|
|
|
{
|
|
|
|
magnitudelimit_low = 50;
|
|
|
|
}
|
2021-01-26 23:49:07 -05:00
|
|
|
|
2018-08-03 22:23:20 -04:00
|
|
|
// put your setup code here, to run once:
|
|
|
|
|
2021-01-21 12:37:27 -05:00
|
|
|
// slave Wire1 configuration for communication with the Raduino
|
|
|
|
Wire1.begin(I2CMETER_ADDR);
|
|
|
|
Wire1.onReceive(i2cReceiveEvent);
|
|
|
|
Wire1.onRequest(i2cRequestEvent);
|
|
|
|
|
|
|
|
// Serial1 configuration for communication with Raduino (RX) and Nextion (TX)
|
|
|
|
Serial1.begin(9600, SERIAL_8N1);
|
|
|
|
Serial1.flush();
|
2021-01-26 23:49:07 -05:00
|
|
|
|
2021-01-21 12:37:27 -05:00
|
|
|
SAMPLE_INTERVAL = round(1000000 * (1.0 / SAMPLE_FREQUENCY));
|
2021-01-21 23:00:35 -05:00
|
|
|
//calculateCoeff(cwDecodeHz); //Set 750Hz //9 * 50 + 300 = 750Hz
|
2021-01-21 12:37:27 -05:00
|
|
|
//Serial1.println("Start...");
|
2018-08-03 22:23:20 -04:00
|
|
|
}
|
|
|
|
|
2021-01-21 23:00:35 -05:00
|
|
|
/*!
|
|
|
|
@brief Receive a command via I2C. The most recent command will be received, which will
|
|
|
|
indicate which data the DSP should be preparing to return.
|
|
|
|
@param numBytes
|
|
|
|
Number of bytes received--not used in this procedure.
|
2021-01-26 23:49:07 -05:00
|
|
|
*/
|
2021-01-21 12:37:27 -05:00
|
|
|
void i2cReceiveEvent(size_t numBytes)
|
2018-08-03 22:23:20 -04:00
|
|
|
{
|
2021-01-21 12:37:27 -05:00
|
|
|
int readCommand = 0;
|
2021-01-26 23:49:07 -05:00
|
|
|
|
2021-01-21 12:37:27 -05:00
|
|
|
while (Wire1.available() > 0) // for Last command
|
2018-08-03 22:23:20 -04:00
|
|
|
{
|
2021-01-26 23:49:07 -05:00
|
|
|
readCommand = Wire1.read();
|
|
|
|
// KC4UPR: Note that this looks to be only reading the last command, i.e.
|
|
|
|
// if multiple commands have been queued up, only the last will get executed.
|
2018-08-03 22:23:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (0x50 <= readCommand && readCommand <= 0x59)
|
|
|
|
{
|
2021-01-31 23:46:43 -05:00
|
|
|
#ifdef DEBUG
|
|
|
|
i2cCmdCounter[readCommand - 0x50]++;
|
|
|
|
#endif
|
2021-01-21 12:37:27 -05:00
|
|
|
i2cCommand = readCommand;
|
2018-08-03 22:23:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-21 23:00:35 -05:00
|
|
|
/*!
|
2021-01-26 23:49:07 -05:00
|
|
|
@brief Respond to a request from the I2C Master (Raduino). Returns the appropriate data
|
|
|
|
based on whatever command was previously issued.
|
|
|
|
*/
|
2021-01-21 12:37:27 -05:00
|
|
|
void i2cRequestEvent(void)
|
2018-08-03 22:23:20 -04:00
|
|
|
{
|
2021-01-26 23:49:07 -05:00
|
|
|
//int maxValue = 0;
|
|
|
|
//int minValue = 30000;
|
|
|
|
//int readValue = 0;
|
|
|
|
//unsigned long curr = 0;
|
2018-08-03 22:23:20 -04:00
|
|
|
|
2021-01-21 23:00:35 -05:00
|
|
|
switch (i2cCommand) {
|
|
|
|
case I2CMETER_CALCS:
|
2021-01-26 23:49:07 -05:00
|
|
|
// Returns an already-calculated S-meter value.
|
|
|
|
Wire1.write(scaledSMeter);
|
2021-01-31 23:46:43 -05:00
|
|
|
#ifdef DEBUG
|
|
|
|
i2cRespCounter[i2cCommand - 0x50]++;
|
|
|
|
#endif
|
2021-01-26 23:49:07 -05:00
|
|
|
break;
|
2021-01-21 23:00:35 -05:00
|
|
|
|
|
|
|
case I2CMETER_UNCALCS:
|
2021-01-26 23:49:07 -05:00
|
|
|
// Returns a raw signal strength value.
|
|
|
|
Wire1.write(Sensors.sMeterUnscaled() >> 2); // divided by 4... do we want this?
|
2021-01-31 23:46:43 -05:00
|
|
|
#ifdef DEBUG
|
|
|
|
i2cRespCounter[i2cCommand - 0x50]++;
|
|
|
|
#endif
|
2021-01-26 23:49:07 -05:00
|
|
|
break;
|
2021-01-21 23:00:35 -05:00
|
|
|
|
|
|
|
case I2CMETER_CALCP:
|
2021-01-26 23:49:07 -05:00
|
|
|
// Returns a raw forward power value.
|
2021-01-31 23:46:43 -05:00
|
|
|
Wire1.write(int(fwdPower * 100.0));
|
|
|
|
#ifdef DEBUG
|
|
|
|
i2cRespCounter[i2cCommand - 0x50]++;
|
|
|
|
#endif
|
2021-01-26 23:49:07 -05:00
|
|
|
break;
|
2021-01-21 23:00:35 -05:00
|
|
|
|
|
|
|
case I2CMETER_CALCR:
|
2021-01-26 23:49:07 -05:00
|
|
|
// Returns a raw reverse power value.
|
2021-01-31 23:46:43 -05:00
|
|
|
Wire1.write(int(revPower * 100.0));
|
|
|
|
#ifdef DEBUG
|
|
|
|
i2cRespCounter[i2cCommand - 0x50]++;
|
|
|
|
#endif
|
2021-01-26 23:49:07 -05:00
|
|
|
break;
|
2021-01-21 23:00:35 -05:00
|
|
|
|
|
|
|
default:
|
2021-01-26 23:49:07 -05:00
|
|
|
break;
|
2018-08-03 22:23:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-26 23:22:02 -05:00
|
|
|
//extern void Decode_Morse(float magnitude);
|
|
|
|
//extern double coeff;
|
2018-08-03 22:23:20 -04:00
|
|
|
|
|
|
|
#define LAST_TIME_INTERVAL 159
|
|
|
|
|
2021-01-21 12:37:27 -05:00
|
|
|
// for boot delay, a lot of data to transfer
|
|
|
|
// Delay 2.5 Sec
|
2018-08-05 22:58:41 -04:00
|
|
|
byte isBooted = 0;
|
|
|
|
|
2021-01-26 23:22:02 -05:00
|
|
|
//======================================================================
|
|
|
|
// ADC PROCESSES
|
|
|
|
//======================================================================
|
|
|
|
|
|
|
|
elapsedMillis sinceFrameMillis = 0;
|
|
|
|
elapsedMillis sinceADCMillis = 0;
|
|
|
|
|
|
|
|
#define FRAME_RATE 40
|
|
|
|
#define FRAME_INTERVAL_MS (1000/FRAME_RATE)
|
|
|
|
|
|
|
|
const int frameIntervalMillis = FRAME_INTERVAL_MS;
|
|
|
|
|
|
|
|
#define ADC_SAMPLE_RATE 120
|
|
|
|
#define ADC_INTERVAL_MS (1000/ADC_SAMPLE_RATE)
|
|
|
|
|
|
|
|
const int adcIntervalMillis = ADC_INTERVAL_MS;
|
|
|
|
|
|
|
|
//======================================================================
|
|
|
|
// MAIN LOOP
|
|
|
|
//======================================================================
|
|
|
|
|
2021-01-30 08:49:19 -05:00
|
|
|
#ifdef DEBUG
|
|
|
|
int frameCounter = 0;
|
|
|
|
#endif
|
|
|
|
|
2021-01-26 23:49:07 -05:00
|
|
|
void loop()
|
2018-08-03 22:23:20 -04:00
|
|
|
{
|
2021-01-26 23:22:02 -05:00
|
|
|
//char isProcess = 0; // 0: init, 1: complete ADC sampling, 2: complete FFT
|
|
|
|
//isProcess = 0;
|
2018-08-05 22:58:41 -04:00
|
|
|
|
2021-01-21 10:44:06 -05:00
|
|
|
forwardData();
|
2021-01-26 23:49:07 -05:00
|
|
|
|
2018-08-05 22:58:41 -04:00
|
|
|
if (isBooted < 100)
|
|
|
|
{
|
|
|
|
//Delay 20msec
|
|
|
|
for (int i = 0; i < 20; i++)
|
|
|
|
{
|
2021-01-21 10:44:06 -05:00
|
|
|
forwardData();
|
2018-08-05 22:58:41 -04:00
|
|
|
delay(1);
|
|
|
|
}
|
|
|
|
isBooted++;
|
|
|
|
return;
|
|
|
|
}
|
2018-08-03 22:23:20 -04:00
|
|
|
|
2021-01-26 23:22:02 -05:00
|
|
|
if (sinceFrameMillis > frameIntervalMillis) {
|
|
|
|
// Do stuff that we do once per frame--I/O.
|
|
|
|
// TODO: debug output (frame skipping / utilization).
|
2021-01-26 23:49:07 -05:00
|
|
|
sinceFrameMillis = 0;
|
2019-04-11 09:37:24 -04:00
|
|
|
|
2021-01-30 08:49:19 -05:00
|
|
|
#ifdef DEBUG
|
|
|
|
// For debugging, output some debug info every 1.0" (40 frames @ 40 Hz).
|
|
|
|
frameCounter++;
|
|
|
|
if (frameCounter % 40 == 0) {
|
2021-01-31 23:46:43 -05:00
|
|
|
Serial.println("======================================================================");
|
2021-01-30 08:49:19 -05:00
|
|
|
Serial.print("DBG: Frame: ");
|
|
|
|
Serial.print(frameCounter);
|
|
|
|
if (isTX) {
|
|
|
|
Serial.print(", Loop State: TX");
|
|
|
|
} else {
|
|
|
|
Serial.print(", Loop State: RX");
|
|
|
|
}
|
|
|
|
if (TR.transmitting()) {
|
|
|
|
Serial.println(", TR State: TX");
|
|
|
|
} else {
|
|
|
|
Serial.println(", TR State: RX");
|
|
|
|
}
|
|
|
|
Serial.print("DBG: S-Meter Raw: ");
|
|
|
|
Serial.print(Sensors.sMeterUnscaled());
|
|
|
|
Serial.print(", S-Meter Scaled: ");
|
|
|
|
Serial.println(scaledSMeter);
|
|
|
|
Serial.print("DBG: VSWR Calc: ");
|
|
|
|
Serial.print(calcVSWR, 2);
|
|
|
|
Serial.print(", VSWR Scaled: ");
|
|
|
|
Serial.print(scaledVSWR);
|
|
|
|
Serial.print(", FWD PWR: ");
|
|
|
|
Serial.print(fwdPower, 2);
|
|
|
|
Serial.print(", REV PWR: ");
|
|
|
|
Serial.println(revPower, 2);
|
2021-01-31 23:46:43 -05:00
|
|
|
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();
|
2021-01-30 08:49:19 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-01-30 01:02:57 -05:00
|
|
|
TR.update();
|
|
|
|
|
2021-01-26 23:22:02 -05:00
|
|
|
if (isTX) {
|
|
|
|
calcVSWR = Sensors.VSWR();
|
|
|
|
scaledVSWR = byte(Sensors.scaledVSWR());
|
|
|
|
fwdPower = Sensors.Pfwd();
|
|
|
|
revPower = Sensors.Prev();
|
2021-01-26 23:49:07 -05:00
|
|
|
|
2021-01-26 23:22:02 -05:00
|
|
|
// Send SWR meter information.
|
|
|
|
if (L_scaledVSWR != scaledVSWR) {
|
2021-01-26 23:49:07 -05:00
|
|
|
L_scaledVSWR = scaledVSWR;
|
2021-01-26 23:22:02 -05:00
|
|
|
sendCommand1Num(CMD_SMETER, scaledVSWR);
|
2019-04-11 09:37:24 -04:00
|
|
|
}
|
|
|
|
|
2021-01-26 23:22:02 -05:00
|
|
|
// Send forward power.
|
|
|
|
if (L_fwdPower != fwdPower) {
|
|
|
|
L_fwdPower = fwdPower;
|
2021-01-31 23:46:43 -05:00
|
|
|
sendCommandL('m', int(fwdPower * 100.0)); // watts x 100?
|
2021-01-26 23:22:02 -05:00
|
|
|
sendCommand1Num('m', 2);
|
2019-04-11 09:37:24 -04:00
|
|
|
}
|
|
|
|
|
2021-01-26 23:22:02 -05:00
|
|
|
// Send reverse power.
|
|
|
|
//if (L_revPower != revPower) {
|
|
|
|
// L_revPower = revPower;
|
2021-01-31 23:46:43 -05:00
|
|
|
// sendCommandL('m', int(revPower * 100.0)); // watts x 100?
|
2021-01-26 23:22:02 -05:00
|
|
|
// sendCommand1Num('m', 2);
|
|
|
|
//}
|
2021-01-26 23:49:07 -05:00
|
|
|
|
2021-01-26 23:22:02 -05:00
|
|
|
// Does there need to be some kind of 250-500ms delay after this???
|
2021-01-26 23:49:07 -05:00
|
|
|
// Delay 250msec ~ 500msec for Nextion LCD Processing (using m protocol)
|
2021-01-26 23:22:02 -05:00
|
|
|
//for (int i = 0; i < 10; i++) {
|
|
|
|
// forwardData();
|
|
|
|
// if (!isTX) { //if TX -> RX break
|
|
|
|
// break;
|
|
|
|
// }
|
|
|
|
// delay(25);
|
|
|
|
//} //end of delay time
|
2021-01-26 23:49:07 -05:00
|
|
|
|
2021-01-26 23:22:02 -05:00
|
|
|
// Send SWR.
|
2021-01-26 23:49:07 -05:00
|
|
|
if (L_calcVSWR != calcVSWR) {
|
|
|
|
L_calcVSWR = calcVSWR;
|
|
|
|
sendCommandL('m', int(calcVSWR * 100.0)); // SWR x 100?
|
2021-01-26 23:22:02 -05:00
|
|
|
sendCommand1Num('m', 3);
|
2019-04-11 09:37:24 -04:00
|
|
|
}
|
2021-01-26 23:49:07 -05:00
|
|
|
|
2021-01-26 23:22:02 -05:00
|
|
|
} else { // RX
|
2019-04-11 09:37:24 -04:00
|
|
|
|
2021-01-26 23:22:02 -05:00
|
|
|
// Send Signal Meter to UART
|
|
|
|
if (SMeterToUartSend == 1 && nowSendingProtocol == 0) //SMeter To Uart Send
|
2019-04-11 09:37:24 -04:00
|
|
|
{
|
2021-01-26 23:22:02 -05:00
|
|
|
//nowSendingProtocol -> not finished data forward, (not found 0xff, 0xff, 0xff yet)
|
|
|
|
sendMeterData(1);
|
|
|
|
} else {
|
|
|
|
sendMeterData(0); //only calculate Signal Level
|
|
|
|
}
|
2021-01-26 23:49:07 -05:00
|
|
|
|
2019-04-11 09:37:24 -04:00
|
|
|
}
|
2018-08-03 22:23:20 -04:00
|
|
|
|
2021-01-26 23:22:02 -05:00
|
|
|
// Forward any data that came in while we were updating stuff.
|
2021-01-26 23:49:07 -05:00
|
|
|
forwardData();
|
2018-08-03 22:23:20 -04:00
|
|
|
}
|
|
|
|
|
2021-01-26 23:22:02 -05:00
|
|
|
if (sinceADCMillis > adcIntervalMillis) {
|
|
|
|
// Do stuff that we do once per ADC interval--ADC colllection.
|
|
|
|
// TODO: debug output (frame skipping / utilization).
|
2021-01-26 23:49:07 -05:00
|
|
|
sinceADCMillis = 0;
|
2018-08-03 22:23:20 -04:00
|
|
|
|
2021-01-26 23:22:02 -05:00
|
|
|
if (isTX) {
|
|
|
|
Sensors.updatePower();
|
|
|
|
} else { // RX
|
|
|
|
Sensors.updateSMeter();
|
|
|
|
Sensors.updateSupply();
|
2018-08-03 22:23:20 -04:00
|
|
|
}
|
2021-01-26 23:22:02 -05:00
|
|
|
|
|
|
|
// Forward any data that came in while we were reading sensors.
|
2021-01-26 23:49:07 -05:00
|
|
|
//forwardData();
|
2018-08-03 22:23:20 -04:00
|
|
|
}
|
|
|
|
|
2021-01-26 23:22:02 -05:00
|
|
|
// Check Response Command
|
2021-01-21 10:44:06 -05:00
|
|
|
if (responseCommand > 0 && sinceForward > LAST_TIME_INTERVAL)
|
2018-08-03 22:23:20 -04:00
|
|
|
{
|
2021-01-21 12:37:27 -05:00
|
|
|
responseConfig();
|
2018-08-03 22:23:20 -04:00
|
|
|
}
|
|
|
|
|
2021-01-26 23:49:07 -05:00
|
|
|
// //===========================================
|
|
|
|
// //TRANSCEIVER STATUS : RX
|
|
|
|
// //===========================================
|
|
|
|
// //===================================================================================
|
|
|
|
// // DSP Routine
|
|
|
|
// //===================================================================================
|
|
|
|
// if (DSPType == 1 && sinceForward > LAST_TIME_INTERVAL) // spectrum: FFT => send To UART
|
|
|
|
// {
|
|
|
|
// FFTToUartIdleCount = 0;
|
|
|
|
//
|
|
|
|
// if (isProcess == 1)
|
|
|
|
// {
|
|
|
|
// FFT(FFTReal, FFTImag, SAMPLESIZE, 7);
|
|
|
|
// isProcess = 2;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// forwardData();
|
|
|
|
//
|
|
|
|
// if (isProcess == 2)
|
|
|
|
// {
|
|
|
|
// for (uint16_t k = 0; k < SAMPLESIZE; k++)
|
|
|
|
// {
|
|
|
|
// FFTReal[k] = sqrt(FFTReal[k] * FFTReal[k] + FFTImag[k] * FFTImag[k]);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// isProcess = 3;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// forwardData();
|
|
|
|
//
|
|
|
|
// if (isProcess == 3)
|
|
|
|
// {
|
|
|
|
// if (nowSendingProtocol == 0) //Idle Status
|
|
|
|
// {
|
|
|
|
// sendFFTData();
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// else if (DSPType == 2) //Decode Morse
|
|
|
|
// {
|
|
|
|
// //Implement Goertzel_algorithm
|
|
|
|
// //https://en.wikipedia.org/wiki/Goertzel_algorithm
|
|
|
|
//
|
|
|
|
// /*
|
|
|
|
// ω = 2 * π * Kterm / Nterms;
|
|
|
|
// cr = cos(ω);
|
|
|
|
// ci = sin(ω);
|
|
|
|
// coeff = 2 * cr;
|
|
|
|
//
|
|
|
|
// sprev = 0;
|
|
|
|
// sprev2 = 0;
|
|
|
|
// for each index n in range 0 to Nterms-1
|
|
|
|
// s = x[n] + coeff * sprev - sprev2;
|
|
|
|
// sprev2 = sprev;
|
|
|
|
// sprev = s;
|
|
|
|
// end
|
|
|
|
//
|
|
|
|
// power = sprev2 * sprev2 + sprev * sprev - coeff * sprev * sprev2;
|
|
|
|
// */
|
|
|
|
// double Q1 = 0;
|
|
|
|
// double Q2 = 0;
|
|
|
|
//
|
|
|
|
// for (unsigned index = 0; index < DECODE_MORSE_SAMPLESIZE; index++)
|
|
|
|
// {
|
|
|
|
// float Q0;
|
|
|
|
// Q0 = coeff * Q1 - Q2 + FFTReal[index];
|
|
|
|
// Q2 = Q1;
|
|
|
|
// Q1 = Q0;
|
|
|
|
// }
|
|
|
|
// double magnitudeSquared = (Q1*Q1)+(Q2*Q2)-Q1*Q2*coeff; // we do only need the real part //
|
|
|
|
// double magnitude = sqrt(magnitudeSquared);
|
|
|
|
//
|
|
|
|
// Decode_Morse(magnitude);
|
|
|
|
// } //end of if
|
2021-01-26 23:22:02 -05:00
|
|
|
|
|
|
|
}
|
2021-01-21 10:44:06 -05:00
|
|
|
|
|
|
|
//======================================================================
|
|
|
|
// EOF
|
|
|
|
//======================================================================
|