main.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. #!/usr/bin/env python
  2. #version : 2023.06.15
  3. #language : ch
  4. #hardware : pi
  5. import time
  6. from maix import *
  7. import gc
  8. import os,sys
  9. import sys
  10. sys.path.append('/root/')
  11. from CocoPi import BUTTON
  12. from CocoPi import stm8s
  13. from CocoPi import multiFuncGpio
  14. gc.enable()
  15. gc.collect()
  16. iic_slaver=stm8s()
  17. iic_slaver.clear()
  18. PIXEL_LED1= multiFuncGpio(0,7)
  19. PIXEL_LED2= multiFuncGpio(1,7)
  20. PIXEL_LED1.pixelInit_()
  21. PIXEL_LED2.pixelInit_()
  22. time.sleep(0.01)
  23. del iic_slaver
  24. del PIXEL_LED1
  25. del PIXEL_LED2
  26. count = 0
  27. def gc_log():
  28. global count
  29. gc.collect()
  30. count = count + 1
  31. splash_theme_color = (15,21,46)
  32. btn_selected = (255,255,255)
  33. btn_unselected = (76,86,127)
  34. txt_selected = (255,255,255)
  35. txt_unselected = (76,86,127)
  36. color_splash_theme = (15,21,46)
  37. color_blue = (31,61,185)
  38. color_blue_lighter = (60,131,211)
  39. color_white = (255,255,255)
  40. color_gray = (60,73,126)
  41. color_orange = (255,165,0)
  42. color_red = (255,0,0)
  43. btn_width = 134
  44. btn_height = 52
  45. btn_y_base = 5
  46. btn_col = (btn_y_base, btn_y_base+btn_width, btn_y_base+152)
  47. btn_row = 180
  48. main_title = (
  49. "运行",
  50. "上次程序",
  51. "打开",
  52. "样例菜单",
  53. "B",
  54. "A"
  55. )
  56. splash_text = (
  57. "系统版本: 2023-06-15 ",
  58. "System version: N/A",
  59. "2. 加载程序后,按下A键两秒退出程序界面.",
  60. " ",
  61. " ",
  62. "启动上次程序中...",
  63. "错误: 未找到程序...",
  64. "错误: 语法有误",
  65. "正在打开样例菜单...",
  66. "未找到样例菜单.",
  67. "运行",
  68. "加载...",
  69. "返回",
  70. "按下C键返回主界面",
  71. "按下D键重启",
  72. "按下B键关机",
  73. "1. 按下A键七秒来关闭或者重启CocoPi系统.",
  74. " ",
  75. " ",
  76. "CocoPi V6",
  77. "/ Linux / AIot /Python /",
  78. "/Graphical programming /",
  79. "------------- 基础 --------------",
  80. "----------- 人工智能 ----------",
  81. "------------ 物联网 ------------",
  82. )
  83. title_logo_text = image.open("/root/preset/img/cocorobo_text.jpg")
  84. iconUpNp = image.open("/root/preset/img/arrow_up_filled.jpg")
  85. iconUpP = image.open("/root/preset/img/arrow_up_pressed.jpg")
  86. iconDownNp = image.open("/root/preset/img/arrow_down_filled.jpg")
  87. iconDownP = image.open("/root/preset/img/arrow_down_pressed.jpg")
  88. frostedGlass = image.open("/root/preset/img/frostedglass_.jpg")
  89. #backUp = image.open("/root/preset/img/backup_48x48.png")
  90. #powerOff = image.open("/root/preset/img/poweroff_48x48.png")
  91. #reBoot = image.open("/root/preset/img/reboot_48x48.png")
  92. run_user_code_path='/root/user_latest_code.py'
  93. run_try_demo_path='/root/play_music.py'
  94. key_A = BUTTON(14)
  95. key_B = BUTTON(8)
  96. key_C = BUTTON(13)
  97. key_D = BUTTON(7)
  98. splash = image.new(size=(320, 240),mode = "RGB")
  99. img_drop=image.new(size=(320,240),mode="RGB")
  100. demo_i=0
  101. demo_j=0
  102. demo_m=0
  103. demo_n=0
  104. pressTime=0
  105. systemRunNum=0
  106. image.load_freetype("/root/preset/fonts/SourceHanSansCN-Regular.otf")
  107. def screenFirstPage():
  108. from maix import display,image,camera
  109. global splash,title_logo_text,btn_col,btn_row,splash_text,txt_unselected,txt_selected,btn_selected,btn_unselected,\
  110. splash_theme_color,btn_height,btn_width,main_title,menuSelect,gc_log,demo_i,demo_j,demo_m,demo_n,screenShow,img_drop,\
  111. frostedGlass
  112. gc_log()
  113. image.load_freetype("/root/preset/fonts/SourceHanSansCN-Regular.otf")
  114. splash.clear()
  115. splash.draw_rectangle(0,0,320,320,color=splash_theme_color,thickness=-1)
  116. splash.draw_image(title_logo_text, btn_col[0]+16, 10)
  117. x, y = image.get_string_size(splash_text[0], 1)
  118. splash.draw_string(btn_col[0]+16, 40, splash_text[19]+" "+splash_text[0], color=(0,255,255), thickness = 1)
  119. # splash.draw_string(btn_col[0], 60, splash_text[20], color=(0,255,255), thickness = 1)
  120. # splash.draw_string(btn_col[0], 80, splash_text[21], color=(0,255,255), thickness = 1)
  121. # splash.draw_string(btn_col[0], 60, splash_text[0], color=(0,255,255), thickness = 1)
  122. splash.draw_string(btn_col[0]+16, btn_row-105, splash_text[16], color=txt_unselected, thickness = 1)
  123. splash.draw_string(btn_col[0]+16, btn_row-85, splash_text[17], color=txt_unselected, thickness = 1)
  124. #splash.draw_string(btn_col[0], btn_row-105, splash_text[18], color=txt_unselected, thickness = 1)
  125. splash.draw_string(btn_col[0]+16, btn_row-65, splash_text[2], color=txt_unselected, thickness = 1)
  126. splash.draw_string(btn_col[0]+16, btn_row-45, splash_text[3], color=txt_unselected, thickness = 1)
  127. splash.draw_string(btn_col[0]+16, btn_row-25, splash_text[4], color=txt_unselected, thickness = 1)
  128. if menuSelect==0:
  129. splash.draw_rectangle(btn_col[1]+40, btn_row, btn_col[1]+btn_width+42, btn_row+btn_height, color=btn_selected, thickness=1)
  130. splash.draw_string(btn_col[1]+52, btn_row+7, main_title[2], color=txt_selected, thickness = 1)
  131. splash.draw_string(btn_col[1]+52, btn_row+27, main_title[3], color=txt_selected, thickness = 1)
  132. splash.draw_string(btn_col[1]+157, btn_row+27, main_title[5], color=txt_unselected, thickness = 1)
  133. splash.draw_rectangle(btn_col[0], btn_row, btn_col[0]+btn_width, btn_row+btn_height, color=btn_selected, thickness=1)
  134. splash.draw_string(btn_col[0]+96, btn_row+7, main_title[0], color=txt_selected, thickness = 1)
  135. splash.draw_string(btn_col[0]+68, btn_row+27, main_title[1], color=txt_selected, thickness = 1)
  136. splash.draw_string(btn_col[0]+7, btn_row+27, main_title[4], color=txt_unselected, thickness = 1)
  137. if menuSelect==1:
  138. splash.draw_rectangle(btn_col[0], btn_row, btn_col[0]+btn_width, btn_row+btn_height, color=btn_unselected, thickness=1)
  139. splash.draw_string(btn_col[0]+7, btn_row+7, main_title[0], color=txt_unselected, thickness = 1)
  140. splash.draw_string(btn_col[0]+7, btn_row+27, main_title[1], color=txt_unselected, thickness = 1)
  141. splash.draw_rectangle(btn_col[1], btn_row, btn_col[1]+btn_width, btn_row+btn_height, color=btn_selected, thickness=1)
  142. splash.draw_string(btn_col[1]+7, btn_row+7, main_title[2], color=txt_selected, thickness = 1)
  143. splash.draw_string(btn_col[1]+7, btn_row+27, main_title[3], color=txt_selected, thickness = 1)
  144. splash.draw_image(frostedGlass, 0, 0,alpha=0.2)
  145. #screenShow()
  146. display.show(splash)
  147. def screenSecondPage():
  148. from maix import display, image,camera
  149. global splash,title_logo_text,btn_col,btn_row,splash_text,txt_unselected,txt_selected,btn_selected,btn_unselected,\
  150. splash_theme_color,btn_height,btn_width,main_title,demoSelect,gc_log,color_splash_theme,color_blue,color_blue_lighter,\
  151. color_white,color_gray,color_orange,color_red,iconUpNp,iconUpP,iconDownNp,iconDownP,demoListText,screenShow,img_drop,\
  152. frostedGlass
  153. gc_log()
  154. image.load_freetype("/root/preset/fonts/SourceHanSansCN-Regular.otf")
  155. splash.clear()
  156. splash.draw_rectangle(0,0,320,240,color=splash_theme_color,thickness=-1)
  157. splash.draw_rectangle(0,0,320,32,color=color_blue,thickness=-1)
  158. splash.draw_image(iconUpNp, 3, 3)
  159. splash.draw_image(iconDownNp, 287, 3)
  160. splash.draw_rectangle(0,214,50,240,color=color_blue,thickness=-1)
  161. splash.draw_rectangle(270,214,320,240,color=color_blue,thickness=-1)
  162. splash.draw_string(12, 218, splash_text[10], color=(255,255,255), thickness = 1)
  163. splash.draw_string(282, 218, splash_text[12], color=(255,255,255), thickness = 1)
  164. demo_i=int(demoSelect/6)
  165. demo_j=demoSelect%6
  166. demo_m=int(demoNum/6)
  167. demo_n=demoNum%6
  168. for j in range(6):
  169. if demo_i==demo_m:
  170. if j>=demo_n:
  171. pass
  172. else:
  173. if j==demo_j:
  174. pass
  175. else:
  176. splash.draw_rectangle(0,33+30*j,319,33+30*(j+1),color=color_gray,thickness=1)
  177. splash.draw_string(8, 39+30*j,demoListText[j+demo_i*6] , color=color_gray, thickness = 1)
  178. else:
  179. if j==demo_j:
  180. pass
  181. else:
  182. splash.draw_rectangle(0,33+30*j,319,33+30*(j+1),color=color_gray,thickness=1)
  183. splash.draw_string(8, 39+30*j,demoListText[j+demo_i*6] , color=color_gray, thickness = 1)
  184. if demoSelect<13:
  185. splash.draw_string(72, 10, splash_text[22], color=(255,255,255), thickness = 1)
  186. elif demoSelect<23:
  187. splash.draw_string(72, 10, splash_text[23], color=(255,255,255), thickness = 1)
  188. else:
  189. splash.draw_string(72, 10, splash_text[24], color=(255,255,255), thickness = 1)
  190. splash.draw_rectangle(0,33+30*demo_j,319,33+30*(demo_j+1),color=color_white,thickness=1)
  191. splash.draw_string(8, 39+30*demo_j,demoListText[demoSelect] , color=color_white, thickness = 1)
  192. splash.draw_string(146, 218, str(demoSelect+1), color=(255,255,255), thickness = 1)
  193. splash.draw_string(160, 218, "/"+str(demoNum), color=(255,255,255), thickness = 1)
  194. splash.draw_image(frostedGlass, 0, 0,alpha=0.2)
  195. #screenShow()
  196. display.show(splash)
  197. def systemPage():
  198. from maix import display, image,camera
  199. import time
  200. global splash,title_logo_text,btn_col,btn_row,splash_text,txt_unselected,txt_selected,btn_selected,btn_unselected,\
  201. splash_theme_color,btn_height,btn_width,main_title,demoSelect,gc_log,color_splash_theme,color_blue,color_blue_lighter,\
  202. color_white,color_gray,color_orange,color_red,iconUpNp,iconUpP,iconDownNp,iconDownP,demoListText,screenShow,img_drop,\
  203. frostedGlass
  204. image.load_freetype("/root/preset/fonts/SourceHanSansCN-Regular.otf")
  205. for i in range(10):
  206. splash.draw_image(frostedGlass, 0, 0,alpha=0.2)
  207. #screenShow()
  208. display.show(splash)
  209. splash.draw_string(80, 68, splash_text[13], 1,color=(0,255,0), thickness = 1)
  210. splash.draw_string(80, 118, splash_text[14], 1,color=(255,255,0), thickness = 1)
  211. splash.draw_string(80, 168, splash_text[15], 1,color=(255,0,0), thickness = 1)
  212. #splash.draw_image(backUp, 20, 140)
  213. #screenShow()
  214. splash.draw_image((image.open("/root/preset/img/backup_00ff00_32x32.png")).rotate(0, adjust=0),40,60,alpha=1)
  215. splash.draw_image((image.open("/root/preset/img/reboot_ffff00_32x32.png")).rotate(0, adjust=0),40,110,alpha=1)
  216. splash.draw_image((image.open("/root/preset/img/poweroff_ff0000_32x32.png")).rotate(0, adjust=0),40,160,alpha=1)
  217. display.show(splash)
  218. pageVal=0
  219. menuSelect=0
  220. demoSelect=0
  221. execVal=0
  222. demoListText=(
  223. "相机",
  224. "相册",
  225. "颜色分析",
  226. "录音机",
  227. "音频播放器",
  228. "播放演示音乐",
  229. "播放演示视频",
  230. "秒表计时器",
  231. "系统时钟显示",
  232. "舵机操控",
  233. "电机操控",
  234. "图片轮播",
  235. "二维码扫描",
  236. "人形识别",
  237. "人脸检测",
  238. "人脸比对",
  239. "物体识别",
  240. "边缘检测",
  241. "手写数字识别",
  242. "车牌识别",
  243. "口罩识别",
  244. "手势识别",
  245. "猜拳识别",
  246. "扫码连接WIFI",
  247. "关于CocoPi",
  248. "无线相机",
  249. "网络天气",
  250. "网络时钟",
  251. "环境监测",
  252. "模型训练:数据集采集",
  253. "模型训练:模型部署",
  254. )
  255. demoNameList=(
  256. "01_camera",
  257. "02_photoViewer",
  258. "03_colorDetection",
  259. "04_recorder",
  260. "05_audioPlayer",
  261. "06_musicPlayer",
  262. "07_videoPlayer",
  263. "08_timeCounter",
  264. "09_localTimeClock",
  265. "10_servoControl",
  266. "11_motorControl",
  267. "12_pictureLoopPlay",
  268. "13_qrCodeScanner",
  269. "14_humanDetection",
  270. "15_faceDetection",
  271. "16_faceComparison",
  272. "17_objectRecognition",
  273. "18_edgeDetection",
  274. "19_handWrittenDigiRecognition",
  275. "20_carLicensePlateRecognition",
  276. "21_maskDetection",
  277. "22_gestureRecognition",
  278. "23_fingerGuessing",
  279. "24_connectWifi",
  280. "25_aboutCocoPi",
  281. "26_internetCamera",
  282. "27_internetWeatherPrediction",
  283. "28_internetTimeClock",
  284. "29_environmentDetection",
  285. "30_train_data_collect",
  286. "31_train_deploy_model",
  287. )
  288. demoNum=len(demoListText)
  289. pageSig=1
  290. selectSig=1
  291. def buttonDectect():
  292. from maix import display, image,camera
  293. import time
  294. import os
  295. global key_A,key_B,key_C,key_D,pageVal,menuSelect,execVal,demoNum,demoSelect,splash,iconUpP,iconDownP,screenShow,img_drop,systemRunNum,pageSig,selectSig
  296. if(pageVal==0):
  297. if(key_A.is_pressed()):
  298. pressTime=time.perf_counter()
  299. while key_A.is_pressed()==1:
  300. if(time.perf_counter()-pressTime)<5:
  301. time.sleep(0.001)
  302. pageVal=1
  303. else:
  304. pageVal=2
  305. break
  306. execVal=0
  307. elif(key_B.is_pressed()):
  308. while key_B.is_pressed()==1:
  309. time.sleep(0.001)
  310. execVal=1
  311. # elif (key_B.is_pressed()):
  312. # while key_B.is_pressed()==1:
  313. # time.sleep(0.001)
  314. # pageVal=1
  315. else:
  316. pass
  317. elif(pageVal==1):
  318. if execVal==0:
  319. if(key_D.is_pressed()):
  320. splash.draw_image(iconUpP, 3, 3)
  321. #screenShow()
  322. display.show(splash)
  323. while key_D.is_pressed()==1:
  324. time.sleep(0.001)
  325. demoSelect=demoSelect-1
  326. if(demoSelect==-1):
  327. demoSelect=demoNum-1
  328. elif (key_C.is_pressed()):
  329. splash.draw_image(iconDownP, 287, 3)
  330. #screenShow()
  331. display.show(splash)
  332. while key_C.is_pressed()==1:
  333. time.sleep(0.001)
  334. demoSelect=demoSelect+1
  335. if(demoSelect==demoNum):
  336. demoSelect=0
  337. elif (key_B.is_pressed()):
  338. while key_B.is_pressed()==1:
  339. time.sleep(0.001)
  340. execVal=2+demoSelect
  341. elif (key_A.is_pressed()):
  342. while key_A.is_pressed()==1:
  343. time.sleep(0.001)
  344. menuSelect=0
  345. pageVal=0
  346. demoSelect=0
  347. execVal=0
  348. try:
  349. with open( '/tmp/page.txt', 'w' ) as f:
  350. f.write(str(pageVal))
  351. except:
  352. pass
  353. try:
  354. with open( '/tmp/menu.txt', 'w' ) as f:
  355. f.write(str(demoSelect))
  356. except:
  357. pass
  358. pageSig=1
  359. selectSig=1
  360. else:
  361. pass
  362. else:
  363. if (key_A.is_pressed()):
  364. while key_A.is_pressed()==1:
  365. time.sleep(0.001)
  366. menuSelect=0
  367. pageVal=0
  368. demoSelect=0
  369. execVal=0
  370. systemRunNum=0
  371. elif pageVal==2:
  372. if(key_C.is_pressed()):
  373. pageVal=0
  374. menuSelect=0
  375. pageVal=0
  376. demoSelect=0
  377. execVal=0
  378. systemRunNum=0
  379. elif key_B.is_pressed():
  380. os.system("poweroff")
  381. elif key_D.is_pressed():
  382. os.system("reboot")
  383. else:
  384. pass
  385. def running():
  386. from maix import display, image,camera
  387. global splash,title_logo_text,btn_col,btn_row,splash_text,txt_unselected,txt_selected,btn_selected,btn_unselected,\
  388. splash_theme_color,btn_height,btn_width,main_title,menuSelect,pageVal,execVal,screenFirstPage,screenSecondPage,\
  389. color_splash_theme,color_blue,color_blue_lighter,color_white,color_gray,color_orange,color_red,demo_i,demo_j,\
  390. demo_m,demo_n,screenShow,img_drop,systemRunNum,systemPage,demoNameList,pageSig,selectSig,demoSelect
  391. if pageSig==1:
  392. try:
  393. with open( '/tmp/page.txt', 'r' ) as f:
  394. pageVal=int(f.read())
  395. print(pageVal)
  396. pageSig=0
  397. except:
  398. pass
  399. if selectSig==1:
  400. try:
  401. with open( '/tmp/menu.txt', 'r' ) as f:
  402. demoSelect=int(f.read())
  403. print(demoSelect)
  404. selectSig=0
  405. except:
  406. pass
  407. if(pageVal==0):
  408. screenFirstPage()
  409. if(execVal==0):
  410. pass
  411. #splash.draw_rectangle(btn_col[0], btn_row-26,btn_col[0]+220,26, color=splash_theme_color, thickness=1)
  412. #screenShow()
  413. #display.show(splash)
  414. else:
  415. try:
  416. splash.draw_string(btn_col[0], btn_row-25, splash_text[5], color=(0,255,255),thickness = 1)
  417. #screenShow()
  418. display.show(splash)
  419. time.sleep(0.5)
  420. os.system("ln -sf %s /tmp/event && touch /tmp/start" % "/root/user_latest_code.py")
  421. except BaseException as e:
  422. execVal=0
  423. print(str(e))
  424. splash.draw_rectangle(btn_col[0], btn_row-26,btn_col[0]+220,26, color=splash_theme_color, thickness=1)
  425. splash.draw_string(btn_col[0], btn_row-85, splash_text[9], color=(255,0,0), thickness = 1)
  426. #screenShow()
  427. display.show(splash)
  428. elif(pageVal==1):
  429. if(execVal==0):
  430. screenSecondPage()
  431. #print("show(demoSelect)")
  432. else:
  433. demo_i=int(demoSelect/6)
  434. demo_j=demoSelect%6
  435. demo_m=int(demoNum/6)
  436. demo_n=demoNum%6
  437. splash.draw_rectangle(0,33+30*demo_j,319,33+30*(demo_j+1),color=color_orange,thickness=-1)
  438. splash.draw_rectangle(0,33+30*demo_j,319,33+30*(demo_j+1),color=color_white,thickness=1)
  439. splash.draw_string(8, 39+30*demo_j,splash_text[11] , color=color_white, thickness = 1)
  440. #screenShow()
  441. display.show(splash)
  442. time.sleep(0.5)
  443. app_path_1='ln -sf /root/preset/app/'
  444. app_path_2='.py /tmp/event && touch /tmp/start'
  445. app_path=app_path_1+demoNameList[execVal-2]+app_path_2
  446. #print(app_path)
  447. try:
  448. with open( '/tmp/page.txt', 'w' ) as f:
  449. f.write(str(pageVal))
  450. except:
  451. pass
  452. try:
  453. with open( '/tmp/menu.txt', 'w' ) as f:
  454. f.write(str(demoSelect))
  455. except:
  456. pass
  457. pageSig=1
  458. selectSig=1
  459. os.system(app_path)
  460. else:
  461. if systemRunNum==0:
  462. systemPage()
  463. systemRunNum=systemRunNum+1
  464. else:
  465. pass
  466. def screenShow():
  467. from maix import image,display,camera
  468. global img_drop
  469. screenCapture=splash.crop(0,0,240,320)
  470. img_drop.draw_image((screenCapture.rotate(0,adjust=1)),0,0)
  471. #img_drop.draw_image((screenCapture.rotate(-90,adjust=1)),0,0)
  472. display.show(img_drop)
  473. try:
  474. with open( '/tmp/filesyStem.txt', 'w' ) as f:
  475. f.write("a")
  476. #os.system("rm /etc/wifi/wpa_supplicant.conf && touch /etc/wifi/wpa_supplicant.conf && sync && fsck.ext4 -p /dev/root && mount -o remount -o rw /dev/root && rm /etc/wifi/wpa_supplicant.conf && touch /etc/wifi/wpa_supplicant.conf && sync &")
  477. os.system("sync && rm /tmp/filesyStem.txt && sync")
  478. #os.system("fsck.ext4 -p /dev/root && mount -o remount -o rw /dev/root")
  479. #os.system("rm /etc/wifi/wpa_supplicant.conf && touch /etc/wifi/wpa_supplicant.conf && sync")
  480. except:
  481. os.system("fsck.ext4 -p /dev/root && mount -o remount -o rw /dev/root")
  482. try:
  483. os.popen("cd /root/preset/server/ && python wirelessServer.py &")
  484. except:
  485. pass
  486. while True:
  487. buttonDectect()
  488. running()
  489. del key_A
  490. del key_B
  491. del key_C
  492. del key_D