Additional modifications to retarget the code for Teensy with the Audio Adapter.

This commit is contained in:
Rob French 2021-01-21 11:37:27 -06:00
parent c1c4dd3f19
commit bb31ccfbe4
4 changed files with 201 additions and 580 deletions

View File

@ -12,25 +12,13 @@ KD8CEC, Ian Lee
//================================================================
//COMMUNICATION SECTION
//================================================================
//#define USE_SW_SERIAL
extern void SWSerial_Write(uint8_t b);
extern void SWSerial_Print(uint8_t *b);
#ifdef USE_SW_SERIAL
extern void SWSerial_Begin(long speedBaud);
extern int SWSerial_Available(void);
extern int SWSerial_Read(void);
#else
#define PRINT_MAX_LENGTH 30
#endif
//================================================================
//FFT and Decode Morse
// FFT and Decode Morse
//================================================================
#define FFTSIZE 64
#define SAMPLE_PREQUENCY 6000
#define SAMPLE_FREQUENCY 6000
#define SAMPLESIZE (FFTSIZE * 2)
#define DECODE_MORSE_SAMPLESIZE 48
@ -38,8 +26,9 @@ extern uint8_t cwDecodeHz;
extern int magnitudelimit_low;
//================================================================
//EEPROM Section
// EEPROM Section
//================================================================
#define MAX_FORWARD_BUFF_LENGTH 128
#define EEPROM_DSPTYPE 100
#define EEPROM_SMETER_UART 111
@ -52,8 +41,9 @@ extern int magnitudelimit_low;
#define EEPROM_RTTYDECODEHZ 130
//================================================================
//DEFINE for I2C Command
// DEFINE for I2C Command
//================================================================
//S-Meter Address
#define I2CMETER_ADDR 0x58 //changed from 0x6A
//VALUE TYPE============================================

View File

@ -21,9 +21,9 @@ char softINTHeader[10] = {'p', 'm', '.', 'v', '0', '.', 'v', 'a', 'l', '='};
char softTemp[20];
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', };
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', };
void FFT(double *x,double *y, int n, long m);
@ -44,6 +44,7 @@ char nowADCSampling = 0; //prevent for loss signal
// vlSendxxx, vloxxx: Reserve for Nextion (Nextion LCD -> Transceiver)
//
//===================================================================
#define CMD_NOW_DISP '0' //c0
char L_nowdisp = -1; //Sended nowdisp
@ -118,6 +119,7 @@ byte L_sdrModeOn;
//Once Send Data, When boot
//arTuneStep, When boot, once send
//long arTuneStep[5];
#define CMD_AR_TUNE1 '1' //v1
#define CMD_AR_TUNE2 '2' //v2
#define CMD_AR_TUNE3 '3' //v3
@ -127,77 +129,113 @@ byte L_sdrModeOn;
//int idleStep = 0;
byte scaledSMeter = 0;
//send data for Nextion LCD
void SendHeader(char varType, char varIndex)
/*!
@brief Send a string or numeric variable to the Nextion LCD.
@param varType
The type of the variable being sent to the Nextion LCD.
@param varIndex
The index (ID) of the variable being sent to the Nextion LCD.
*/
void sendHeader(char varType, char varIndex)
{
if (varType == SWS_HEADER_STR_TYPE)
{
softSTRHeader[4] = varIndex;
for (int i = 0; i < 11; i++)
SWSerial_Write(softSTRHeader[i]);
for (unsigned i = 0; i < sizeof(softSTRHeader)/sizeof(softSTRHeader[0]); i++)
Serial1.write(softSTRHeader[i]);
}
else
{
softINTHeader[4] = varIndex;
for (int i = 0; i < 10; i++)
SWSerial_Write(softINTHeader[i]);
for (unsigned i = 0; i < sizeof(softINTHeader)/sizeof(softINTHeader[0]); i++)
Serial1.write(softINTHeader[i]);
}
}
void SendCommandUL(char varIndex, unsigned long sendValue)
/*!
@brief Send an unsigned long variable to the Nextion LCD.
@param varIndex
The index (ID) of the variable being sent to the Nextion LCD.
@param sendValue
The value of the variable being sent to the Nextion LCD.
*/
void sendCommandUL(char varIndex, unsigned long sendValue)
{
SendHeader(SWS_HEADER_INT_TYPE, varIndex);
sendHeader(SWS_HEADER_INT_TYPE, varIndex);
memset(softTemp, 0, 20);
ultoa(sendValue, softTemp, DEC);
SWSerial_Print(softTemp);
SWSerial_Write(0xff);
SWSerial_Write(0xff);
SWSerial_Write(0xff);
Serial1.print(softTemp);
Serial1.write(0xff);
Serial1.write(0xff);
Serial1.write(0xff);
}
void SendCommandL(char varIndex, long sendValue)
/*!
@brief Send a (signed) long variable to the Nextion LCD.
@param varIndex
The index (ID) of the variable being sent to the Nextion LCD.
@param sendValue
The value of the variable being sent to the Nextion LCD.
*/
void sendCommandL(char varIndex, long sendValue)
{
SendHeader(SWS_HEADER_INT_TYPE, varIndex);
sendHeader(SWS_HEADER_INT_TYPE, varIndex);
memset(softTemp, 0, 20);
ltoa(sendValue, softTemp, DEC);
SWSerial_Print(softTemp);
SWSerial_Write(0xff);
SWSerial_Write(0xff);
SWSerial_Write(0xff);
Serial1.print(softTemp);
Serial1.write(0xff);
Serial1.write(0xff);
Serial1.write(0xff);
}
void SendCommandStr(char varIndex, char* sendValue)
/*!
@brief Send a string variable to the Nextion LCD.
@param varIndex
The index (ID) of the variable being sent to the Nextion LCD.
@param sendValue
The value of the variable being sent to the Nextion LCD.
*/
void sendCommandStr(char varIndex, const char* sendValue)
{
SendHeader(SWS_HEADER_STR_TYPE, varIndex);
sendHeader(SWS_HEADER_STR_TYPE, varIndex);
SWSerial_Print(sendValue);
SWSerial_Write('\"');
SWSerial_Write(0xFF);
SWSerial_Write(0xFF);
SWSerial_Write(0xFF);
Serial1.print(sendValue);
Serial1.write('\"');
Serial1.write(0xFF);
Serial1.write(0xFF);
Serial1.write(0xFF);
}
unsigned char softBuff1Num[14] = {'p', 'm', '.', 'c', '0', '.', 'v', 'a', 'l', '=', 0, 0xFF, 0xFF, 0xFF};
void SendCommand1Num(char varType, char sendValue) //0~9 : Mode, nowDisp, ActiveVFO, IsDialLock, IsTxtType, IsSplitType
{
softBuff1Num[4] = varType;
softBuff1Num[10] = sendValue + 0x30;
for (int i = 0; i < 14; i++)
SWSerial_Write(softBuff1Num[i]);
/*!
@brief Send a single digit variable to the Nextion LCD.
@param varIndex
The index (ID) of the variable being sent to the Nextion LCD.
Values 0~9 are: Mode, nowDisp, ActiveVFO, IsDialLock, IsTxtType, IsSplitType.
@param sendValue
The value of the variable being sent to the Nextion LCD.
*/
void sendCommand1Num(char varIndex, char sendValue)
{
softBuff1Num[4] = varIndex;
softBuff1Num[10] = sendValue + 0x30; // convert to character digit
for (unsigned i = 0; i < sizeof(softBuff1Num)/sizeof(softBuff1Num[0]); i++)
Serial1.write(softBuff1Num[i]);
}
//=======================================================
//END OF Nextion Protocol
//=======================================================
int I2CCommand = 0;
int i2cCommand = 0;
void CalculateCoeff(uint8_t freqIndex);
char forwardBuff[MAX_FORWARD_BUFF_LENGTH + 1];
static char nowBuffIndex = 0;
static int nowBuffIndex = 0;
static char etxCount = 0;
static char nowSendingProtocol = 0;
@ -212,7 +250,7 @@ char FFTToUartIdleCount = 0;
elapsedMillis sinceForward = 0;
uint8_t responseCommand = 0; //
uint8_t TXStatus = 0; //0:RX, 1:TX
void ResponseConfig()
void responseConfig()
{
if (responseCommand == 2)
{
@ -242,16 +280,20 @@ void ResponseConfig()
tmpValue = magnitudelimit_low;
returnValue = returnValue | (tmpValue & 0xFF);
SendCommandUL('v', returnValue); //Return data
SendCommandUL('g', 0x6A); //Return data
sendCommandUL('v', returnValue); //Return data
sendCommandUL('g', 0x6A); //Return data
}
responseCommand = 0;
}
//Result : if found .val=, 1 else 0
char CommandPasrser(int lastIndex)
/*!
@brief Parse commands...
*/
char commandParser(int lastIndex)
{
//Analysing Forwrd data
//Analysing Forward data
//59 58 68 4A 1C 5F 6A E5 FF FF 73
//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
@ -287,75 +329,20 @@ char CommandPasrser(int lastIndex)
// i-4 //.
forwardBuff[lastIndex - 2] = 0;
long commandVal=atol(&forwardBuff[i + 1]);
uint8_t *ReadBuff = (uint8_t *)&commandVal;
uint8_t *readBuff = (uint8_t *)&commandVal;
//Loop Back
if (command1 == 'v' && command2 == 'v')
{
//===========================================================
//Test Code 1
/*
long tmpVal = 0;
Serial.print("Found :");
for (int k = i + 1; k <= lastIndex - 3; k++)
{
Serial.write(forwardBuff[k]);
}
Serial.println();
Serial.print("Reverse :");
for (int k = lastIndex - 3; k >= i + 1; k--)
{
Serial.write(forwardBuff[k]);
}
Serial.println();
forwardBuff[lastIndex - 2] = 0;
tmpVal=atol(&forwardBuff[i + 1]);
Serial.println(tmpVal);
uint8_t *ReadBuff = (uint8_t *)&tmpVal;
char buff[10];
sprintf(buff, "%x,%x,%x,%x", ReadBuff[0], ReadBuff[1], ReadBuff[2], ReadBuff[3]);
Serial.println(buff);
int calcChecksumA = ReadBuff[0] + ReadBuff[1] + ReadBuff[2];
calcChecksumA = calcChecksumA % 256;
if (calcChecksumA == ReadBuff[3])
{
Serial.print("Correct Checksum : ");
Serial.print(ReadBuff[3]);
Serial.print(":");
Serial.println(calcChecksumA);
}
//End of Test Code
*/
/*
//Test Code 2
Serial.print("Found :");
Serial.print(startIndex);
Serial.print(",");
Serial.print(lastIndex);
Serial.print(":");
for (int k = i + 1; k <= lastIndex - 3; k++)
{
Serial.write(forwardBuff[k]);
}
Serial.println("");
//End of Tet Code 2
*/
int calcChecksum = ReadBuff[0] + ReadBuff[1] + ReadBuff[2];
int calcChecksum = readBuff[0] + readBuff[1] + readBuff[2];
calcChecksum = calcChecksum % 256;
//Correct Checksum and Receiver is DSP Moudle protocol v1.0
if (calcChecksum == ReadBuff[3] && ReadBuff[2] == 0x6A)
if (calcChecksum == readBuff[3] && readBuff[2] == 0x6A)
{
//Serial.print("Correct Checksum Command : ");
//Serial.println(ReadBuff[1]);
uint8_t cmd1 = ReadBuff[1];
//Serial1.print("Correct Checksum Command : ");
//Serial1.println(readBuff[1]);
uint8_t cmd1 = readBuff[1];
if (cmd1 == 94)
{
DSPType = 0;
@ -363,7 +350,7 @@ char CommandPasrser(int lastIndex)
}
else if (cmd1 == 95)
{
//Serial.println("Spectrum Mode");
//Serial1.println("Spectrum Mode");
DSPType = 1;
EEPROM.put(EEPROM_DSPTYPE, DSPType);
}
@ -422,20 +409,20 @@ char CommandPasrser(int lastIndex)
@brief Forwards serial data from the RX line to the TX line.
*/
int forwardData(void)
void forwardData(void)
{
int recvChar;
char recvChar;
if (Serial.available() > 0)
if (Serial1.available() > 0)
{
Serial.flush();
Serial1.flush();
// Check RX buffer for available data.
while (Serial.available() > 0)
while (Serial1.available() > 0)
{
recvChar = Serial.read();
recvChar = char(Serial1.read());
forwardBuff[nowBuffIndex] = char(recvChar);
forwardBuff[nowBuffIndex] = recvChar;
if (recvChar == 0xFF) // found ETX
{
@ -443,7 +430,7 @@ int forwardData(void)
if (etxCount >= 3)
{
// Finished Protocol
if (CommandPasrser(nowBuffIndex) == 1)
if (commandParser(nowBuffIndex) == 1)
{
nowSendingProtocol = 0; // finished 1 set command
etxCount = 0;
@ -453,11 +440,11 @@ int forwardData(void)
}
else
{
etxCount = 0x00;
nowSendingProtocol = 1; // sending data
etxCount = 0;
}
Serial.write(recvChar);
Serial1.write(recvChar);
sinceForward = 0;
nowBuffIndex++;
@ -465,9 +452,9 @@ int forwardData(void)
{
nowBuffIndex = 0;
}
} //end of while
}
Serial.flush();
Serial1.flush();
}
else
{
@ -475,7 +462,7 @@ int forwardData(void)
}
}
int SendMeterData(uint8_t isSend)
void SendMeterData(uint8_t isSend)
{
//basic : 1.5Khz
int newScaledSMeter = 0;
@ -535,15 +522,15 @@ int SendMeterData(uint8_t isSend)
if (L_scaledSMeter != scaledSMeter)
{
L_scaledSMeter = scaledSMeter;
SendCommand1Num(CMD_SMETER, L_scaledSMeter);
sendCommand1Num(CMD_SMETER, L_scaledSMeter);
}
}
}
void GrepADC(void)
void grepADC(void)
{
int readedValue = 0;
int readValue = 0;
unsigned long currentms = 0;
int readSampleCount = 0;
@ -566,72 +553,73 @@ void GrepADC(void)
for(int i=0; i < readSampleCount; i++)
{
currentms = micros();
readedValue = analogRead(SIGNAL_METER_ADC);;
FFTReal[i] = readedValue;
readValue = analogRead(SIGNAL_METER_ADC);;
FFTReal[i] = readValue;
FFTImag[i] = 0;
if (ADC_MAX < readedValue)
if (ADC_MAX < readValue)
{
ADC_MAX = readedValue;
ADC_MAX = readValue;
}
if (ADC_MIN > readedValue)
if (ADC_MIN > readValue)
{
ADC_MIN = readedValue;
ADC_MIN = readValue;
}
while(micros() < (currentms + SAMPLE_INTERVAL)){}
} //end of for
}
void SendFFTData(void)
void sendFFTData(void)
{
int readedValue = 0;
int readValue = 0;
for (int i = 0; i < 11; i++)
SWSerial_Write(ResponseHeader[i]);
Serial1.write(responseHeader[i]);
for(int i = 1; i < 64; i++)
{
readedValue = (int)(FFTReal[i]);
if (readedValue < 0)
readValue = (int)(FFTReal[i]);
if (readValue < 0)
{
readedValue = 0;
readValue = 0;
}
else if (readedValue>255)
else if (readValue>255)
{
readedValue=255;
readValue=255;
}
SWSerial_Write(HexCodes[readedValue >> 4]);
SWSerial_Write(HexCodes[readedValue & 0xf]);
Serial1.write(hexCodes[readValue >> 4]);
Serial1.write(hexCodes[readValue & 0xf]);
}
for (int i = 0; i < 4; i++)
SWSerial_Write(ResponseFooter[i]);
Serial1.write(responseFooter[i]);
}
void setup()
{
//Load Configuration
// load configuration
EEPROM.get(EEPROM_DSPTYPE, DSPType);
if (DSPType > 5)
{
DSPType = 1;
}
//Signal Meter
// signal meter
EEPROM.get(EEPROM_SMETER_UART, SMeterToUartSend);
if (SMeterToUartSend > 2)
{
SMeterToUartSend = 1;
}
//
// something with CW decoding...
EEPROM.get(EEPROM_CW_FREQ, cwDecodeHz);
if (cwDecodeHz > 40 || cwDecodeHz < 1)
{
cwDecodeHz = 9;
}
//EEPROM_CW_MAG_LOW
// EEPROM_CW_MAG_LOW
EEPROM.get(EEPROM_CW_MAG_LOW, magnitudelimit_low);
if (magnitudelimit_low > 1000 || magnitudelimit_low < 1)
{
@ -639,42 +627,41 @@ void setup()
}
// put your setup code here, to run once:
Wire1.begin(I2CMETER_ADDR); //j : S-Meter Slave Address
//Wire1.begin(0x21); //j : S-Meter Slave Address
Wire1.onReceive(I2CReceiveEvent); //
Wire1.onRequest(I2CRequestEvent);
//#ifdef USE_SW_SERIAL
// SWSerial_Begin(9600);
//#endif
Serial.begin(9600, SERIAL_8N1);
Serial.flush();
SAMPLE_INTERVAL = round(1000000 * (1.0 / SAMPLE_PREQUENCY));
// 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();
SAMPLE_INTERVAL = round(1000000 * (1.0 / SAMPLE_FREQUENCY));
//CalculateCoeff(cwDecodeHz); //Set 750Hz //9 * 50 + 300 = 750Hz
//Serial.println("Start...");
//Serial1.println("Start...");
}
void I2CReceiveEvent(void)
void i2cReceiveEvent(size_t numBytes)
{
int readCommand = 0; // byte를 읽어 int로 변환
int readCommand = 0;
while(Wire1.available() > 0) // for Last command
while (Wire1.available() > 0) // for Last command
{
readCommand = Wire1.read();
}
if (0x50 <= readCommand && readCommand <= 0x59)
{
I2CCommand = readCommand;
i2cCommand = readCommand;
}
}
void I2CRequestEvent(void)
void i2cRequestEvent(void)
{
int maxValue = 0;
int minValue = 30000;
int readedValue = 0;
int readValue = 0;
unsigned long curr = 0;
//if (nowADCSampling == 1) //Now Sampling ADC's
@ -683,53 +670,53 @@ void I2CRequestEvent(void)
// return;
//} //end of if
if (I2CCommand == I2CMETER_CALCS)
if (i2cCommand == I2CMETER_CALCS)
{
Wire1.write(scaledSMeter);
}
else if (I2CCommand == I2CMETER_UNCALCS)
else if (i2cCommand == I2CMETER_UNCALCS)
{
//Wire1.write(ADC_DIFF);
//8292Hz
for(int i=0; i < 7; i++)
{
curr = micros();
readedValue = analogRead(SIGNAL_METER_ADC);;
readValue = analogRead(SIGNAL_METER_ADC);;
if (readedValue > maxValue)
if (readValue > maxValue)
{
maxValue = readedValue;
maxValue = readValue;
}
if (readedValue < minValue)
if (readValue < minValue)
{
minValue = readedValue;
minValue = readValue;
}
while(micros() < (curr + 127)){} //8Khz / 7
} //end of for
readedValue = maxValue - minValue;
readedValue = readedValue * readedValue;
//readedValue = readedValue / 2;
if (readedValue < 0)
readValue = maxValue - minValue;
readValue = readValue * readValue;
//readValue = readValue / 2;
if (readValue < 0)
{
readedValue = 0;
readValue = 0;
}
else if (readedValue > 255)
else if (readValue > 255)
{
readedValue = 255;
readValue = 255;
}
Wire1.write(readedValue);
Wire1.write(readValue);
}
else if (I2CCommand == I2CMETER_CALCP)
else if (i2cCommand == I2CMETER_CALCP)
{
readedValue = analogRead(POWER_METER_ADC); //POWER
Wire1.write(readedValue);
readValue = analogRead(POWER_METER_ADC); //POWER
Wire1.write(readValue);
}
else if (I2CCommand == I2CMETER_CALCR) //SWR
else if (i2cCommand == I2CMETER_CALCR) //SWR
{
readedValue = analogRead(SWR_METER_ADC);
Wire1.write(readedValue);
readValue = analogRead(SWR_METER_ADC);
Wire1.write(readValue);
}
}
@ -748,13 +735,13 @@ float swrMeasured;
int clcCount = 0;
//for boot Delay, a alot of data transfer
//Delay 2.5 Sec
// for boot delay, a lot of data to transfer
// Delay 2.5 Sec
byte isBooted = 0;
void loop()
{
char isProcess = 0; //0 : Init, 1 : Complete ADC Sampling, 2 : Complete FFT
char isProcess = 0; // 0: init, 1: complete ADC sampling, 2: complete FFT
isProcess = 0;
forwardData();
@ -777,21 +764,21 @@ void loop()
if (TXStatus == 1) //TX Mode
{
/*
int readedValue = 0;
int readValue = 0;
SMeterToUartIdleCount++;
if (SMeterToUartIdleCount > 130) //SWR
{
//SWR Send
SendCommandL('m', SWRAdcValue);
SendCommand1Num('m', 3);
sendCommandL('m', SWRAdcValue);
sendCommand1Num('m', 3);
SMeterToUartIdleCount = 0;
}
else if (SMeterToUartIdleCount == 100) //POWER 500msec interval
{
readedValue = analogRead(POWER_METER_ADC );
readValue = analogRead(POWER_METER_ADC );
SWRAdcValue = analogRead(SWR_METER_ADC);
SendCommandL('m', readedValue);
SendCommand1Num('m',2);
sendCommandL('m', readValue);
sendCommand1Num('m',2);
//PWR Send
}
*/
@ -806,7 +793,7 @@ void loop()
adcRWD = adcFWD;
}
//for Realtime LCD Display
//for realtime LCD Display
forwardData();
if (clcCount++ > 10)
@ -845,20 +832,20 @@ void loop()
if (L_scaledSMeter != scaledSMeter)
{
L_scaledSMeter = scaledSMeter;
SendCommand1Num(CMD_SMETER, L_scaledSMeter);
sendCommand1Num(CMD_SMETER, L_scaledSMeter);
}
//For Version 1.0
//need mod uBITX firmware and Nextion LCD and add Power adjust value option (uBITX Manager)
//Send Information
//SWR Send
//SendCommandL('m', SWRAdcValue);
//SendCommand1Num('m', 3);
//sendCommandL('m', SWRAdcValue);
//sendCommand1Num('m', 3);
//SMeterToUartIdleCount = 0;
//Send Power Information
int readedValue = (int)(PWR * 100);
SendCommandL('m', readedValue);
SendCommand1Num('m',2);
int readValue = (int)(PWR * 100);
sendCommandL('m', readValue);
sendCommand1Num('m',2);
//Delay 250msec ~ 500msec for Nextion LCD Processing (using m protocol)
@ -873,9 +860,9 @@ void loop()
} //end of delay time
//Send SWR
readedValue = (int)(swrMeasured * 100);
SendCommandL('m', readedValue);
SendCommand1Num('m', 3);
readValue = (int)(swrMeasured * 100);
sendCommandL('m', readValue);
sendCommand1Num('m', 3);
//delay(50);
//return;
@ -894,12 +881,12 @@ void loop()
if (nowSendingProtocol == 0) //Idle Status
{
nowADCSampling = 1; //Mark => Start Sampling
GrepADC();
grepADC();
//if(nowADCSampling == 2) //Marked ? While ADC Sampling, receive I2C
//{
// nowADCSampling = 0; //Mark => Finish Sampling
// I2CRequestEvent();
// i2cRequestEvent();
//}
nowADCSampling = 0; //Mark => Finish Sampling
@ -939,7 +926,7 @@ void loop()
//Check Response Command
if (responseCommand > 0 && sinceForward > LAST_TIME_INTERVAL)
{
ResponseConfig();
responseConfig();
}
//===================================================================================
@ -973,7 +960,7 @@ void loop()
{
if (nowSendingProtocol == 0) //Idle Status
{
SendFFTData();
sendFFTData();
}
}
}
@ -1001,7 +988,7 @@ void loop()
double Q1 = 0;
double Q2 = 0;
for (char index = 0; index < DECODE_MORSE_SAMPLESIZE; index++)
for (unsigned index = 0; index < DECODE_MORSE_SAMPLESIZE; index++)
{
float Q0;
Q0 = coeff * Q1 - Q2 + FFTReal[index];

View File

@ -7,7 +7,7 @@ KD8CEC, Ian Lee
License : I follow the license of the previous code and I do not add any extra constraints.
I hope that the Comment I made or the Comment of OZ1JHM will be maintained.
**********************************************************************/
#include <arduino.h>
#include <Arduino.h>
#include "TeensyDSP.h"
// Code Referency : http://paulbourke.net/miscellaneous/dft/
@ -92,7 +92,7 @@ void CalculateCoeff(uint8_t freqIndex)
{
float omega;
int targetFrequency = freqIndex * 50 + 300;
int k = (int) (0.5 + ((DECODE_MORSE_SAMPLESIZE * targetFrequency) / SAMPLE_PREQUENCY));
int k = (int) (0.5 + ((DECODE_MORSE_SAMPLESIZE * targetFrequency) / SAMPLE_FREQUENCY));
omega = (2.0 * PI * k) / DECODE_MORSE_SAMPLESIZE;
coeff = 2.0 * cos(omega);
}
@ -143,7 +143,7 @@ uint8_t stop = LOW;
int wpm;
uint8_t cwDecodeHz = 9;
extern void SendCommandStr(char varIndex, char* sendValue);
extern void sendCommandStr(char varIndex, const char* sendValue);
void printascii(int asciinumber)
{
@ -165,7 +165,7 @@ void printascii(int asciinumber)
{
rstDecode[0] = asciinumber;
}
SendCommandStr('b', rstDecode);
sendCommandStr('b', rstDecode);
//Serial.write(asciinumber);
//if (writeCount++ > 20)
@ -359,4 +359,3 @@ void Decode_Morse(float magnitude)
lasthighduration = highduration;
filteredstatebefore = filteredstate;
}

View File

@ -1,355 +0,0 @@
/*
Softserial for Nextion LCD and Control MCU
KD8CEC, Ian Lee
-----------------------------------------------------------------------
It is a library rewritten in C format based on SoftwareSerial.c.
I tried to use as much as possible without modifying the SoftwareSerial.
But eventually I had to modify the code.
I rewrote it in C for the following reasons.
- Problems occurred when increasing Program Size and Program Memory
- We had to reduce the program size.
Of course, Software Serial is limited to one.
- reduce the steps for transmitting and receiving
useage
extern void SWSerial_Begin(long speedBaud);
extern void SWSerial_Write(uint8_t b);
extern int SWSerial_Available(void);
extern int SWSerial_Read(void);
extern void SWSerial_Print(uint8_t *b);
If you use Softwreserial library instead of this library, you can modify the code as shown below.
I kept the function name of SoftwareSerial so you only need to modify a few lines of code.
define top of source code
#include <SoftwareSerial.h>
SoftwareSerial sSerial(10, 11); // RX, TX
replace source code
SWSerial_Begin to sSerial.begin
SWSerial_Write to sSerial.write
SWSerial_Available to sSerial.available
SWSerial_Read to sSerial.read
KD8CEC, Ian Lee
-----------------------------------------------------------------------
License
All licenses for the source code are subject to the license of the original source SoftwareSerial Library.
However, if you use or modify this code, please keep the all comments in this source code.
KD8CEC
-----------------------------------------------------------------------
License from SoftwareSerial
-----------------------------------------------------------------------
SoftwareSerial.cpp (formerly NewSoftSerial.cpp) -
Multi-instance software serial library for Arduino/Wiring
-- Interrupt-driven receive and other improvements by ladyada
(http://ladyada.net)
-- Tuning, circular buffer, derivation from class Print/Stream,
multi-instance support, porting to 8MHz processors,
various optimizations, PROGMEM delay tables, inverse logic and
direct port writing by Mikal Hart (http://www.arduiniana.org)
-- Pin change interrupt macros by Paul Stoffregen (http://www.pjrc.com)
-- 20MHz processor support by Garrett Mace (http://www.macetech.com)
-- ATmega1280/2560 support by Brett Hagman (http://www.roguerobotics.com/)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
The latest version of this library can always be found at
http://arduiniana.org.
*/
#include "TeensyDSP.h"
#ifdef USE_SW_SERIAL
//================================================================
//Public Variable
//================================================================
#define TX_PIN 9
#define RX_PIN 8
#define _SS_MAX_RX_BUFF 35 // RX buffer size
#define PRINT_MAX_LENGTH 30
//================================================================
//Internal Variable from SoftwareSerial.c and SoftwareSerial.h
//================================================================
//variable from softwareserial.c and softwareserial.h
static uint8_t swr_receive_buffer[_SS_MAX_RX_BUFF];
volatile uint8_t *_transmitPortRegister; //Write Port Register
uint8_t transmit_RegMask; //use Mask bit 1
uint8_t transmit_InvMask; //use mask bit 0
volatile uint8_t *_receivePortRegister; //Read Port Register
uint8_t _receiveBitMask;
//delay value for Bit
uint16_t _tx_delay;
//delay value for Receive
uint16_t _rx_delay_stopbit;
uint16_t _rx_delay_centering;
uint16_t _rx_delay_intrabit;
//Customize for uBITX Protocol
int8_t receiveIndex = 0;
int8_t receivedCommandLength = 0;
int8_t ffCount = 0;
//Values for Receive Buffer
//uint16_t _buffer_overflow;
//static volatile uint8_t _receive_buffer_head;
//static volatile uint8_t _receive_buffer_tail;
//Values for Interrupt (check Start Bit)
volatile uint8_t *_pcint_maskreg;
uint8_t _pcint_maskvalue;
//================================================================
//Internal Function from SoftwareSerial.c
//================================================================
uint16_t subtract_cap(uint16_t num, uint16_t sub)
{
if (num > sub)
return num - sub;
else
return 1;
}
inline void tunedDelay(uint16_t delay)
{
_delay_loop_2(delay);
}
void setRxIntMsk(bool enable)
{
if (enable)
*_pcint_maskreg |= _pcint_maskvalue;
else
*_pcint_maskreg &= ~_pcint_maskvalue;
}
uint8_t rx_pin_read()
{
return *_receivePortRegister & _receiveBitMask;
}
//
// The receive routine called by the interrupt handler
//
void softSerail_Recv()
{
#if GCC_VERSION < 40302
// Work-around for avr-gcc 4.3.0 OSX version bug
// Preserve the registers that the compiler misses
// (courtesy of Arduino forum user *etracer*)
asm volatile(
"push r18 \n\t"
"push r19 \n\t"
"push r20 \n\t"
"push r21 \n\t"
"push r22 \n\t"
"push r23 \n\t"
"push r26 \n\t"
"push r27 \n\t"
::);
#endif
uint8_t d = 0;
// If RX line is high, then we don't see any start bit
// so interrupt is probably not for us
if (!rx_pin_read()) //Start Bit
{
// Disable further interrupts during reception, this prevents
// triggering another interrupt directly after we return, which can
// cause problems at higher baudrates.
setRxIntMsk(false);
// Wait approximately 1/2 of a bit width to "center" the sample
tunedDelay(_rx_delay_centering);
// Read each of the 8 bits
for (uint8_t i=8; i > 0; --i)
{
tunedDelay(_rx_delay_intrabit);
d >>= 1;
if (rx_pin_read())
d |= 0x80;
}
if (receivedCommandLength == 0) //check Already Command
{
//Set Received Data
swr_receive_buffer[receiveIndex++] = d;
//Finded Command
if (d == 0x73 && ffCount > 1 && receiveIndex > 6)
{
receivedCommandLength = receiveIndex;
receiveIndex = 0;
ffCount = 0;
}
else if (receiveIndex > _SS_MAX_RX_BUFF)
{
//Buffer Overflow
receiveIndex = 0;
ffCount = 0;
}
else if (d == 0xFF)
{
ffCount++;
}
else
{
ffCount = 0;
}
}
// skip the stop bit
tunedDelay(_rx_delay_stopbit);
// Re-enable interrupts when we're sure to be inside the stop bit
setRxIntMsk(true);
}
#if GCC_VERSION < 40302
// Work-around for avr-gcc 4.3.0 OSX version bug
// Restore the registers that the compiler misses
asm volatile(
"pop r27 \n\t"
"pop r26 \n\t"
"pop r23 \n\t"
"pop r22 \n\t"
"pop r21 \n\t"
"pop r20 \n\t"
"pop r19 \n\t"
"pop r18 \n\t"
::);
#endif
}
ISR(PCINT0_vect)
{
softSerail_Recv();
}
//================================================================
//Public Function from SoftwareSerial.c and modified and create
//================================================================
// Read data from buffer
void SWSerial_Read(uint8_t * receive_cmdBuffer)
{
for (int i = 0; i < receivedCommandLength; i++)
receive_cmdBuffer[i] = swr_receive_buffer[i];
}
void SWSerial_Write(uint8_t b)
{
volatile uint8_t *reg = _transmitPortRegister;
uint8_t oldSREG = SREG;
uint16_t delay = _tx_delay;
cli(); // turn off interrupts for a clean txmit
// Write the start bit
*reg &= transmit_InvMask;
tunedDelay(delay);
// Write each of the 8 bits
for (uint8_t i = 8; i > 0; --i)
{
if (b & 1) // choose bit
*reg |= transmit_RegMask; // send 1
else
*reg &= transmit_InvMask; // send 0
tunedDelay(delay);
b >>= 1;
}
// restore pin to natural state
*reg |= transmit_RegMask;
SREG = oldSREG; // turn interrupts back on
tunedDelay(_tx_delay);
}
void SWSerial_Print(uint8_t *b)
{
for (int i = 0; i < PRINT_MAX_LENGTH; i++)
{
if (b[i] == 0x00)
break;
else
SWSerial_Write(b[i]);
}
}
void SWSerial_Begin(long speedBaud)
{
//INT TX_PIN
digitalWrite(TX_PIN, HIGH);
pinMode(TX_PIN, OUTPUT);
transmit_RegMask = digitalPinToBitMask(TX_PIN); //use Bit 1
transmit_InvMask = ~digitalPinToBitMask(TX_PIN); //use Bit 0
_transmitPortRegister = portOutputRegister(digitalPinToPort(TX_PIN));
//INIT RX_PIN
pinMode(RX_PIN, INPUT);
digitalWrite(RX_PIN, HIGH); // pullup for normal logic!
_receiveBitMask = digitalPinToBitMask(RX_PIN);
_receivePortRegister = portInputRegister(digitalPinToPort(RX_PIN));
//Set Values
uint16_t bit_delay = (F_CPU / speedBaud) / 4;
_tx_delay = subtract_cap(bit_delay, 15 / 4);
if (digitalPinToPCICR(RX_PIN))
{
_rx_delay_centering = subtract_cap(bit_delay / 2, (4 + 4 + 75 + 17 - 23) / 4);
_rx_delay_intrabit = subtract_cap(bit_delay, 23 / 4);
_rx_delay_stopbit = subtract_cap(bit_delay * 3 / 4, (37 + 11) / 4);
*digitalPinToPCICR(RX_PIN) |= _BV(digitalPinToPCICRbit(RX_PIN));
_pcint_maskreg = digitalPinToPCMSK(RX_PIN);
_pcint_maskvalue = _BV(digitalPinToPCMSKbit(RX_PIN));
tunedDelay(_tx_delay); // if we were low this establishes the end
}
//Start Listen
setRxIntMsk(true);
}
#else
void SWSerial_Write(uint8_t b)
{
Serial.write(b);
//Serial.flush();
}
void SWSerial_Print(uint8_t *b)
{
for (int i = 0; i < PRINT_MAX_LENGTH; i++)
{
if (b[i] == 0x00)
break;
else
SWSerial_Write(b[i]);
}
}
#endif