1- /* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
1+ /* Copyright 2022 The TensorFlow Authors. All Rights Reserved.
22Licensed under the Apache License, Version 2.0 (the "License");
33you may not use this file except in compliance with the License.
44You may obtain a copy of the License at
@@ -18,8 +18,8 @@ limitations under the License.
1818
1919#include " magic_wand_model_data.h"
2020#include " rasterize_stroke.h"
21- #include " tensorflow/lite/micro/micro_error_reporter.h"
2221#include " tensorflow/lite/micro/micro_interpreter.h"
22+ #include " tensorflow/lite/micro/micro_log.h"
2323#include " tensorflow/lite/micro/micro_mutable_op_resolver.h"
2424#include " tensorflow/lite/micro/system_setup.h"
2525#include " tensorflow/lite/schema/schema_generated.h"
9797constexpr int kTensorArenaSize = 30 * 1024 ;
9898uint8_t tensor_arena[kTensorArenaSize ];
9999
100- tflite::ErrorReporter* error_reporter = nullptr ;
101100const tflite::Model* model = nullptr ;
102101tflite::MicroInterpreter* interpreter = nullptr ;
103102
@@ -117,13 +116,12 @@ void SetupIMU() {
117116 float rate_frac;
118117 float rate_int;
119118 rate_frac = modf (acceleration_sample_rate, &rate_int);
120- TF_LITE_REPORT_ERROR (error_reporter, " Acceleration sample rate %d.%d Hz" ,
121- static_cast <int32_t >(rate_int),
122- static_cast <int32_t >(rate_frac * 100 ));
119+ MicroPrintf ( " Acceleration sample rate %d.%d Hz" ,
120+ static_cast <int32_t >(rate_int),
121+ static_cast <int32_t >(rate_frac * 100 ));
123122 rate_frac = modf (gyroscope_sample_rate, &rate_int);
124- TF_LITE_REPORT_ERROR (error_reporter, " Gyroscope sample rate %d.%d Hz" ,
125- static_cast <int32_t >(rate_int),
126- static_cast <int32_t >(rate_frac * 100 ));
123+ MicroPrintf (" Gyroscope sample rate %d.%d Hz" , static_cast <int32_t >(rate_int),
124+ static_cast <int32_t >(rate_frac * 100 ));
127125#endif // MAGIC_WAND_DEBUG
128126}
129127
@@ -140,7 +138,7 @@ void ReadAccelerometerAndGyroscope(int* new_accelerometer_samples,
140138 // Read each sample, removing it from the device's FIFO buffer
141139 if (!IMU.readGyroscope (current_gyroscope_data[0 ], current_gyroscope_data[1 ],
142140 current_gyroscope_data[2 ])) {
143- TF_LITE_REPORT_ERROR (error_reporter, " Failed to read gyroscope data" );
141+ MicroPrintf ( " Failed to read gyroscope data" );
144142 break ;
145143 }
146144 *new_gyroscope_samples += 1 ;
@@ -153,7 +151,7 @@ void ReadAccelerometerAndGyroscope(int* new_accelerometer_samples,
153151 if (!IMU.readAcceleration (current_acceleration_data[0 ],
154152 current_acceleration_data[1 ],
155153 current_acceleration_data[2 ])) {
156- TF_LITE_REPORT_ERROR (error_reporter, " Failed to read acceleration data" );
154+ MicroPrintf ( " Failed to read acceleration data" );
157155 break ;
158156 }
159157 *new_accelerometer_samples += 1 ;
@@ -366,7 +364,7 @@ void UpdateStroke(int new_samples, bool* done_just_triggered) {
366364 stroke_length = 0 ;
367365 *stroke_state = eWaiting;
368366#ifdef MAGIC_WAND_DEBUG
369- TF_LITE_REPORT_ERROR (error_reporter, " stroke length too small" );
367+ MicroPrintf ( " stroke length too small" );
370368#endif // MAGIC_WAND_DEBUG
371369 }
372370 }
@@ -496,7 +494,7 @@ void UpdateStroke(int new_samples, bool* done_just_triggered) {
496494 *stroke_transmit_length = 0 ;
497495 stroke_length = 0 ;
498496#ifdef MAGIC_WAND_DEBUG
499- TF_LITE_REPORT_ERROR (error_reporter, " stroke too small" );
497+ MicroPrintf ( " stroke too small" );
500498#endif // MAGIC_WAND_DEBUG
501499 }
502500 }
@@ -508,15 +506,10 @@ void UpdateStroke(int new_samples, bool* done_just_triggered) {
508506void setup () {
509507 tflite::InitializeTarget (); // setup serial port
510508
511- // Set up logging. Google style is to avoid globals or statics because of
512- // lifetime uncertainty, but since this has a trivial destructor it's okay.
513- static tflite::MicroErrorReporter micro_error_reporter; // NOLINT
514- error_reporter = µ_error_reporter;
515-
516- TF_LITE_REPORT_ERROR (error_reporter, " Started" );
509+ MicroPrintf (" Started" );
517510
518511 if (!IMU.begin ()) {
519- TF_LITE_REPORT_ERROR (error_reporter, " Failed to initialized IMU!" );
512+ MicroPrintf ( " Failed to initialized IMU!" );
520513 while (true ) {
521514 // NORETURN
522515 }
@@ -525,15 +518,15 @@ void setup() {
525518 SetupIMU ();
526519
527520 if (!BLE.begin ()) {
528- TF_LITE_REPORT_ERROR (error_reporter, " Failed to initialized BLE!" );
521+ MicroPrintf ( " Failed to initialized BLE!" );
529522 while (true ) {
530523 // NORETURN
531524 }
532525 }
533526
534527 String address = BLE.address ();
535528
536- TF_LITE_REPORT_ERROR (error_reporter, " address = %s" , address.c_str ());
529+ MicroPrintf ( " address = %s" , address.c_str ());
537530
538531 address.toUpperCase ();
539532
@@ -543,7 +536,7 @@ void setup() {
543536 name += address[address.length () - 2 ];
544537 name += address[address.length () - 1 ];
545538
546- TF_LITE_REPORT_ERROR (error_reporter, " name = %s" , name.c_str ());
539+ MicroPrintf ( " name = %s" , name.c_str ());
547540
548541 BLE.setLocalName (name.c_str ());
549542 BLE.setDeviceName (name.c_str ());
@@ -559,10 +552,10 @@ void setup() {
559552 // copying or parsing, it's a very lightweight operation.
560553 model = tflite::GetModel (g_magic_wand_model_data);
561554 if (model->version () != TFLITE_SCHEMA_VERSION) {
562- TF_LITE_REPORT_ERROR (error_reporter,
563- " Model provided is schema version %d not equal "
564- " to supported version %d." ,
565- model->version (), TFLITE_SCHEMA_VERSION);
555+ MicroPrintf (
556+ " Model provided is schema version %d not equal "
557+ " to supported version %d." ,
558+ model->version (), TFLITE_SCHEMA_VERSION);
566559 return ;
567560 }
568561
@@ -579,7 +572,7 @@ void setup() {
579572
580573 // Build an interpreter to run the model with.
581574 static tflite::MicroInterpreter static_interpreter (
582- model, micro_op_resolver, tensor_arena, kTensorArenaSize , error_reporter );
575+ model, micro_op_resolver, tensor_arena, kTensorArenaSize );
583576 interpreter = &static_interpreter;
584577
585578 // Allocate memory from the tensor_arena for the model's tensors.
@@ -593,17 +586,15 @@ void setup() {
593586 (model_input->type != kTfLiteInt8 ) ||
594587 (model_input->params .zero_point != -128 ) ||
595588 (model_input->params .scale != 1.0 )) {
596- TF_LITE_REPORT_ERROR (error_reporter,
597- " Bad input tensor parameters in model" );
589+ MicroPrintf (" Bad input tensor parameters in model" );
598590 return ;
599591 }
600592
601593 TfLiteTensor* model_output = interpreter->output (0 );
602594 if ((model_output->dims ->size != 2 ) || (model_output->dims ->data [0 ] != 1 ) ||
603595 (model_output->dims ->data [1 ] != label_count) ||
604596 (model_output->type != kTfLiteInt8 )) {
605- TF_LITE_REPORT_ERROR (error_reporter,
606- " Bad output tensor parameters in model" );
597+ MicroPrintf (" Bad output tensor parameters in model" );
607598 return ;
608599 }
609600}
@@ -615,8 +606,7 @@ void loop() {
615606 static bool was_connected_last = false ;
616607 if (central && !was_connected_last) {
617608 // print the central's BT address:
618- TF_LITE_REPORT_ERROR (error_reporter, " Connected to central: %s" ,
619- central.address ().c_str ());
609+ MicroPrintf (" Connected to central: %s" , central.address ().c_str ());
620610 }
621611 was_connected_last = central;
622612
@@ -669,10 +659,10 @@ void loop() {
669659 line[x] = output;
670660 }
671661 line[raster_width] = 0 ;
672- TF_LITE_REPORT_ERROR (error_reporter, line);
662+ MicroPrintf ( line);
673663 }
674664#ifdef MAGIC_WAND_DEBUG
675- TF_LITE_REPORT_ERROR (error_reporter, " tx len: %d" , *stroke_transmit_length);
665+ MicroPrintf ( " tx len: %d" , *stroke_transmit_length);
676666#endif // MAGIC_WAND_DEBUG
677667
678668 TfLiteTensor* model_input = interpreter->input (0 );
@@ -682,7 +672,7 @@ void loop() {
682672
683673 TfLiteStatus invoke_status = interpreter->Invoke ();
684674 if (invoke_status != kTfLiteOk ) {
685- TF_LITE_REPORT_ERROR (error_reporter, " Invoke failed" );
675+ MicroPrintf ( " Invoke failed" );
686676 return ;
687677 }
688678
@@ -701,8 +691,8 @@ void loop() {
701691 (max_score - output->params .zero_point ) * output->params .scale ;
702692 float max_score_int;
703693 float max_score_frac = modf (max_score_f * 100 , &max_score_int);
704- TF_LITE_REPORT_ERROR (error_reporter, " Found %s (%d.%d%%)" ,
705- labels[max_index], static_cast <int >(max_score_int),
706- static_cast <int >(max_score_frac * 100 ));
694+ MicroPrintf ( " Found %s (%d.%d%%)" , labels[max_index] ,
695+ static_cast <int >(max_score_int),
696+ static_cast <int >(max_score_frac * 100 ));
707697 }
708698}
0 commit comments