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

ColorAttr: Add led color attr

parent d49d7227
#include "ColorAttr.h"
const ColorAttr::LedColor ColorAttr::colors[COLOR_ATTR_PRE_DEFINED_COLOR_MAX] = {
{ "black", RGBLed::BLACK },
{ "white", RGBLed::WHITE },
{ "red", RGBLed::RED },
{ "green", RGBLed::GREEN },
{ "blue", RGBLed::BLUE },
{ "magenta", RGBLed::MAGENTA },
{ "cyan", RGBLed::CYAN },
{ "yellow", RGBLed::YELLOW },
};
ColorAttr::ColorAttr(RGBLed* _led) :
led(_led),
json(NULL)
{
init_attr();
set_color("black");
}
ColorAttr::~ColorAttr()
{
if (json) {
cJSON_Delete(json);
}
}
void ColorAttr::set_color(const char* _color_name)
{
int i;
bool found = false;
if (_color_name == NULL) {
return;
}
for (i = 0; i < COLOR_ATTR_PRE_DEFINED_COLOR_MAX; i++) {
if (strcmp(_color_name, ColorAttr::colors[i].id) == 0) {
found = true;
led->setColor(ColorAttr::colors[i].color);
break;
}
}
if(found) {
update_attr(_color_name);
} else {
led->setColor(ColorAttr::colors[0].color);
update_attr(ColorAttr::colors[0].id);
}
}
void ColorAttr::init_attr()
{
json = cJSON_CreateString("this is a color placeholder");
}
void ColorAttr::update_attr(const char* _color_name)
{
char* buf;
size_t len;
if (!_color_name) {
return;
}
buf = json->valuestring;
len = strlen(_color_name) + 1;
memset(buf, 0, len);
strcpy(buf, _color_name);
}
cJSON* ColorAttr::get_color()
{
return json;
}
\ No newline at end of file
#ifndef __COLOR_ATTR_H__
#define __COLOR_ATTR_H__
#include "RGBLed.h"
#include "cJSON.h"
#define COLOR_ATTR_PRE_DEFINED_COLOR_MAX 8
class ColorAttr
{
public:
ColorAttr(RGBLed* _led);
~ColorAttr();
void set_color(const char* _color_name);
cJSON* get_color();
private:
void init_attr();
void update_attr(const char* _color_name);
private:
typedef struct {
const char* id;
RGBLed::Color& color;
}LedColor;
static const LedColor colors[COLOR_ATTR_PRE_DEFINED_COLOR_MAX];
RGBLed* led;
cJSON *json;
};
#endif
\ No newline at end of file
......@@ -2,34 +2,55 @@
#include "mbed.h"
#include "mbed_trace.h"
#include "GyroAttr.h"
#include "ColorAttr.h"
#define TRACE_GROUP "Main"
I2C i2c(I2C_SDA, I2C_SCL);
RGBLed led_rgb(LED_RED, LED_GREEN, LED_BLUE);
BMI160_I2C *imu = NULL;
GyroAttr *gyro = NULL;
ColorAttr *color = NULL;
static const char* colors[8] = {
"red", "green", "blue","magenta",
"cyan", "yellow", "white", "black"
};
int main() {
cJSON *attr = NULL;
char *js = NULL;
int i = 0;
mbed_trace_init();
i2c.frequency(400000);
imu = new BMI160_I2C(i2c, BMI160_I2C::I2C_ADRS_SDO_LO);
gyro = new GyroAttr(imu);
color = new ColorAttr(&led_rgb);
attr = cJSON_CreateObject();
cJSON_AddItemReferenceToObject(attr, "gyro", gyro->get_gyro());
cJSON_AddItemReferenceToObject(attr, "color", color->get_color());
gyro->sensor_init();
gyro->start_sensor_update(500);
while(true) {
js = cJSON_PrintUnformatted(gyro->get_gyro());
js = cJSON_PrintUnformatted(attr);
if (js) {
printf("%s\n", js);
free(js);
}
color->set_color(colors[i++]);
if (i >= 8) {
i = 0;
}
ThisThread::sleep_for(2000);
}
......
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