-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
140 lines (111 loc) · 3.55 KB
/
main.cpp
File metadata and controls
140 lines (111 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <cmath>
#include <iostream>
#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <string>
#include "pico/stdlib.h"
#include "pico/multicore.h"
#include "pico/sync.h"
#include "hardware/watchdog.h"
#include "hardware/sync.h"
#include "LCD_Driver.h"
#include "LCD_Touch.h"
#include "LCD_GUI.h"
#include "DEV_Config.h"
#include "model.h"
#include "model_settings.h"
#include "mnist_model_data.h"
using namespace std;
#define HALT_CORE_1() while (1) { tight_loop_contents(); }
INFERENCE inference;
Model ml_model;
mutex_t mutex; // Declare a mutex
int count_digits(int number) {
if (number == 0) {
return 1; // Special case for 0, which has 1 digit
}
number = abs(number); // Handle negative numbers
return std::floor(std::log10(number)) + 1;
}
// run core0 loop that displays UI and handle user interaction
void core1_entry() {
uint16_t cnt=0;
while(true) {
for(cnt=1000;cnt>2;cnt--)
{
LCD_SetBackLight(1000);
// pass the ML inputs, output, and semaphore
TP_DrawBoard();
}
}
}
int main(void)
{
System_Init();
mutex_init(&mutex); // Initialize the mutex
sleep_ms(5000);
// initialize LCD display
LCD_SCAN_DIR lcd_scan_dir = SCAN_DIR_DFT;
LCD_Init(lcd_scan_dir,1000);
TP_Init(lcd_scan_dir);
LCD_SCAN_DIR bmp_scan_dir = D2U_R2L;
TP_GetAdFac();
reset_inference(&inference);
init_gui();
// run core1 loop that handles user interface
multicore_launch_core1(core1_entry);
// initialize ML model, set the model name (initial_model, mobile_net, squeeze_net, lenet-5)
if (!ml_model.setup()) {
printf("Failed to initialize ML model!\n");
HALT_CORE_1();
}
printf("Model initialized\n");
uint8_t* test_image_input = ml_model.input_data();
if (test_image_input == nullptr) {
printf("Cannot set input\n");
HALT_CORE_1();
}
int byte_size = ml_model.byte_size();
if (!byte_size) {
printf("Byte size not found\n");
HALT_CORE_1();
}
while (true) {
// Block the process until data being filled
uint32_t g = multicore_fifo_pop_blocking();
// Acquire the mutex (blocking)
mutex_enter_blocking(&mutex);
inference.IsProcessing = true;
// Run inference and set predicted result
// This for loop is used for debugging purpose.
for (int index=0; index<DIGIT_INPUT_COUNT; index++) {
for (int i=0; i<INPUT_IMAGE_SIZE; i++) {
for (int j=0; j<INPUT_IMAGE_SIZE; j++) {
uint8_t num = inference.UserInputs[index].InputData[i*INPUT_IMAGE_SIZE + j];
int space = 3 - count_digits(num);
printf("%d", num);
for (int i = 0; i < space; ++i) {
printf(" ");
}
}
printf("\n");
}
memcpy(test_image_input, inference.UserInputs[index].InputData, byte_size);
int result = ml_model.predict();
if (result == -1) {
printf("Failed to run inference\n");
inference.UserInputs[index].PredictedDigit = UNKNOWN_PREDICTION;
} else {
printf("Predicted: %d\n", result);
inference.UserInputs[index].PredictedDigit = result;
}
sleep_ms(200); //increased from 200
}
printf("Login process finished.\n");
inference.IsProcessing = false;
//Return the resource
mutex_exit(&mutex);
}
return 0;
}