• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "tensorflow/lite/micro/examples/image_recognition_experimental/stm32f746_discovery/display_util.h"
17 
18 #include <stdint.h>
19 
20 #include "LCD_DISCO_F746NG/LCD_DISCO_F746NG.h"
21 
22 LCD_DISCO_F746NG lcd;
23 
24 extern "C" {
25 // defined in stm32746g_discovery_camera.c
26 extern DCMI_HandleTypeDef hDcmiHandler;
DCMI_IRQHandler(void)27 void DCMI_IRQHandler(void) { HAL_DCMI_IRQHandler(&hDcmiHandler); }
DMA2_Stream1_IRQHandler(void)28 void DMA2_Stream1_IRQHandler(void) {
29   HAL_DMA_IRQHandler(hDcmiHandler.DMA_Handle);
30 }
31 }
32 
33 static char lcd_output_string[50];
34 
init_lcd()35 void init_lcd() { lcd.Clear(LCD_COLOR_WHITE); }
36 
display_image_rgb888(int x_dim,int y_dim,const uint8_t * image_data,int x_loc,int y_loc)37 void display_image_rgb888(int x_dim, int y_dim, const uint8_t* image_data,
38                           int x_loc, int y_loc) {
39   for (int y = 0; y < y_dim; ++y) {
40     for (int x = 0; x < x_dim; ++x, image_data += 3) {
41       uint8_t a = 0xFF;
42       auto r = image_data[0];
43       auto g = image_data[1];
44       auto b = image_data[2];
45       int pixel = a << 24 | r << 16 | g << 8 | b;
46       lcd.DrawPixel(x_loc + x, y_loc + y, pixel);
47     }
48   }
49 }
50 
display_image_rgb565(int x_dim,int y_dim,const uint8_t * image_data,int x_loc,int y_loc)51 void display_image_rgb565(int x_dim, int y_dim, const uint8_t* image_data,
52                           int x_loc, int y_loc) {
53   for (int y = 0; y < y_dim; ++y) {
54     for (int x = 0; x < x_dim; ++x, image_data += 2) {
55       uint8_t a = 0xFF;
56       uint8_t pix_lo = image_data[0];
57       uint8_t pix_hi = image_data[1];
58       uint8_t r = (0xF8 & pix_hi);
59       uint8_t g = ((0x07 & pix_hi) << 5) | ((0xE0 & pix_lo) >> 3);
60       uint8_t b = (0x1F & pix_lo) << 3;
61       int pixel = a << 24 | r << 16 | g << 8 | b;
62       // inverted image, so draw from bottom-right to top-left
63       lcd.DrawPixel(x_loc + (x_dim - x), y_loc + (y_dim - y), pixel);
64     }
65   }
66 }
67 
print_prediction(const char * prediction)68 void print_prediction(const char* prediction) {
69   // NOLINTNEXTLINE
70   sprintf(lcd_output_string, "  Prediction: %s       ", prediction);
71   lcd.DisplayStringAt(0, LINE(8), (uint8_t*)lcd_output_string, LEFT_MODE);
72 }
73 
print_confidence(uint8_t max_score)74 void print_confidence(uint8_t max_score) {
75   // NOLINTNEXTLINE
76   sprintf(lcd_output_string, "  Confidence: %.1f%%   ",
77           (max_score / 255.0) * 100.0);
78   lcd.DisplayStringAt(0, LINE(9), (uint8_t*)lcd_output_string, LEFT_MODE);
79 }
80