Skip to content

Commit 347dd58

Browse files
committed
TS COM - add simple communication sample
Signed-off-by: Bobby Noelte <[email protected]>
1 parent e54870e commit 347dd58

File tree

7 files changed

+166
-0
lines changed

7 files changed

+166
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Copyright (c) 2021 Bobby Noelte.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
cmake_minimum_required(VERSION 3.13.1)
5+
6+
# Get base directory.
7+
# Assumption: this file is within the zephyr/samples/basic subdir of the thingset library.
8+
get_filename_component(THINGSET_BASE "${CMAKE_CURRENT_SOURCE_DIR}/../../.." ABSOLUTE)
9+
message(STATUS "THINGSET_BASE: ${THINGSET_BASE}")
10+
11+
# Search for Zepyhr installed side by side to thingset device library.
12+
# This is a fallback in case ZEPHYR_BASE is not given.
13+
set(zephyr_base $ENV{ZEPHYR_BASE})
14+
get_filename_component(MODULES_BASE "${THINGSET_BASE}/.." ABSOLUTE)
15+
FILE(GLOB modules LIST_DIRECTORIES true "${MODULES_BASE}/*")
16+
foreach(module ${modules})
17+
set(full_path ${module}/Kconfig.zephyr)
18+
if(EXISTS ${full_path})
19+
set(zephyr_base "${module}")
20+
break()
21+
endif()
22+
endforeach()
23+
message(STATUS "ZEPHYR_BASE: ${zephyr_base}")
24+
25+
# Prepare native posix testing on host
26+
set(BOARD "native_posix")
27+
set(ZEPHYR_TOOLCHAIN_VARIANT "host")
28+
29+
set(ZEPHYR_MODULES ${THINGSET_BASE})
30+
31+
find_package(Zephyr REQUIRED HINTS ${zephyr_base})
32+
project(thingset_zephyr_simplecom)
33+
34+
target_sources(app PRIVATE src/objdb.c src/porttab.c src/main.c)

zephyr/samples/simplecom/Kconfig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
mainmenu "ThingSet for Zepyhr - samples/simplecom configuration"
4+
5+
source "Kconfig.zephyr"

zephyr/samples/simplecom/prj.conf

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
CONFIG_THINGSET=y
4+
5+
# enable all optional features to increase test coverage
6+
CONFIG_THINGSET_64BIT_TYPES_SUPPORT=y
7+
CONFIG_THINGSET_DECFRAC_TYPE_SUPPORT=y
8+
CONFIG_THINGSET_BYTE_STRING_TYPE_SUPPORT=y
9+
10+
CONFIG_LOG=n
11+
CONFIG_THINGSET_LOG_LEVEL_DBG=y
12+
13+
# enable communication support
14+
CONFIG_THINGSET_COM=y
15+
16+
# enable loopback port for testing
17+
CONFIG_THINGSET_PORT_LOOPBACK_SIMPLE=y
18+
19+
CONFIG_STDOUT_CONSOLE=y

zephyr/samples/simplecom/sample.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
sample:
2+
name: Blinky Sample
3+
tests:
4+
sample.basic.blinky:
5+
tags: LED gpio
6+
filter: dt_enabled_alias_with_parent_compat("led0", "gpio-leds")
7+
depends_on: gpio
8+
harness: led
9+
integration_platforms:
10+
- frdm_k64f

zephyr/samples/simplecom/src/main.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (c) 2021 Martin Jäger / Libre Solar
3+
* Copyright (c) 2021 Bobby Noelte
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <stdio.h>
8+
9+
#include <thingset.h>
10+
11+
/* variables to be exposed via ThingSet */
12+
extern char device_id[];
13+
extern bool enable_switch;
14+
extern float ambient_temp;
15+
16+
/* the ThingSet data objects database */
17+
TS_OBJ_DATABASE_DECLARE(data_objects_db);
18+
19+
/* The ports table */
20+
TS_PORT_TABLE_DECLARE(ports_table);
21+
22+
/* Create context and assign data objects and ports */
23+
TS_CONTEXT_DEFINE(tsc, data_objects_db, ports_table);
24+
25+
void main(void)
26+
{
27+
uint8_t response[100];
28+
29+
/* initialize ThingSet context and assign data objects */
30+
tsc_init(&tsc);
31+
32+
/*
33+
* Below requests are for demonstration of the ThingSet process function only. They would
34+
* usually be received from a connected serial interface or other communication channels.
35+
*/
36+
37+
/* TBD --------------
38+
const char request1[] = "= {\"HeaterEnable\":true}";
39+
printf("Request: %s\n", request1);
40+
ts_process(&ts, (uint8_t *)request1, strlen(request1), response, sizeof(response));
41+
printf("Response: %s\n\n", (char *)response);
42+
43+
const char request2[] = "!x-reset";
44+
printf("Request: %s\n", request2);
45+
ts_process(&ts, (uint8_t *)request2, strlen(request2), response, sizeof(response));
46+
printf("Response: %s\n\n", (char *)response);
47+
48+
const char request3[] = "?";
49+
printf("Request: %s\n", request3);
50+
ts_process(&ts, (uint8_t *)request3, strlen(request3), response, sizeof(response));
51+
printf("Response: %s\n\n", (char *)response);
52+
*/
53+
54+
}

zephyr/samples/simplecom/src/objdb.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (c) 2021 Martin Jäger / Libre Solar
3+
* Copyright (c) 2021 Bobby Noelte.
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <thingset.h>
8+
9+
/* variables to be exposed via ThingSet */
10+
char device_id[] = "ABCD1234";
11+
bool enable_switch = false;
12+
float ambient_temp = 22.3;
13+
14+
/* function that can be called via ThingSet */
15+
void reset(void)
16+
{
17+
LOG_DBG("Reset function called!\n");
18+
}
19+
20+
/* the ThingSet data objects database */
21+
TS_OBJ_DATABASE_DEFINE(data_objects_db,
22+
23+
TS_ITEM_STRING(0x1D, "DeviceID", device_id, sizeof(device_id),
24+
TS_ID_ROOT, TS_ANY_R | TS_MKR_W, 0),
25+
26+
TS_ITEM_FLOAT(0x71, "Ambient_degC", &ambient_temp, 1, TS_ID_ROOT, TS_ANY_R, 0),
27+
28+
TS_ITEM_BOOL(0x61, "HeaterEnable", &enable_switch, TS_ID_ROOT, TS_ANY_RW, 0),
29+
30+
TS_FUNCTION(0xE1, "x-reset", &reset, TS_ID_ROOT, TS_ANY_RW),
31+
32+
);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* Copyright (c) 2021 Bobby Noelte.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include <thingset.h>
7+
#include <ports/loopback_simple/loopback_simple.h>
8+
9+
TS_PORT_TABLE_DEFINE(ports_table,
10+
TS_PORT("loopa", &loopback_simple_api, &loopback_simple_a_config, &loopback_simple_a_data),
11+
TS_PORT("loopb", &loopback_simple_api, &loopback_simple_b_config, &loopback_simple_b_data),
12+
);

0 commit comments

Comments
 (0)