Menu

Meaning of code

Help
MBB
2025-01-18
2025-01-18
  • MBB

    MBB - 2025-01-18

    Does anyone know what this code means:

    float Adafruit_seesaw::getTemp() {
    uint8_t buf[4];
    this->read(SEESAW_STATUS_BASE, SEESAW_STATUS_TEMP, buf, 4, 1000);
    int32_t ret = ((uint32_t)buf[0] << 24) | ((uint32_t)buf[1] << 16) |
    ((uint32_t)buf[2] << 8) | (uint32_t)buf[3];
    return (1.0 / (1UL << 16)) * ret;
    }

    or in python this:

    def get_temp(self):
        """Read the temperature"""
        buf = bytearray(4)
        self.read(_STATUS_BASE, _STATUS_TEMP, buf, 0.005)
        buf[0] = buf[0] & 0x3F
        ret = struct.unpack(">I", buf)[0]
        return 0.00001525878 * ret
    
     
  • Anobium

    Anobium - 2025-01-18

    The C conversion follows.

    The real question would be ... what is the sensor and how do you get the result with integer maths.. So, what is the sensor?

    Evan

    function getTemp as Long
    Dim buf(4) at GetTemp
    //! This is another routine called read()
      //! SEESAW_STATUS_BASE  - some constant value
      //! SEESAW_STATUS_TEMP  - some constant value
      //! buf()               - the array/buffer being used to pass the data
      //! 4                   - the size of the array/buffer - my guess
      //! 1000                - a timeout - my guess
      //!
      //! return range is unknown.
    read( SEESAW_STATUS_BASE, SEESAW_STATUS_TEMP, buf(), 4, 1000)
    
    //! build up a the return..
    //! Note array starts at element 1 
    //!  equates to getemp_E = buf(1)
    //!  equates to getemp_U = buf(2)
    //!  equates to getemp_H = buf(3)
    //!  equates to getemp = buf(4)
    int32_t ret = ((uint32_t)buf[0] << 24) | ((uint32_t)buf[1] << 16) |
    ((uint32_t)buf[2] << 8) | (uint32_t)buf[3];
    
    //! return the value...
    //!  1UL << 16 means:
    /*
      1UL is 1 as an unsigned long integer
      << 16 shifts it left by 16 bits
      This is equivalent to multiplying by 2^16
      2^16 = 65,536
    
    
      So 1UL << 16 = 65,536
      Now we're calculating 1.0 / 65,536
      This equals approximately 0.0000152587890625
    
    The exact result is 1/65,536 = 0.0000152587890625 (this is precise as it's a power of 2 division)
    */
      return (1.0 / (1UL << 16)) * ret;
    
    end Sub
    
     
  • MBB

    MBB - 2025-01-18

    Thanks!

    I'm using this board https://www.adafruit.com/product/3657

     
    👍
    1

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.