1 /* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15
16 #include "tensorflow/lite/micro/examples/magic_wand/accelerometer_handler.h"
17
18 #include <device.h>
19 #include <drivers/sensor.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <zephyr.h>
23
24 #define BUFLEN 300
25 int begin_index = 0;
26 struct device* sensor = NULL;
27 int current_index = 0;
28
29 float bufx[BUFLEN] = {0.0f};
30 float bufy[BUFLEN] = {0.0f};
31 float bufz[BUFLEN] = {0.0f};
32
33 bool initial = true;
34
SetupAccelerometer(tflite::ErrorReporter * error_reporter)35 TfLiteStatus SetupAccelerometer(tflite::ErrorReporter* error_reporter) {
36 sensor = device_get_binding(DT_INST_0_ADI_ADXL345_LABEL);
37 if (sensor == NULL) {
38 TF_LITE_REPORT_ERROR(error_reporter,
39 "Failed to get accelerometer, label: %s\n",
40 DT_INST_0_ADI_ADXL345_LABEL);
41 } else {
42 TF_LITE_REPORT_ERROR(error_reporter, "Got accelerometer, label: %s\n",
43 DT_INST_0_ADI_ADXL345_LABEL);
44 }
45 return kTfLiteOk;
46 }
47
ReadAccelerometer(tflite::ErrorReporter * error_reporter,float * input,int length)48 bool ReadAccelerometer(tflite::ErrorReporter* error_reporter, float* input,
49 int length) {
50 int rc;
51 struct sensor_value accel[3];
52 int samples_count;
53
54 rc = sensor_sample_fetch(sensor);
55 if (rc < 0) {
56 TF_LITE_REPORT_ERROR(error_reporter, "Fetch failed\n");
57 return false;
58 }
59 // skip if there is no data
60 if (!rc) {
61 return false;
62 }
63
64 samples_count = rc;
65 for (int i = 0; i < samples_count; i++) {
66 rc = sensor_channel_get(sensor, SENSOR_CHAN_ACCEL_XYZ, accel);
67 if (rc < 0) {
68 TF_LITE_REPORT_ERROR(error_reporter, "ERROR: Update failed: %d\n", rc);
69 return false;
70 }
71 bufx[begin_index] = (float)sensor_value_to_double(&accel[0]);
72 bufy[begin_index] = (float)sensor_value_to_double(&accel[1]);
73 bufz[begin_index] = (float)sensor_value_to_double(&accel[2]);
74 begin_index++;
75 if (begin_index >= BUFLEN) begin_index = 0;
76 }
77
78 if (initial && begin_index >= 100) {
79 initial = false;
80 }
81
82 if (initial) {
83 return false;
84 }
85
86 int sample = 0;
87 for (int i = 0; i < (length - 3); i += 3) {
88 int ring_index = begin_index + sample - length / 3;
89 if (ring_index < 0) {
90 ring_index += BUFLEN;
91 }
92 input[i] = bufx[ring_index];
93 input[i + 1] = bufy[ring_index];
94 input[i + 2] = bufz[ring_index];
95 sample++;
96 }
97 return true;
98 }
99