6955| 1
|
[入门教程] 掌控板功能展示菜单制作 |
初衷:通过菜单调用展示掌控板的基本硬件功能。 掌控板载传感器及相应执行器如下:
设计功能菜单项目有12个,列表如下:
掌控板oled液晶屏无法一次展示所有项目内容,故需分页展示。 考虑人机关系特点,设计掌控板菜单按键组合如下:
编写烧录程序后效果如图所示: 相关程序代码如下所示(各位同仁可以根据兴趣自行添加修改菜单项目): [mw_shl_code=python,true]from machine import Pin, ADC, PWM, I2C, SPI, Timer, TouchPad from mpython import * import time,os,random def clearScreen(): display.fill(0) display.show() def f01(): #"显示环境声音" clearScreen() display.DispChar(menuitems[0],0,0) def f02(): #"显示环境光线" clearScreen() display.DispChar(menuitems[1],0,0) def f03(): #"显示三轴加速度计各轴值" clearScreen() display.DispChar(menuitems[2],0,0) def f04(): #"显示物理按键值" clearScreen() display.DispChar(menuitems[3],0,0) def f05(): #"显示触摸按键值" clearScreen() display.DispChar(menuitems[4],0,0) def f06(): #"无源蜂鸣器奏乐" clearScreen() display.DispChar(menuitems[5],0,0) song = [1, 2, 3, 1, 1, 2, 3, 1, 3, 4, 5, 3, 4, 5] delaytime = [250, 250, 250, 250, 250, 250,250, 250, 250, 250, 500, 250, 250, 500] m = [525, 589, 661, 700, 786, 882, 988] display.DispChar("触摸H结束当前功能展示",0,48) display.show() isContinue = True while isContinue: buzz = PWM(Pin(16), freq=500, duty=0) jp="" display.fill_rect(0,28,128,16,0) for i in range(14): buzz.freq(m[song]) #调用数组频率(嵌套音符数组) #buzz.pwm.freq(m[song]) #调用数组频率(嵌套音符数组) buzz.duty(512) #音量 512 jp=jp+str(song) display.DispChar(jp,14,28) display.show() time.sleep_ms(delaytime) #时长 250 毫秒 isContinue=touchPad_H.read()>100 if not isContinue: buzz.duty(0) break def f07(): #"oled随机显示点线圆" clearScreen() display.DispChar(menuitems[6],0,0) def f08(): #"oled显示你好世界" clearScreen() display.DispChar(menuitems[7],0,0) def f09(): #"变色灯" clearScreen() display.DispChar(menuitems[8],0,0) def f10(): #"流水灯" clearScreen() display.DispChar(menuitems[9],0,0) def f11(): #"呼吸灯" clearScreen() display.DispChar(menuitems[10],0,0) def f12(): #"内部文件系统" clearScreen() display.DispChar(menuitems[11],0,0) BTNA = Pin(0, mode=Pin.OPEN_DRAIN,pull=Pin.PULL_UP,value=1) BTNB = Pin(2, mode=Pin.OPEN_DRAIN,pull=Pin.PULL_UP,value=1) touchPad_P = TouchPad(Pin(27)) touchPad_Y = TouchPad(Pin(14)) touchPad_T = TouchPad(Pin(12)) touchPad_H = TouchPad(Pin(13)) touchPad_O = TouchPad(Pin(15)) touchPad_N = TouchPad(Pin(4)) display.fill(0) display.show() display.DispChar("功能选择按键A下B上",0,0) display.DispChar("功能翻页触摸Y下O上",0,16) display.DispChar("全局开始触摸P结束N",0,32) display.DispChar("当前功能触摸T结束H",0,48) display.show() while True: if touchPad_P.read() < 100: break #触摸P开始各种功能尝试 menuitems=["显示环境声音","显示环境光线","显示三轴加速度计各轴值","显示物理按键值","显示触摸按键值","无源蜂鸣器奏乐","oled随机显示点线圆","oled显示你好世界","变色灯","流水灯","呼吸灯","内部文件系统"] menuselectedchar="★" menuselectednumthstart=1 menuselectednumth=menuselectednumthstart menuselectednumth_before=0 menuselectednumthend=12 numperpage=4 pagenumth=1 isTrue=True while isTrue: if touchPad_N.read() < 100: #全局退出 isTrue=False if touchPad_T.read() < 100: #当前功能开始 print(str(touchPad_T.read())+"///menuselectednumth:"+str(menuselectednumth)) menuselectednumth_before=0 #当前功能结束后可以重新绘制菜单 if menuselectednumth==1: f01() elif menuselectednumth==2: f02() elif menuselectednumth==3: f03() elif menuselectednumth==4: f04() elif menuselectednumth==5: f05() elif menuselectednumth==6: f06() elif menuselectednumth==7: f07() elif menuselectednumth==8: f08() elif menuselectednumth==9: f09() elif menuselectednumth==10: f10() elif menuselectednumth==11: f11() elif menuselectednumth==12: f12() #按键AB处理menuselectedID开始 if BTNA.value() == 0 and BTNB.value() == 1 : menuselectednumth=menuselectednumth+1 elif BTNA.value() == 1 and BTNB.value() == 0 : menuselectednumth=menuselectednumth-1 #按键AB处理menuselectedID结束 #触摸YO处理menuselectedID开始,触摸Y功能翻页下移,触摸O功能翻页上移 if touchPad_Y.read() < 100: if menuselectednumth/numperpage==menuselectednumth//numperpage: menuselectednumth=menuselectednumth+1 else: menuselectednumth=(menuselectednumth//numperpage+1)*numperpage+1 elif touchPad_O.read() < 100: if menuselectednumth/numperpage==menuselectednumth//numperpage: menuselectednumth=menuselectednumth-numperpage else: menuselectednumth=(menuselectednumth//numperpage-1)*numperpage+1 #触摸YO处理menuselectedID结束 #处理过界开始 if menuselectednumth<menuselectednumthstart: menuselectednumth=menuselectednumthstart elif menuselectednumth>menuselectednumthend: menuselectednumth=menuselectednumthend #处理过界结束 #显示开始 if menuselectednumth_before!=menuselectednumth: display.fill(0) display.show() pagenumth=(menuselectednumth-1)//numperpage+1 print("menuselectednumth:"+str(menuselectednumth)+"///pagenumth:"+str(pagenumth)) for menuid in range(menuselectednumthend): if (menuid+1)>=(pagenumth-1)*numperpage+1 and (menuid+1)<=pagenumth*numperpage: if (menuid+1)%numperpage==0: if (menuid+1)==menuselectednumth: display.DispChar(menuselectedchar,0,(numperpage-1)*16) display.DispChar(menuitems[menuid],10,(numperpage-1)*16) else: if (menuid+1)==menuselectednumth: display.DispChar(menuselectedchar,0,((menuid+1)%numperpage-1)*16) display.DispChar(menuitems[menuid],10,((menuid+1)%numperpage-1)*16) menuselectednumth_before=menuselectednumth display.show() #显示结束 time.sleep_ms(200) else: display.fill(0) display.DispChar("end.",56,28) display.show() [/mw_shl_code] |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed