在上一贴中已经实现了页面的基本控制,在这里我们需要实现按键的采集和页面的前期规划,本次实现的是电子测光表,上一贴实现的可以把其命名为页面1,内容就是欢迎页面,应该还需要一个正式测试界面和一个设置界面,在设置界面还应该具有菜单选择。按键方面我们一共有5个(包括外扩的按键模块)。
我们定义一下按键功能:
D2 ——上键;
D1 —— 设置键;
D3 —— 下键;
蓝键 ——确认键;
红键 ——取消键;
按键的采集通过循环扫描的方式进行,D0和D1、D2的连接方式不是痛的,D1需要上拉,D1D2需要下来,模块按键原理图如下:
这两个按键也需要配置上拉,初始化代码如下:import digitalio import board buttonRedTime_cnt = 0 buttonRedDown_flag = 0 buttonBlueTime_cnt = 0 buttonBlueDown_flag = 0 buttonD0_Timecnt = 0 buttonD0Down_flag = 0 buttonD1_Timecnt = 0 buttonD1Down_flag = 0 buttonD2_Timecnt = 0 buttonD2Down_flag = 0 buttonBlue = digitalio.DigitalInOut(board.D12) buttonBlue.switch_to_input(pull=digitalio.Pull.UP) buttonRed = digitalio.DigitalInOut(board.D13) buttonRed.switch_to_input(pull=digitalio.Pull.UP) buttonD0= digitalio.DigitalInOut(board.D0) buttonD0.switch_to_input(pull=digitalio.Pull.UP) buttonD1 = digitalio.DigitalInOut(board.D1) buttonD1.switch_to_input(pull=digitalio.Pull.DOWN) buttonD2 = digitalio.DigitalInOut(board.D2) buttonD2.switch_to_input(pull=digitalio.Pull.DOWN) def Sub_Hey_Collect(): global buttonRedTime_cnt global buttonRedDown_flag global buttonBlueTime_cnt global buttonBlueDown_flag global buttonD0_Timecnt global buttonD0Down_flag global buttonD1_Timecnt global buttonD1Down_flag global buttonD2_Timecnt global buttonD2Down_flag # 红键采集 if not buttonRed.value: if not buttonRedDown_flag: buttonRedTime_cnt += 1 else: if buttonRedTime_cnt > 15: buttonRedDown_flag = 1 buttonRedTime_cnt = 0 # 蓝键采集 if not buttonBlue.value: if not buttonBlueDown_flag: buttonBlueTime_cnt += 1 else: if buttonBlueTime_cnt > 15: buttonBlueDown_flag = 1 buttonBlueTime_cnt = 0 # D0键采集 if not buttonD0.value: if not buttonD0Down_flag: buttonD0_Timecnt += 1 else: if buttonD0_Timecnt > 15: buttonD0Down_flag = 1 buttonD0_Timecnt = 0 # D1键采集 if buttonD1.value: if not buttonD1Down_flag: buttonD1_Timecnt += 1 else: if buttonD1_Timecnt > 15: buttonD1Down_flag = 1 buttonD1_Timecnt = 0 # D2键采集 if buttonD2.value: if not buttonD2Down_flag: buttonD2_Timecnt += 1 else: if buttonD2_Timecnt > 15: buttonD2Down_flag = 1 buttonD2_Timecnt = 0 def Sub_Hey_deal(): global buttonRedDown_flag global buttonBlueDown_flag global buttonD0Down_flag global buttonD1Down_flag global buttonD2Down_flag if buttonD0Down_flag: buttonD0Down_flag = 0 print("buttonD0Down") if buttonD1Down_flag: buttonD1Down_flag = 0 print("buttonD1Down") if buttonD2Down_flag: buttonD2Down_flag = 0 print("buttonD2Down") if buttonRedDown_flag: buttonRedDown_flag = 0 print("buttonRedDown") if buttonBlueDown_flag: buttonBlueDown_flag = 0 print("buttonBlueDown")
定义了两个函数,一个是采集函数Sub_Hey_Collect(),一个是处理函数Sub_Hey_deal(),后续的操作就可以在处理函数里面添加就可以,效果如下: