34_CocoGPT.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #!/usr/bin/env python
  2. #version : 2024.08.08
  3. #language : en
  4. from maix import display
  5. from maix import image
  6. from maix import camera
  7. from maix import mjpg
  8. from maix import utils
  9. import base64
  10. import time
  11. import requests
  12. import json
  13. from evdev import InputDevice, categorize, ecodes
  14. import os
  15. import sys
  16. sys.path.append("/root/")
  17. sys.path.append("/root/preset/drivers/pylib/")
  18. from CocoPi import BUTTON
  19. camera.camera.config(size=(320,240))
  20. image.load_freetype("/root/preset/fonts/simhei.ttf")
  21. canvas = image.new(size = (320, 240))
  22. try:
  23. # 获取键盘设备
  24. keyboard = InputDevice('/dev/input/event1') # 替换成实际的键盘设备路径
  25. except:
  26. keyboard = ""
  27. if keyboard=="":
  28. canvas.draw_string(0,12, "未检测到键盘", scale = 1.3, color = (255,255,255) , thickness = 1)
  29. display.show(canvas)
  30. time.sleep(3)
  31. sys.exit()
  32. def v831_display_show_canvas(displayShow):
  33. global _canvas_y,_canvas_x
  34. CANVASSHOWIMGAGE = ""
  35. if ScreenOrientation:
  36. displayShowCanvas = image.new(size = (240, 320))
  37. displayShowCanvas.draw_rectangle(0,0,240,320, color=(0,0,0), thickness=-1)
  38. displayShowCanvas.draw_image(displayShow,_canvas_x,_canvas_y,alpha=1)
  39. displayShowVER = displayShowCanvas.crop(0,0,240,320)
  40. displayShowVER = displayShowVER.rotate(-90, adjust=1)
  41. CANVASSHOWIMGAGE = displayShowVER
  42. display.show(displayShowVER)
  43. else:
  44. displayShowCanvas = image.new(size = (320, 240))
  45. displayShowCanvas.draw_rectangle(0,0,320,240, color=(0,0,0), thickness=-1)
  46. displayShowCanvas.draw_image(displayShow,_canvas_x,_canvas_y,alpha=1)
  47. CANVASSHOWIMGAGE = displayShowCanvas
  48. display.show(displayShowCanvas)
  49. def get_post_chatgpt(datas):
  50. global list_num
  51. # 目标URL
  52. url = "https://gpt.cocorobo.cn/askForTopic"
  53. payload = json.dumps({"topic": datas})
  54. headers={
  55. "Content-Type":"application/json"
  56. }
  57. response=requests.request("POST", url, headers=headers, data=payload)
  58. # try:
  59. # keyboardStr[list_num] = "result"
  60. # except:
  61. # keyboardStr[list_num] = "fail"
  62. # pass
  63. if response.status_code == 200:
  64. # keyboardStr[list_num] = json.loads(response.text)["FunctionResponse"]["result"]
  65. result_num = len(json.loads(response.text)["FunctionResponse"]["result"])//20
  66. for i in range(result_num+1):
  67. keyboardStr.append("")
  68. keyboardStr[list_num] = json.loads(response.text)["FunctionResponse"]["result"][i*20:(i+1)*20]
  69. list_num = list_num+1
  70. else:
  71. keyboardStr[list_num] = "fail"
  72. ScreenOrientation = False
  73. _canvas_x = 0
  74. _canvas_y = 0
  75. keyboardStr = ["请输入:",""]
  76. list_num = 1
  77. # 判断是否联网
  78. key_B = BUTTON(8)
  79. key_C = BUTTON(13)
  80. key_D = BUTTON(7)
  81. def wifi_is_content():
  82. global getDateNum
  83. cmd = "ifconfig"
  84. res = os.popen(cmd).read()
  85. data = False
  86. if res.rfind("inet addr:")!=48:
  87. data = True
  88. return data
  89. if ScreenOrientation:
  90. CAMERAROTATE = +180
  91. else:
  92. CAMERAROTATE = +90
  93. ssidInfo = ""
  94. passwordInfo = ""
  95. checkConnectState = False
  96. connectText = ""
  97. startConnect=False
  98. wifiConnectState = False
  99. # PublicIp=""
  100. PrivateIP=""
  101. runConnectSig=True
  102. connectSuccessSig=False
  103. while True:
  104. if wifi_is_content():
  105. break
  106. else:
  107. canvas = camera.capture()
  108. if ssidInfo!="" and passwordInfo!="":
  109. startConnect=True
  110. #connectText = "Waitting for Connection..."
  111. #canvas.draw_string(10,40, ssidInfo+" "+str(len(ssidInfo))+" "+str(type(ssidInfo)), scale = 1.5, color = (0,0,0), thickness = 1)
  112. canvas.draw_string(10,50, "正在连接WIFI,请等待...", scale = 1.5, color = (0,0,0), thickness = 1)
  113. display.show(canvas)
  114. if startConnect==True:
  115. canvas_1 = image.new(size = (320, 320), color = (255,255,255), mode = "RGB")
  116. canvas.draw_image(canvas_1,0,0, alpha=0.4)
  117. if wifiConnectState == True:
  118. if key_B.is_pressed():
  119. while (key_B.is_pressed() == True):
  120. time.sleep(0.001)
  121. startConnect= False
  122. connectText = ""
  123. checkConnectState = False
  124. connectSuccessSig = False
  125. wifiConnectState = False
  126. runConnectSig== True
  127. ssidInfo=""
  128. passwordInfo=""
  129. if checkConnectState == True:
  130. if connectSuccessSig == False:
  131. # PublicIp=getPublicIp()
  132. connectSuccessSig = True
  133. connectText = "Wifi connection successfully!"
  134. # canvas.draw_string(10,80, "The WLAN PUBLIC IP:" + PublicIp, scale = 1.5, color = (0,0,0), thickness = 1)
  135. canvas.draw_string(10,110, "局域网IP:" + PrivateIP, scale = 1.5, color = (0,0,0), thickness = 1)
  136. # canvas.draw_image((image.open("/root/preset/img/restart_ff0000_24x24.png")).rotate(0, adjust=0),8,216,alpha=1)
  137. ssidInfo=""
  138. passwordInfo=""
  139. canvas.clear()
  140. break
  141. else:
  142. pass
  143. else:
  144. if checkConnectState == False:
  145. os.system("wifi_disconnect_ap_test")
  146. os.system('wifi_connect_chinese_ap_test '+ssidInfo+' '+passwordInfo+'')
  147. checkConnectState = True
  148. if checkConnectState == True:
  149. if key_B.is_pressed():
  150. while (key_B.is_pressed() == True):
  151. time.sleep(0.001)
  152. startConnect= False
  153. connectText = ""
  154. checkConnectState = False
  155. connectSuccessSig = False
  156. wifiConnectState = False
  157. runConnectSig== True
  158. passwordInfo = ""
  159. ssidInfo = ""
  160. else:
  161. if runConnectSig== True:
  162. connectText = "正在连接WIFI,请等待..."
  163. runConnectSig= False
  164. else:
  165. passwordInfo = ""
  166. ssidInfo = ""
  167. connectText = "WIFI连接失败!"
  168. canvas.draw_image((image.open("/root/preset/img/restart_ff0000_24x24.png")).rotate(0, adjust=0),8,216,alpha=1)
  169. canvas.draw_string(10,50, connectText, scale = 1.5, color = (0,0,0) , thickness = 1)
  170. else:
  171. mks = canvas.find_qrcodes()
  172. for mk in mks:
  173. #外框数据
  174. X = mk['x']
  175. Y = mk['y']
  176. W = mk['w']
  177. H = mk['h']
  178. #二维码信息
  179. string = mk['payload']
  180. codeData = string.split(";")
  181. ssidInfo = codeData[0].split(":")[1]
  182. passwordInfo = codeData[1].split(":")[1]
  183. #画外框
  184. canvas.draw_rectangle(X, Y, X + W, Y + H, color=(0, 0, 255), thickness = 2)
  185. #打印信息
  186. canvas.draw_string(int(X) , int(Y - 45) , "WIFI名:"+ssidInfo, scale = 1, color = (255, 0, 0), thickness = 2) #内框ID
  187. canvas.draw_string(int(X) , int(Y - 25) , "密码:"+passwordInfo, scale = 1, color = (255, 0, 0), thickness = 2) #内框ID
  188. canvas.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),288,216,alpha=1)
  189. v831_display_show_canvas(canvas)
  190. # 键盘事件
  191. canvas.draw_string(0,0, str("请输入:"), scale = 1, color = (255,255,255) , thickness = 1)
  192. v831_display_show_canvas(canvas)
  193. for event in keyboard.read_loop():
  194. if event.type == ecodes.EV_KEY:
  195. key_event = categorize(event)
  196. if key_event.keystate == key_event.key_down:
  197. # print(keyboardStr,key_event.keycode[4:].lower())
  198. if key_event.keycode[4:] == "SPACE":
  199. keyboardStr[list_num] = keyboardStr[list_num] + " "
  200. elif key_event.keycode[4:] == "BACKSPACE":
  201. keyboardStr[list_num] = keyboardStr[list_num][:-1]
  202. if len(keyboardStr[list_num]) ==0 and list_num>1:
  203. list_num = list_num - 1
  204. elif key_event.keycode[4:] == "ENTER":
  205. keyboardStr.append("")
  206. list_num = list_num + 1
  207. get_post_chatgpt(keyboardStr[list_num-1])
  208. else:
  209. keyboardStr[list_num] = keyboardStr[list_num] + key_event.keycode[4:].lower()
  210. # print(list_num,keyboardStr)
  211. if len(keyboardStr[list_num]) >40:
  212. keyboardStr.append("")
  213. list_num = list_num + 1
  214. canvas.clear()
  215. if list_num>16:
  216. for i in range(list_num,list_num-16,-1):
  217. canvas.draw_string(0,15*(i-(list_num-15)), str(keyboardStr[i]), scale = 1, color = (255,255,255) , thickness = 1)
  218. else:
  219. for i in range(list_num+1):
  220. canvas.draw_string(0,15*i, str(keyboardStr[i]), scale = 1, color = (255,255,255) , thickness = 1)
  221. canvas.draw_string(0,0, str("请输入:"), scale = 1, color = (255,255,255) , thickness = 1)
  222. v831_display_show_canvas(canvas)