Last active
December 8, 2019 05:17
-
-
Save boochow/7c64ccd295ab90d1317df5f6e7aa704a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<Arduino.h> | |
#include<math.h> | |
#include "tensorflow/lite/experimental/micro/kernels/all_ops_resolver.h" | |
#include "tensorflow/lite/experimental/micro/micro_error_reporter.h" | |
#include "tensorflow/lite/experimental/micro/micro_interpreter.h" | |
#include "sine_model_data.h" | |
#include <M5Stack.h> | |
#define BLACK 0x0000 | |
#define WHITE 0xFFFF | |
constexpr int tensor_pool_size = 2 * 1024; | |
uint8_t tensor_pool[tensor_pool_size]; | |
const tflite::Model* sine_model; | |
tflite::MicroInterpreter* interpreter; | |
TfLiteTensor* input; | |
TfLiteTensor* output; | |
float user_input; | |
void setup() { | |
M5.begin(); | |
Serial.begin(115200); | |
Serial.println("Loading Tensorflow model..."); | |
sine_model = tflite::GetModel(g_sine_model_data); | |
Serial.println("Sine model loaded!"); | |
static tflite::ops::micro::AllOpsResolver resolver; | |
static tflite::ErrorReporter* error_reporter; | |
static tflite::MicroErrorReporter micro_error; | |
error_reporter = µ_error; | |
static tflite::MicroInterpreter static_interpreter( | |
sine_model, resolver, tensor_pool, tensor_pool_size, error_reporter | |
); | |
interpreter = &static_interpreter; | |
Serial.println("Allocating tensors to memory pool"); | |
if (interpreter->AllocateTensors() != kTfLiteOk) { | |
Serial.println("There was an error allocating the memory...ooof"); | |
return; | |
} | |
input = interpreter->input(0); | |
output = interpreter->output(0); | |
Serial.println("Starting inferences..."); | |
user_input = 0.0f; | |
M5.Lcd.drawString("TensorFlow Lite for Microcontrollers", 60, 0); | |
} | |
void loop() { | |
M5.update(); | |
M5.Lcd.fillCircle(int(user_input * 50 + 3), int(140 - output->data.f[0] * 100), 8, BLACK); | |
user_input = user_input + 0.1; | |
if (user_input > 6.28) { | |
user_input = 0; | |
} | |
input->data.f[0] = user_input; | |
if (interpreter->Invoke() != kTfLiteOk) { | |
Serial.println("There was an error invoking the interpreter!"); | |
Serial.print("Input: "); | |
Serial.println(user_input); | |
return; | |
} | |
M5.Lcd.fillCircle(int(user_input * 50 + 3), int(140 - output->data.f[0] * 100), 8, WHITE); | |
delay(10); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment