Check if the value changed since last time



  • I am writing a little wrapper for mymessage to check some thinks like "When was the message send the last time?" and "Did the value change since the last update?"

    Realizing the "complicated" way mysensor stores values it is very difficulty for me to find an function that works with all value-types.

    Since I have to use a template for the equal-function and I apparently can't use static inside this function since I have differnt instances .. is there an easier way to check if the value changed since last time?

    Currently I store the value in an 8byte array which should cope with all data types including float.
    memcopy, memset and an short while-loop are hopefully not the most efficient way to do this.

    And nope .. I don't want to store the last value in the loop ...

      private:
        byte _lastmsg[8];
        byte _currmsg[8];
        
        template <typename T>
        bool ismyEq(T a)
        {
          //Serial.print("ismyEq: ");
          //Serial.print(_description);
          //Serial.print(" | ");
               
          uint8_t asize = sizeof(a);
          const uint8_t asize2 = asize;
          
          //Serial.print("asize: "); Serial.print(asize); Serial.print(" | ");
    
          // Current a-value to _currmsg
          memset(this->_currmsg,0x00,8);
          memcpy(this->_currmsg,&a,asize2);
    
          while(asize-- > 0 && this->_currmsg[asize] == this->_lastmsg[asize]);
          Serial.print(" asize: "); Serial.print(asize); Serial.print(" | ");
          // WARNING .. ASIZE = 255 !!!! Meaning both bytes are equal
          
          // Current -> Last
          memset(this->_lastmsg,0x00,8);
          memcpy(this->_lastmsg,&a,asize2);
          
          this->_skip = (asize == 255);
          
          return asize != 0;
        }
    

  • Mod

    @cimba007 you could store the sent and new value in separate messages, and then do a byte-by-byte comparison of these two messages.
    Not very nice, especially for floats which should probably be compared for almost-equality.
    I think I'm not getting what you are trying to achieve though. Your code seems like an attempt to create a generic compare for any type. You still have to store the old value to compare to somewhere...
    @theol also tried to solve this issue in a generic way. You should join forces!



  • @Yveaux

    I am doing exactly the thing you proposed .. except I don't create a duplicate message. This function takes an arbitrary datetype and writes it to an 8-byte array.
    Having two arrays I compare them both in the while-loop.

    My goal is to have a very clean and slim loop in my sketch while having a very generic wrapper.
    The wrapper includes sending and receiving and processing of the software ack (as my china counterfeight nrf24-modules dont't support hardware-autoack @ 250kbps). Furthermore I want to ensure that the message is not send too often (battery-node friendly) but at least every 32 seconds (example) or when the values changes from the last sent value.

    void loop()
    {
      // Reedsensor1
      reedSensor1.set(digitalRead(7));
      reedSensor1.waitsend(8,32); // Wait 8ms for response, otherwise retry, send max 1x every 32seconds
      
      sleep(2000);
      millis_offset += 2000;
    }
    

  • Mod

    @cimba007 can you give an example how you would like to use isMyEq() then?



  • @Yveaux

    Ideally I would like to use something like this:

        template <typename T>
        T lastValue;
    
        template <typename T>
        bool ismyEq2(T a)
        {
          bool iseq = (a == lastValue);
          lastValue = a;
          return iseq;
        }
    

    inside my class. But I learned that in C every variable has to be defined on compile time and this would not possible with lastValue.

    Currently I am very happy with ismyEq as it currently is simply because it is working for now 😉 Still I am open for better options - maybe some fancy template tricky I don't know yet. (Overall I know very little about proper Template using).


  • Contest Winner

    I think that @Yveaux refers to my generic threshold library (github). It really makes it easy to add sensors and reporting their values to the gateway.


Log in to reply
 

Suggested Topics

  • 1
  • 3
  • 10
  • 3
  • 6
  • 2

14
Online

11.4k
Users

11.1k
Topics

112.7k
Posts