From a17c276f3532d4963d5dbb12354b8095ad18e3df Mon Sep 17 00:00:00 2001 From: "R.P.Clark" Date: Fri, 3 Nov 2023 21:40:45 +0000 Subject: [PATCH] cleanup. gonna add gas stuff --- Makefile | 21 +++++++++++--- hedgehog.c | 84 ------------------------------------------------------ main.c | 5 +++- 3 files changed, 21 insertions(+), 89 deletions(-) delete mode 100644 hedgehog.c diff --git a/Makefile b/Makefile index e64594a..d20a631 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,17 @@ -all: - gcc -O2 -std=gnu99 -Wall -Wextra main.c bme680.c i2c.c -o bme680 -hedgehog: - gcc -O2 -std=gnu99 -Wall -Wextra hedgehog.c bme680.c i2c.c -o hedgehog +CC=gcc +OPT=-O2 -std=c99 -Wall -Wextra +CFLAGS=-I. $(OPT) +CFILES=$(wildcard ./*.c) +OBJECTS=$(patsubst %.c,%.o, $(CFILES)) +BINARY=bme680 + +all: $(BINARY) + +$(BINARY): $(OBJECTS) + $(CC) $^ -o $@ + +%.o:%.c + $(CC) $(CFLAGS) -c $< -o $@ + +clean: + @rm -rf $(OBJECTS) $(BINARY) diff --git a/hedgehog.c b/hedgehog.c deleted file mode 100644 index 461c93f..0000000 --- a/hedgehog.c +++ /dev/null @@ -1,84 +0,0 @@ -#include -#include "bme680.h" -#include "i2c.h" -#include - -#define DEVICE "/dev/i2c-1" -#define ADDRESS 0x77 - -int main(){ - - int fd = i2c_init(DEVICE, ADDRESS); - bme680_t bme680; - - //printf("%X\n", i2c_read_reg2(fd, 0xD0)); - if (bme680_calibrate(fd, &bme680.cal) != 0) { - fprintf(stderr, "error bme680 calibration\n"); - } else { - //print_calibration(&bme680.cal); - } - - - - //1. write osrs fields to diff regs - uint8_t ctrl_meas, ctrl_hum; - - uint8_t osrs_t, osrs_p, osrs_h, ctrl_mode; - osrs_t = osrs_p = osrs_h = 0b110; - ctrl_mode = 0; - - ctrl_meas = (osrs_t << 5) | (osrs_p << 2) | ctrl_mode; - ctrl_hum = osrs_h; - - i2c_write_reg(fd, 0x74, ctrl_meas); - i2c_write_reg(fd, 0x72, ctrl_hum); - - // set filter mode - - uint8_t filter_reg = 0b101 << 2; - i2c_write_reg(fd, 0x75, filter_reg); - - // todo gas stuff - - // resend ctrl_meas but with ctrl_mode set to 0b01 in it, then it should work - ctrl_meas |= 1; - i2c_write_reg(fd, 0x74, ctrl_meas); - - // poll reg `meas_status_0 bit 5. It will be 0 when all scheduled conversions are done. - uint8_t qq; - while ((qq = i2c_read_reg2(fd, 0x1D)) >> 4) { - usleep(2000); // 2 ms - } - - // now read the temp/press/hum adc and convert in order. - uint32_t temp_adc, press_adc, hum_adc; - - - uint8_t buffer[3]; - - i2c_read_reg(fd, 0x22, 3, buffer); - temp_adc = (buffer[0] << 12) | (buffer[1] << 4) | (buffer[2] >> 4); - - i2c_read_reg(fd, 0x1F, 3, buffer); - press_adc = (buffer[0] << 12) | (buffer[1] << 4) | (buffer[2] >> 4); - - i2c_read_reg(fd, 0x25, 2, buffer); - hum_adc = (buffer[0] << 8) | buffer[1]; - - // adc readings are only 20-bit when the IIR filter is enabled, otherwise - // the size depends on the oversample settings (osrs_X). - - double temperature = calc_temp_comp_1 ( temp_adc , &bme680 ); - - double pressure = calc_press_comp_1 ( press_adc , &bme680 ); - - double humidity = calc_hum_comp_1 ( hum_adc, &bme680 ); - - if (humidity > 100.0) humidity = 100.0; - if (humidity < 0.0) humidity = 0.0; - - printf("%.1lfoC %.2lf hPa %.1lf %%RH\n", temperature, pressure/100, humidity); - - close(fd); - return 0; -} diff --git a/main.c b/main.c index 37d7115..1fa69ce 100644 --- a/main.c +++ b/main.c @@ -1,7 +1,10 @@ +/* usleep() */ +#define _DEFAULT_SOURCE + #include +#include #include "bme680.h" #include "i2c.h" -#include #define DEVICE "/dev/i2c-1" #define ADDRESS 0x77