blob: 3acf53f15f347d60ae8fb0cf835ce7cff818ab35 [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
2 * Copyright (c) 2021 Arm Limited. All rights reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17#include "UseCaseCommonUtils.hpp"
18
19#include "InputFiles.hpp"
20
21namespace arm {
22namespace app {
23
Isabella Gottardi8df12f32021-04-07 17:15:31 +010024 bool RunInference(arm::app::Model& model, Profiler& profiler)
alexander3c798932021-03-26 21:42:19 +000025 {
Isabella Gottardi8df12f32021-04-07 17:15:31 +010026 profiler.StartProfiling("Inference");
alexander3c798932021-03-26 21:42:19 +000027 bool runInf = model.RunInference();
alexander3c798932021-03-26 21:42:19 +000028 profiler.StopProfiling();
alexander3c798932021-03-26 21:42:19 +000029
30 return runInf;
31 }
32
33 int ReadUserInputAsInt(hal_platform& platform)
34 {
35 char chInput[128];
36 memset(chInput, 0, sizeof(chInput));
37
38 platform.data_acq->get_input(chInput, sizeof(chInput));
39 return atoi(chInput);
40 }
41
42 void DumpTensor(TfLiteTensor* tensor, const size_t lineBreakForNumElements)
43 {
44 char strhex[8];
45 std::string strdump;
46
47 if (!tensor) {
48 printf_err("invalid tensor\n");
49 return;
50 }
51
52 const uint32_t tensorSz = tensor->bytes;
53 const uint8_t* tensorData = tflite::GetTensorData<uint8_t>(tensor);
54
55 for (size_t i = 0; i < tensorSz; ++i) {
56 if (0 == i % lineBreakForNumElements) {
57 printf("%s\n\t", strdump.c_str());
58 strdump.clear();
59 }
60 snprintf(strhex, sizeof(strhex) - 1,
61 "0x%02x, ", tensorData[i]);
62 strdump += std::string(strhex);
63 }
64
65 if (strdump.size()) {
66 printf("%s\n", strdump.c_str());
67 }
68 }
69
70 bool ListFilesHandler(ApplicationContext& ctx)
71 {
72 auto& model = ctx.Get<Model&>("model");
73 auto& platform = ctx.Get<hal_platform&>("platform");
74
75 constexpr uint32_t dataPsnTxtStartX = 20;
76 constexpr uint32_t dataPsnTxtStartY = 40;
77
78 if (!model.IsInited()) {
79 printf_err("Model is not initialised! Terminating processing.\n");
80 return false;
81 }
82
83 /* Clear the LCD */
84 platform.data_psn->clear(COLOR_BLACK);
85
86 /* Show the total number of embedded files. */
87 std::string strNumFiles = std::string{"Total Number of Files: "} +
88 std::to_string(NUMBER_OF_FILES);
89 platform.data_psn->present_data_text(strNumFiles.c_str(),
90 strNumFiles.size(),
91 dataPsnTxtStartX,
92 dataPsnTxtStartY,
93 0);
94
95#if NUMBER_OF_FILES > 0
96 constexpr uint32_t dataPsnTxtYIncr = 16;
97 info("List of Files:\n");
98 uint32_t yVal = dataPsnTxtStartY + dataPsnTxtYIncr;
99 for (uint32_t i = 0; i < NUMBER_OF_FILES; ++i, yVal += dataPsnTxtYIncr) {
100
101 std::string currentFilename{get_filename(i)};
102 platform.data_psn->present_data_text(currentFilename.c_str(),
103 currentFilename.size(),
104 dataPsnTxtStartX, yVal, 0);
105
106 info("\t%u => %s\n", i, currentFilename.c_str());
107 }
108#endif /* NUMBER_OF_FILES > 0 */
109
110 return true;
111 }
112
113} /* namespace app */
114} /* namespace arm */