Skip to content

Commit a09a0a1

Browse files
committed
samples: hello_ei: Use EI C linkage
- Edge Impulse C linkage used intead of C++ wrapper. Jira: NCSDK-36946 Signed-off-by: Bartosz Meus <bartosz.meus@nordicsemi.no>
1 parent c09aad0 commit a09a0a1

6 files changed

Lines changed: 36 additions & 105 deletions

File tree

samples/edge_impulse/hello_ei/CMakeLists.txt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,18 @@ cmake_minimum_required(VERSION 3.20.0)
99
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
1010
project(hello_ei)
1111

12-
# Add Edge Impulse library as a separate module
13-
add_subdirectory(edge_impulse)
14-
1512
# Add application source files
1613
target_sources(app PRIVATE
1714
src/main.c
18-
src/ei_wrapper.cpp
1915
)
2016

2117
# Add application include directories
2218
target_include_directories(app PRIVATE
2319
src/include
2420
)
2521

22+
# Add Edge Impulse library as a separate module
23+
add_subdirectory(edge_impulse)
24+
2625
# Link Edge Impulse library to application
2726
target_link_libraries(app PRIVATE edge_impulse)

samples/edge_impulse/hello_ei/edge_impulse/CMakeLists.ei.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# Copyright (c) 2020 Nordic Semiconductor ASA
2+
# Copyright (c) 2026 Nordic Semiconductor ASA
33
#
44
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
55
#

samples/edge_impulse/hello_ei/edge_impulse/CMakeLists.txt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# Copyright (c) 2020 Nordic Semiconductor ASA
2+
# Copyright (c) 2026 Nordic Semiconductor ASA
33
#
44
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
55
#
@@ -31,7 +31,9 @@ foreach(EI_URI IN LISTS EI_URI_PREPARE_LIST)
3131
# Remove trailing spaces
3232
string(STRIP ${EI_URI} EI_URI_STRING)
3333

34+
# # If URI is NOT a URL (http://, https://, file://), treat it as a local path
3435
if(NOT ${EI_URI_STRING} MATCHES "^[a-z]+://")
36+
# Expand any ${VARIABLES} in the path
3537
string(CONFIGURE ${EI_URI_STRING} EI_URI_STRING)
3638
if(NOT IS_ABSOLUTE ${EI_URI_STRING})
3739
# Using application source directory as base directory for relative path.
@@ -52,6 +54,12 @@ else()
5254
set(EI_API_KEY_HEADER "")
5355
endif()
5456

57+
# Enable linkage of Edge Impulse library from C code
58+
target_compile_definitions(zephyr_interface INTERFACE
59+
EI_C_LINKAGE=1
60+
EIDSP_SIGNAL_C_FN_POINTER=1
61+
)
62+
5563
include(ExternalProject)
5664
ExternalProject_Add(edge_impulse_project
5765
URL ${EI_URI_LIST}
@@ -136,9 +144,3 @@ target_compile_options(edge_impulse
136144
INTERFACE -Wno-double-promotion
137145
INTERFACE -Wno-unused
138146
)
139-
140-
if(CONFIG_EI_WRAPPER)
141-
zephyr_library_named(ei_wrapper)
142-
zephyr_library_sources(ei_wrapper.cpp)
143-
zephyr_library_link_libraries(edge_impulse)
144-
endif()

samples/edge_impulse/hello_ei/src/ei_wrapper.cpp

Lines changed: 0 additions & 40 deletions
This file was deleted.

samples/edge_impulse/hello_ei/src/include/ei_wrapper.h

Lines changed: 0 additions & 44 deletions
This file was deleted.

samples/edge_impulse/hello_ei/src/main.c

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77
#include <zephyr/kernel.h>
88
#include <zephyr/logging/log.h>
99

10-
#include "ei_classifier_types.h"
11-
#include "model-parameters/model_metadata.h"
10+
#include "edge-impulse-sdk/dsp/numpy_types.h"
11+
#include "edge-impulse-sdk/classifier/ei_classifier_types.h"
1212

13-
#include "ei_wrapper.h"
1413
#include "input_data.h"
1514

1615
#define LOG_MODULE_NAME hello_ei
@@ -28,6 +27,12 @@ static struct {
2827

2928
static size_t inference_cnt;
3029

30+
/* External function declaration needed because the function is declared in
31+
* edge-impulse-sdk/classifier/ei_run_classifier.h which includes some
32+
* C++ source code files thus cannot be included in this C file.
33+
*/
34+
extern EI_IMPULSE_ERROR run_classifier(signal_t *signal, ei_impulse_result_t *result, bool debug);
35+
3136
static void print_inference_result(const ei_impulse_result_t *result, int64_t duration)
3237
{
3338
LOG_INF("=== Inference result ===");
@@ -110,6 +115,17 @@ static int get_samples_from_buffer(size_t offset, size_t length, float *out_ptr)
110115
return 0;
111116
}
112117

118+
static EI_IMPULSE_ERROR run_ei_classification(ei_impulse_result_t *ei_result, size_t window_size)
119+
{
120+
signal_t features_signal = {
121+
.get_data = get_samples_from_buffer,
122+
.total_length = window_size
123+
};
124+
125+
return run_classifier(&features_signal, ei_result,
126+
IS_ENABLED(CONFIG_EI_WRAPPER_DEBUG_MODE));
127+
}
128+
113129
static int run_model(const float *input_data, size_t input_data_size)
114130
{
115131
__ASSERT_NO_MSG(input_data != NULL);
@@ -118,7 +134,7 @@ static int run_model(const float *input_data, size_t input_data_size)
118134
size_t sample_cnt = 0;
119135
int64_t start_time, delta;
120136
ei_impulse_result_t inference_result;
121-
int err;
137+
EI_IMPULSE_ERROR err;
122138

123139
clear_sample_buffer();
124140
inference_cnt = 0;
@@ -133,11 +149,11 @@ static int run_model(const float *input_data, size_t input_data_size)
133149
*/
134150
if (sample_cnt >= EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE) {
135151
start_time = k_uptime_get();
136-
err = ei_wrapper_run_inference(&inference_result, EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE);
152+
err = run_ei_classification(&inference_result, EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE);
137153
delta = k_uptime_delta(&start_time);
138154

139-
if (err != 0) {
140-
LOG_ERR("Inference failed with error code: %d", err);
155+
if (err != EI_IMPULSE_OK) {
156+
LOG_ERR("Classification failed with error code: %d", err);
141157
return -1;
142158
}
143159

@@ -158,8 +174,6 @@ int main(void)
158174
{
159175
print_model_info();
160176

161-
ei_wrapper_init(&get_samples_from_buffer);
162-
163177
LOG_INF("Running inference on sine wave input data");
164178
LOG_SEPARATOR();
165179

0 commit comments

Comments
 (0)