Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix: Fix below 0 temperatures #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 22 additions & 91 deletions src/SparkFun_GridEYE_Arduino_Library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ void GridEYE::setI2CAddress(uint8_t addr)
_deviceAddress = addr;
}

int16_t extend12to16(int16_t temperature) {
// temperature is reported as 12-bit twos complement
// extend sign bit to 16-bit twos complement
temperature &= 0x0fff;
temperature |= (temperature & (1 << 11))<<1;
temperature |= (temperature & (1 << 11))<<2;
temperature |= (temperature & (1 << 11))<<3;
temperature |= (temperature & (1 << 11))<<4;
return temperature;
}

/********************************************************
* Functions for retreiving the temperature of
* a single pixel.
Expand All @@ -65,15 +76,7 @@ float GridEYE::getPixelTemperature(unsigned char pixelAddr)
unsigned char pixelLowRegister = TEMPERATURE_REGISTER_START + (2 * pixelAddr);
int16_t temperature = getRegister(pixelLowRegister, 2);

// temperature is reported as 12-bit twos complement
// check if temperature is negative
if(temperature & (1 << 11))
{
// if temperature is negative, mask out the sign byte and
// make the float negative
temperature &= ~(1 << 11);
temperature = temperature * -1;
}
temperature = extend12to16(temperature);

float DegreesC = temperature * 0.25;

Expand All @@ -89,15 +92,7 @@ float GridEYE::getPixelTemperatureFahrenheit(unsigned char pixelAddr)
unsigned char pixelLowRegister = TEMPERATURE_REGISTER_START + (2 * pixelAddr);
int16_t temperature = getRegister(pixelLowRegister, 2);

// temperature is reported as 12-bit twos complement
// check if temperature is negative
if(temperature & (1 << 11))
{
// if temperature is negative, mask out the sign byte and
// make the float negative
temperature &= ~(1 << 11);
temperature = temperature * -1;
}
temperature = extend12to16(temperature);

float DegreesF = (temperature * 0.25) * 1.8 + 32;

Expand Down Expand Up @@ -136,15 +131,7 @@ float GridEYE::getDeviceTemperature()

int16_t temperature = getRegister(THERMISTOR_REGISTER_LSB, 2);

// temperature is reported as 12-bit twos complement
// check if temperature is negative
if(temperature & (1 << 11))
{
// if temperature is negative, mask out the sign byte and
// make the float negative
temperature &= ~(1 << 11);
temperature = temperature * -1;
}
temperature = extend12to16(temperature);

float realTemperature = temperature * 0.0625;

Expand All @@ -157,15 +144,7 @@ float GridEYE::getDeviceTemperatureFahrenheit()

int16_t temperature = getRegister(THERMISTOR_REGISTER_LSB, 2);

// temperature is reported as 12-bit twos complement
// check if temperature is negative
if(temperature & (1 << 11))
{
// if temperature is negative, mask out the sign byte and
// make the float negative
temperature &= ~(1 << 11);
temperature = temperature * -1;
}
temperature = extend12to16(temperature);

float realTemperatureF = (temperature * 0.0625) * 1.8 + 32;

Expand Down Expand Up @@ -722,15 +701,7 @@ float GridEYE::getUpperInterruptValue()

int16_t temperature = getRegister(INT_LEVEL_REGISTER_UPPER_LSB, 2);

// temperature is reported as 12-bit twos complement
// check if temperature is negative
if(temperature & (1 << 11))
{
// if temperature is negative, mask out the sign byte and
// make the float negative
temperature &= ~(1 << 11);
temperature = temperature * -1;
}
temperature = extend12to16(temperature);

float DegreesC = temperature * 0.25;

Expand All @@ -750,15 +721,7 @@ float GridEYE::getUpperInterruptValueFahrenheit()

int16_t temperature = getRegister(INT_LEVEL_REGISTER_UPPER_LSB, 2);

// temperature is reported as 12-bit twos complement
// check if temperature is negative
if(temperature & (1 << 11))
{
// if temperature is negative, mask out the sign byte and
// make the float negative
temperature &= ~(1 << 11);
temperature = temperature * -1;
}
temperature = extend12to16(temperature);

float DegreesF = (temperature * 0.25) * 1.8 + 32;

Expand All @@ -771,15 +734,7 @@ float GridEYE::getLowerInterruptValue()

int16_t temperature = getRegister(INT_LEVEL_REGISTER_LOWER_LSB, 2);

// temperature is reported as 12-bit twos complement
// check if temperature is negative
if(temperature & (1 << 11))
{
// if temperature is negative, mask out the sign byte and
// make the float negative
temperature &= ~(1 << 11);
temperature = temperature * -1;
}
temperature = extend12to16(temperature);

float DegreesC = temperature * 0.25;

Expand All @@ -792,15 +747,7 @@ float GridEYE::getLowerInterruptValueFahrenheit()

int16_t temperature = getRegister(INT_LEVEL_REGISTER_LOWER_LSB, 2);

// temperature is reported as 12-bit twos complement
// check if temperature is negative
if(temperature & (1 << 11))
{
// if temperature is negative, mask out the sign byte and
// make the float negative
temperature &= ~(1 << 11);
temperature = temperature * -1;
}
temperature = extend12to16(temperature);

float DegreesF = (temperature * 0.25) * 1.8 + 32;

Expand All @@ -820,15 +767,7 @@ float GridEYE::getInterruptHysteresis()

int16_t temperature = getRegister(INT_LEVEL_REGISTER_HYST_LSB, 2);

// temperature is reported as 12-bit twos complement
// check if temperature is negative
if(temperature & (1 << 11))
{
// if temperature is negative, mask out the sign byte and
// make the float negative
temperature &= ~(1 << 11);
temperature = temperature * -1;
}
temperature = extend12to16(temperature);

float DegreesC = temperature * 0.25;

Expand All @@ -841,15 +780,7 @@ float GridEYE::getInterruptHysteresisFahrenheit()

int16_t temperature = getRegister(INT_LEVEL_REGISTER_HYST_LSB, 2);

// temperature is reported as 12-bit twos complement
// check if temperature is negative
if(temperature & (1 << 11))
{
// if temperature is negative, mask out the sign byte and
// make the float negative
temperature &= ~(1 << 11);
temperature = temperature * -1;
}
temperature = extend12to16(temperature);

float DegreesF = (temperature * 0.25) * 1.8 + 32;

Expand Down Expand Up @@ -908,4 +839,4 @@ int16_t GridEYE::getRegister(unsigned char reg, int8_t len)

return result;

}
}