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 #include <stdio.h>
16 #include <string.h>
17 #include "micro_speech/command_responder.h"
18 #include "micro_speech/micro_features/micro_model_settings.h"
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif  // __cplusplus
23 #include <ulog/ulog.h>
24 #include "led.h"
25 #include "player/player.h"
26 #include "oled/oled.h"
27 #include "aos/kernel.h"
28 #ifdef __cplusplus
29 }  // extern "C"
30 #endif  // __cplusplus
31 
32 static aos_task_t cmd_task_handle;
33 static aos_sem_t cmd_sem;
34 static int control_type = 0;
35 
command_process_main(void * p)36 static void command_process_main(void *p)
37 {
38     while (1) {
39         oled_show(OLED_STR_IDLE);
40         aos_sem_wait(&cmd_sem, AOS_WAIT_FOREVER);
41         // player_wait_complete();
42         if (control_type == 1) {
43             led_switch(LED1_NUM, LED_ON);
44             led_switch(LED2_NUM, LED_ON);
45             led_switch(LED3_NUM, LED_ON);
46             oled_show(OLED_STR_LIGHT_ON);
47             player_play(PLAYER_MP3_LIGHT_ON);
48         } else if (control_type == 2) {
49             led_switch(LED1_NUM, LED_OFF);
50             led_switch(LED2_NUM, LED_OFF);
51             led_switch(LED3_NUM, LED_OFF);
52             oled_show(OLED_STR_LIGHT_OFF);
53             player_play(PLAYER_MP3_LIGHT_OFF);
54         } else {
55             oled_show(OLED_STR_IDLE);
56         }
57     }
58 }
59 
RespondCommandThreadInit(void)60 void RespondCommandThreadInit(void)
61 {
62     aos_sem_new(&cmd_sem, 0);
63     aos_task_new_ext(&cmd_task_handle,
64       "cmd_callback", command_process_main,
65       NULL, 1024 * 10, AOS_DEFAULT_APP_PRI);
66 }
67 
68 // The default implementation writes out the name of the recognized command
69 // to the error console. Real applications will want to take some custom
70 // action instead, and should implement their own versions of this function.
RespondToCommand(tflite::ErrorReporter * error_reporter,int32_t current_time,const char * found_command,uint8_t score,bool is_new_command)71 void RespondToCommand(tflite::ErrorReporter* error_reporter,
72                       int32_t current_time, const char* found_command,
73                       uint8_t score, bool is_new_command) {
74     if (is_new_command) {
75         TF_LITE_REPORT_ERROR(error_reporter, "Heard %s (%d) @%dms", found_command,
76                                 score, current_time);
77 
78         if (!strcmp(found_command, kCategoryLabels[2])) { // 打开
79             control_type = 1;
80             LOG("Heard 2 %s (%d) @%dms", found_command, score, current_time);
81         } else if (!strcmp(found_command, kCategoryLabels[3])) { // 关闭
82             control_type = 2;
83             LOG("Heard 3 %s (%d) @%dms", found_command, score, current_time);
84         } else {
85             control_type = 3;
86         }
87         aos_sem_signal(&cmd_sem);
88     }
89 }
90 
91