Skip to content

Driver not working with negative temps #2

@kellyho67

Description

@kellyho67

I used some example code from another temper project to deal with negatives. This has been tested for my system only but seems to work.

static int interrupt_read_temperatura(usb_dev_handle *dev, float *tempC) {

    int r,i, temperature;
    char answer[reqIntLen];
    bzero(answer, reqIntLen);

    r = usb_interrupt_read(dev, 0x82, answer, reqIntLen, timeout);
    if( r != reqIntLen )
    {
        if(debug){
            printf("USB interrupt read");
        }
        return -1;
    }


    if(debug) {
        for (i=0;i<reqIntLen; i++) printf("%02x ",answer[i]  & 0xFF);

        printf("\n");
    }

    temperature = (answer[3] & 0xFF) + (answer[2] << 8);

    /* msb means the temperature is negative -- less than 0 Celsius -- and in 2'complement form.
    * We can't be sure that the host uses 2's complement to store negative numbers
    * so if the temperature is negative, we 'manually' get its magnitude
    * by explicity getting it's 2's complement and then we return the negative of that.
    */

        if ((answer[2] & 0x80)!=0) {
        /* return the negative of magnitude of the temperature */
            temperature = -((temperature ^ 0xffff)+1);
        }
    // end of the updates made for netgative temps
    *tempC = temperature * (125.0 / 32000.0);
    return 0;
  }

static int get_temperature(usb_dev_handle *dev, float *tempC){
    char buf[256];
    int ret, i, temperature;

    control_transfer(dev, uCmd1 );
    control_transfer(dev, uCmd4 );
    for(i = 0; i < 7; i++) {
        control_transfer(dev, uCmd0 );
    }
    control_transfer(dev, uCmd2 );
    ret = get_data(dev, buf, 256);
    if(ret < 2) {
        return -1;
    }

    temperature = (buf[1] & 0xFF) + (buf[0] << 8);


    /* msb means the temperature is negative -- less than 0 Celsius -- and in 2'complement form.
    * We can't be sure that the host uses 2's complement to store negative numbers
    * so if the temperature is negative, we 'manually' get its magnitude
    * by explicity getting it's 2's complement and then we return the negative of that.
    */

        if ((buf[0] & 0x80)!=0) {
        /* return the negative of magnitude of the temperature */
            temperature = -((temperature ^ 0xffff)+1);
        }

    // end up the updates made.

    *tempC = temperature * (125.0 / 32000.0);
    return 0;
 } 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions