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

Add gyro attributes and pub to IoTConnect

parent 82955862
#include "GyroAttr.h"
#include "mbed_trace.h"
#define TRACE_GROUP "Gyro"
GyroAttr::GyroAttr(LSM6DSLSensor* _sensor) :
sensor(_sensor),
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);
}
}
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::sensor_init()
{
uint8_t id;
sensor->init(NULL);
sensor->enable_x();
sensor->read_id(&id);
tr_debug("LSM6DSLSensor init done with id: 0x%02x", id);
return 0;
}
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()
{
int32_t axes[3];
sensor->get_x_axes(axes);
while(1) {
sensor->get_x_axes(axes);
cJSON_SetNumberValue(x, axes[0]);
cJSON_SetNumberValue(y, axes[1]);
cJSON_SetNumberValue(z, axes[2]);
// tr_debug("Read Gyro RAW Date:x=%d, y=%d, z=%d",
// axes[0], axes[1], axes[2]);
ThisThread::sleep_for(interval_ms);
}
}
\ No newline at end of file
#ifndef __GYRO_ATTR_H__
#define __GYRO_ATTR_H__
#include "mbed.h"
#include "cJSON.h"
#include "LSM6DSLSensor.h"
#define GYRO_SENSOR_UPDATE_THREAD_STACK_SIZE 2048
class GyroAttr
{
public:
GyroAttr(LSM6DSLSensor* _sensor);
~GyroAttr();
int sensor_init();
int start_sensor_update(int _interval_ms);
cJSON* get_gyro();
private:
void init_attr();
void sensor_update_main_loop();
private:
LSM6DSLSensor* sensor;
cJSON *gyro;
cJSON *x, *y, *z;
int interval_ms;
Thread thread;
};
#endif
......@@ -6,6 +6,7 @@
#include "IoTConnectHttpClient.h"
#include "devices_settings.h"
#include "HumTempAttr.h"
#include "GyroAttr.h"
#define TRACE_GROUP "Main"
......@@ -31,7 +32,10 @@ DevI2C i2c(PB_11, PB_10);
#endif
HTS221Sensor* hum_temp = NULL;
LSM6DSLSensor* acc_gyro = NULL;
HumTempAttr* hum_temp_attr = NULL;
GyroAttr *gyro_attr = NULL;
NetworkInterface* connect_network() {
NetworkInterface* net = NULL;
......@@ -96,15 +100,22 @@ int main() {
hum_temp_attr = new HumTempAttr(hum_temp);
acc_gyro = new LSM6DSLSensor(&i2c, LSM6DSL_ACC_GYRO_I2C_ADDRESS_LOW, PD_11);
gyro_attr = new GyroAttr(acc_gyro);
stm32_dev = new IoTConnectDevice(EMBEST_STM32L4_KIT_DEBUG_ID, EMBEST_STM32L4_KIT_DEBUG_NAME, &embest);
attr = stm32_dev->get_attr();
cJSON_AddItemToObject(attr, "humidity", hum_temp_attr->get_hum());
cJSON_AddItemToObject(attr, "temperature", hum_temp_attr->get_temp());
cJSON_AddItemReferenceToObject(attr, "gyro", gyro_attr->get_gyro());
hum_temp_attr->sensor_init();
hum_temp_attr->start_sensor_update(SENSOR_UPDATE_RATE_MS);
gyro_attr->sensor_init();
gyro_attr->start_sensor_update(SENSOR_UPDATE_RATE_MS);
// Connect to network
network = connect_network();
if (network == NULL) {
......
https://xterra2.avnet.com/embest/iotconnect/mbed/mbed-lsm6dsl.git/#a7dfdb62ca685857e76c1fae0d014805e51cddd2
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