Some code realignment, including changing from millis to elapsedMillis.

This commit is contained in:
Rob French 2021-01-21 09:44:06 -06:00
parent d97f282f7b
commit c1c4dd3f19
2 changed files with 68 additions and 64 deletions

View File

@ -7,12 +7,12 @@ KD8CEC, Ian Lee
**********************************************************************/
#include <arduino.h>
#include <Arduino.h>
//================================================================
//COMMUNICATION SECTION
//================================================================
#define USE_SW_SERIAL
//#define USE_SW_SERIAL
extern void SWSerial_Write(uint8_t b);
extern void SWSerial_Print(uint8_t *b);
@ -72,5 +72,3 @@ extern int magnitudelimit_low;
#define SIGNAL_METER_ADC A7
#define POWER_METER_ADC A3
#define SWR_METER_ADC A2

View File

@ -7,7 +7,7 @@ Version : 0.8
License : See fftfunctions.cpp for FFT and CW Decode.
**********************************************************************/
#include <Wire.h>
#include <i2c_t3.h> // using i2c_t3 library for multiple I2C busses
#include <EEPROM.h>
#include "TeensyDSP.h"
@ -196,7 +196,7 @@ void SendCommand1Num(char varType, char sendValue) //0~9 : Mode, nowDisp, Active
int I2CCommand = 0;
void CalculateCoeff(uint8_t freqIndex);
char ForwardBuff[MAX_FORWARD_BUFF_LENGTH + 1];
char forwardBuff[MAX_FORWARD_BUFF_LENGTH + 1];
static char nowBuffIndex = 0;
static char etxCount = 0;
static char nowSendingProtocol = 0;
@ -209,7 +209,7 @@ char DSPType = 1; //0 : Not Use, 1 : FFT, 2 : Morse Decoder, 3 : RTTY Decoder
char FFTToUartIdleCount = 0;
#define FFTToUartInterval 2
unsigned long lastForwardmili = 0;
elapsedMillis sinceForward = 0;
uint8_t responseCommand = 0; //
uint8_t TXStatus = 0; //0:RX, 1:TX
void ResponseConfig()
@ -280,13 +280,13 @@ char CommandPasrser(int lastIndex)
for (int i = lastIndex - 3; i >= startIndex + 7; i--)
{
//Find =
if (ForwardBuff[i-3] == 'v' && ForwardBuff[i-2] == 'a' && ForwardBuff[i-1] == 'l' && ForwardBuff[i] == '=') //0x3D
if (forwardBuff[i-3] == 'v' && forwardBuff[i-2] == 'a' && forwardBuff[i-1] == 'l' && forwardBuff[i] == '=') //0x3D
{
uint8_t command1 = ForwardBuff[i-6]; //v
uint8_t command2 = ForwardBuff[i-5]; //v
uint8_t command1 = forwardBuff[i-6]; //v
uint8_t command2 = forwardBuff[i-5]; //v
// i-4 //.
ForwardBuff[lastIndex - 2] = 0;
long commandVal=atol(&ForwardBuff[i + 1]);
forwardBuff[lastIndex - 2] = 0;
long commandVal=atol(&forwardBuff[i + 1]);
uint8_t *ReadBuff = (uint8_t *)&commandVal;
//Loop Back
@ -299,19 +299,19 @@ char CommandPasrser(int lastIndex)
Serial.print("Found :");
for (int k = i + 1; k <= lastIndex - 3; k++)
{
Serial.write(ForwardBuff[k]);
Serial.write(forwardBuff[k]);
}
Serial.println();
Serial.print("Reverse :");
for (int k = lastIndex - 3; k >= i + 1; k--)
{
Serial.write(ForwardBuff[k]);
Serial.write(forwardBuff[k]);
}
Serial.println();
ForwardBuff[lastIndex - 2] = 0;
forwardBuff[lastIndex - 2] = 0;
tmpVal=atol(&ForwardBuff[i + 1]);
tmpVal=atol(&forwardBuff[i + 1]);
Serial.println(tmpVal);
uint8_t *ReadBuff = (uint8_t *)&tmpVal;
@ -341,7 +341,7 @@ char CommandPasrser(int lastIndex)
Serial.print(":");
for (int k = i + 1; k <= lastIndex - 3; k++)
{
Serial.write(ForwardBuff[k]);
Serial.write(forwardBuff[k]);
}
Serial.println("");
//End of Tet Code 2
@ -414,32 +414,38 @@ char CommandPasrser(int lastIndex)
return 0;
}
//#define PROTOCOL_TIMEOUT = 100
int ForwardData(void)
/*!
@brief Forwards serial data from the RX line to the TX line.
*/
int forwardData(void)
{
uint8_t recvChar;
int recvChar;
if (Serial.available() > 0)
{
#ifndef USE_SW_SERIAL
Serial.flush();
#endif
//Check RX Buffer
// Check RX buffer for available data.
while (Serial.available() > 0)
{
recvChar = Serial.read();
ForwardBuff[nowBuffIndex] = recvChar;
forwardBuff[nowBuffIndex] = char(recvChar);
if (recvChar == 0xFF) //found ETX
if (recvChar == 0xFF) // found ETX
{
etxCount++; //Nextion Protocol, ETX : 0xFF, 0xFF, 0xFF
etxCount++; // Nextion protocol, ETX: 0xFF, 0xFF, 0xFF
if (etxCount >= 3)
{
//Finished Protocol
// Finished Protocol
if (CommandPasrser(nowBuffIndex) == 1)
{
nowSendingProtocol = 0; //Finished 1 Set Command
nowSendingProtocol = 0; // finished 1 set command
etxCount = 0;
nowBuffIndex = 0;
}
@ -448,29 +454,24 @@ int ForwardData(void)
else
{
etxCount = 0x00;
nowSendingProtocol = 1; //Sending Data
nowSendingProtocol = 1; // sending data
}
SWSerial_Write(recvChar);
lastForwardmili = millis();
Serial.write(recvChar);
sinceForward = 0;
nowBuffIndex++;
if (nowBuffIndex > MAX_FORWARD_BUFF_LENGTH -2)
if (nowBuffIndex > MAX_FORWARD_BUFF_LENGTH - 2)
{
nowBuffIndex = 0;
}
} //end of while
#ifndef USE_SW_SERIAL
Serial.flush();
#endif
//lastReceivedTime = millis();
} //end if (Serial.available
}
else
{
//check Timeout
// check timeout
}
}
@ -638,19 +639,19 @@ void setup()
}
// put your setup code here, to run once:
Wire.begin(I2CMETER_ADDR); //j : S-Meter Slave Address
//Wire.begin(0x21); //j : S-Meter Slave Address
Wire.onReceive(I2CReceiveEvent); //
Wire.onRequest(I2CRequestEvent);
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
//#ifdef USE_SW_SERIAL
// SWSerial_Begin(9600);
//#endif
Serial.begin(9600, SERIAL_8N1);
Serial.flush();
SAMPLE_INTERVAL = round(1000000 * (1.0 / SAMPLE_PREQUENCY));
CalculateCoeff(cwDecodeHz); //Set 750Hz //9 * 50 + 300 = 750Hz
//CalculateCoeff(cwDecodeHz); //Set 750Hz //9 * 50 + 300 = 750Hz
//Serial.println("Start...");
}
@ -658,9 +659,9 @@ void I2CReceiveEvent(void)
{
int readCommand = 0; // byte를 읽어 int로 변환
while(Wire.available() > 0) // for Last command
while(Wire1.available() > 0) // for Last command
{
readCommand = Wire.read();
readCommand = Wire1.read();
}
if (0x50 <= readCommand && readCommand <= 0x59)
@ -684,11 +685,11 @@ void I2CRequestEvent(void)
if (I2CCommand == I2CMETER_CALCS)
{
Wire.write(scaledSMeter);
Wire1.write(scaledSMeter);
}
else if (I2CCommand == I2CMETER_UNCALCS)
{
//Wire.write(ADC_DIFF);
//Wire1.write(ADC_DIFF);
//8292Hz
for(int i=0; i < 7; i++)
{
@ -718,17 +719,17 @@ void I2CRequestEvent(void)
{
readedValue = 255;
}
Wire.write(readedValue);
Wire1.write(readedValue);
}
else if (I2CCommand == I2CMETER_CALCP)
{
readedValue = analogRead(POWER_METER_ADC); //POWER
Wire.write(readedValue);
Wire1.write(readedValue);
}
else if (I2CCommand == I2CMETER_CALCR) //SWR
{
readedValue = analogRead(SWR_METER_ADC);
Wire.write(readedValue);
Wire1.write(readedValue);
}
}
@ -756,13 +757,14 @@ void loop()
char isProcess = 0; //0 : Init, 1 : Complete ADC Sampling, 2 : Complete FFT
isProcess = 0;
ForwardData();
forwardData();
if (isBooted < 100)
{
//Delay 20msec
for (int i = 0; i < 20; i++)
{
ForwardData();
forwardData();
delay(1);
}
isBooted++;
@ -805,7 +807,7 @@ void loop()
}
//for Realtime LCD Display
ForwardData();
forwardData();
if (clcCount++ > 10)
{
@ -862,7 +864,7 @@ void loop()
for (int i = 0; i < 10; i++)
{
ForwardData();
forwardData();
if (TXStatus != 1) //if TX -> RX break
{
break;
@ -907,7 +909,7 @@ void loop()
isProcess = 1; //Mark => Complete ADC Sampling
}
ForwardData();
forwardData();
//===========================================
// Send Signal Meter to UART
@ -932,10 +934,10 @@ void loop()
SendMeterData(0); //only calculate Signal Level
}
ForwardData();
forwardData();
//Check Response Command
if (responseCommand > 0 && millis() > lastForwardmili + LAST_TIME_INTERVAL)
if (responseCommand > 0 && sinceForward > LAST_TIME_INTERVAL)
{
ResponseConfig();
}
@ -943,7 +945,7 @@ void loop()
//===================================================================================
// DSP Routine
//===================================================================================
if (DSPType == 1 && millis() > lastForwardmili + LAST_TIME_INTERVAL) //Spectrum : FFT => Send To UART
if (DSPType == 1 && sinceForward > LAST_TIME_INTERVAL) // spectrum: FFT => send To UART
{
FFTToUartIdleCount = 0;
@ -953,7 +955,7 @@ void loop()
isProcess = 2;
}
ForwardData();
forwardData();
if (isProcess == 2)
{
@ -965,7 +967,7 @@ void loop()
isProcess = 3;
}
ForwardData();
forwardData();
if (isProcess == 3)
{
@ -1012,3 +1014,7 @@ void loop()
Decode_Morse(magnitude);
} //enf of if
} //end of main
//======================================================================
// EOF
//======================================================================