user-provided 'sleep' func, for targets without usleep()

This commit is contained in:
William Clark 2023-11-22 11:48:03 +00:00
parent 9329817cb0
commit 8911be0f32
7 changed files with 29 additions and 5 deletions

View File

@ -1,7 +1,4 @@
#define _DEFAULT_SOURCE
#include <stdio.h>
#include <unistd.h>
#include "bme680.h"
#include "registers.h"
@ -147,7 +144,7 @@ int bme680_reset(bme680_t *bme680) {
}
ret = bme680->dev.write(reg, magic);
usleep(2000); /* sleep for 2 ms */
bme680->dev.sleep(2000); /* sleep for 2 ms */
return ret;
}
@ -215,7 +212,7 @@ int bme680_poll(bme680_t *bme680) {
do {
usleep(5000); /* 5 ms */
bme680->dev.sleep(5000); /* 5 ms */
err |= read_dev(bme680, REG_MEAS_STATUS, &meas_status, 1);
gas_measuring = (meas_status >> 6) & 1;
any_measuring = (meas_status >> 5) & 1;

View File

@ -50,6 +50,7 @@ struct bme680_dev {
int (*init) (void);
int (*read) (uint8_t reg, uint8_t *dst, uint32_t size);
int (*write) (uint8_t reg, uint8_t value);
int (*sleep) (uint32_t dur_ms);
int (*deinit) (void);
};

View File

@ -1,3 +1,6 @@
/* for usleep() */
#define _DEFAULT_SOURCE
#include <stdio.h>
#include <stdlib.h>
@ -23,6 +26,8 @@ int main(void) {
bme680.dev.write = spi_write;
bme680.dev.deinit = spi_deinit;
bme680.dev.sleep = usleep;
mode = BME680_MODE_FLOAT | BME680_SPI | BME680_ENABLE_GAS;
if (bme680_init(&bme680, mode) != 0) {

View File

@ -1,3 +1,6 @@
/* for usleep() */
#define _DEFAULT_SOURCE
#include <stdio.h>
#include <stdlib.h>
@ -28,6 +31,8 @@ int main(void) {
bme680.dev.write = spi_write;
bme680.dev.deinit = spi_deinit;
bme680.dev.sleep = usleep;
mode = BME680_MODE_FLOAT | BME680_SPI | BME680_ENABLE_GAS;
if (bme680_init(&bme680, mode) != 0) {

6
i2c.c
View File

@ -1,3 +1,9 @@
/*
Example I2C use on linux/raspberry pi
*/
#include <stdio.h>
#include <fcntl.h>
#include <linux/i2c.h>

4
main.c
View File

@ -1,3 +1,5 @@
/* for usleep() */
#define _DEFAULT_SOURCE
#include <stdio.h>
#include <stdlib.h>
@ -28,6 +30,8 @@ int main(void) {
bme680.dev.write = spi_write;
bme680.dev.deinit = spi_deinit;
bme680.dev.sleep = usleep;
/* 2. set the device mode */
mode = BME680_MODE_FLOAT | BME680_SPI | BME680_ENABLE_GAS;
/* BME680_MODE_INT | BME680_I2C; */

6
spi.c
View File

@ -1,3 +1,9 @@
/*
Example SPI use on linux/raspberry pi
*/
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>