34_CocoGPT.py 9.5 KB

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