main.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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/simhei.ttf")
  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/simhei.ttf")
  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/simhei.ttf")
  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/simhei.ttf")
  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. "检查更新",
  256. "撤销更新",
  257. )
  258. demoNameList=(
  259. "01_camera",
  260. "02_photoViewer",
  261. "03_colorDetection",
  262. "04_recorder",
  263. "05_audioPlayer",
  264. "06_musicPlayer",
  265. "07_videoPlayer",
  266. "08_timeCounter",
  267. "09_localTimeClock",
  268. "10_servoControl",
  269. "11_motorControl",
  270. "12_pictureLoopPlay",
  271. "13_qrCodeScanner",
  272. "14_humanDetection",
  273. "15_faceDetection",
  274. "16_faceComparison",
  275. "17_objectRecognition",
  276. "18_edgeDetection",
  277. "19_handWrittenDigiRecognition",
  278. "20_carLicensePlateRecognition",
  279. "21_maskDetection",
  280. "22_gestureRecognition",
  281. "23_fingerGuessing",
  282. "24_connectWifi",
  283. "25_aboutCocoPi",
  284. "26_internetCamera",
  285. "27_internetWeatherPrediction",
  286. "28_internetTimeClock",
  287. "29_environmentDetection",
  288. "30_train_data_collect",
  289. "31_train_deploy_model",
  290. "checkout",
  291. "pull",
  292. "reset",
  293. )
  294. demoNum=len(demoListText)
  295. pageSig=1
  296. selectSig=1
  297. def buttonDectect():
  298. from maix import display, image,camera
  299. import time
  300. import os
  301. global key_A,key_B,key_C,key_D,pageVal,menuSelect,execVal,demoNum,demoSelect,splash,iconUpP,iconDownP,screenShow,img_drop,systemRunNum,pageSig,selectSig
  302. if(pageVal==0):
  303. if(key_A.is_pressed()):
  304. pressTime=time.perf_counter()
  305. while key_A.is_pressed()==1:
  306. if(time.perf_counter()-pressTime)<5:
  307. time.sleep(0.001)
  308. pageVal=1
  309. else:
  310. pageVal=2
  311. break
  312. execVal=0
  313. elif(key_B.is_pressed()):
  314. while key_B.is_pressed()==1:
  315. time.sleep(0.001)
  316. execVal=1
  317. # elif (key_B.is_pressed()):
  318. # while key_B.is_pressed()==1:
  319. # time.sleep(0.001)
  320. # pageVal=1
  321. else:
  322. pass
  323. elif(pageVal==1):
  324. if execVal==0:
  325. if(key_D.is_pressed()):
  326. splash.draw_image(iconUpP, 3, 3)
  327. #screenShow()
  328. display.show(splash)
  329. while key_D.is_pressed()==1:
  330. time.sleep(0.001)
  331. demoSelect=demoSelect-1
  332. if(demoSelect==-1):
  333. demoSelect=demoNum-1
  334. elif (key_C.is_pressed()):
  335. splash.draw_image(iconDownP, 287, 3)
  336. #screenShow()
  337. display.show(splash)
  338. while key_C.is_pressed()==1:
  339. time.sleep(0.001)
  340. demoSelect=demoSelect+1
  341. if(demoSelect==demoNum):
  342. demoSelect=0
  343. elif (key_B.is_pressed()):
  344. while key_B.is_pressed()==1:
  345. time.sleep(0.001)
  346. execVal=2+demoSelect
  347. elif (key_A.is_pressed()):
  348. while key_A.is_pressed()==1:
  349. time.sleep(0.001)
  350. menuSelect=0
  351. pageVal=0
  352. demoSelect=0
  353. execVal=0
  354. try:
  355. with open( '/tmp/page.txt', 'w' ) as f:
  356. f.write(str(pageVal))
  357. except:
  358. pass
  359. try:
  360. with open( '/tmp/menu.txt', 'w' ) as f:
  361. f.write(str(demoSelect))
  362. except:
  363. pass
  364. pageSig=1
  365. selectSig=1
  366. else:
  367. pass
  368. else:
  369. if (key_A.is_pressed()):
  370. while key_A.is_pressed()==1:
  371. time.sleep(0.001)
  372. menuSelect=0
  373. pageVal=0
  374. demoSelect=0
  375. execVal=0
  376. systemRunNum=0
  377. elif pageVal==2:
  378. if(key_C.is_pressed()):
  379. pageVal=0
  380. menuSelect=0
  381. pageVal=0
  382. demoSelect=0
  383. execVal=0
  384. systemRunNum=0
  385. elif key_B.is_pressed():
  386. os.system("poweroff")
  387. elif key_D.is_pressed():
  388. os.system("reboot")
  389. else:
  390. pass
  391. def running():
  392. from maix import display, image,camera
  393. global splash,title_logo_text,btn_col,btn_row,splash_text,txt_unselected,txt_selected,btn_selected,btn_unselected,\
  394. splash_theme_color,btn_height,btn_width,main_title,menuSelect,pageVal,execVal,screenFirstPage,screenSecondPage,\
  395. color_splash_theme,color_blue,color_blue_lighter,color_white,color_gray,color_orange,color_red,demo_i,demo_j,\
  396. demo_m,demo_n,screenShow,img_drop,systemRunNum,systemPage,demoNameList,pageSig,selectSig,demoSelect
  397. if pageSig==1:
  398. try:
  399. with open( '/tmp/page.txt', 'r' ) as f:
  400. pageVal=int(f.read())
  401. print(pageVal)
  402. pageSig=0
  403. except:
  404. pass
  405. if selectSig==1:
  406. try:
  407. with open( '/tmp/menu.txt', 'r' ) as f:
  408. demoSelect=int(f.read())
  409. print(demoSelect)
  410. selectSig=0
  411. except:
  412. pass
  413. if(pageVal==0):
  414. screenFirstPage()
  415. if(execVal==0):
  416. pass
  417. #splash.draw_rectangle(btn_col[0], btn_row-26,btn_col[0]+220,26, color=splash_theme_color, thickness=1)
  418. #screenShow()
  419. #display.show(splash)
  420. else:
  421. try:
  422. splash.draw_string(btn_col[0], btn_row-25, splash_text[5], color=(0,255,255),thickness = 1)
  423. #screenShow()
  424. display.show(splash)
  425. time.sleep(0.5)
  426. os.system("ln -sf %s /tmp/event && touch /tmp/start" % "/root/user_latest_code.py")
  427. except BaseException as e:
  428. execVal=0
  429. print(str(e))
  430. splash.draw_rectangle(btn_col[0], btn_row-26,btn_col[0]+220,26, color=splash_theme_color, thickness=1)
  431. splash.draw_string(btn_col[0], btn_row-85, splash_text[9], color=(255,0,0), thickness = 1)
  432. #screenShow()
  433. display.show(splash)
  434. elif(pageVal==1):
  435. if(execVal==0):
  436. screenSecondPage()
  437. #print("show(demoSelect)")
  438. else:
  439. demo_i=int(demoSelect/6)
  440. demo_j=demoSelect%6
  441. demo_m=int(demoNum/6)
  442. demo_n=demoNum%6
  443. splash.draw_rectangle(0,33+30*demo_j,319,33+30*(demo_j+1),color=color_orange,thickness=-1)
  444. splash.draw_rectangle(0,33+30*demo_j,319,33+30*(demo_j+1),color=color_white,thickness=1)
  445. splash.draw_string(8, 39+30*demo_j,splash_text[11] , color=color_white, thickness = 1)
  446. #screenShow()
  447. display.show(splash)
  448. time.sleep(0.5)
  449. app_path_1='ln -sf /root/preset/app/'
  450. app_path_2='.py /tmp/event && touch /tmp/start'
  451. app_path=app_path_1+demoNameList[execVal-2]+app_path_2
  452. #print(app_path)
  453. try:
  454. with open( '/tmp/page.txt', 'w' ) as f:
  455. f.write(str(pageVal))
  456. except:
  457. pass
  458. try:
  459. with open( '/tmp/menu.txt', 'w' ) as f:
  460. f.write(str(demoSelect))
  461. except:
  462. pass
  463. pageSig=1
  464. selectSig=1
  465. os.system(app_path)
  466. else:
  467. if systemRunNum==0:
  468. systemPage()
  469. systemRunNum=systemRunNum+1
  470. else:
  471. pass
  472. def screenShow():
  473. from maix import image,display,camera
  474. global img_drop
  475. screenCapture=splash.crop(0,0,240,320)
  476. img_drop.draw_image((screenCapture.rotate(0,adjust=1)),0,0)
  477. #img_drop.draw_image((screenCapture.rotate(-90,adjust=1)),0,0)
  478. display.show(img_drop)
  479. try:
  480. with open( '/tmp/filesyStem.txt', 'w' ) as f:
  481. f.write("a")
  482. #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 &")
  483. os.system("sync && rm /tmp/filesyStem.txt && sync")
  484. #os.system("fsck.ext4 -p /dev/root && mount -o remount -o rw /dev/root")
  485. #os.system("rm /etc/wifi/wpa_supplicant.conf && touch /etc/wifi/wpa_supplicant.conf && sync")
  486. except:
  487. os.system("fsck.ext4 -p /dev/root && mount -o remount -o rw /dev/root")
  488. try:
  489. os.popen("cd /root/preset/server/ && python wirelessServer.py &")
  490. except:
  491. pass
  492. while True:
  493. buttonDectect()
  494. running()
  495. del key_A
  496. del key_B
  497. del key_C
  498. del key_D