-
Notifications
You must be signed in to change notification settings - Fork 0
/
arduino_command_responder.cpp
133 lines (112 loc) · 4.83 KB
/
arduino_command_responder.cpp
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
/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#if defined(ARDUINO) && !defined(ARDUINO_ARDUINO_NANO33BLE)
#define ARDUINO_EXCLUDE_CODE
#endif // defined(ARDUINO) && !defined(ARDUINO_ARDUINO_NANO33BLE)
#ifndef ARDUINO_EXCLUDE_CODE
#include "command_responder.h"
#include "Arduino.h"
// definisco i pin che verranno usati per inviare i segnali all'arduino UNO
#define SI_PIN 2
#define NO_PIN 3
#define DANIELE_PIN 4
#define UNKNOW_PIN 5
// Toggles the built-in LED every inference, and lights a colored LED depending
// on which word was detected.
void RespondToCommand(tflite::ErrorReporter* error_reporter,
int32_t current_time, const char* found_command,
uint8_t score, bool is_new_command) {
/*qui ci sono tutte le inizializzazioni, vengono impostati i pin come output e vengono inizializzati con valore BASSO (LOW) */
static bool is_initialized = false;
if (!is_initialized) {
pinMode(LED_BUILTIN, OUTPUT);
// Pins for the built-in RGB LEDs on the Arduino Nano 33 BLE Sense
pinMode(LEDR, OUTPUT);
pinMode(LEDG, OUTPUT);
pinMode(LEDB, OUTPUT);
pinMode(SI_PIN, OUTPUT);
pinMode(NO_PIN, OUTPUT);
pinMode(DANIELE_PIN, OUTPUT);
pinMode(UNKNOW_PIN, OUTPUT);
// Ensure the LED is off by default.
// Note: The RGB LEDs on the Arduino Nano 33 BLE
// Sense are on when the pin is LOW, off when HIGH.
digitalWrite(LEDR, HIGH);
digitalWrite(LEDG, HIGH);
digitalWrite(LEDB, HIGH);
digitalWrite(SI_PIN, LOW);
digitalWrite(NO_PIN, LOW);
digitalWrite(DANIELE_PIN, LOW);
digitalWrite(UNKNOW_PIN, LOW);
is_initialized = true;
}
static int32_t last_command_time = 0;
static int count = 0;
static int certainty = 220;
/* in questa parte di codice ci sono i vari if in funzione dell'avvenuto riconoscimento della parola */
if (is_new_command) {
TF_LITE_REPORT_ERROR(error_reporter, "Riconosciuto: %s (%d) @%dms", found_command,
score, current_time);
// If we hear a command, light up the appropriate LED
if (found_command[0] == 's' && found_command[1] == 'i' && found_command[2] == 'i') {
last_command_time = current_time;
digitalWrite(LEDG, LOW); // Green for "si"
digitalWrite(SI_PIN, HIGH);
last_command_time = current_time;
}
if (found_command[0] == 'n' && found_command[1] == 'o' && found_command[2] == 'o') {
last_command_time = current_time;
digitalWrite(LEDR, LOW); // Red for "no"
digitalWrite(NO_PIN, HIGH);
last_command_time = current_time;
}
if (found_command[0] == 'd') {
last_command_time = current_time;
digitalWrite(LEDB, LOW); // Blu for "daniele"
digitalWrite(DANIELE_PIN, HIGH);
last_command_time = current_time;
}
if (found_command[0] == 'u') {
last_command_time = current_time;
digitalWrite(LEDB, LOW); // Blue + Green for "unknown"
digitalWrite(LEDG, LOW); // Blue + Green for "unknown"
digitalWrite(UNKNOW_PIN, HIGH);
last_command_time = current_time;
}
}
// If last_command_time is non-zero but was >3 seconds ago, zero it
// and switch off the LED.
if (last_command_time != 0) {
if (last_command_time < (current_time - 3000)) {
last_command_time = 0;
digitalWrite(LED_BUILTIN, LOW);
digitalWrite(LEDR, HIGH); // i led integrati nell'arduino NANO sono spenti se viene impostato il valore HIGH
digitalWrite(LEDG, HIGH);
digitalWrite(LEDB, HIGH);
/* se last_command_time è non-zero ma maggiore di 3 secondi, oltre i led integrati vengono riportati a LOW i pin utilizzati per inviare i segnali all'ARDUINO UNO */
digitalWrite(SI_PIN, LOW);
digitalWrite(NO_PIN, LOW);
digitalWrite(DANIELE_PIN, LOW);
digitalWrite(UNKNOW_PIN, LOW);
}
// If it is non-zero but <3 seconds ago, do nothing.
return;
}
// Otherwise, toggle the LED every time an inference is performed.
++count;
if (count & 1) {
digitalWrite(LED_BUILTIN, HIGH);
} else {
digitalWrite(LED_BUILTIN, LOW);
}
}
#endif // ARDUINO_EXCLUDE_CODE