Commit d49d7227 authored by Yongjia Niu's avatar Yongjia Niu
Browse files

GyroAttr: Add gyro attr to pub to iotconnect

parent 1e54214c
#include "GyroAttr.h"
#include "mbed_trace.h"
#define TRACE_GROUP "Gyro"
GyroAttr::GyroAttr(BMI160* _imu) :
imu(_imu),
gyro(NULL),
x(NULL),
y(NULL),
z(NULL),
interval_ms(0),
thread(osPriorityNormal, GYRO_SENSOR_UPDATE_THREAD_STACK_SIZE)
{
init_attr();
}
GyroAttr::~GyroAttr()
{
if (gyro) {
cJSON_Delete(gyro);
}
}
int GyroAttr::sensor_init()
{
int ret = 0;
ret = imu->setSensorPowerMode(BMI160::GYRO, BMI160::NORMAL);
if (ret != BMI160::RTN_NO_ERROR) {
tr_error("Sensor power mode set failed: %d", ret);
return ret;
}
ThisThread::sleep_for(100);
ret = imu->setSensorPowerMode(BMI160::ACC, BMI160::NORMAL);
if (ret != BMI160::RTN_NO_ERROR) {
tr_error("Sensor power mode set failed: %d", ret);
return ret;
}
ThisThread::sleep_for(100);
ret = imu->getSensorConfig(acc_config);
if (ret != BMI160::RTN_NO_ERROR) {
tr_error("Sensor get config failed: %d", ret);
return ret;
}
acc_config.range = BMI160::SENS_4G;
acc_config.us = BMI160::ACC_US_OFF;
acc_config.bwp = BMI160::ACC_BWP_2;
acc_config.odr = BMI160::ACC_ODR_8;
ret = imu->setSensorConfig(acc_config);
if (ret != BMI160::RTN_NO_ERROR) {
tr_error("Sensor set config failed: %d", ret);
return ret;
}
ret = imu->getSensorConfig(gyro_config);
if (ret != BMI160::RTN_NO_ERROR) {
tr_error("Sensor get config failed: %d", ret);
return ret;
}
ThisThread::sleep_for(100);
return BMI160::RTN_NO_ERROR;
}
void GyroAttr::init_attr()
{
gyro = cJSON_CreateObject();
x = cJSON_AddNumberToObject(gyro, "x", 0);
y = cJSON_AddNumberToObject(gyro, "y", 0);
z = cJSON_AddNumberToObject(gyro, "z", 0);
}
cJSON* GyroAttr::get_gyro()
{
return gyro;
}
int GyroAttr::start_sensor_update(int _interval_ms)
{
osStatus ret;
interval_ms = _interval_ms;
ret = thread.start(callback(this, &GyroAttr::sensor_update_main_loop));
if (ret != osOK) {
tr_error("Start thread failed with osStatus: %d", ret);
return -1;
}
return 0;
}
void GyroAttr::sensor_update_main_loop()
{
BMI160::SensorData acc_data;
BMI160::SensorData gyro_data;
BMI160::SensorTime sensor_time;
while(1) {
imu->getGyroAccXYZandSensorTime(acc_data, gyro_data, sensor_time,
acc_config.range, gyro_config.range);
x->valueint = gyro_data.xAxis.raw;
y->valueint = gyro_data.yAxis.raw;
z->valueint = gyro_data.zAxis.raw;
tr_debug("Read Gyro RAW Date:x=%d, y=%d, z=%d",
gyro_data.xAxis.raw,
gyro_data.yAxis.raw,
gyro_data.zAxis.raw);
ThisThread::sleep_for(interval_ms);
}
}
\ No newline at end of file
#ifndef __GYRO_ATTR_H__
#define __GYRO_ATTR_H__
#include "mbed.h"
#include "bmi160.h"
#include "cJSON.h"
#define GYRO_SENSOR_UPDATE_THREAD_STACK_SIZE 2048
class GyroAttr
{
public:
GyroAttr(BMI160* _imu);
~GyroAttr();
int sensor_init();
int start_sensor_update(int _interval_ms);
cJSON* get_gyro();
private:
void init_attr();
void sensor_update_main_loop();
private:
BMI160* imu;
BMI160::AccConfig acc_config;
BMI160::GyroConfig gyro_config;
cJSON *gyro;
cJSON *x, *y, *z;
int interval_ms;
Thread thread;
};
#endif
\ No newline at end of file
#include "mbed.h"
#include "mbed_trace.h"
#include "GyroAttr.h"
#define TRACE_GROUP "Main"
I2C i2c(I2C_SDA, I2C_SCL);
BMI160_I2C *imu = NULL;
GyroAttr *gyro = NULL;
int main() {
char *js = NULL;
mbed_trace_init();
i2c.frequency(400000);
imu = new BMI160_I2C(i2c, BMI160_I2C::I2C_ADRS_SDO_LO);
gyro = new GyroAttr(imu);
gyro->sensor_init();
gyro->start_sensor_update(500);
while(true) {
js = cJSON_PrintUnformatted(gyro->get_gyro());
if (js) {
printf("%s\n", js);
free(js);
}
ThisThread::sleep_for(2000);
}
return 0;
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment