forked from begass/LoraAPRSGW
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTempPressHum.c
126 lines (109 loc) · 3.62 KB
/
TempPressHum.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "TempPressHum.h"
//////////////////////////////////////////////
//C Functions needed to get function pointers right for bme280.h
int fd_Temp;
float dTemp=0;
float dPressure=0;
float dHuminity=0;
struct bme280_dev dev;
int8_t rslt = BME280_OK;
char sI2C_Dev[15]="/dev/i2c-1";
int8_t user_i2c_read(uint8_t reg_addr, uint8_t *reg_data, uint32_t len, void *intf_ptr)
{
write(fd_Temp, ®_addr,1);
read(fd_Temp, reg_data, len);
return 0;
}
/*int8_t user_i2c_read(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len)
{
write(fd_Temp, ®_addr,1);
read(fd_Temp, data, len);
return 0;
}*/
void user_delay_us(uint32_t period, void *intf_ptr)
{
usleep(period);
}
int8_t user_i2c_write(uint8_t reg_addr, const uint8_t *reg_data, uint32_t len, void *intf_ptr)
{
int8_t *buf;
buf = (int8_t *)malloc(len +1);
buf[0] = reg_addr;
memcpy(buf +1, reg_data, len);
write(fd_Temp, buf, len +1);
free(buf);
return 0;
}
/*int8_t user_i2c_write(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len)
{
int8_t *buf;
buf = (int8_t *)malloc(len +1);
buf[0] = reg_addr;
memcpy(buf +1, data, len);
write(fd_Temp, buf, len +1);
free(buf);
return 0;
}*/
//////////////////////////////////////////////
int8_t setupBME280(){
//Test BME280
if ((fd_Temp = open(sI2C_Dev, O_RDWR)) < 0) {
printf("Failed to open the i2c bus %s \n", sI2C_Dev);
return 1;
}
if (ioctl(fd_Temp, I2C_SLAVE, 0x76) < 0) {
printf("Failed to acquire bus access and/or talk to slave.\n");
return 1;
}else{
printf("Open i2s %s success.\n", sI2C_Dev);
}
// dev.dev_id = BME280_I2C_ADDR_PRIM;
dev.chip_id = BME280_I2C_ADDR_PRIM;
dev.intf = BME280_I2C_INTF;
//printf("Testpoint ....1\n");
dev.read = user_i2c_read;
dev.write = user_i2c_write;
// dev.delay_ms = user_delay_ms;
dev.delay_us = user_delay_us;
rslt = bme280_init(&dev);
printf("setupBME280 finished\n");
return 0;
}
int8_t readBME280(){
uint8_t settings_sel;
struct bme280_data comp_data;
/* Recommended mode of operation: Indoor navigation */
dev.settings.osr_h = BME280_OVERSAMPLING_1X;
dev.settings.osr_p = BME280_OVERSAMPLING_16X;
dev.settings.osr_t = BME280_OVERSAMPLING_2X;
dev.settings.filter = BME280_FILTER_COEFF_16;
settings_sel = BME280_OSR_PRESS_SEL | BME280_OSR_TEMP_SEL | BME280_OSR_HUM_SEL | BME280_FILTER_SEL;
rslt = bme280_set_sensor_settings(settings_sel, &dev);
rslt = bme280_set_sensor_mode(BME280_FORCED_MODE, &dev);
//Wait for the measurement to complete and print data @25Hz
// dev.delay_ms(40);
usleep(40000);
rslt = bme280_get_sensor_data(BME280_ALL, &comp_data, &dev);
dTemp=comp_data.temperature;
dPressure=comp_data.pressure;
dHuminity=comp_data.humidity;
/*
printf("Temperature, Pressure, Humidity\r\n");
#ifdef BME280_FLOAT_ENABLE
//printf("temp %0.2f, p %0.2f, hum %0.2f\r\n",comp_data.temperature, comp_data.pressure, comp_data.humidity);
printf("temp %0.2f, p %0.2f, hum %0.2f\r\n",dTemp, dPressure, dHuminity);
#else
printf("temp %ld, p %ld, hum %ld\r\n",comp_data.temperature, comp_data.pressure, comp_data.humidity);
#endif
*/
return 0;
}
float getTemp(){
return dTemp;
}
float getPressure(){
return dPressure;
}
float getHuminity(){
return dHuminity;
}