forked from nrfconnect/sdk-nrf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ei_wrapper.h
210 lines (173 loc) · 6.19 KB
/
ei_wrapper.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
* Copyright (c) 2020 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/
/** @file
* @brief Edge Impulse wrapper header.
*/
#ifndef _EI_WRAPPER_H_
#define _EI_WRAPPER_H_
/**
* @defgroup ei_wrapper Edge Impulse wrapper
* @brief Wrapper that uses Edge Impulse lib to run machine learning on device.
*
* @{
*/
#include <zephyr/kernel.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @typedef ei_wrapper_result_ready_cb
* @brief Callback executed by the wrapper when the result is ready.
*
* @param[in] err Zero (if operation was successful) or negative error code.
*/
typedef void (*ei_wrapper_result_ready_cb)(int err);
/** Check if classifier calculates anomaly value.
*
* @retval true If the classifier calculates the anomaly value.
* Otherwise, false is returned.
*/
bool ei_wrapper_classifier_has_anomaly(void);
/** Get the size of the input frame.
*
* @return Size of the input frame, expressed as a number of floating-point
* values.
*/
size_t ei_wrapper_get_frame_size(void);
/** Get the size of the input window.
*
* @return Size of the input window, expressed as a number of floating-point
* values.
*/
size_t ei_wrapper_get_window_size(void);
/** Get input data sampling frequency of the classifier.
*
* @return The sampling frequency in Hz.
*/
size_t ei_wrapper_get_classifier_frequency(void);
/** Get number of labels used by the classifier.
*
* @return Number of labels.
*/
size_t ei_wrapper_get_classifier_label_count(void);
/** Get classifier label with given index.
*
* Index can be number from 0 to number of labels used by classifier minus one.
*
* @param[in] idx Index of the selected classification label.
*
* @return Classifier label or NULL if the index is out of range.
*/
const char *ei_wrapper_get_classifier_label(size_t idx);
/** Add input data for the library.
*
* Size of the added data must be divisible by input frame size.
*
* @param[in] data Pointer to the buffer with input data.
* @param[in] data_size Size of the data (number of floating-point values).
*
* @retval 0 If the operation was successful.
* Otherwise, a (negative) error code is returned.
*/
int ei_wrapper_add_data(const float *data, size_t data_size);
/** Clear all buffered data.
*
* The buffer cannot be cleared if the prediction was already started and the
* wrapper is not waiting for data. In that case, user must wait until the
* prediction is finished.
*
* If the wrapper is waiting for data, the prediction is cancelled.
*
* @param[out] cancelled Pointer to the variable that is used to store information
* if prediction was cancelled.
*
* @retval 0 If the operation was successful.
* Otherwise, a (negative) error code is returned.
*/
int ei_wrapper_clear_data(bool *cancelled);
/** Start a prediction using the Edge Impulse library.
*
* If there is not enough data in the input buffer, the prediction start is
* delayed until the missing data is added.
*
* @param[in] window_shift Number of windows the input window is shifted before
* prediction.
* @param[in] frame_shift Number of frames the input window is shifted before
* prediction.
*
* @retval 0 If the operation was successful.
* Otherwise, a (negative) error code is returned.
*/
int ei_wrapper_start_prediction(size_t window_shift, size_t frame_shift);
/** Get next classification result.
*
* Results are ordered based on descending classification value. If there are more results with the
* given value, they are ordered based on ascending index.
*
* This function can be executed only from the wrapper's callback context.
* Otherwise it returns a (negative) error code.
*
* @param[out] label Pointer to the variable that is used to store the pointer
* to the classification label.
* @param[out] value Pointer to the variable that is used to store the classification value.
* @param[out] idx Pointer to the variable that is used to store the index of the classification
* label.
*
* @retval 0 On success.
* @retval -EACCES If function is executed from other context that the wrapper's callback.
* @retval -ENOENT If no more results are available.
*/
int ei_wrapper_get_next_classification_result(const char **label, float *value, size_t *idx);
/** Get anomaly value.
*
* This function can be executed only from the wrapper's callback context.
* Otherwise it returns a (negative) error code.
*
* @param[out] anomaly Pointer to the variable that is used to store the anomaly.
*
* @retval 0 On success.
* @retval -EACCES If function is executed from other context that the wrapper's callback.
* @retval -ENOTSUP If calculating anomaly value is not supported.
*/
int ei_wrapper_get_anomaly(float *anomaly);
/** Get execution times for operations performed by the library.
*
* This function can be executed only from the wrapper's callback context.
* Otherwise, it returns a (negative) error code.
*
* The library uses Zephyr's uptime for calculations. Because of that execution
* times can be affected by other operations performed by the CPU.
*
* If calculating the anomaly value is not supported, anomaly_time is set to
* the value of -1.
*
* @param[out] dsp_time Pointer to the variable that is used to store
* the dsp time.
* @param[out] classification_time Pointer to the variable that is used to store
* the classification time.
* @param[out] anomaly_time Pointer to the variable that is used to store
* the anomaly time.
*
* @retval 0 If the operation was successful.
* Otherwise, a (negative) error code is returned.
*/
int ei_wrapper_get_timing(int *dsp_time, int *classification_time,
int *anomaly_time);
/** Initialize the Edge Impulse wrapper.
*
* @param[in] cb Callback used to receive results.
*
* @retval 0 If the operation was successful.
* Otherwise, a (negative) error code is returned.
*/
int ei_wrapper_init(ei_wrapper_result_ready_cb cb);
#ifdef __cplusplus
}
#endif
/**
* @}
*/
#endif /* _EI_WRAPPER_H_ */