diff --git a/Raduino/ubitx.h b/Raduino/ubitx.h index ce94955..ec4dda5 100644 --- a/Raduino/ubitx.h +++ b/Raduino/ubitx.h @@ -255,11 +255,11 @@ extern byte I2C_LCD_SECOND_ADDRESS; //only using Dual LCD Mode #define I2CMETER_CALCR 0x55 //Calculated SWR Meter #define I2CMETER_UNCALCR 0x54 //Uncalculated SWR Meter -// Raduino provides updated data to TeensyDSP +// Raduino<=>TeensyDSP data exchange #define I2CMETER_RIGINF 0x50 // Raduino requests any CAT updates from TeensyDSP -#define I2CMETER_REQCAT 0x51 +//#define I2CMETER_REQCAT 0x51 //============================================================================== // for public, Variable, functions diff --git a/TeensyDSP/Rig.h b/TeensyDSP/Rig.h index 88c0239..bbb67fe 100644 --- a/TeensyDSP/Rig.h +++ b/TeensyDSP/Rig.h @@ -3,36 +3,25 @@ #include "RigState.h" -enum UpdateSource { - NoSource, - RaduinoSource, - CATSource -}; - class UBitxRig { public: inline void begin() {} inline void update() {} - inline unsigned getFreqA() const { return catState.getFreqA(); } - inline unsigned getFreqB() const { return catState.getFreqB(); } - inline int getRIT() const { return catState.getRIT(); } - inline int getXIT() const { return catState.getXIT(); } - inline bool isVFOA() const { return catState.isVFOA(); } - inline bool isVFOB() const { return catState.isVFOB(); } - inline bool isSplit() const { return catState.isSplit(); } - inline bool isRITOn() const { return catState.isRITOn(); } - inline bool isXITOn() const { return catState.isXITOn(); } - inline bool isModeCW() const { return catState.isCW() && catState.isUSB(); } - inline bool isModeCWR() const { return catState.isCW() && catState.isLSB(); } - inline bool isModeUSB() const { return catState.isUSB() && !catState.isCW(); } - inline bool isModeLSB() const { return catState.isLSB() && !catState.isCW(); } + + inline unsigned getFreqA() const { return radState.getFreqA(); } + inline unsigned getFreqB() const { return radState.getFreqB(); } + inline int getRIT() const { return radState.getRIT(); } + inline int getXIT() const { return radState.getXIT(); } + inline bool isVFOA() const { return radState.isVFOA(); } + inline bool isVFOB() const { return radState.isVFOB(); } + inline bool isSplit() const { return radState.isSplit(); } + inline bool isRITOn() const { return radState.isRITOn(); } + inline bool isXITOn() const { return radState.isXITOn(); } + inline bool isModeCW() const { return radState.isCW() && radState.isUSB(); } + inline bool isModeCWR() const { return radState.isCW() && radState.isLSB(); } + inline bool isModeUSB() const { return radState.isUSB() && !radState.isCW(); } + inline bool isModeLSB() const { return radState.isLSB() && !radState.isCW(); } inline bool isAI() const { return autoInfo; } - //inline bool updatedByCAT() const { return lastUpdatedBy == CATSource; } - //inline bool updatedByRaduino() const { return lastUpdatedBy == RaduinoSource; } - - //inline void clearUpdate() { lastUpdatedBy = NoSource; } - //inline void setRaduinoUpdate() { lastUpdatedBy = RaduinoSource; } - //inline void setCATUpdate() { lastUpdatedBy = CATSource; } inline void setFreqA(unsigned freq) { catState.setFreqA(freq); } inline void setFreqB(unsigned freq) { catState.setFreqB(freq); } @@ -55,18 +44,8 @@ class UBitxRig { inline void aiOn() { autoInfo = true; } inline void aiOff() { autoInfo = false; } - inline void updateState(UBitxRigState& r, bool isCAT = false) { - if ((r.vfo[0] == state.vfo[0]) && - (r.vfo[1] == state.vfo[1]) && - (r.rit == state.rit) && - (r.xit == state.xit) && - (r.flags == state.flags)) { - return; - } else { - state = r; - lastUpdatedBy = isCAT ? CATSource : RaduinoSource; - } - } + inline UBitxRigState& cat() { return catState; } + inline UBitxRigState& rad() { return radState; } /********************************************************************/ // New functional/mode-based Rig methods @@ -80,10 +59,9 @@ class UBitxRig { //void getBand(); private: - bool autoInfo = false; - UpdateSource lastUpdatedBy = NoSource; UBitxRigState catState; UBitxRigState radState; + bool autoInfo = false; }; extern UBitxRig Rig; diff --git a/TeensyDSP/RigState.cpp b/TeensyDSP/RigState.cpp index 2a0fa8a..37adef0 100644 --- a/TeensyDSP/RigState.cpp +++ b/TeensyDSP/RigState.cpp @@ -182,9 +182,14 @@ BaseField* raduinoFields[WIREBUS_NUM_FIELDS] = { /**********************************************************************/ -RaduinoState::RaduinoState(): RigState(raduinoFields, WIREBUS_NUM_FIELDS) {} +RigState::RigState(): RigState(raduinoFields, WIREBUS_NUM_FIELDS) {} -void RaduinoState::begin() { +/*! + * @brief Begin using the RigState object. In order to force an + * update (e.g. sending current state to the remote device), + * all fields are marked dirty. + */ +void RigState::begin() { for (byte i = 0; i < numFields; i++) { if (read(i)) { makeDirty(i); @@ -192,7 +197,7 @@ void RaduinoState::begin() { } } -void RaduinoState::update() { +void RigState::update() { // First we need to determine which fields have changed (and are // thus dirty and need to be sent to the TeensyDSP). for (byte i = 0; i < numFields; i++) { @@ -202,35 +207,19 @@ void RaduinoState::update() { } // Next we need to send the current (changed) Raduino information - // to the TeensyDSP. The expected response is the number of fields - // (in bytes) that the TeensyDSP needs to send in response. + // to the TeensyDSP. Wire.beginTransmission(I2CMETER_ADDR); Wire.write(I2CMETER_RIGINF); - // NOTE - I don't think I actually need to do this line, i.e. I don't think the slave cares... it'll read what is sent to it. - //Wire.write(numDirty * (sizeof(byte) + sizeof(uint32_t)), 1); // Write the number of dirty fields (in bytes). for (byte i = 0; i < numFields; i++) { - if (isDirty(i)) { // Write each field that is dirty to the bus. - Wire.write(i); // write the field number/ID - Wire.write(data(i), dataSize(i)); // write the field data + if (isDirty(i)) { // Write each field that is dirty to the bus. + Wire.write(i); // - write the field number/ID + Wire.write(data(i), dataSize(i)); // - write the field data makeClean(i); } } Wire.endTransmission(); - // Now we're going to read the response from the TeensyDSP. All - // fields should be marked as clean at this point (unless there's - // something that has been updated via interrupt???). - Wire.requestFrom(I2CMETER_ADDR, 1); - byte numBytes; - while (Wire.available()) { - numBytes = Wire.read(); // Should only get executed for one byte... but just in case. - } - if (numBytes == 0) return; - - // Let the TeensyDSP know that we want it to send its deltas now. - Wire.beginTransmission(I2CMETER_ADDR); - Wire.write(I2CMETER_RIGREQ); - Wire.endTransmission(); + delay(1); // some delay required between ending transmission and requesting? // Retrieve all of the deltas. Mark any received field as dirty. Wire.requestFrom(I2CMETER_ADDR, numBytes); @@ -276,7 +265,7 @@ void RaduinoState::update() { * instance, when a RIGINF signal is received via I2C. It * receives the incoming data from the Raduino. */ -void RaduinoState::receiveRIGINF() { +void RigState::receive_RIGINF() { // 1st (-1) byte read should be a field index. // 2nd (0) thru 5th (3) bytes are bytes of the field. // We'll read as many fields as the Raduino sends. @@ -312,7 +301,7 @@ void RaduinoState::receiveRIGINF() { * instance, when a RIGINF signal is received via I2C. It * sends a response to the Raduino */ -void RaduinoState::respondRIGINF(byte numBytes, RigState& catState) { +void RigState::send_RIGINF(byte numBytes, RigState& catState) { // Now we need to determine the differences from the other state (i.e. // from the catState) and send those differences. byte rigRegBytes = 0; diff --git a/TeensyDSP/RigState.h b/TeensyDSP/RigState.h index b1d2b25..289be49 100644 --- a/TeensyDSP/RigState.h +++ b/TeensyDSP/RigState.h @@ -40,7 +40,7 @@ struct BaseField { }; template -struct Field : public BaesField { +struct Field : public BaseField { /*! * @brief Using the supplied read function, which should take a * pointer to data as its input, read from (some source) and diff --git a/TeensyDSP/TeensyDSP.h b/TeensyDSP/TeensyDSP.h index 9e1063d..3bb0857 100644 --- a/TeensyDSP/TeensyDSP.h +++ b/TeensyDSP/TeensyDSP.h @@ -68,8 +68,8 @@ extern int magnitudelimit_low; #define I2CMETER_CALCR 0x55 //Calculated SWR Meter #define I2CMETER_UNCALCR 0x54 //Uncalculated SWR Meter -// Raduino provides updated data to TeensyDSP +// Raduino<=>TeensyDSP data exchange #define I2CMETER_RIGINF 0x50 // Raduino requests any CAT updates from TeensyDSP -#define I2CMETER_REQCAT 0x51 +//#define I2CMETER_REQCAT 0x51 diff --git a/TeensyDSP/TeensyDSP.ino b/TeensyDSP/TeensyDSP.ino index 3f03398..bfeb3bd 100644 --- a/TeensyDSP/TeensyDSP.ino +++ b/TeensyDSP/TeensyDSP.ino @@ -374,12 +374,11 @@ void i2cReceiveEvent(size_t numBytes) int readCommand = 0; bool exitLoop = false; + // Does this really need to be a while loop? Don't we know the number of bytes? while (Wire1.available() > 0 && !exitLoop) { readCommand = Wire1.read(); if (readCommand == I2CMETER_RIGINF) { - // NEEDS TO GET UPDATED - //rigState.getSizeOfChanges(); - //rigState.update(I2CMETER_RIGINF); + Rig.rad().receive_RIGINF(numBytes - 1); exitLoop = true; } } @@ -415,37 +414,26 @@ 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(int(fwdPower * 100.0)); - #ifdef DEBUG - i2cRespCounter[i2cCommand - 0x50]++; - #endif break; case I2CMETER_CALCR: // Returns a raw reverse power value. Wire1.write(int(revPower * 100.0)); - #ifdef DEBUG - i2cRespCounter[i2cCommand - 0x50]++; - #endif break; case I2CMETER_RIGINF: // Receive current rig state; transmit any CAT updates, if required. + Rig.cat().send_RIGINF(); //Wire1.write(catState.header); // temporary - just writing a single, null byte // NEEDS TO GET UPDATED break; @@ -468,14 +456,18 @@ void i2cRequestEvent(void) //Wire1.write(Rig.stateAsBytes(), sizeof(uint8_t)); Wire1.write(0); } - #ifdef DEBUG - i2cRespCounter[i2cCommand - 0x50]++; - #endif break; default: break; } + +#ifdef DEBUG + if (0x50 <= i2cCommand && i2cCommand <= 0x59) + { + i2cRespCounter[i2cCommand - 0x50]++; + } +#endif } //extern void Decode_Morse(float magnitude);