#include "esp_log.h"
#include <string.h>
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include <esp_err.h>
//include <driver/spi_master.h>
#include <esp_task_wdt.h>
#include "SPI.h"
#include "BMP280.h"
static const char *TAG = "SPI";
#define LOG_LOCAL_LEVEL ESP_LOG_VERBOSE
//#define REG_ID 0xD0
//#define BMP280_ID 0x58
//tatic uint16_t dig_T1;
//tatic int16_t dig_T2, dig_T3;
//static int32_t t_fine;
/*
//≠============================================================
// Read temp calibration
void bmp280_read_temp_calib(void) {
uint8_t buff[6];
ESP_ERROR_CHECK(bmp280_read_bytes(0x88, buff, 6));
dig_T1 = (uint16_t) (buff[1] << 8 | buff[0]);
dig_T2 = (int16_t) (buff[3] << 8 | buff[2]);
dig_T3 = (int16_t) (buff[5] << 8 | buff[4]);
}
*/
//≠============================================================
/*
// Read temperature 20 bit
int32_t bmp280_read_temp(void) {
uint8_t d[3];
ESP_ERROR_CHECK(bmp280_read_bytes(0xfa, d, 3));
return (int32_t) ((d[0] << 12) | (d[1] << 4) | (d[2] >> 4)); // 20 bit
}
*/
//≠============================================================
/*
// Bosh compensation formula
int32_t bmp280_compensation(int32_t adc_T) {
int32_t var1, var2, T;
var1 = ((((adc_T >> 3) - ((int32_t)dig_T1 << 1))) * ((int32_t)dig_T2)) >> 11;
var2 = (((((adc_T >> 4) - ((int32_t)dig_T1)) * ((adc_T>>4) - ((int32_t)dig_T1))) >> 12) *
((int32_t)dig_T3)) >> 14;
t_fine = var1 + var2;
T = (t_fine * 5 + 128) >> 8;
return T;
}
*/
//≠============================================================
void app_main() {
esp_task_wdt_deinit();
esp_log_level_set("*", ESP_LOG_VERBOSE);
spi_bus_init();
vTaskDelay(pdMS_TO_TICKS(50));
spi_dev_init();
/*
uint8_t id = 0;
uint8_t Tx[2] ={(REG_ID | 0x80), 0};
uint8_t Rx[2];
//ESP_ERROR_CHECK(bmp280_read_byte(REG_ID, &id));
ESP_ERROR_CHECK(SPI_read(Tx, Rx, 2));
printf("ID = %d\n", Rx[1]);
*/
BMP280_init();
//bmp280_read_temp_calib();
// Enable only temperature measurement
ESP_ERROR_CHECK(bmp280_write_byte(0xf4, 0x23));
while (true) {
int32_t adc_T = bmp280_read_temp();
//int32_t T_01C = bmp280_compensation(adc_T);
float tempC = adc_T / 100.0f;
ESP_LOGI(TAG, "Temp = %.2f C", tempC);
vTaskDelay(pdMS_TO_TICKS(3000));
}
//vTaskDelay(pdMS_TO_TICKS(3000));
}