|
| 1 | +#include "embARC.h" |
| 2 | +#include "board.h" |
| 3 | +#include "dev_uart.h" |
| 4 | +#include "dev_pinmux.h" |
| 5 | + |
| 6 | +#include "vlsi_esp8266.h" |
| 7 | +#include "vlsi_ftdi.h" |
| 8 | +#include "vlsi_svm.h" |
| 9 | + |
| 10 | +#include <stdio.h> |
| 11 | +#include <stdlib.h> |
| 12 | +#include "embARC_debug.h" |
| 13 | + |
| 14 | +struct vlsi_ftdi *vlsi_ftdi_obj; |
| 15 | +struct vlsi_esp *vlsi_esp_obj; |
| 16 | + |
| 17 | +static volatile unsigned int start; |
| 18 | +static unsigned int perf_id = 0xFF; |
| 19 | + |
| 20 | +static void perf_init(unsigned int id) |
| 21 | +{ |
| 22 | + if (timer_start(id, TIMER_CTRL_NH, 0xFFFFFFFF) < 0) |
| 23 | + EMBARC_PRINTF("perf timer init failed\r\n"); |
| 24 | + perf_id = id; |
| 25 | +} |
| 26 | + |
| 27 | +/** Performance timer start */ |
| 28 | +static void perf_start(void) |
| 29 | +{ |
| 30 | + if (timer_current(perf_id, (void *)(&start)) < 0) |
| 31 | + start = 0; |
| 32 | +} |
| 33 | + |
| 34 | +/** Performance timer end, and return the time passed */ |
| 35 | +static unsigned int perf_end(void) |
| 36 | +{ |
| 37 | + unsigned int end = 0; |
| 38 | + |
| 39 | + if (timer_current(perf_id, (void *)(&end)) < 0) |
| 40 | + return 0; |
| 41 | + if (start < end) |
| 42 | + end = end - start; |
| 43 | + else |
| 44 | + end = 0xFFFFFFFF - start + end; |
| 45 | + return end; |
| 46 | +} |
| 47 | + |
| 48 | +int main(void) |
| 49 | +{ |
| 50 | + unsigned int exe_time; |
| 51 | + |
| 52 | + float predict_data[80]; |
| 53 | + |
| 54 | + int real_data[84]; |
| 55 | + |
| 56 | + int r_hand_y = 0; |
| 57 | + int l_hand_y = 0; |
| 58 | + int hand_top_point[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 59 | + |
| 60 | + int final_anw = 0; |
| 61 | + int anw_fifo_0 = 0; |
| 62 | + int anw_fifo_1 = 0; |
| 63 | + int anw_fifo_2 = 0; |
| 64 | + int info_index; |
| 65 | + unsigned int voice_info; |
| 66 | + |
| 67 | + start = 0; |
| 68 | + |
| 69 | + /** PMOD_C = UART1 mux.c line 281 */ |
| 70 | + io_pmod_config(PMOD_C, PMOD_UART, IO_PINMUX_ENABLE); |
| 71 | + |
| 72 | + /** ARDUINO = UART2 mux.c line 255 */ |
| 73 | + io_arduino_config_uart(IO_PINMUX_ENABLE); |
| 74 | + |
| 75 | + vlsi_ftdi_obj = (VLSI_FTDI *) malloc(sizeof(VLSI_FTDI)); |
| 76 | + vlsi_esp_obj = (VLSI_ESP *) malloc(sizeof(VLSI_ESP)); |
| 77 | + |
| 78 | + vlsi_ftdi_uart_init(vlsi_ftdi_obj, 115200); |
| 79 | + vlsi_esp_uart_init(vlsi_esp_obj, 115200); |
| 80 | + |
| 81 | + perf_init(TIMER_1); |
| 82 | + |
| 83 | + int f = 0; |
| 84 | + |
| 85 | + EMBARC_PRINTF("ESP8266 TEST\r\n"); |
| 86 | + f = vlsi_esp_test(vlsi_esp_obj); |
| 87 | + EMBARC_PRINTF("test %d \r\n", f); |
| 88 | + board_delay_ms(1000, 1); |
| 89 | + |
| 90 | + EMBARC_PRINTF("ESP8266 MODE SET\r\n"); |
| 91 | + f = vlsi_esp_mode_set(vlsi_esp_obj); |
| 92 | + EMBARC_PRINTF("test %d \r\n", f); |
| 93 | + board_delay_ms(1000, 1); |
| 94 | + |
| 95 | + EMBARC_PRINTF("ESP8266 CONNECT\r\n"); |
| 96 | + f = vlsi_esp_connect(vlsi_esp_obj); |
| 97 | + EMBARC_PRINTF("test %d \r\n", f); |
| 98 | + if (f == 0) |
| 99 | + EMBARC_PRINTF("Connect WIFI OK!\r\n"); |
| 100 | + else |
| 101 | + EMBARC_PRINTF("Connect WIFI ERROR!\r\n"); |
| 102 | + board_delay_ms(1000, 1); |
| 103 | + |
| 104 | + EMBARC_PRINTF("ESP8266 MULTI\r\n"); |
| 105 | + f = vlsi_esp8266_multi(vlsi_esp_obj); |
| 106 | + EMBARC_PRINTF("test %d \r\n", f); |
| 107 | + board_delay_ms(1000, 1); |
| 108 | + |
| 109 | + EMBARC_PRINTF("ESP8266 SERVER\r\n"); |
| 110 | + f = vlsi_esp8266_server(vlsi_esp_obj); |
| 111 | + EMBARC_PRINTF("test %d \r\n", f); |
| 112 | + board_delay_ms(1000, 1); |
| 113 | + |
| 114 | + EMBARC_PRINTF("ESP8266 GET IP\r\n"); |
| 115 | + f = vlsi_esp8266_get_ip(vlsi_esp_obj); |
| 116 | + EMBARC_PRINTF("test %d \r\n", f); |
| 117 | + board_delay_ms(1000, 1); |
| 118 | + |
| 119 | + EMBARC_PRINTF("ESP8266 LINK\r\n"); |
| 120 | + f = vlsi_esp8266_link(vlsi_esp_obj); |
| 121 | + EMBARC_PRINTF("test %d \r\n", f); |
| 122 | + board_delay_ms(1000, 1); |
| 123 | + |
| 124 | + EMBARC_PRINTF("FTDI LINK\r\n"); |
| 125 | + f = vlsi_ftdi_test(vlsi_ftdi_obj); |
| 126 | + EMBARC_PRINTF("test %d \r\n", f); |
| 127 | + board_delay_ms(10, 1); |
| 128 | + |
| 129 | + perf_start(); /** point 1 */ |
| 130 | + |
| 131 | + while (1) { |
| 132 | + /** Receive data from OpenPose */ |
| 133 | + vlsi_ftdi_receive(vlsi_ftdi_obj, real_data); |
| 134 | + |
| 135 | + exe_time = perf_end(); |
| 136 | + EMBARC_PRINTF("OpenPose Time : %d cycles\r\n", exe_time); |
| 137 | + perf_start(); /** point 2 */ |
| 138 | + |
| 139 | + /** Pre-Process */ |
| 140 | + { |
| 141 | + /** right hand */ |
| 142 | + for (int i = 0; i < 20; i++) { /** 0~39 */ |
| 143 | + if ((i == 4) || (i == 8) || (i == 12) |
| 144 | + || (i == 16)) { |
| 145 | + predict_data[2 * i] = |
| 146 | + ((float)real_data[2 * i + 2] - |
| 147 | + (float)real_data[0]) / 10; |
| 148 | + predict_data[2 * i + 1] = |
| 149 | + ((float)real_data[2 * i + 3] - |
| 150 | + (float)real_data[1]) / 10; |
| 151 | + } else { |
| 152 | + predict_data[2 * i] = |
| 153 | + ((float)real_data[2 * i + 2] - |
| 154 | + (float)real_data[2 * i]) / 10; |
| 155 | + predict_data[2 * i + 1] = |
| 156 | + ((float)real_data[2 * i + 3] - |
| 157 | + (float)real_data[2 * i + 1]) / 10; |
| 158 | + } |
| 159 | + } |
| 160 | + /** left hand */ |
| 161 | + for (int i = 21; i < 41; i++) { /** 40~79 */ |
| 162 | + if ((i == 25) || (i == 29) || (i == 33) |
| 163 | + || (i == 37)) { |
| 164 | + predict_data[2 * i - 2] = |
| 165 | + ((float)real_data[2 * i + 2] - |
| 166 | + (float)real_data[42]) / 10; |
| 167 | + predict_data[2 * i - 1] = |
| 168 | + ((float)real_data[2 * i + 3] - |
| 169 | + (float)real_data[43]) / 10; |
| 170 | + } else { |
| 171 | + predict_data[2 * i - 2] = |
| 172 | + ((float)real_data[2 * i + 2] - |
| 173 | + (float)real_data[2 * i]) / 10; |
| 174 | + predict_data[2 * i - 1] = |
| 175 | + ((float)real_data[2 * i + 3] - |
| 176 | + (float)real_data[2 * i + 1]) / 10; |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + exe_time = perf_end(); |
| 182 | + EMBARC_PRINTF("Pre-Process Time : %d cycles\r\n", exe_time); |
| 183 | + perf_start(); /** point 3 */ |
| 184 | + |
| 185 | + /** SVM Predict */ |
| 186 | + anw_fifo_2 = anw_fifo_1; |
| 187 | + anw_fifo_1 = anw_fifo_0; |
| 188 | + anw_fifo_0 = vlsi_svm_predict(predict_data); |
| 189 | + |
| 190 | + if ((anw_fifo_2 == anw_fifo_1) && (anw_fifo_2 == anw_fifo_0)) |
| 191 | + final_anw = anw_fifo_2; |
| 192 | + |
| 193 | + exe_time = perf_end(); |
| 194 | + EMBARC_PRINTF("SVM Time : %d cycles\r\n", exe_time); |
| 195 | + perf_start(); /** point 4 */ |
| 196 | + |
| 197 | + /** Voice and Information */ |
| 198 | + { |
| 199 | + voice_info = 0; |
| 200 | + /** voice_info 31,30 |
| 201 | + * 2'b00 = none / 2'b01 = piano / 2'b10 = drum / 2'b11 = guitar |
| 202 | + * drum,guitar |
| 203 | + * 29,28 : voice |
| 204 | + * 27~21 : left hand x_index |
| 205 | + * 20~14 : left hand y_index |
| 206 | + * 13~ 7 : right hand x_index |
| 207 | + * 6~ 0 : right hand x_index |
| 208 | + * piano |
| 209 | + * 29,28 : don't care |
| 210 | + * 27~ 0 : voice per key |
| 211 | + * Piano |
| 212 | + */ |
| 213 | + if (final_anw == 0) { |
| 214 | + voice_info |= 0x1 << 30; |
| 215 | + for (int i = 0; i < 10; i++) { |
| 216 | + int j = 0; |
| 217 | + if (i >= 5) |
| 218 | + j = 2; |
| 219 | + if (((real_data[8 * i + 9 + j] - |
| 220 | + hand_top_point[i]) > 8) |
| 221 | + && |
| 222 | + ((real_data[8 * i + 9 + j] - |
| 223 | + hand_top_point[i]) < 50)) { |
| 224 | + /** bound */ |
| 225 | + if (real_data[8 * i + 8 + j] < |
| 226 | + 54) |
| 227 | + info_index = 0; |
| 228 | + else if (real_data |
| 229 | + [8 * i + 8 + j] > 567) |
| 230 | + info_index = 27; |
| 231 | + else |
| 232 | + info_index = |
| 233 | + (real_data |
| 234 | + [8 * i + 8 + j] - |
| 235 | + 54) / 19; |
| 236 | + voice_info |= 0x1 << info_index; |
| 237 | + } |
| 238 | + } |
| 239 | + } else { |
| 240 | + /** Drum */ |
| 241 | + if (final_anw == 1) { |
| 242 | + voice_info |= 0x1 << 31; |
| 243 | + if ((real_data[9] - r_hand_y) > 50) |
| 244 | + voice_info |= 0x1 << 28; |
| 245 | + if ((real_data[51] - l_hand_y) > 50) |
| 246 | + voice_info |= 0x1 << 29; |
| 247 | + } |
| 248 | + /** Guitar */ |
| 249 | + else { |
| 250 | + voice_info |= 0x1 << 31; |
| 251 | + voice_info |= 0x1 << 30; |
| 252 | + if ((real_data[9] - r_hand_y) > 50) |
| 253 | + voice_info |= 0x1 << 28; |
| 254 | + } |
| 255 | + /** hand information */ |
| 256 | + { |
| 257 | + /** right hand x_index */ |
| 258 | + if (real_data[0] > 570) |
| 259 | + info_index = 100; |
| 260 | + else if (real_data[0] < 70) |
| 261 | + info_index = 0; |
| 262 | + else { |
| 263 | + info_index = |
| 264 | + (real_data[0] - 70) / 5; |
| 265 | + } |
| 266 | + voice_info += info_index << 7; |
| 267 | + /** right hand y_index */ |
| 268 | + if (real_data[1] > 490) |
| 269 | + info_index = 100; |
| 270 | + else { |
| 271 | + info_index = |
| 272 | + (real_data[1] + 10) / 5; |
| 273 | + } |
| 274 | + voice_info += info_index; |
| 275 | + /** left hand x_index */ |
| 276 | + if (real_data[42] > 570) |
| 277 | + info_index = 100; |
| 278 | + else if (real_data[42] < 70) |
| 279 | + info_index = 0; |
| 280 | + else { |
| 281 | + info_index = |
| 282 | + (real_data[42] - 70) / 5; |
| 283 | + } |
| 284 | + voice_info += info_index << 21; |
| 285 | + /** left hand y_index */ |
| 286 | + if (real_data[43] > 490) |
| 287 | + info_index = 100; |
| 288 | + else { |
| 289 | + info_index = |
| 290 | + (real_data[43] + 10) / 5; |
| 291 | + } |
| 292 | + voice_info += info_index << 14; |
| 293 | + } |
| 294 | + } |
| 295 | + } |
| 296 | + |
| 297 | + exe_time = perf_end(); |
| 298 | + EMBARC_PRINTF("Voice time : %d cycles\r\n", exe_time); |
| 299 | + perf_start(); /** point 5 */ |
| 300 | + |
| 301 | + vlsi_ftdi_send_uint(vlsi_ftdi_obj, voice_info); |
| 302 | + vlsi_esp_send_uint(vlsi_esp_obj, voice_info); |
| 303 | + |
| 304 | + exe_time = perf_end(); |
| 305 | + EMBARC_PRINTF("Transfer time : %d cycles\r\n", exe_time); |
| 306 | + perf_start(); /** point 6 and 1 */ |
| 307 | + |
| 308 | + EMBARC_PRINTF("voice information : %d %d\r\n", final_anw, |
| 309 | + voice_info); |
| 310 | + |
| 311 | + /** last data update */ |
| 312 | + r_hand_y = real_data[9]; |
| 313 | + l_hand_y = real_data[51]; |
| 314 | + hand_top_point[0] = real_data[9]; |
| 315 | + hand_top_point[1] = real_data[17]; |
| 316 | + hand_top_point[2] = real_data[25]; |
| 317 | + hand_top_point[3] = real_data[33]; |
| 318 | + hand_top_point[4] = real_data[41]; |
| 319 | + hand_top_point[5] = real_data[51]; |
| 320 | + hand_top_point[6] = real_data[59]; |
| 321 | + hand_top_point[7] = real_data[67]; |
| 322 | + hand_top_point[8] = real_data[75]; |
| 323 | + hand_top_point[9] = real_data[83]; |
| 324 | + } |
| 325 | + |
| 326 | + return E_OK; |
| 327 | +} |
0 commit comments