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/person_detection/detection_responder.h"
23 
24 #include "Arduino.h"
25 
26 // Flash the blue LED after each inference
RespondToDetection(tflite::ErrorReporter * error_reporter,int8_t person_score,int8_t no_person_score)27 void RespondToDetection(tflite::ErrorReporter* error_reporter,
28                         int8_t person_score, int8_t no_person_score) {
29   static bool is_initialized = false;
30   if (!is_initialized) {
31     // Pins for the built-in RGB LEDs on the Arduino Nano 33 BLE Sense
32     pinMode(LEDR, OUTPUT);
33     pinMode(LEDG, OUTPUT);
34     pinMode(LEDB, OUTPUT);
35     is_initialized = true;
36   }
37 
38   // Note: The RGB LEDs on the Arduino Nano 33 BLE
39   // Sense are on when the pin is LOW, off when HIGH.
40 
41   // Switch the person/not person LEDs off
42   digitalWrite(LEDG, HIGH);
43   digitalWrite(LEDR, HIGH);
44 
45   // Flash the blue LED after every inference.
46   digitalWrite(LEDB, LOW);
47   delay(100);
48   digitalWrite(LEDB, HIGH);
49 
50   // Switch on the green LED when a person is detected,
51   // the red when no person is detected
52   if (person_score > no_person_score) {
53     digitalWrite(LEDG, LOW);
54     digitalWrite(LEDR, HIGH);
55   } else {
56     digitalWrite(LEDG, HIGH);
57     digitalWrite(LEDR, LOW);
58   }
59 
60   TF_LITE_REPORT_ERROR(error_reporter, "Person score: %d No person score: %d",
61                        person_score, no_person_score);
62 }
63 
64 #endif  // ARDUINO_EXCLUDE_CODE
65