|
| 1 | +import sim |
| 2 | +from usr.ui import SelectWindow, ChatWindow |
| 3 | +from usr.jobs import scheduler |
| 4 | +from usr import pypubsub as pub |
| 5 | + |
| 6 | +import dataCall |
| 7 | +import utime as time |
| 8 | +import TiktokRTC |
| 9 | +import atcmd |
| 10 | +import _thread |
| 11 | + |
| 12 | +from machine import Pin |
| 13 | + |
| 14 | +PA = Pin.GPIO39 |
| 15 | + |
| 16 | +from machine import ExtInt |
| 17 | +from queue import Queue |
| 18 | + |
| 19 | +def key1(args): |
| 20 | + global rtc_queue |
| 21 | + rtc_queue.put(1) |
| 22 | + |
| 23 | +def key2(args): |
| 24 | + global rtc_queue |
| 25 | + rtc_queue.put(2) |
| 26 | + |
| 27 | +def enable_pid2(): |
| 28 | + resp = bytearray(50) |
| 29 | + atcmd.sendSync('AT+qicsgp=2,1,3gnet,"","",0\r\n',resp,'',20) |
| 30 | + atcmd.sendSync('at+cgact=1,2\r\n',resp,'',20) |
| 31 | + |
| 32 | +def ai_callback(args): |
| 33 | + global GPIO39 |
| 34 | + event = args[0] |
| 35 | + msg = args[1] |
| 36 | + global chat_win |
| 37 | + if event == 1: |
| 38 | + print('TIKTOK_RTC_EVENT_START') |
| 39 | + GPIO39.write(1) |
| 40 | + chat_win.update_status("Please speak to me") |
| 41 | + elif event == 2: |
| 42 | + print('TIKTOK_RTC_EVENT_STOP') |
| 43 | + GPIO39.write(0) |
| 44 | + elif event == 3: |
| 45 | + #chat_win.update_status("AI speaking . . .") |
| 46 | + print('TIKTOK_RTC_EVENT_TTS_TEXT {}'.format(msg)) |
| 47 | + #call.stopAudioService() |
| 48 | + elif event == 4: |
| 49 | + #chat_win.update_status("AI listening . . .") |
| 50 | + print('TIKTOK_RTC_EVENT_ASR_TEXT {}'.format(msg)) |
| 51 | + #call.stopAudioService() |
| 52 | + elif event == 5: |
| 53 | + print('TIKTOK_RTC_EVENT_ERROR {}'.format(msg)) |
| 54 | + else: |
| 55 | + print('TIKTOK_RTC_EVENT UNKNOWN {}'.format(event)) |
| 56 | + |
| 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) |
| 82 | +def ai_task(): |
| 83 | + global rtc_queue |
| 84 | + global extint1 |
| 85 | + global extint2 |
| 86 | + global tiktok |
| 87 | + global chat_win |
| 88 | + global selsct_win |
| 89 | + while True: |
| 90 | + lte = dataCall.getInfo(1, 0) |
| 91 | + if lte[2][0] == 1: |
| 92 | + print('lte network normal') |
| 93 | + #pub.publish('update_status', status="ready") |
| 94 | + break |
| 95 | + print('wait lte network normal...') |
| 96 | + pub.publish('update_status', status="connect network") |
| 97 | + time.sleep(3) |
| 98 | + |
| 99 | + extint1.enable() |
| 100 | + extint2.enable() |
| 101 | + print('ai task running') |
| 102 | + while True: |
| 103 | + data = rtc_queue.get() |
| 104 | + print('rtc_queue key event {}'.format(data)) |
| 105 | + if data == 1: |
| 106 | + perform_initialization(chat_win, tiktok) |
| 107 | + elif data == 2: |
| 108 | + print('stop rtc') |
| 109 | + selsct_win.show() |
| 110 | + tiktok.active(False) |
| 111 | + |
| 112 | + |
| 113 | +if __name__ == "__main__": |
| 114 | + |
| 115 | + enable_pid2() |
| 116 | + |
| 117 | + # 使能sim卡热插拔 |
| 118 | + sim.setSimDet(1, 1) |
| 119 | + |
| 120 | + # 设置按键中断 |
| 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) |
| 123 | + |
| 124 | + rtc_queue = Queue() |
| 125 | + |
| 126 | + # 初始化界面 |
| 127 | + selsct_win = SelectWindow() |
| 128 | + selsct_win.show() |
| 129 | + |
| 130 | + chat_win = ChatWindow() |
| 131 | + |
| 132 | + # 启动后台任务调度器 |
| 133 | + scheduler.start() |
| 134 | + |
| 135 | + print('window show over') |
| 136 | + |
| 137 | + tiktok = TiktokRTC(300000, ai_callback) |
| 138 | + GPIO39 = Pin(PA, Pin.OUT, Pin.PULL_DISABLE, 0) |
| 139 | + tiktok.config(volume=6) |
| 140 | + print('volume: {}'.format(tiktok.config('volume'))) |
| 141 | + |
| 142 | + _thread.start_new_thread(ai_task, ()) |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | + |
0 commit comments