Skip to content

Commit

Permalink
add correct timer calls
Browse files Browse the repository at this point in the history
  • Loading branch information
aldwinhermanudin committed Dec 22, 2023
1 parent 09044e3 commit 052e8c0
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 80 deletions.
2 changes: 1 addition & 1 deletion src/mbedos/test-bme688/mbed_app.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

"target.c_lib": "std",
"target.printf_lib": "std",
"platform.stdio-buffered-serial": 0,
"platform.stdio-buffered-serial": 1,
"platform.stdio-convert-newlines": true,
"platform.stdio-baud-rate": 115200,
"platform.default-serial-baud-rate": 115200,
Expand Down
18 changes: 11 additions & 7 deletions src/mbedos/test-bme688/source/bme688/BME688.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include "BME688.h"
#include "Defer.h"

using namespace std::chrono;

static int8_t ReadRegister(uint8_t reg_addr, uint8_t *regdata, uint32_t length, void *intf_ptr);
static int8_t WriteRegister(uint8_t reg_addr,const uint8_t *reg_data, uint32_t length,void *intf_ptr);
static void delay(uint32_t period, void *intf_ptr);
static void delay_us(uint32_t time_us, void *intf_ptr);

BME688::BME688( PinName i2c_sda, PinName i2c_scl, uint32_t bme688_addr )
{
Expand Down Expand Up @@ -66,7 +68,7 @@ BME688::ReturnCode BME688::InitialiseSensor()
sensor.dev.intf = BME68X_I2C_INTF;
sensor.dev.read = ReadRegister;
sensor.dev.write = WriteRegister;
sensor.dev.delay_us = delay;
sensor.dev.delay_us = delay_us;
sensor.dev.amb_temp = 25; // used in arduino example

this->sensor.status = bme68x_init(&sensor.dev);
Expand Down Expand Up @@ -449,8 +451,9 @@ BME688::ReturnCode BME688::ProcessData(const int64_t curr_time_ns, const bme68x_
BME688::ReturnCode BME688::Run(void)
{
ReturnCode ret = ReturnCode::kError;
// int64_t curr_time_ns = getTimeMs() * INT64_C(1000000);
int64_t curr_time_ns; // FIXME: need to implement

// TODO: verify needed. Implementation might be wrong.
int64_t curr_time_ns = std::chrono::time_point_cast<std::chrono::nanoseconds>(Kernel::Clock::now()).time_since_epoch().count();
this->sensor.last_op_mode = this->bsec.sensor_conf.op_mode;

if (curr_time_ns >= this->bsec.sensor_conf.next_call)
Expand Down Expand Up @@ -574,10 +577,11 @@ static int8_t WriteRegister(uint8_t reg_addr, const uint8_t *reg_data, uint32_t
return rslt;
}


static void delay(uint32_t period, void *intf_ptr)
static void delay_us(uint32_t time_us, void *intf_ptr)
{
wait_us ( period );
// use wait_us to wait without sleep
// if system goes to sleep, I2C comm need to be re-init
wait_us ( time_us );
}


160 changes: 88 additions & 72 deletions src/mbedos/test-bme688/source/main.cpp
Original file line number Diff line number Diff line change
@@ -1,85 +1,101 @@
/*
* Copyright (c) 2006-2020 Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*/
#include "mbed.h"
#include "bme688/BME688.h"

#define ARRAY_LEN(array) (sizeof(array)/sizeof(array[0]))
int main()
{
// wait a while
ThisThread::sleep_for(11s);

auto now_ns = std::chrono::time_point_cast<std::chrono::nanoseconds>(Kernel::Clock::now()).time_since_epoch().count(); // Convert time_point to one in microsecond accuracy
printf("epoch printf %lld\n", now_ns);
}

#define FEATHER_RAK3172
// #define BWLR1E

// define on-board led to use
#ifdef FEATHER_RAK3172
mbed::DigitalOut led0(PA_8);
#endif
#ifdef BWLR1E
mbed::DigitalOut led0(PA_15);
mbed::DigitalOut led1(PA_1);
#endif
// #include "mbed.h"
// #include "bme688/BME688.h"

// Spawns a thread to measurement every 500ms
Thread measure_thread;
// #define ARRAY_LEN(array) (sizeof(array)/sizeof(array[0]))

// measure environment
static void measure(BME688* bme688);
// #define FEATHER_RAK3172
// // #define BWLR1E

int main()
{
// // define on-board led to use
// #ifdef FEATHER_RAK3172
// mbed::DigitalOut led0(PA_8);
// #endif
// #ifdef BWLR1E
// mbed::DigitalOut led0(PA_15);
// mbed::DigitalOut led1(PA_1);
// #endif

// turn-off led for now
led0 = 0;
#ifdef BWLR1E
led01 = 0;
#endif
// // Spawns a thread to measurement every 500ms
// Thread measure_thread;

// initialise bmr688
BME688 iaq_sensor( I2C_SDA, I2C_SCL, 0x77 );
// // measure environment
// static void measure(BME688* bme688);

/* Desired subscription list of BSEC2 outputs */
bsec_virtual_sensor_t sensor_list[] = {
BSEC_OUTPUT_IAQ,
BSEC_OUTPUT_RAW_TEMPERATURE,
BSEC_OUTPUT_RAW_PRESSURE,
BSEC_OUTPUT_RAW_HUMIDITY,
BSEC_OUTPUT_RAW_GAS,
BSEC_OUTPUT_STABILIZATION_STATUS,
BSEC_OUTPUT_RUN_IN_STATUS
};
BME688::ReturnCode result = iaq_sensor.Initialise( sensor_list, ARRAY_LEN(sensor_list), BSEC_SAMPLE_RATE_LP );
if( result != BME688::ReturnCode::kOk )
{
while( true )
{
// blink led infinity to indicate bme688
// fail to initialise
ThisThread::sleep_for(500ms);
led0 = !led0;
}
}
// int main()
// {

// start measurement thread
measure_thread.start(callback(measure, &iaq_sensor));
while ( true )
{
ThisThread::sleep_for(15s);
printf("checking if data is available\n\r");
// if(iaq_sensor.IsNewDataAvailable())
// {
// // toggle pin if new data found
// led0 = !led0;
// printf("data available!\n\r");
// // turn-off led for now
// led0 = 0;
// #ifdef BWLR1E
// led01 = 0;
// #endif

// // initialise bmr688
// BME688 iaq_sensor( I2C_SDA, I2C_SCL, 0x77 );

// /* Desired subscription list of BSEC2 outputs */
// bsec_virtual_sensor_t sensor_list[] = {
// BSEC_OUTPUT_IAQ,
// BSEC_OUTPUT_RAW_TEMPERATURE,
// BSEC_OUTPUT_RAW_PRESSURE,
// BSEC_OUTPUT_RAW_HUMIDITY,
// BSEC_OUTPUT_RAW_GAS,
// BSEC_OUTPUT_STABILIZATION_STATUS,
// BSEC_OUTPUT_RUN_IN_STATUS
// };
// BME688::ReturnCode result = iaq_sensor.Initialise( sensor_list, ARRAY_LEN(sensor_list), BSEC_SAMPLE_RATE_LP );
// if( result != BME688::ReturnCode::kOk )
// {
// while( true )
// {
// // blink led infinity to indicate bme688
// // fail to initialise
// ThisThread::sleep_for(500ms);
// led0 = !led0;
// }
// }

// // start measurement thread
// measure_thread.start(callback(measure, &iaq_sensor));
// while ( true )
// {
// ThisThread::sleep_for(15s);
// printf("checking if data is available\n\r");
// // if(iaq_sensor.IsNewDataAvailable())
// // {
// // // toggle pin if new data found
// // led0 = !led0;
// // printf("data available!\n\r");

// // enable platform.stdio-buffered-serial in mbed_app.json
// // to see the dumped data
// // iaq_sensor.DumpData();
// }
}
measure_thread.join();
}
// // // enable platform.stdio-buffered-serial in mbed_app.json
// // // to see the dumped data
// // // iaq_sensor.DumpData();
// // }
// }
// measure_thread.join();
// }

// measure environment
void measure(BME688* bme688)
{
while ( true ) {
// bme688->DoMeasurements();
ThisThread::sleep_for(500ms);
}
}
// // measure environment
// void measure(BME688* bme688)
// {
// while ( true ) {
// // bme688->DoMeasurements();
// ThisThread::sleep_for(500ms);
// }
// }

0 comments on commit 052e8c0

Please sign in to comment.