diff --git a/TeensyDSP/HamFuncs.h b/TeensyDSP/HamFuncs.h new file mode 100644 index 0000000..fc46396 --- /dev/null +++ b/TeensyDSP/HamFuncs.h @@ -0,0 +1,186 @@ +#ifndef __HamFuncs_h__ +#define __HamFuncs_h__ + +/**********************************************************************/ + +#ifndef HF_PWR_DEFAULT_LOAD +#define HF_PWR_DEDAULT_LOAD 50.0 +#endif + +#ifndef HF_VSWR_MAX_REPORTED +#define HF_VSWR_MAX_REPORTED 9.9 +#endif + +#ifndef HF_BRIDGE_FWD_VRECT +#define HF_BRIDGE_FWD_VRECT 0.25 +#endif + +#ifndef HF_BRIDGE_FWD_TURNS +#define HF_BRIDGE_FWD_TURNS 10.0 +#endif + +#ifndef HF_BRIDGE_REV_VRECT +#define HF_BRIDGE_REV_VRECT 0.25 +#endif + +#ifndef HF_BRIDGE_REV_TURNS +#define HF_BRIDGE_REV_TURNS 10.0 +#endif + +#ifndef HF_ADC_DEFAULT_BITS +#define HF_ADC_DEFAULT_BITS 10 +#endif + +#ifndef HF_ADC_DEFAULT_VREF +#define HF_ADC_DEFAULT_VREF 3.3 +#endif + +/**********************************************************************/ + +namespace HF { + + const float pwrDefaultLoad = HF_PWR_DEFAULT_LOAD; + const float vswrMaxReported = HF_VSWR_MAX_REPORTED; + const float bridgeFwdVrect = HF_BRIDGE_FWD_VRECT; + const float bridgeFwdTurns = HF_BRIDGE_FWD_TURNS; + const float bridgeRevVrect = HF_BRIDGE_REV_VRECT; + const float bridgeRevTurns = HF_BRIDGE_REV_TURNS; + const unsigned adcDefaultBits = HF_ADC_DEFAULT_BITS; + const float adcDefaultVref = HF_ADC_DEFAULT_VREF; + + const float rms = sqrt(2.0) / 2.0; + + /********************************************************************/ + + /*! + * @brief Calculate the output voltage of a resistive divider + * network, given the input voltage and the values of the + * resistors. The input voltage is applied to R1, the output + * voltage is taken from the junction of R1 and R2, and R2 is + * connected to ground. + * @param Vin + * Input voltage. + * @param R1 + * Input resistor (ohms). Input voltage is measured between + * the top of this resistor and ground. + * @param R2 + * Output resistor (ohms). Output voltage is measured + * between the top of this resistor and ground. + * @return Output voltage. + */ + inline float divOut(float Vin, float R1, float R2) { + return Vin * R2 / (R1 + R2); + } + + /*! + * @brief Calculate the input voltage of a resistive divider + * network, given the output voltage and the values of the + * resistors. The input voltage is applied to R1, the output + * voltage is taken from the junction of R1 and R2, and R2 is + * connected to ground. + * @param Vout + * Output voltage. + * @param R1 + * Input resistor (ohms). Input voltage is measured between + * the top of this resistor and ground. + * @param R2 + * Output resistor (ohms). Output voltage is measured between + * the top of this resistor and ground. + * @return Input voltage. + */ + inline float divIn(float Vout, float R1, float R2) { + return Vout * (R1 + R2) / R2; + } + + /*! + * @brief Calculate and return the power in watts, given a + * resistance and the voltage across the resistance. + * @param V + * Voltage across the load. + * @param R + * (optional) Resistance of the load (ohms). If not provided, + * a default is used (HF_PWR_DEFAULT_LOAD). + * @return Power dissipated (watts). This is calculated as + * P = V^2/R. + */ + inline float P(float V, float R = pwrDefaultLoad) { + return (V * V) / R; + } + + /*! + * @brief Calculate and return the Voltage Standing Wave Ratio + * (VSWR) based on the given forward and reverse voltages. + * @param Vfwd + * Measured forward voltage. + * @param Vrev + * Measured reverse voltage. + * @param VSWRmax + * (optional) Maximum reported VSWR. The output will be + * clamped to this value if necessary (HF_VSWR_MAX_REPORTED). + * @return Voltage Standing Wave Ratio (VSWR). This is calculated + * as VSWR = (Vfwd + Vrev) / (Vfwd - Vrev). + */ + float VSWR(float Vfwd, float Vrev, float VSWRmax = vswrMaxReported) { + if (Vfwd - Vrev == 0.0) { + return VSWRmax; + } else { + float swr = (Vfwd + Vrev) / (Vfwd - Vrev); + return (swr > VSWRmax ? VSWRmax : swr); + } + } + + /*! + * @brief Calculate and return the forward RMS input voltage across + * a Stockton bridge. + * @param Vout + * Rectified output voltage (e.g. read via an ADC). + * @param Vrect + * (optional) Voltage drop across the rectifier diode. If + * not provided, a default is used (HF_BRIDGE_FWD_VRECT). + * @param turns + * (optional) Coupling transformer turns ratio. If not + * provided, a default is used (HF_BRIDGE_FWD_TURNS). + * @return Input voltage (i.e. the actual forward voltage). + */ + inline float bridgeFwd(float Vout, float Vrect = bridgeFwdVrect, float turns = bridgeFwdTurns) { + return (Vout + Vrect) * turns * rms; + } + + /*! + * @brief Calculate and return the reverse RMS input voltage across + * a Stockton bridge. + * @param Vout + * Rectified output voltage (e.g. read via an ADC). + * @param Vrect + * (optional) Voltage drop across the rectifier diode. If + * not provided, a default is used (HF_BRIDGE_REV_VRECT). + * @param turns + * (optional) Coupling transformer turns ratio. If not + * provided, a default is used (HF_BRIDGE_REV_TURNS). + * @return Input voltage (i.e. the actual reverse voltage). + */ + inline float bridgeRev(float Vout, float Vrect = bridgeRevVrect, float turns = bridgeRevTurns) { + return (Vout + Vrect) * turns * rms; + } + + /*! + * @brief Calculate and return the input voltage to an Analog-to- + * Digital Converter (ADC) given the resolution (number of + * bits) and the voltage reference of the ADC. + * @param counts + * Value of the ADC measurement (in unitless counts). + * @param res + * (optional) Resolution (in bits) of the ADC. If not + * provided, the default is used (HF_ADC_DEFAULT_BITS). + * @param Vref + * (optional) Voltage reference of the ADC. If not + * provided, the default is used (HF_ADC_DEFAULT_VREF). + * @return Input voltage to the ADC. + */ + inline float adcIn(unsigned counts, unsigned res = adcDefaultBits, float Vref = adcDefaultVref) { + return float(counts) * Vref / float(1 << res); + } + +}; + +#endif diff --git a/TeensyDSP/Nextion.cpp b/TeensyDSP/Nextion.cpp new file mode 100644 index 0000000..0eee8b1 --- /dev/null +++ b/TeensyDSP/Nextion.cpp @@ -0,0 +1,105 @@ +#include "Nextion.h" + +//Control must have prefix 'v' or 's' +char softSTRHeader[11] = {'p', 'm', '.', 's', '0', '.', 't', 'x', 't', '=', '\"'}; +char softINTHeader[10] = {'p', 'm', '.', 'v', '0', '.', 'v', 'a', 'l', '='}; + +char softTemp[20]; + +/*! + @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 (unsigned i = 0; i < sizeof(softSTRHeader)/sizeof(softSTRHeader[0]); i++) + Serial1.write(softSTRHeader[i]); + } + else + { + softINTHeader[4] = varIndex; + for (unsigned i = 0; i < sizeof(softINTHeader)/sizeof(softINTHeader[0]); i++) + Serial1.write(softINTHeader[i]); + } +} + +/*! + @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); + + memset(softTemp, 0, 20); + ultoa(sendValue, softTemp, DEC); + Serial1.print(softTemp); + Serial1.write(0xff); + Serial1.write(0xff); + Serial1.write(0xff); +} + +/*! + @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); + + memset(softTemp, 0, 20); + ltoa(sendValue, softTemp, DEC); + Serial1.print(softTemp); + Serial1.write(0xff); + Serial1.write(0xff); + Serial1.write(0xff); +} + +/*! + @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); + + 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}; + +/*! + @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]); +} diff --git a/TeensyDSP/Nextion.h b/TeensyDSP/Nextion.h new file mode 100644 index 0000000..2a66e64 --- /dev/null +++ b/TeensyDSP/Nextion.h @@ -0,0 +1,123 @@ +#ifndef __Nextion_h__ +#define __Nextion_h__ + +#define SWS_HEADER_CHAR_TYPE 'c' //1Byte Protocol Prefix +#define SWS_HEADER_INT_TYPE 'v' //Numeric Protocol Prefex +#define SWS_HEADER_STR_TYPE 's' //for TEXT Line compatiable Character LCD Control + +//=================================================================== +//Begin of Nextion LCD Protocol +// +// v0~v9, va~vz : Numeric (Transceiver -> Nextion LCD) +// s0~s9 : String (Text) (Transceiver -> Nextion LCD) +// vlSendxxx, vloxxx: Reserve for Nextion (Nextion LCD -> Transceiver) +// +//=================================================================== + +#define CMD_NOW_DISP '0' //c0 +char L_nowdisp = -1; //Sended nowdisp + +#define CMD_VFO_TYPE 'v' //cv +char L_vfoActive; //vfoActive + +#define CMD_CURR_FREQ 'c' //vc +unsigned long L_vfoCurr; //vfoA +#define CMD_CURR_MODE 'c' //cc +byte L_vfoCurr_mode; //vfoA_mode + +#define CMD_VFOA_FREQ 'a' //va +unsigned long L_vfoA; //vfoA +#define CMD_VFOA_MODE 'a' //ca +byte L_vfoA_mode; //vfoA_mode + +#define CMD_VFOB_FREQ 'b' //vb +unsigned long L_vfoB; //vfoB +#define CMD_VFOB_MODE 'b' //cb +byte L_vfoB_mode; //vfoB_mode + +#define CMD_IS_RIT 'r' //cr +char L_ritOn; +#define CMD_RIT_FREQ 'r' //vr +unsigned long L_ritTxFrequency; //ritTxFrequency + +#define CMD_IS_TX 't' //ct +char L_inTx; + +#define CMD_IS_DIALLOCK 'l' //cl +byte L_isDialLock; //byte isDialLock + +#define CMD_IS_SPLIT 's' //cs +byte L_Split; //isTxType +#define CMD_IS_TXSTOP 'x' //cx +byte L_TXStop; //isTxType + +#define CMD_TUNEINDEX 'n' //cn +byte L_tuneStepIndex; //byte tuneStepIndex + +#define CMD_SMETER 'p' //cs +byte L_scaledSMeter; //scaledSMeter + +#define CMD_SIDE_TONE 't' //vt +unsigned long L_sideTone; //sideTone +#define CMD_KEY_TYPE 'k' //ck +byte L_cwKeyType; //L_cwKeyType 0: straight, 1 : iambica, 2: iambicb + +#define CMD_CW_SPEED 's' //vs +unsigned int L_cwSpeed; //cwSpeed + +#define CMD_CW_DELAY 'y' //vy +byte L_cwDelayTime; //cwDelayTime + +#define CMD_CW_STARTDELAY 'e' //ve +byte L_delayBeforeCWStartTime; //byte delayBeforeCWStartTime + +#define CMD_ATT_LEVEL 'f' //vf +byte L_attLevel; + +byte L_isIFShift; //1 = ifShift, 2 extend +#define CMD_IS_IFSHIFT 'i' //ci + +int L_ifShiftValue; +#define CMD_IFSHIFT_VALUE 'i' //vi + +byte L_sdrModeOn; +#define CMD_SDR_MODE 'j' //cj + +#define CMD_UBITX_INFO 'm' //cm Complete Send uBITX Information + +//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 +#define CMD_AR_TUNE4 '4' //v4 +#define CMD_AR_TUNE5 '5' //v5 + +//int idleStep = 0; +byte scaledSMeter = 0; + +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; + +void sendHeader(char varType, char varIndex); +void sendCommandUL(char varIndex, unsigned long sendValue); +void sendCommandL(char varIndex, long sendValue); +void sendCommandStr(char varIndex, const char* sendValue); +void sendCommand1Num(char varIndex, char sendValue); + +//======================================================= +//END OF Nextion Protocol +//======================================================= + +#endif diff --git a/TeensyDSP/Sensors.cpp b/TeensyDSP/Sensors.cpp new file mode 100644 index 0000000..d572260 --- /dev/null +++ b/TeensyDSP/Sensors.cpp @@ -0,0 +1,3 @@ +#include "Sensors.h" + +UBitxSensors Sensors; diff --git a/TeensyDSP/Sensors.h b/TeensyDSP/Sensors.h new file mode 100644 index 0000000..a9fd820 --- /dev/null +++ b/TeensyDSP/Sensors.h @@ -0,0 +1,338 @@ +#ifndef __Sensor_h__ +#define __Sensor_h__ + +#include +#include "HamFuncs.h" + +/**********************************************************************/ + +#ifndef UBITX_SENSORS_S_METER_PIN +#define UBITX_SENSORS_S_METER_PIN 27 +#endif + +#ifndef UBITX_SENSORS_FWD_PWR_PIN +#define UBITX_SENSORS_FWD_PWR_PIN 20 +#endif + +#ifndef UBITX_SENSORS_REV_PWR_PIN +#define UBITX_SENSORS_REV_PWR_PIN 28 +#endif + +#ifndef UBITX_SENSORS_SUPPLY_PIN +#define UBITX_SENSORS_SUPPLY_PIN 21 +#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 +#endif + +#ifndef UBITX_SENSORS_S_METER_R2 +#define UBITX_SENSORS_S_METER_R2 33000.0 +#endif + +#ifndef UBITX_SENSORS_FWD_PWR_R1 +#define UBITX_SENSORS_FWD_PWR_R1 22000.0 +#endif + +#ifndef UBITX_SENSORS_FWD_PWR_R2 +#define UBITX_SENSORS_FWD_PWR_R2 33000.0 +#endif + +#ifndef UBITX_SENSORS_REV_PWR_R1 +#define UBITX_SENSORS_REV_PWR_R1 22000.0 +#endif + +#ifndef UBITX_SENSORS_REV_PWR_R2 +#define UBITX_SENSORS_REV_PWR_R2 33000.0 +#endif + +#ifndef UBITX_SENSORS_SUPPLY_R1 +#define UBITX_SENSORS_SUPPLY_R1 56000.0 +#endif + +#ifndef UBITX_SENSORS_SUPPLY_R2 +#define UBITX_SENSORS_SUPPLY_R2 10000.0 +#endif + +#ifndef UBITX_SENSORS_S_METER_LVL0 +#define UBITX_SENSORS_S_METER_LVL0 2 +#endif + +#ifndef UBITX_SENSORS_S_METER_LVL1 +#define UBITX_SENSORS_S_METER_LVL1 4 +#endif + +#ifndef UBITX_SENSORS_S_METER_LVL2 +#define UBITX_SENSORS_S_METER_LVL2 8 +#endif + +#ifndef UBITX_SENSORS_S_METER_LVL3 +#define UBITX_SENSORS_S_METER_LVL3 16 +#endif + +#ifndef UBITX_SENSORS_S_METER_LVL4 +#define UBITX_SENSORS_S_METER_LVL4 32 +#endif + +#ifndef UBITX_SENSORS_S_METER_LVL5 +#define UBITX_SENSORS_S_METER_LVL5 64 +#endif + +#ifndef UBITX_SENSORS_S_METER_LVL6 +#define UBITX_SENSORS_S_METER_LVL6 128 +#endif + +#ifndef UBITX_SENSORS_S_METER_LVL7 +#define UBITX_SENSORS_S_METER_LVL7 256 +#endif + +#ifndef UBITX_SENSORS_S_METER_LVL8 +#define UBITX_SENSORS_S_METER_LVL8 512 +#endif + +/**********************************************************************/ + +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 uBitxSensorsAvgSamples = UBITX_SENSORS_AVERAGE_SAMPLES; +const float uBitxSensorsSMeterR1 = UBITX_SENSORS_S_METER_R1; +const float uBitxSensorsSMeterR2 = UBITX_SENSORS_S_METER_R2; +const float uBitxSensorsFwdPwrR1 = UBITX_SENSORS_FWD_PWR_R1; +const float uBitxSensorsFwdPwrR2 = UBITX_SENSORS_FWD_PWR_R2; +const float uBitxSensorsRevPwrR1 = UBITX_SENSORS_REV_PWR_R1; +const float uBitxSensorsRevPwrR2 = UBITX_SENSORS_REV_PWR_R2; +const float uBitxSensorsSupplyR1 = UBITX_SENSORS_SUPPLY_R1; +const float uBitxSensorsSupplyR2 = UBITX_SENSORS_SUPPLY_R2; + +const int uBitxSensorsSMeterValues[] = { + UBITX_SENSORS_S_METER_LVL0, + UBITX_SENSORS_S_METER_LVL1, + UBITX_SENSORS_S_METER_LVL2, + UBITX_SENSORS_S_METER_LVL3, + UBITX_SENSORS_S_METER_LVL4, + UBITX_SENSORS_S_METER_LVL5, + UBITX_SENSORS_S_METER_LVL6, + UBITX_SENSORS_S_METER_LVL7, + UBITX_SENSORS_S_METER_LVL8 +}; +const int uBitxSensorsSMeterLevels = sizeof(uBitxSensorsSMeterValues) / + sizeof(uBitxSensorsSMeterValues[0]); + +/**********************************************************************/ + +/*! + * @brief Class that maintains a "trailing average" of the last X + * samples provided. It is a template that can be instantiated + * with both the (numeric) data type that is being stored and + * averaged, as well as the number of samples to maintain the + * trailing average across. + */ +template +class TrailingAverage { + public: + /*! + * @brief Create a new TrailingAverage object. Data type averaged, + * and number of elements to average, are determined when the + * template is instantiated. + */ + TrailingAverage(): + average(T(0)), + current(0), + divisor(T(N)) + { + for (i = 0; i < N; i++) { + data[i] = T(0); + } + } + + /*! + * @brief Add a new element to the average. The current last (Nth) + * element is removed, and the new element is added. + * @param val + * The new element/value to incorporate into the average. + */ + inline void add(T val) { + int last = (current - 1) % N; + average -= data[last]; + current = (current + 1) % N; + average += data[current] / divisor; + } + + /*! + * @brief Read the current value of the average. + * @return The current average. + */ + inline T read() { + return average; + } + + private: + T data[N]; + T average; + int current; + T divisor; +}; + +/**********************************************************************/ + +/*! + * @brief Class that handles the various sensors in the uBitx: + * S-Meter, forward/reverse power and SWR, and supply voltage. + */ +class UBitxSensors { + public: + /*! + * @brief Create a new UBitxSensors object. It uses the default + * S-Meter, Forward Power, Reverse Power, and Supply Voltage + * ADC pins. + */ + UBitxSensors(): + sMeterPin(uBitxSensorsSMeterPin), + fwdPwrPin(uBitxSensorsFwdPwrPin), + revPwrPin(uBitxSensorsRevPwrPin), + supplyPin(uBitxSensorsSupplyPin) + { + pinMode(sMeterPin, INPUT); // analog + pinMode(fwdPwrPin, INPUT); // analog + pinMode(revPwrPin, INPUT); // analog + pinMode(supplyPin, INPUT); // analog + } + + /*! + * @brief Update the value of the S-Meter by reading the associated + * ADC pin. + */ + inline void updateSMeter() { + int value = adc.analogRead(sMeterPin); + sMeter.add(value); + } + + /*! + * @brief Update the value of the Forward and Reverse Power + * measurements by reading the associated ADC pin. + */ + void updatePower() { + ADC::Sync_result value = adc.analogSyncRead(fwdPwrPin, revPwrPin); + + float fwdV = HF::adcIn(value.result_adc0); + float revV = HF::adcIn(value.result_adc1); + + fwdV = HF::divIn(fwdV); + fwdV = HF::bridgeFwd(fwdV); + + revV = HF::divIn(revV); + revV = HF::bridgeFwd(revV); + + fwdPwr.add(HF::P(fwdV)); + revPwr.add(HF::P(revV)); + vswr.add(HF::VSWR(fwdV, revV)); + } + + /*! + * @brief Update the value of the Supply Voltage measurement by + * reading the associated ADC pin. + */ + inline void updateSupply() { + float value = HF::adcIn(adc.analogRead(supplyPin); + value = HF::divIn(value); + supply.add(value); + } + + /*! + * @brief Return the unscaled value of the S-Meter reading. + * @return Unscaled S-Meter reading. + */ + inline int sMeterUnscaled() { + return sMeter.read(); + } + + /*! + * @brief Return the scaled value of the S-Meter reading. This + * is the value that is used to directly control the S-Meter + * display on the Nextion LCD. + * @return Scaled S-Meter reading. + */ + int sMeterScaled() { + float sig = sMeter.read(); + // small number of elements; just doing a linear search + for (int i = uBitxSensorsSMeterLevels; i > 0; i--) { + if (sig > uBitxSensorsSMeterValues[i - 1]) { + return i; + } + } + return 0; + } + + /*! + * @brief Return the current Forward Power measurement. + * @return Forward Power measurement. + */ + inline float Pfwd() { + return fwdPwr.read(); + } + + /*! + * @brief Return the current Reverse Power measurement. + * @return Reverse Power measurement. + */ + inline float Prev() { + return revPwr.read(); + } + + /*! + * @brief Return the current Voltage Standing Wave Ration (VSWR). + * @return Current VSWR calculation. + */ + inline float VSWR() { + return vswr.read(); + } + + /*! + * @brief Return the current Voltage Standing Wave Ration (VSWR), + * scaled for the Nextion display protocol. + * @return Current VSWR calculation (scaled). + */ + float scaledVSWR() { + int val = int(vswr.read()); + if (val < 0) { + return 0; + } else if (val > uBitxSensorsSMeterLevels) { + return uBitxSensorsSMeterLevels; + } else { + return val; + } + } + + /*! + * @brief Return the current Supply Voltage measurement. + * @return Current Supply Voltage. + */ + inline float supplyVoltage() { + return supply.read(); + } + + private: + + // Pins + int sMeterPin; + int fwdPwrPin; + int revPwdPin; + int supplyPin; + + // Buffers for averages + AverageBuffer sMeter; + AverageBuffer fwdPwr; + AverageBuffer revPwr; + AverageBuffer vswr; + AverageBuffer supply; +}; + +extern UBitxSensors Sensors; + +#endif