mirror of
https://github.com/pvvx/RTL00MP3.git
synced 2025-07-31 12:41:06 +00:00
first commit
This commit is contained in:
parent
2ee525362e
commit
d108756e9b
792 changed files with 336059 additions and 0 deletions
87
RTL00_SDKV35a/component/common/example/cJSON/cJSON_example.c
Normal file
87
RTL00_SDKV35a/component/common/example/cJSON/cJSON_example.c
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
#include "cmsis_os.h"
|
||||
#include <cJSON.h>
|
||||
|
||||
#undef malloc
|
||||
#define malloc pvPortMalloc
|
||||
|
||||
#undef free
|
||||
#define free vPortFree
|
||||
|
||||
/* The data structure for this example
|
||||
|
||||
{
|
||||
"Motion_Sensor" : "i",
|
||||
"Light" : {
|
||||
"Red" : "0",
|
||||
"Green" : "0",
|
||||
"Blue" : "0",
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
*/
|
||||
static void gen_json_data(int i, int r, int g, int b)
|
||||
{
|
||||
|
||||
|
||||
cJSON_Hooks memoryHook;
|
||||
|
||||
memoryHook.malloc_fn = malloc;
|
||||
memoryHook.free_fn = free;
|
||||
cJSON_InitHooks(&memoryHook);
|
||||
|
||||
|
||||
cJSON *IOTJSObject = NULL, *colorJSObject = NULL;
|
||||
char *iot_json = NULL;
|
||||
|
||||
if((IOTJSObject = cJSON_CreateObject()) != NULL) {
|
||||
|
||||
cJSON_AddItemToObject(IOTJSObject, "Motion_Sensor", cJSON_CreateNumber(i));
|
||||
cJSON_AddItemToObject(IOTJSObject, "Light", colorJSObject = cJSON_CreateObject());
|
||||
|
||||
cJSON_AddItemToObject(colorJSObject, "Red", cJSON_CreateNumber(r));
|
||||
cJSON_AddItemToObject(colorJSObject, "Green", cJSON_CreateNumber(g));
|
||||
cJSON_AddItemToObject(colorJSObject, "Blue", cJSON_CreateNumber(b));
|
||||
|
||||
iot_json = cJSON_Print(IOTJSObject);
|
||||
cJSON_Delete(IOTJSObject);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static void handle_json_data(char *iot_json)
|
||||
{
|
||||
cJSON_Hooks memoryHook;
|
||||
|
||||
memoryHook.malloc_fn = malloc;
|
||||
memoryHook.free_fn = free;
|
||||
cJSON_InitHooks(&memoryHook);
|
||||
|
||||
|
||||
cJSON *IOTJSObject, *sensorJSObject, *lightJSObject, *redJSObject, *greenJSObject, *blueJSObject;
|
||||
int sensor_data, red, green, blue;
|
||||
|
||||
if((IOTJSObject = cJSON_Parse(iot_json)) != NULL) {
|
||||
sensorJSObject = cJSON_GetObjectItem(IOTJSObject, "Motion_Sensor");
|
||||
if(sensorJSObject)
|
||||
sensor_data = sensorJSObject->valueint;
|
||||
|
||||
lightJSObject = cJSON_GetObjectItem(IOTJSObject, "Light");
|
||||
|
||||
if(lightJSObject){
|
||||
redJSObject = cJSON_GetObjectItem(lightJSObject, "Red");
|
||||
greenJSObject = cJSON_GetObjectItem(lightJSObject, "Green");
|
||||
blueJSObject = cJSON_GetObjectItem(lightJSObject, "Blue");
|
||||
|
||||
if(redJSObject)
|
||||
red = redJSObject->valueint;
|
||||
if(greenJSObject)
|
||||
green = greenJSObject->valueint;
|
||||
if(blueJSObject)
|
||||
blue = blueJSObject->valueint;
|
||||
}
|
||||
|
||||
cJSON_Delete(IOTJSObject);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue