11import sim
2- from usr .ui import SelectWindow , ChatWindow
32from usr .jobs import scheduler
43from usr import pypubsub as pub
54
@@ -33,59 +32,28 @@ def ai_callback(args):
3332 global GPIO39
3433 event = args [0 ]
3534 msg = args [1 ]
36- global chat_win
3735 if event == 1 :
3836 print ('TIKTOK_RTC_EVENT_START' )
3937 GPIO39 .write (1 )
40- chat_win .update_status ("Please speak to me" )
4138 elif event == 2 :
4239 print ('TIKTOK_RTC_EVENT_STOP' )
4340 GPIO39 .write (0 )
4441 elif event == 3 :
45- #chat_win.update_status("AI speaking . . .")
4642 print ('TIKTOK_RTC_EVENT_TTS_TEXT {}' .format (msg ))
4743 #call.stopAudioService()
4844 elif event == 4 :
49- #chat_win.update_status("AI listening . . .")
5045 print ('TIKTOK_RTC_EVENT_ASR_TEXT {}' .format (msg ))
5146 #call.stopAudioService()
5247 elif event == 5 :
5348 print ('TIKTOK_RTC_EVENT_ERROR {}' .format (msg ))
5449 else :
5550 print ('TIKTOK_RTC_EVENT UNKNOWN {}' .format (event ))
5651
57- def update_status_with_animation (chat_win , base_message , steps = 3 , delay_ms = 400 , final_wait = 2 ):
58- # 更新动画
59- for i in range (steps + 1 ):
60- chat_win .update_status (base_message + " " + " ." * i )
61- time .sleep_ms (delay_ms )
62- time .sleep (final_wait )
63-
64- def perform_initialization (chat_win , tiktok ):
65- # 初始化动画加载状态
66- print ('start rtc' )
67- chat_win .show ()
68- tiktok .active (True )
69-
70- # 需要展示的状态列表
71- status_list = [
72- "Connecting to the server" ,
73- "Building the AI engine" ,
74- "Joining the AI room" ,
75- "Loading AI personality" ,
76- "Creating AI characters"
77- ]
78-
79- # 依次遍历状态并显示动画
80- for status in status_list :
81- update_status_with_animation (chat_win , status )
8252def ai_task ():
8353 global rtc_queue
8454 global extint1
8555 global extint2
8656 global tiktok
87- global chat_win
88- global selsct_win
8957 while True :
9058 lte = dataCall .getInfo (1 , 0 )
9159 if lte [2 ][0 ] == 1 :
@@ -99,16 +67,16 @@ def ai_task():
9967 extint1 .enable ()
10068 extint2 .enable ()
10169 print ('ai task running' )
70+
10271 while True :
10372 data = rtc_queue .get ()
10473 print ('rtc_queue key event {}' .format (data ))
10574 if data == 1 :
106- perform_initialization (chat_win , tiktok )
75+ print ('start rtc' )
76+ tiktok .active (True )
10777 elif data == 2 :
10878 print ('stop rtc' )
109- selsct_win .show ()
11079 tiktok .active (False )
111-
11280
11381if __name__ == "__main__" :
11482
@@ -118,16 +86,10 @@ def ai_task():
11886 sim .setSimDet (1 , 1 )
11987
12088 # 设置按键中断
121- extint1 = ExtInt (ExtInt .GPIO13 , ExtInt .IRQ_FALLING , ExtInt .PULL_PU , key1 , filter_time = 50 )
122- extint2 = ExtInt (ExtInt .GPIO12 , ExtInt .IRQ_FALLING , ExtInt .PULL_PU , key2 , filter_time = 50 )
89+ extint1 = ExtInt (ExtInt .GPIO45 , ExtInt .IRQ_FALLING , ExtInt .PULL_PU , key1 , filter_time = 50 )
90+ extint2 = ExtInt (ExtInt .GPIO46 , ExtInt .IRQ_FALLING , ExtInt .PULL_PU , key2 , filter_time = 50 )
12391
12492 rtc_queue = Queue ()
125-
126- # 初始化界面
127- selsct_win = SelectWindow ()
128- selsct_win .show ()
129-
130- chat_win = ChatWindow ()
13193
13294 # 启动后台任务调度器
13395 scheduler .start ()
@@ -136,7 +98,7 @@ def ai_task():
13698
13799 tiktok = TiktokRTC (300000 , ai_callback )
138100 GPIO39 = Pin (PA , Pin .OUT , Pin .PULL_DISABLE , 0 )
139- tiktok .config (volume = 6 )
101+ tiktok .config (volume = 11 )
140102 print ('volume: {}' .format (tiktok .config ('volume' )))
141103
142104 _thread .start_new_thread (ai_task , ())
0 commit comments