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 #if defined(ARDUINO) && !defined(ARDUINO_ARDUINO_NANO33BLE)
17 #define ARDUINO_EXCLUDE_CODE
18 #endif // defined(ARDUINO) && !defined(ARDUINO_ARDUINO_NANO33BLE)
19
20 #ifndef ARDUINO_EXCLUDE_CODE
21
22 #include "tensorflow/lite/micro/examples/micro_speech/command_responder.h"
23
24 #include "Arduino.h"
25
26 // Toggles the built-in LED every inference, and lights a colored LED depending
27 // on which word was detected.
RespondToCommand(tflite::ErrorReporter * error_reporter,int32_t current_time,const char * found_command,uint8_t score,bool is_new_command)28 void RespondToCommand(tflite::ErrorReporter* error_reporter,
29 int32_t current_time, const char* found_command,
30 uint8_t score, bool is_new_command) {
31 static bool is_initialized = false;
32 if (!is_initialized) {
33 pinMode(LED_BUILTIN, OUTPUT);
34 // Pins for the built-in RGB LEDs on the Arduino Nano 33 BLE Sense
35 pinMode(LEDR, OUTPUT);
36 pinMode(LEDG, OUTPUT);
37 pinMode(LEDB, OUTPUT);
38 // Ensure the LED is off by default.
39 // Note: The RGB LEDs on the Arduino Nano 33 BLE
40 // Sense are on when the pin is LOW, off when HIGH.
41 digitalWrite(LEDR, HIGH);
42 digitalWrite(LEDG, HIGH);
43 digitalWrite(LEDB, HIGH);
44 is_initialized = true;
45 }
46 static int32_t last_command_time = 0;
47 static int count = 0;
48 static int certainty = 220;
49
50 if (is_new_command) {
51 TF_LITE_REPORT_ERROR(error_reporter, "Heard %s (%d) @%dms", found_command,
52 score, current_time);
53 // If we hear a command, light up the appropriate LED
54 if (found_command[0] == 'y') {
55 last_command_time = current_time;
56 digitalWrite(LEDG, LOW); // Green for yes
57 }
58
59 if (found_command[0] == 'n') {
60 last_command_time = current_time;
61 digitalWrite(LEDR, LOW); // Red for no
62 }
63
64 if (found_command[0] == 'u') {
65 last_command_time = current_time;
66 digitalWrite(LEDB, LOW); // Blue for unknown
67 }
68 }
69
70 // If last_command_time is non-zero but was >3 seconds ago, zero it
71 // and switch off the LED.
72 if (last_command_time != 0) {
73 if (last_command_time < (current_time - 3000)) {
74 last_command_time = 0;
75 digitalWrite(LED_BUILTIN, LOW);
76 digitalWrite(LEDR, HIGH);
77 digitalWrite(LEDG, HIGH);
78 digitalWrite(LEDB, HIGH);
79 }
80 // If it is non-zero but <3 seconds ago, do nothing.
81 return;
82 }
83
84 // Otherwise, toggle the LED every time an inference is performed.
85 ++count;
86 if (count & 1) {
87 digitalWrite(LED_BUILTIN, HIGH);
88 } else {
89 digitalWrite(LED_BUILTIN, LOW);
90 }
91 }
92
93 #endif // ARDUINO_EXCLUDE_CODE
94