按键检测做完了,下一步就是打通环境光传感器和屏幕显示。
在开箱中有提到规格书好像是5V工作电压,但芯片规格又写的是3.3V的工作电压。后来找到了环境光传感器的原理图,发现实际上设计上的工作电压还是3.3V,不知道为何外部丝印会写个5V。
硬件接线
其中,INT脚有些无厘头,BH1750并没有接此接口,但端子上留了一个int功能的管脚,实际上并未接到任何地方去,个人只能猜测可能是M5搞了端口标准化,即使没有,也留个接口放那。
软件编码
按键检测部分
过程贴已给出,后续有改成keypad框架,但是三个按键上下拉不一致的情况下,容易出现D1按键乱跳问题,由于无法看到keypad底层代码,无法解决这个坑,因此后续又改回了自己实现的按键解析
import time import board import terminalio import displayio import adafruit_bh1750 import math from digitalio import DigitalInOut, Direction, Pull from adafruit_display_text import label from adafruit_bh1750 import Resolution #keypad KEY_NULL = 0 KEY_D0 = 1 KEY_D1 = 2 KEY_D2 = 4 key0 = DigitalInOut(board.BOOT0) key1 = DigitalInOut(board.D1) key2 = DigitalInOut(board.D2) def keyInit(): key0.direction = Direction.INPUT key0.pull = Pull.UP key1.direction = Direction.INPUT key1.pull = Pull.DOWN key2.direction = Direction.INPUT key2.pull = Pull.DOWN def keyDetect(): key_value = KEY_NULL if key0.value == 0: key_value +=KEY_D0 if key1.value: key_value +=KEY_D1 if key2.value: key_value +=KEY_D2 return key_value def isKeyPressed(): key_value = keyDetect() if key_value: time.sleep(0.01) if key_value == keyDetect(): return key_value return 0
功能菜单部分
# display BORDER = 20 TEXT_HIGH = 26 FONTSCALE = 2 BACKGROUND_COLOR = 0x00FF00 # Bright Green FOREGROUND_COLOR = 0xAA0088 # Purple TEXT_COLOR = 0xFFFF00 # Yellow TEXT_COLOR_RED = 0xFF0000 # Red TEXT_COLOR_WHITE = 0xFFFFFF # White def showFunctionMenu(index: int): display = board.DISPLAY splash = displayio.Group() display.root_group = splash text_line0 = "Hello EEPW & DigiKey" if index == 0: text_line0_area = label.Label(terminalio.FONT, text=text_line0, background_color=TEXT_COLOR_WHITE, color=TEXT_COLOR_RED) else: text_line0_area = label.Label(terminalio.FONT, text=text_line0, color=TEXT_COLOR) text_line0_group = displayio.Group( scale=FONTSCALE, x=0, y=TEXT_HIGH // 2 ) text_line0_group.append(text_line0_area) splash.append(text_line0_group) text_line1 = "Lux Control" if index == 1: text_line1_area = label.Label(terminalio.FONT, text=text_line1, background_color=TEXT_COLOR_WHITE, color=TEXT_COLOR_RED) else: text_line1_area = label.Label(terminalio.FONT, text=text_line1, color=TEXT_COLOR) text_line1_group = displayio.Group( scale=FONTSCALE, x=0, y=TEXT_HIGH * 2 - TEXT_HIGH // 2 ) text_line1_group.append(text_line1_area) splash.append(text_line1_group) text_line2 = "ISO" if index == 2: text_line2_area = label.Label(terminalio.FONT, text=text_line2, background_color=TEXT_COLOR_WHITE, color=TEXT_COLOR_RED) else: text_line2_area = label.Label(terminalio.FONT, text=text_line2, color=TEXT_COLOR) text_line2_group = displayio.Group( scale=FONTSCALE, x=0, y=TEXT_HIGH * 3 - TEXT_HIGH // 2 ) text_line2_group.append(text_line2_area) splash.append(text_line2_group) # Main function key_value = 0 old_key_value = key_value i = 0 keyInit() showFunctionMenu(i) while True: key_value=isKeyPressed() if old_key_value != key_value: old_key_value = key_value if key_value == KEY_D0: if i == 0: showEEPWHdl(key_value) elif i == 1: luxHdl(key_value) elif i == 2: cameraInfoHdl(key_value) elif key_value == KEY_D1: if i == 0: i = 2 else: i -= 1 elif key_value == KEY_D2: if i == 2: i = 0 else: i += 1 showFunctionMenu(i)
功能菜单就简单做了三个功能的选择,通过D1 和 D2键切换
显示logo部分
logo部分对应基础任务三
def showHelloEEPW(): display = board.DISPLAY splash = displayio.Group() display.root_group = splash color_palette = displayio.Palette(1) color_bitmap = displayio.Bitmap(display.width, display.height, 1) color_palette[0] = BACKGROUND_COLOR bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0) splash.append(bg_sprite) inner_bitmap = displayio.Bitmap( display.width, display.height - BORDER * 2, 1 ) inner_palette = displayio.Palette(1) inner_palette[0] = FOREGROUND_COLOR inner_sprite = displayio.TileGrid( inner_bitmap, pixel_shader=inner_palette, x=0, y=BORDER ) splash.append(inner_sprite) text = "Hello EEPW & DigiKey" text_area = label.Label(terminalio.FONT, text=text, color=TEXT_COLOR) text_width = text_area.bounding_box[2] * FONTSCALE text_group = displayio.Group( scale=FONTSCALE, x=display.width // 2 - text_width // 2, y=display.height // 2 ) text_group.append(text_area) splash.append(text_group) def showEEPWHdl(old_key_value : int): key_value = old_key_value showHelloEEPW() while True: key_value=isKeyPressed() if old_key_value != key_value: old_key_value = key_value if key_value == KEY_D0: break
环境光检测部分
环境光检测模式部分
此部分对应基础部分四和进阶部分一,按D1切换环境光检测模式,按D0返回上一级菜单
BH1750驱动修改
修改arafruit_bh1750.py中的BH1750类,添加以下实现
def set_mode(self, resolution: Resolution ) -> None: self.resolution = resolution
copy.py修改
def showLux(mode : int): display = board.DISPLAY splash = displayio.Group() display.root_group = splash if mode == 0: text_mode = "Mode High" elif mode == 1: text_mode = "Mode Low" elif mode == 2: text_mode = "Mode Middle" text_mode_area = label.Label(terminalio.FONT, text=text_mode, color=TEXT_COLOR) text_mode_group = displayio.Group( scale=FONTSCALE, x=0, y=display.height // 4 ) text_mode_group.append(text_mode_area) splash.append(text_mode_group) text_lux = ("%0.2f" % sensor.lux) + "Lux" text_lux_area = label.Label(terminalio.FONT, text=text_lux, color=TEXT_COLOR) text_lux_group = displayio.Group( scale=FONTSCALE, x=0, y=display.height // 2 ) text_lux_group.append(text_lux_area) splash.append(text_lux_group) def luxHdl(old_key_value : int): key_value = old_key_value i = 0 showLux(i) while True: key_value=isKeyPressed() if old_key_value != key_value: old_key_value = key_value if key_value == KEY_D0: break elif key_value == KEY_D1: i += 1 if i == 1: sensor.set_mode(Resolution.LOW) elif i == 2: sensor.set_mode(Resolution.MID) elif i == 3: sensor.set_mode(Resolution.HIGH) i = 0 showLux(i)
曝光关系调整部分
前面坛子有人提供了最关键的ISO100和F T之间的转换关系表,因此仅需要按照此关系表实现cpy代码逻辑
# BH1750 i2c = board.I2C() sensor = adafruit_bh1750.BH1750(i2c) AV = [ '1', '1.4', '2', '2.8', '4', '5.6', '8', '11', '16', '22', '32' ] TV = [ '1', '1/2', '1/4', '1/8', '1/15', '1/30', '1/60', '1/125', '1/250', '1/500', '1/1000' ] SV = [ '100 ', '200 ', '400 ', '800 ', '1600' ] def showCameraInfo(svIdx : int, avIdx : int): lux = sensor.lux ev = math.floor(2 + math.log(lux / 10) / math.log(2)) display = board.DISPLAY splash = displayio.Group() display.root_group = splash text_Line0 = "LUX: " + ("%0.2f" % lux) text_Line0_area = label.Label(terminalio.FONT, text=text_Line0, color=TEXT_COLOR) text_Line0_group = displayio.Group( scale=FONTSCALE, x=0, y=TEXT_HIGH // 2 ) text_Line0_group.append(text_Line0_area) splash.append(text_Line0_group) text_Line1 = "EV: " + str(ev) text_Line1_area = label.Label(terminalio.FONT, text=text_Line1, color=TEXT_COLOR) text_Line1_group = displayio.Group( scale=FONTSCALE, x=0, y=TEXT_HIGH * 2 - TEXT_HIGH // 2 ) text_Line1_group.append(text_Line1_area) splash.append(text_Line1_group) text_Line2 = "F: " + AV[avIdx] text_Line2_area = label.Label(terminalio.FONT, text=text_Line2, color=TEXT_COLOR) text_Line2_group = displayio.Group( scale=FONTSCALE, x=0, y=TEXT_HIGH * 3 - TEXT_HIGH // 2 ) text_Line2_group.append(text_Line2_area) splash.append(text_Line2_group) text_Line3 = "ISO: " + SV[svIdx] text_Line3_area = label.Label(terminalio.FONT, text=text_Line3, color=TEXT_COLOR) text_Line3_group = displayio.Group( scale=FONTSCALE, x=0, y=TEXT_HIGH * 4 - TEXT_HIGH // 2 ) text_Line3_group.append(text_Line3_area) splash.append(text_Line3_group) text_Line4 = "S: " + TV[ev + svIdx - avIdx] text_Line4_area = label.Label(terminalio.FONT, text=text_Line4, color=TEXT_COLOR) text_Line4_group = displayio.Group( scale=FONTSCALE, x=0, y=TEXT_HIGH * 5 - TEXT_HIGH // 2 ) text_Line4_group.append(text_Line4_area) splash.append(text_Line4_group) def cameraInfoHdl(old_key_value : int): key_value = old_key_value svIdx = 0 avIdx = 0 showCameraInfo(svIdx, avIdx) while True: key_value=isKeyPressed() if old_key_value != key_value: old_key_value = key_value if key_value == KEY_D0: break elif key_value == KEY_D1: svIdx += 1 if len(SV) <= svIdx : svIdx = 0 showCameraInfo(svIdx, avIdx) elif key_value == KEY_D2: avIdx +=1 if avIdx >= len(AV): avIdx = 0 showCameraInfo(svIdx, avIdx)
蓝牙BLE通信部份
circuitpython固件集成的库之中未找到ble相关的库,官方提供的第三方库也没能用的ble实现,目前看cpy走不通,只能等后面弄其他语言时再去看
运行效果
上电后显示功能菜单,按D1和D2选择功能项,按D0进入功能项。
进入亮度检测模式后,按D1键切换亮度检测精度,D2键刷新亮度,D0键返回上一级
进入logo界面,按D0键返回上一级
进入ISO界面,按D2切换光圈大小,D1键切换ISO,D0键退出
总结
import的部分模块内部看内部接口无法跳转很难受,只能边写边看文档一点一点的抠,效率不算高。
另外,不支持多线程,不支持gpio中断处理,有些限制cpy的使用了,愣是把程序变成了单线程状态机处理了。