Skip to content

Commit 50100f8

Browse files
hongye-samsungKwang-Hui
authored andcommitted
Add light_example for rtl8720c and update README.md
(cherry-picked from commit 8097868) Signed-off-by: hong.ye <52810083+hongye-samsung@users.noreply.github.com>
1 parent de71b93 commit 50100f8

20 files changed

+1810
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# SmartThings SDK for Direct Connected Devices for C - Light Example
2+
3+
## Introduction
4+
5+
SmartThings direct-connected device is Wi-Fi enabled device that uses the SmartThings cloud as its primary cloud infrastructure. And this device will use the MQTT protocol for communication.
6+
7+
## Getting started
8+
9+
For information on detailed workflow, please refer to the [Getting Started](../../../doc/getting_started.md)
10+
11+
## Components and Capabilities
12+
13+
SmartThings Device is defined using components and capabilities. Capabilities define the features of the device, and capabilities are grouped into components.
14+
Components and Capabilities are contained in device profile. You can create a device profile in Developer Workspace and associate it with an integration.
15+
16+
This example assumes the following components and capabilities are used. :
17+
18+
`main` component
19+
- `healthCheck` capability
20+
- `switch` capability
21+
- `switchLevel` capability
22+
- `colorTemperature` capability
23+
- `activityLightingMode` capability
24+
25+
`monitor` component
26+
- `dustSensor` capability
27+
28+
(`healthCheck` capability is automatically added by Developer Workspace. It doesn't need handler at device side)
29+
30+
## SmartThings SDK for Direct Connected Devices - Config
31+
If you want to use specific SmartThings Device SDK build options, you can directly modify the build configuration file. For this example, SmartThings Device SDK config is saved in 'sdkconfig.h' file. If you want to change this, please execute the following :
32+
```sh
33+
$ cd ~/st-device-sdk-c-ref
34+
$ vim apps/rtl8720c/light_example/sdkconfig.h
35+
```
36+
37+
## Test device schematics
38+
This example uses RTL8720c GPIO like below.
39+
Please refer below picture for __Ameba RTL8720C__.
40+
> Note: If your device's schematics doesn't match with belows.
41+
> Please modify GPIO defines for your device at [device_control.h](main/device_control.h)
42+
> ```c
43+
> #define GPIO_INPUT_BUTTON PA_19
44+
> #define GPIO_OUTPUT_COLORLED_R PA_2
45+
> #define GPIO_OUTPUT_COLORLED_G PA_3
46+
> #define GPIO_OUTPUT_COLORLED_B PA_4
47+
> #define GPIO_OUTPUT_COLORLED_0 PA_0
48+
> ```
49+
50+
### Ameba RTL8720C
51+
| Ameba RTL8720C |
52+
|---------------------------------------------------------------------|
53+
|![Ameba RTL8720C](../../../doc/res/Light_Example_AMEBA_RTL8720C.png) |
54+
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/* ***************************************************************************
2+
*
3+
* Copyright 2019-2020 Samsung Electronics All Rights Reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing,
12+
* software distributed under the License is distributed on an
13+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
14+
* either express or implied. See the License for the specific
15+
* language governing permissions and limitations under the License.
16+
*
17+
****************************************************************************/
18+
19+
#include <string.h>
20+
#include <stdio.h>
21+
#include <stdlib.h>
22+
23+
#include "st_dev.h"
24+
#include "caps_activityLightingMode.h"
25+
26+
static int caps_activityLightingMode_attr_lightingMode_str2idx(const char *value)
27+
{
28+
int index;
29+
30+
for (index = 0; index < CAP_ENUM_ACTIVITYLIGHTINGMODE_LIGHTINGMODE_VALUE_MAX; index++) {
31+
if (!strcmp(value, caps_helper_activityLightingMode.attr_lightingMode.values[index])) {
32+
return index;
33+
}
34+
}
35+
return -1;
36+
}
37+
38+
static const char *caps_activityLightingMode_get_lightingMode_value(caps_activityLightingMode_data_t *caps_data)
39+
{
40+
if (!caps_data) {
41+
printf("caps_data is NULL\n");
42+
return NULL;
43+
}
44+
return caps_data->lightingMode_value;
45+
}
46+
47+
static void caps_activityLightingMode_set_lightingMode_value(caps_activityLightingMode_data_t *caps_data, const char *value)
48+
{
49+
if (!caps_data) {
50+
printf("caps_data is NULL\n");
51+
return;
52+
}
53+
if (caps_data->lightingMode_value) {
54+
free(caps_data->lightingMode_value);
55+
}
56+
caps_data->lightingMode_value = strdup(value);
57+
}
58+
59+
static void caps_activityLightingMode_attr_lightingMode_send(caps_activityLightingMode_data_t *caps_data)
60+
{
61+
int sequence_no = -1;
62+
63+
if (!caps_data || !caps_data->handle) {
64+
printf("fail to get handle\n");
65+
return;
66+
}
67+
if (!caps_data->lightingMode_value) {
68+
printf("value is NULL\n");
69+
return;
70+
}
71+
72+
ST_CAP_SEND_ATTR_STRING(caps_data->handle,
73+
(char *)caps_helper_activityLightingMode.attr_lightingMode.name,
74+
caps_data->lightingMode_value,
75+
NULL,
76+
NULL,
77+
sequence_no);
78+
79+
if (sequence_no < 0)
80+
printf("fail to send lightingMode value\n");
81+
else
82+
printf("Sequence number return : %d\n", sequence_no);
83+
84+
}
85+
86+
87+
static void caps_activityLightingMode_cmd_setLightingMode_cb(IOT_CAP_HANDLE *handle, iot_cap_cmd_data_t *cmd_data, void *usr_data)
88+
{
89+
caps_activityLightingMode_data_t *caps_data = (caps_activityLightingMode_data_t *)usr_data;
90+
char *value;
91+
int index;
92+
93+
printf("called [%s] func with num_args:%u\n", __func__, cmd_data->num_args);
94+
95+
index = caps_activityLightingMode_attr_lightingMode_str2idx(cmd_data->cmd_data[0].string);
96+
if (index < 0) {
97+
printf("%s is not supported value for setLightingMode\n", cmd_data->cmd_data[0].string);
98+
return;
99+
}
100+
value = (char *)caps_helper_activityLightingMode.attr_lightingMode.values[index];
101+
102+
caps_activityLightingMode_set_lightingMode_value(caps_data, value);
103+
if (caps_data && caps_data->cmd_setLightingMode_usr_cb)
104+
caps_data->cmd_setLightingMode_usr_cb(caps_data);
105+
caps_activityLightingMode_attr_lightingMode_send(caps_data);
106+
}
107+
108+
static void caps_activityLightingMode_init_cb(IOT_CAP_HANDLE *handle, void *usr_data)
109+
{
110+
caps_activityLightingMode_data_t *caps_data = usr_data;
111+
if (caps_data && caps_data->init_usr_cb)
112+
caps_data->init_usr_cb(caps_data);
113+
caps_activityLightingMode_attr_lightingMode_send(caps_data);
114+
}
115+
116+
caps_activityLightingMode_data_t *caps_activityLightingMode_initialize(IOT_CTX *ctx, const char *component, void *init_usr_cb, void *usr_data)
117+
{
118+
caps_activityLightingMode_data_t *caps_data = NULL;
119+
int err;
120+
121+
caps_data = malloc(sizeof(caps_activityLightingMode_data_t));
122+
if (!caps_data) {
123+
printf("fail to malloc for caps_activityLightingMode_data\n");
124+
return NULL;
125+
}
126+
127+
memset(caps_data, 0, sizeof(caps_activityLightingMode_data_t));
128+
129+
caps_data->init_usr_cb = init_usr_cb;
130+
caps_data->usr_data = usr_data;
131+
132+
caps_data->get_lightingMode_value = caps_activityLightingMode_get_lightingMode_value;
133+
caps_data->set_lightingMode_value = caps_activityLightingMode_set_lightingMode_value;
134+
caps_data->attr_lightingMode_str2idx = caps_activityLightingMode_attr_lightingMode_str2idx;
135+
caps_data->attr_lightingMode_send = caps_activityLightingMode_attr_lightingMode_send;
136+
if (ctx) {
137+
caps_data->handle = st_cap_handle_init(ctx, component, caps_helper_activityLightingMode.id, caps_activityLightingMode_init_cb, caps_data);
138+
}
139+
if (caps_data->handle) {
140+
err = st_cap_cmd_set_cb(caps_data->handle, caps_helper_activityLightingMode.cmd_setLightingMode.name, caps_activityLightingMode_cmd_setLightingMode_cb, caps_data);
141+
if (err) {
142+
printf("fail to set cmd_cb for setLightingMode of activityLightingMode\n");
143+
}
144+
} else {
145+
printf("fail to init activityLightingMode handle\n");
146+
}
147+
148+
return caps_data;
149+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* ***************************************************************************
2+
*
3+
* Copyright 2019-2020 Samsung Electronics All Rights Reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing,
12+
* software distributed under the License is distributed on an
13+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
14+
* either express or implied. See the License for the specific
15+
* language governing permissions and limitations under the License.
16+
*
17+
****************************************************************************/
18+
19+
#include "caps/iot_caps_helper_activityLightingMode.h"
20+
21+
#ifdef __cplusplus
22+
extern "C" {
23+
#endif
24+
25+
typedef struct caps_activityLightingMode_data {
26+
IOT_CAP_HANDLE* handle;
27+
void *usr_data;
28+
void *cmd_data;
29+
30+
char *lightingMode_value;
31+
32+
const char *(*get_lightingMode_value)(struct caps_activityLightingMode_data *caps_data);
33+
void (*set_lightingMode_value)(struct caps_activityLightingMode_data *caps_data, const char *value);
34+
int (*attr_lightingMode_str2idx)(const char *value);
35+
void (*attr_lightingMode_send)(struct caps_activityLightingMode_data *caps_data);
36+
37+
void (*init_usr_cb)(struct caps_activityLightingMode_data *caps_data);
38+
39+
void (*cmd_setLightingMode_usr_cb)(struct caps_activityLightingMode_data *caps_data);
40+
} caps_activityLightingMode_data_t;
41+
42+
caps_activityLightingMode_data_t *caps_activityLightingMode_initialize(IOT_CTX *ctx, const char *component, void *init_usr_cb, void *usr_data);
43+
#ifdef __cplusplus
44+
}
45+
#endif
46+
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/* ***************************************************************************
2+
*
3+
* Copyright 2019-2020 Samsung Electronics All Rights Reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing,
12+
* software distributed under the License is distributed on an
13+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
14+
* either express or implied. See the License for the specific
15+
* language governing permissions and limitations under the License.
16+
*
17+
****************************************************************************/
18+
19+
#include <string.h>
20+
#include <stdio.h>
21+
#include <stdlib.h>
22+
23+
#include "st_dev.h"
24+
#include "caps_colorTemperature.h"
25+
26+
static int caps_colorTemperature_get_colorTemperature_value(caps_colorTemperature_data_t *caps_data)
27+
{
28+
if (!caps_data) {
29+
printf("caps_data is NULL\n");
30+
return caps_helper_colorTemperature.attr_colorTemperature.min - 1;
31+
}
32+
return caps_data->colorTemperature_value;
33+
}
34+
35+
static void caps_colorTemperature_set_colorTemperature_value(caps_colorTemperature_data_t *caps_data, int value)
36+
{
37+
if (!caps_data) {
38+
printf("caps_data is NULL\n");
39+
return;
40+
}
41+
caps_data->colorTemperature_value = value;
42+
}
43+
44+
static const char *caps_colorTemperature_get_colorTemperature_unit(caps_colorTemperature_data_t *caps_data)
45+
{
46+
if (!caps_data) {
47+
printf("caps_data is NULL\n");
48+
return NULL;
49+
}
50+
return caps_data->colorTemperature_unit;
51+
}
52+
53+
static void caps_colorTemperature_set_colorTemperature_unit(caps_colorTemperature_data_t *caps_data, const char *unit)
54+
{
55+
if (!caps_data) {
56+
printf("caps_data is NULL\n");
57+
return;
58+
}
59+
caps_data->colorTemperature_unit = (char *)unit;
60+
}
61+
62+
static void caps_colorTemperature_attr_colorTemperature_send(caps_colorTemperature_data_t *caps_data)
63+
{
64+
int sequence_no = -1;
65+
66+
if (!caps_data || !caps_data->handle) {
67+
printf("fail to get handle\n");
68+
return;
69+
}
70+
71+
ST_CAP_SEND_ATTR_NUMBER(caps_data->handle,
72+
(char *)caps_helper_colorTemperature.attr_colorTemperature.name,
73+
caps_data->colorTemperature_value,
74+
caps_data->colorTemperature_unit,
75+
NULL,
76+
sequence_no);
77+
78+
if (sequence_no < 0)
79+
printf("fail to send colorTemperature value\n");
80+
else
81+
printf("Sequence number return : %d\n", sequence_no);
82+
}
83+
84+
85+
static void caps_colorTemperature_cmd_setColorTemperature_cb(IOT_CAP_HANDLE *handle, iot_cap_cmd_data_t *cmd_data, void *usr_data)
86+
{
87+
caps_colorTemperature_data_t *caps_data = (caps_colorTemperature_data_t *)usr_data;
88+
int value;
89+
90+
printf("called [%s] func with num_args:%u\n", __func__, cmd_data->num_args);
91+
92+
value = cmd_data->cmd_data[0].integer;
93+
94+
caps_colorTemperature_set_colorTemperature_value(caps_data, value);
95+
if (caps_data && caps_data->cmd_setColorTemperature_usr_cb)
96+
caps_data->cmd_setColorTemperature_usr_cb(caps_data);
97+
caps_colorTemperature_attr_colorTemperature_send(caps_data);
98+
}
99+
100+
static void caps_colorTemperature_init_cb(IOT_CAP_HANDLE *handle, void *usr_data)
101+
{
102+
caps_colorTemperature_data_t *caps_data = usr_data;
103+
if (caps_data && caps_data->init_usr_cb)
104+
caps_data->init_usr_cb(caps_data);
105+
caps_colorTemperature_attr_colorTemperature_send(caps_data);
106+
}
107+
108+
caps_colorTemperature_data_t *caps_colorTemperature_initialize(IOT_CTX *ctx, const char *component, void *init_usr_cb, void *usr_data)
109+
{
110+
caps_colorTemperature_data_t *caps_data = NULL;
111+
int err;
112+
113+
caps_data = malloc(sizeof(caps_colorTemperature_data_t));
114+
if (!caps_data) {
115+
printf("fail to malloc for caps_colorTemperature_data\n");
116+
return NULL;
117+
}
118+
119+
memset(caps_data, 0, sizeof(caps_colorTemperature_data_t));
120+
121+
caps_data->init_usr_cb = init_usr_cb;
122+
caps_data->usr_data = usr_data;
123+
124+
caps_data->get_colorTemperature_value = caps_colorTemperature_get_colorTemperature_value;
125+
caps_data->set_colorTemperature_value = caps_colorTemperature_set_colorTemperature_value;
126+
caps_data->get_colorTemperature_unit = caps_colorTemperature_get_colorTemperature_unit;
127+
caps_data->set_colorTemperature_unit = caps_colorTemperature_set_colorTemperature_unit;
128+
caps_data->attr_colorTemperature_send = caps_colorTemperature_attr_colorTemperature_send;
129+
caps_data->colorTemperature_value = 1;
130+
if (ctx) {
131+
caps_data->handle = st_cap_handle_init(ctx, component, caps_helper_colorTemperature.id, caps_colorTemperature_init_cb, caps_data);
132+
}
133+
if (caps_data->handle) {
134+
err = st_cap_cmd_set_cb(caps_data->handle, caps_helper_colorTemperature.cmd_setColorTemperature.name, caps_colorTemperature_cmd_setColorTemperature_cb, caps_data);
135+
if (err) {
136+
printf("fail to set cmd_cb for setColorTemperature of colorTemperature\n");
137+
}
138+
} else {
139+
printf("fail to init colorTemperature handle\n");
140+
}
141+
142+
return caps_data;
143+
}

0 commit comments

Comments
 (0)