Skip to content
Open
Show file tree
Hide file tree
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
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Raspberry Pi C driver and Python bindings for the sensor BMP180.
#include "bmp180.h"
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char **argv){
char *i2c_device = "/dev/i2c-1";
Expand All @@ -19,11 +21,29 @@ int main(int argc, char **argv){
void *bmp = bmp180_init(address, i2c_device);

if(bmp != NULL){
int i;
int i, error;
for(i = 0; i < 10; i++) {
float t = bmp180_temperature(bmp);
error = bmp180_get_last_errno(bmp);
if (error != 0) {
printf("Error reading temperature: %s\n", strerror(error));
exit(1);
}

long p = bmp180_pressure(bmp);
error = bmp180_get_last_errno(bmp);
if (error != 0) {
printf("Error reading pressure: %s\n", strerror(error));
exit(1);
}

float alt = bmp180_altitude(bmp);
error = bmp180_get_last_errno(bmp);
if (error != 0) {
printf("Error reading altitude: %s\n", strerror(error));
exit(1);
}

printf("t = %f, p = %lu, a = %f\n", t, p, alt);
usleep(2 * 1000 * 1000);
}
Expand Down
53 changes: 51 additions & 2 deletions src/bmp180.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#include <linux/i2c-dev.h>
#include <time.h>
#include <math.h>
#include <limits.h>
#include <float.h>
#endif


Expand Down Expand Up @@ -160,6 +162,9 @@ typedef struct {
int32_t mb;
int32_t mc;
int32_t md;

/* Last errno */
int error;
} bmp180_t;


Expand Down Expand Up @@ -289,7 +294,13 @@ void bmp180_read_eprom(void *_bmp) {
*/
int32_t bmp180_read_raw_temperature(void *_bmp) {
bmp180_t* bmp = TO_BMP(_bmp);
i2c_smbus_write_byte_data(bmp->file, BMP180_CTRL, BMP180_TMP_READ_CMD);

bmp->error = i2c_smbus_write_byte_data(bmp->file, BMP180_CTRL, BMP180_TMP_READ_CMD);
if (bmp->error < 0) {
DEBUG("error: i2c_smbus_write_byte_data failed with error %d\n", bmp->error);
bmp->error *= -1;
return -1;
}

usleep(BMP180_TMP_READ_WAIT_US);
int32_t data = i2c_smbus_read_word_data(bmp->file, BMP180_REG_TMP) & 0xFFFF;
Expand Down Expand Up @@ -329,7 +340,12 @@ int32_t bmp180_read_raw_pressure(void *_bmp, uint8_t oss) {
break;
}

i2c_smbus_write_byte_data(bmp->file, BMP180_CTRL, cmd);
bmp->error = i2c_smbus_write_byte_data(bmp->file, BMP180_CTRL, cmd);
if (bmp->error < 0) {
DEBUG("error: i2c_smbus_write_byte_data failed with error %d\n", bmp->error);
bmp->error *= -1;
return -1;
}

usleep(wait);

Expand Down Expand Up @@ -421,6 +437,7 @@ void *bmp180_init(int address, const char* i2c_device_filepath) {

DEBUG("device: open ok\n");

bmp->error = 0;
return _bmp;
}

Expand Down Expand Up @@ -449,6 +466,22 @@ void bmp180_close(void *_bmp) {
}


/**
* Determine if the last BMP180 operation was successful.
*
* @param bmp180 sensor
*
* @ret 0 if the last operation was successful; otherwise the errno from the
* last failed operation
*/

int bmp180_get_last_errno(void *_bmp)
{
bmp180_t* bmp = TO_BMP(_bmp);
return bmp->error;
}


/**
* Returns the measured temperature in celsius.
*
Expand All @@ -461,6 +494,9 @@ float bmp180_temperature(void *_bmp) {
float T;

UT = bmp180_read_raw_temperature(_bmp);
if (bmp->error != 0) {
return -FLT_MAX; // error
}

DEBUG("UT=%lu\n",UT);

Expand All @@ -485,7 +521,14 @@ long bmp180_pressure(void *_bmp) {
unsigned long B4, B7;

UT = bmp180_read_raw_temperature(_bmp);
if (bmp->error != 0) {
return LONG_MIN; // error
}

UP = bmp180_read_raw_pressure(_bmp, bmp->oss);
if (bmp->error != 0) {
return LONG_MIN; // error
}

X1 = ((UT - bmp->ac6) * bmp->ac5) >> 15;
X2 = (bmp->mc << 11) / (X1 + bmp->md);
Expand Down Expand Up @@ -530,8 +573,14 @@ long bmp180_pressure(void *_bmp) {
* @return altitude
*/
float bmp180_altitude(void *_bmp) {
bmp180_t* bmp = TO_BMP(_bmp);
float p, alt;

p = bmp180_pressure(_bmp);
if (bmp->error != 0) {
return -FLT_MAX; // error
}

alt = 44330 * (1 - pow(( (p/100) / BMP180_SEA_LEVEL),1/5.255));

return alt;
Expand Down
2 changes: 2 additions & 0 deletions src/bmp180.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,7 @@ float bmp180_temperature(void *_bmp);

float bmp180_altitude(void *_bmp);

int bmp180_get_last_errno(void *_bmp);

void bmp180_dump_eprom(void *_bmp, bmp180_eprom_t *eprom);

22 changes: 21 additions & 1 deletion src/test.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "bmp180.h"
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char **argv){
char *i2c_device = "/dev/i2c-1";
Expand All @@ -15,11 +17,29 @@ int main(int argc, char **argv){
bmp180_set_oss(bmp, 1);

if(bmp != NULL){
int i;
int i, error;
for(i = 0; i < 10; i++) {
float t = bmp180_temperature(bmp);
error = bmp180_get_last_errno(bmp);
if (error != 0) {
printf("Error reading temperature: %s\n", strerror(error));
exit(1);
}

long p = bmp180_pressure(bmp);
error = bmp180_get_last_errno(bmp);
if (error != 0) {
printf("Error reading pressure: %s\n", strerror(error));
exit(1);
}

float alt = bmp180_altitude(bmp);
error = bmp180_get_last_errno(bmp);
if (error != 0) {
printf("Error reading altitude: %s\n", strerror(error));
exit(1);
}

printf("t = %f, p = %lu, a= %f\n", t, p, alt);
usleep(2 * 1000 * 1000);
}
Expand Down