Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Development
  3. Check if the value changed since last time

Check if the value changed since last time

Scheduled Pinned Locked Moved Development
6 Posts 3 Posters 1.4k Views 4 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • cimba007C Offline
    cimba007C Offline
    cimba007
    wrote on last edited by
    #1

    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;
        }
    
    YveauxY 1 Reply Last reply
    0
    • cimba007C cimba007

      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;
          }
      
      YveauxY Offline
      YveauxY Offline
      Yveaux
      Mod
      wrote on last edited by
      #2

      @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!

      http://yveaux.blogspot.nl

      cimba007C 1 Reply Last reply
      1
      • YveauxY Yveaux

        @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!

        cimba007C Offline
        cimba007C Offline
        cimba007
        wrote on last edited by
        #3

        @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;
        }
        
        YveauxY 1 Reply Last reply
        0
        • cimba007C cimba007

          @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;
          }
          
          YveauxY Offline
          YveauxY Offline
          Yveaux
          Mod
          wrote on last edited by
          #4

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

          http://yveaux.blogspot.nl

          cimba007C 1 Reply Last reply
          0
          • YveauxY Yveaux

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

            cimba007C Offline
            cimba007C Offline
            cimba007
            wrote on last edited by
            #5

            @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).

            1 Reply Last reply
            0
            • TheoLT Offline
              TheoLT Offline
              TheoL
              Contest Winner
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              1
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              6

              Online

              11.7k

              Users

              11.2k

              Topics

              113.0k

              Posts


              Copyright 2019 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • MySensors
              • OpenHardware.io
              • Categories
              • Recent
              • Tags
              • Popular