3826浏览
查看: 3826|回复: 29

[MP动手做] MicroPython动手做(15)——掌控板之AB按键

[复制链接]
1、按键开关
主要是指轻触式按键开关,也称之为轻触开关。按键开关是一种电子开关,属于电子元器件类,最早出现在日本[称之为:敏感型开关],使用时以满足操作力的条件向开关操作方向施压开关功能闭合接通,当撤销压力时开关即断开,其内部结构是靠金属弹片受力变化来实现通断的。按键开关由嵌件、基座、弹片、按钮、盖板组成,其中防水类轻触开关在弹片上加一层聚酰亚胺薄膜。

按键开关有接触电阻荷小、精确的操作力误差、规格多样化等方面的优势,在电子设备及白色家电等方面得到广泛的应用如:影音产品、数码产品、遥控器、通讯产品、家用电器、安防产品、玩具、电脑产品、健身器材、医疗器材、验钞笔、雷射笔按键等等。因为按键开关对环境的条件(施压力小于2倍的弹力/环境温湿度条件以及电气性能)大型设备及高负荷的按钮都使用导电橡胶或锅仔开关五金弹片直接来代替,比如医疗器材、电视机遥控器等。

关于五脚按键开关的脚位问题:两个引脚为一组,向开关体正确施压时四个引脚相导通,第五个引脚为接地作用。

按键开关是随着电子技术发展的要求而开发的第四代开关产品,最早的体积为12mm*12mm,8mm*8mm两种,现在为6mm*6mm。产品结构有立式、卧式和卧式带地端三种,现在又有组合式(3M、4M、SM、6M、SM)和电位器按键开关组合两类,满足国内各种电子产品要求。安装尺寸有6.5mm*4.5mm,5.5mm*4mm和6mm*4mm三种。国外已有4.5mm*4.5mm小型按键开关和片式按键开关,片式按键开关适合于表面组装。

现在已有第五代开关—薄膜开关,功能与按键开关相同,主要用于电子仪器和数控机床,但电阻大、手感差。为了克服手感差的现象,也有在薄膜开关内不是用银层做接触点,而是装上接触簧片。

按键开关分成两大类:利用金属簧片作为开关接触片的称按键开关,接触电阻小,手感好,有“滴答”清脆声。利用导电橡胶作为接触通路的开关习惯称为导电橡胶开关。开关手感好,但接触电阻大,一般在100一300n。按键开关的结构是靠按键向下移动,使接触簧片或导电橡胶块接触焊片,形成通路。

按键开关的操作力与簧片所处的状态有关。开始力与簧片的压缩的距离成正比当簧片压缩到5%一70%时操作力突然减少,并伴有“滴答”声。导龟橡胶开关一般有两种结构。操作力曲线随橡胶块的几何形状不同而有很大的差异。

相对湿度:<95%
额定电压:12V
额定电流:50mA
温度:-25~70℃

按键开关的主要用途有彩色电视机、黑白电视机、音响设备、录像机、摄像机、计算机、游戏机、传真机、对讲机、警棍、机床控制装置、复印、打印机、电子仪器、仪表和其它家用电器。

MicroPython动手做(15)——掌控板之AB按键图1

驴友花雕  中级技神
 楼主|

发表于 2020-4-24 20:29:14

11、A、B键控制图片转换显示

  1. #MicroPython动手做(15)——掌控板之AB按键
  2. #A、B键控制图片转换显示
  3. from mpython import *
  4. import music
  5. def callback_button_a_pressed():
  6.   oled.fill(0)
  7.   bmp_wZZv = bytearray([0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfb,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xe3,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x89,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x19,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x39,0xff,0xff,0xff,0xff,0xff,0xff,0xf8,0x0,0x3f,0xff,0xff,0xff,0xff,0xff,0xfe,0x79,0xff,0xff,0xff,0xff,0xff,0xff,0xe0,0x1a,0xf,0xff,0xff,0xff,0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xbf,0xff,0xff,0xc1,0xff,0x87,0xff,0xff,0xff,0xff,0xff,0xfc,0xf9,0xff,0xff,0xff,0xff,0xff,0xff,0xf0,0x0,0x1f,0xff,0xff,0xff,0xff,0xff,0xfc,0xf9,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xe7,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xf9,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0x79,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x39,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x99,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xc1,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xe3,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfb,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff])
  8.   oled.Bitmap(0, 0, bmp_wZZv, 128, 64, 1)
  9.   oled.show()
  10. def callback_button_b_pressed():
  11.   oled.fill(0)
  12.   bmp_h61P = bytearray([0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xdf,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xc7,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x83,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x99,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x9c,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x9e,0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x9f,0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xe7,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x9f,0x3f,0xff,0xff,0xff,0xff,0xff,0xf8,0x0,0xf,0xff,0xff,0xff,0xff,0xff,0xff,0x9f,0x3f,0xff,0xff,0xff,0xff,0xff,0xe1,0xff,0x83,0xff,0xff,0xfd,0xff,0xff,0xff,0x9f,0x3f,0xff,0xfc,0x0,0x0,0x0,0x7,0xff,0xc0,0xff,0xff,0xf0,0x7f,0xff,0xff,0x9f,0x3f,0xff,0xfc,0x0,0x0,0x0,0x1f,0xfc,0x0,0x7f,0xff,0xe6,0x0,0x1f,0xff,0x9f,0x3f,0xff,0xf9,0xff,0xcf,0xfe,0x3f,0xf0,0xff,0x1f,0xff,0xc0,0x0,0x7,0xff,0x9f,0x7f,0xf0,0x1,0xff,0xcf,0xfc,0x7f,0xe7,0xff,0x9f,0xc0,0x0,0x0,0x3,0xff,0x9e,0x60,0x0,0x1,0xff,0xcf,0xfc,0xff,0x8f,0xff,0xce,0x3,0xff,0xf0,0x1,0xff,0x9f,0x0,0x0,0x1,0xff,0xcf,0xf9,0xff,0x9f,0xff,0xe0,0x7f,0xff,0xf8,0x0,0xff,0x9f,0x0,0x0,0x1,0xff,0xcf,0xf1,0xff,0x3f,0xff,0xe3,0xff,0xff,0xf8,0x0,0x7f,0x9f,0x0,0x0,0x1,0xff,0xcf,0xf0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ff,0xf0,0x0,0x7,0x9f,0x7e,0x0,0x1,0xff,0xcf,0xf9,0xff,0x83,0xff,0xce,0x3,0xff,0xe0,0x0,0x7f,0x9f,0x7f,0xfc,0x1,0xff,0xcf,0xfc,0xff,0xc3,0xff,0xcf,0xc0,0x0,0x0,0x1,0xff,0x9f,0x3f,0xff,0xf9,0xff,0xcf,0xfe,0x7f,0xe3,0xff,0x9f,0xff,0x80,0x0,0x7,0xff,0x9f,0x3f,0xff,0xfd,0xff,0xcf,0xff,0x3f,0xf0,0x7e,0x3f,0xff,0xe7,0x0,0x1f,0xff,0x9f,0x3f,0xff,0xfc,0x0,0x0,0x0,0xf,0xfe,0x0,0x7f,0xff,0xf0,0x3f,0xff,0xff,0x9f,0x3f,0xff,0xfe,0x0,0x0,0x0,0x7,0xff,0xc1,0xff,0xff,0xfc,0xff,0xff,0xff,0x9f,0x7f,0xff,0xff,0xff,0xff,0xff,0xf0,0x58,0x7,0xff,0xff,0xff,0xff,0xff,0xff,0x9e,0x7f,0xff,0xff,0xff,0xff,0xff,0xfc,0x0,0x1f,0xff,0xff,0xff,0xff,0xff,0xff,0x9c,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x98,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x91,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xc7,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xdf,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff])
  13.   oled.Bitmap(0, 0, bmp_h61P, 128, 64, 1)
  14.   oled.show()
  15. def on_button_a_pressed():
  16.   while True:
  17.     if button_a.value() == 0:
  18.       yield callback_button_a_pressed()
  19.     else:
  20.       yield
  21. def on_button_b_pressed():
  22.   while True:
  23.     if button_b.value() == 0:
  24.       yield callback_button_b_pressed()
  25.     else:
  26.       yield
  27. func_button_a_pressed = on_button_a_pressed()
  28. func_button_b_pressed = on_button_b_pressed()
  29. while True:
  30.   next(func_button_a_pressed)
  31.   next(func_button_b_pressed)
复制代码
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-4-21 19:07:05

10、AB键与玫瑰曲线
玫瑰曲线,按A加1,按B开始

玫瑰曲线,基本的意思是:平面内,围绕某一中心点平均分布整数个正弦花瓣的曲线。数学公式是:ρ=a*sin(nθ),a为定长,n为整数(不一定是花瓣数)。

由于这个图形不是由直线组成,所以,掌控板上的所有绘图函数均无法使用,只能使用其中的像素点显示功能。命令如下:
        display.pixel(50,0,1)
这个命令中:display.pixel(x, y [,c ]) x , y 为点坐标(x,y)。当如果未给出c,则获取指定像素的颜色值。 如果给出c,则将指定的像素设置为给定的颜色,由于掌控板用的是黑白屏幕,所以用1代表点亮就可以了。

  1. #MicroPython动手做(15)——掌控板之AB按键
  2. #玫瑰曲线,按A加1,按B开始,
  3. from mpython import *
  4. import math,time#引入库文件<div align="left">
  5. def DrawRoseCurve(a,n):#定义函数,函数名称可以自己命名
  6.     for t in range(0,360):#循环次数,由于是画一圈,所以是360;可以自行设定
  7.         x = math.floor(math.cos(t)*a*math.sin(n*t))#计算x坐标的值,注意:这里需要取整
  8.         y = math.floor(math.sin(t)*a*math.sin(n*t))#计算y坐标的值,并且取整
  9.         display.pixel(x+64,y+32,1) #显示坐标像素点,为什么要+64、+32,哪是因为要把中心坐标(64,32)作为起点
  10.         display.show() #执行</div><div align="left">
  11. # 按键引脚初始化
  12.   
  13. display.fill(0)#清屏
  14. display.show()#清屏执行
  15.   
  16. BTNA = Pin(0, mode=Pin.OPEN_DRAIN,pull=Pin.PULL_UP,value=1)
  17. BTNB = Pin(2, mode=Pin.OPEN_DRAIN,pull=Pin.PULL_UP,value=1)
  18.   
  19. display.DispChar('玫瑰曲线', 38, 0)
  20. display.DispChar('输入花瓣数量,按A加', 0, 17)
  21. display.DispChar('按B开始', 0, 34)
  22. display.show() #执行
  23.   
  24. n1 = 0
  25. jishu = True
  26. Start = False
  27. while True:
  28.     if jishu:
  29.         if BTNA.value() == 0 and  BTNB.value() == 1 :
  30.             display.fill(0)#清屏
  31.             display.show()#清屏执行
  32.             n1 = n1+1
  33.             display.DispChar('数量:%d' % n1, 0, 0)
  34.             display.show()#执行
  35.             time.sleep_ms(400)
  36.         if BTNA.value() == 1 and  BTNB.value() == 0 :
  37.             jishu = False
  38.             Start = True
  39.         if Start:
  40.             if n1%2 == 0:
  41.                 n1=n1/2
  42.                 display.fill(0)#清屏
  43.                 display.show()#清屏执行
  44.                 DrawRoseCurve(30,n1)#调用函数
  45. Break</div>
复制代码
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-4-21 17:32:24

9、模拟“图灵测试”
图灵1950年的时候发表一篇论文,《计算机器与智能》,提出了一个设想。测试者与被测试者(一个人和一台机器)隔开的情况下,通过一些装置(如键盘)向被测试者随意提问。进行多次测试后,如果有超过30%的测试者不能确定出被测试者是人还是机器,那么这台机器就通过了测试,并被认为具有人类智能。

  1. #MicroPython动手做(15)——掌控板之AB按键
  2. #模拟“图灵测试”
  3. from mpython import * #从mpython中调用所有的库文件
  4. oled.DispChar('图灵测试', 40, 25) #在x=40,y=25的位置显示“图灵测试”
  5. oled.show() #打开显示
  6. while True: #循环执行
  7.     if button_a.value() == 0 or button_b.value() == 0: #如果按钮a或者按钮b被按下
  8.         oled.fill(0) #屏幕熄灭
  9.         oled.DispChar('你会下国际象棋吗?', 0, 0)
  10.         oled.DispChar('A.是的', 0, 16)
  11.         oled.DispChar('B.是的', 0, 32)
  12.         oled.show()
  13.         break#跳出次循环
  14. while True:
  15.     if button_a.value() == 0 or button_b.value() == 0:
  16.         oled.fill(0)
  17.         oled.DispChar('你会下象棋吗?', 0, 0)
  18.         oled.DispChar('A.是的', 0, 16)
  19.         oled.DispChar('B.我不是说过了吗?', 0, 32)
  20.         oled.show()
  21.         break
  22. while True:
  23.     if button_a.value() == 0 or button_b.value() == 0:
  24.         oled.fill(0)
  25.         oled.DispChar('你会下象棋吗?', 0, 0)
  26.         oled.DispChar('A.是的', 0, 16)
  27.         oled.DispChar('B.你烦不烦,干嘛老提', 0, 32)
  28.         oled.DispChar('同样的问题。', 0, 48)
  29.         oled.show()
  30.         break
  31. while True:
  32.     if button_a.value() == 0 or button_b.value() == 0:
  33.         oled.fill(0)
  34.         oled.DispChar('A-A-A,A-B-A', 0, 0)
  35.         oled.DispChar('A-A-B,A-B-B', 0, 16)
  36.         oled.DispChar('你认为哪个是人的回答?', 0, 32)
  37.         oled.DispChar('哪一个是机器人的回答?', 0, 48)
  38.         oled.show()
  39.         break
复制代码
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-4-19 16:55:08

MicroPython动手做(15)——掌控板之AB按键图2
2、在掌控板上部边沿有按压式A、B两个按键

当按下按键时为低电平,否则高电平。在掌控板A,B按键按下的过程如下述,当按下时,电平从高变低,在高电平(1)变为低电平(0)的那一瞬间叫作下降沿。当按键松开时,电平从低变高,在低电平(0)变为高电平(1)的那一瞬间叫作上升沿。 我们可以通过获取电平变化来获取,当前按键状态。

P5、P11是掌控板的按键A、B的IO引脚,为避免冲突,限定拓展板的P5、P11引脚只能用于数字电平输入,且拓展板会对输入电平进行翻转。


MicroPython动手做(15)——掌控板之AB按键图1



回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-4-19 20:05:19

3、通过A、B按键,控制OLED屏幕显示A或B

  1. #MicroPython动手做(13)——掌控板之RGB三色灯
  2. #通过A、B按键,控制OLED屏幕显示A或B
  3. from mpython import *
  4. import framebuf
  5. import font.dvsm_16
  6. def display_font(_font, _str, _x, _y, _wrap, _z=0):
  7.     _start = _x
  8.     for _c in _str:
  9.         _d = _font.get_ch(_c)
  10.         if _wrap and _x > 128 - _d[2]: _x = _start; _y += _d[1]
  11.         if _c == '1' and _z > 0: oled.fill_rect(_x, _y, _d[2], _d[1], 0)
  12.         oled.blit(framebuf.FrameBuffer(bytearray(_d[0]), _d[2], _d[1],
  13.         framebuf.MONO_HLSB), (_x+int(_d[2]/_z)) if _c=='1' and _z>0 else _x, _y)
  14.         _x += _d[2]
  15. while True:
  16.     if button_a.value() == 0:
  17.         oled.fill(0)
  18.         display_font(font.dvsm_16, "A", 62, 26, False)
  19.         oled.show()
  20.     if button_b.value() == 0:
  21.         oled.fill(0)
  22.         display_font(font.dvsm_16, "B", 62, 26, False)
  23.         oled.show()
复制代码
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-4-19 20:09:37

mPython X 图形编程

MicroPython动手做(15)——掌控板之AB按键图1
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-4-20 13:39:36

4、按键A、B点亮RGB灯

  1. #MicroPython动手做(15)——掌控板之AB按键
  2. #A、B按键点亮DEB灯
  3. from mpython import *
  4. import time
  5. def on_button_a_down(_):
  6.     time.sleep_ms(10)
  7.     if button_a.value() == 1: return
  8.     rgb.fill((int(0), int(153), int(0)))
  9.     rgb.write()
  10.     time.sleep_ms(1)
  11.     time.sleep(0.5)
  12.     rgb.fill( (0, 0, 0) )
  13.     rgb.write()
  14.     time.sleep_ms(1)
  15. def on_button_b_down(_):
  16.     time.sleep_ms(10)
  17.     if button_b.value() == 1: return
  18.     rgb.fill((int(155), int(0), int(0)))
  19.     rgb.write()
  20.     time.sleep_ms(1)
  21.     time.sleep(0.5)
  22.     rgb.fill( (0, 0, 0) )
  23.     rgb.write()
  24.     time.sleep_ms(1)
  25. button_a.irq(trigger=Pin.IRQ_FALLING, handler=on_button_a_down)
  26. button_b.irq(trigger=Pin.IRQ_FALLING, handler=on_button_b_down)
复制代码
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-4-20 13:45:18

mPython 图形编程

MicroPython动手做(15)——掌控板之AB按键图1
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-4-20 17:26:01

5、按下A、B键播放播放不同音符
掌控板A和B引脚同时按下,显示A和B。只按A(全黑)或B(全白)键,则会清除


  1. #MicroPython动手做(15)——掌控板之AB按键
  2. #按下A、B键播放播放不同音符
  3. from mpython import *
  4. import music
  5. def callback_button_ab_pressed():
  6.   music.pitch(262, 250);
  7.   oled.fill(0)
  8.   oled.DispChar("AB", 55, 26)
  9.   oled.show()
  10. def callback_button_a_pressed():
  11.   music.pitch(165, 250);
  12.   oled.fill(0)
  13.   oled.show()
  14. def callback_button_b_pressed():
  15.   music.pitch(523, 250);
  16.   oled.fill(1)
  17.   oled.show()
  18. def on_button_ab_pressed():
  19.   while True:
  20.     if button_a.value() == 0 and button_b.value() == 0:
  21.       yield callback_button_ab_pressed()
  22.     else:
  23.       yield
  24. def on_button_a_pressed():
  25.   while True:
  26.     if button_a.value() == 0:
  27.       yield callback_button_a_pressed()
  28.     else:
  29.       yield
  30. def on_button_b_pressed():
  31.   while True:
  32.     if button_b.value() == 0:
  33.       yield callback_button_b_pressed()
  34.     else:
  35.       yield
  36. func_button_ab_pressed = on_button_ab_pressed()
  37. func_button_a_pressed = on_button_a_pressed()
  38. func_button_b_pressed = on_button_b_pressed()
  39. while True:
  40.   next(func_button_ab_pressed)
  41.   next(func_button_a_pressed)
  42.   next(func_button_b_pressed)
复制代码
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-4-20 17:38:00

Mind+  图形编程

MicroPython动手做(15)——掌控板之AB按键图1
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-4-20 19:53:43

6、获取按键状态,按A键开灯,按B键关灯

  1. #MicroPython动手做(15)——掌控板之AB按键
  2. #按A键开灯,按B键关灯
  3. from mpython import *
  4. while True:
  5.     # 按键A按下为低电平
  6.     if button_a.value() == 0 :   
  7.         # 按键延时
  8.         sleep_ms(20)
  9.         # 再次检测是否按下
  10.         if button_a.value()==0:
  11.             # 设置为三色
  12.             rgb[0] = (130,0,0)  
  13.             rgb[1] = (0,100,0)
  14.             rgb[2] = (0,0,255)
  15.         rgb.write()
  16.     # 按键B按下为低电平
  17.     if button_b.value() == 0 :   
  18.         # 按键延时
  19.         sleep_ms(20)
  20.         # 再次检测是否按下
  21.         if button_b.value()==0:
  22.             # 关灯
  23.             rgb[0] = (0, 0, 0)   
  24.             rgb[1] = (0, 0, 0)
  25.             rgb[2] = (0, 0, 0)
  26.             rgb.write()
复制代码

使用前,导入mpython模块:
from mpython import *

按键 A 和按键 B 按下:
button_a.value() == 0      #按键 A 按下
button_b.value() == 0      #按键 B 按下

注解
button_a 为按键 A 对象名,按键 B 对象名为 button_b ,是 machine.Pin 衍生类,继承Pin的方法,所以可使用 value 函数读取引脚值,返回 1 代表高电平信号,返回 0 代表低电平信号,因此当按键未按下状态时value==1,按下状态时value==0。

回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-4-21 13:53:02

7、按键中断
按下按键 A 打开板载灯和蜂鸣器,按下按键 B 关闭板载灯和蜂鸣器

  1. #MicroPython动手做(15)——掌控板之AB按键
  2. #按键中断
  3. from mpython import *     #导入mpython模块
  4. import music              #导入music模块
  5. def ledon(_):             #先定义中断处理函数:开灯和蜂鸣器
  6.     rgb.fill((128,0,0))   #打开板载灯,全部设置为红色,半亮度
  7.     rgb.write()           #将颜色输出到灯
  8.     music.pitch(1000)     #打开蜂鸣器:1000赫兹
  9. def ledoff(_):            #先定义中断处理函数:关灯和蜂鸣器
  10.     rgb.fill((0,0,0))     #关闭全部板载灯
  11.     rgb.write()           #将颜色输出到灯
  12.     music.pitch(0)        #关闭蜂鸣器
  13. button_a.irq(trigger=Pin.IRQ_FALLING, handler=ledon)     #设置按键 A 中断,下降沿触发,开灯和蜂鸣器
  14. button_b.irq(trigger=Pin.IRQ_FALLING, handler=ledoff)    #设置按键 B 中断,下降沿触发,关灯和蜂鸣器
  15. while True:                            #在没有中断时,程序执行在循环内
  16.     pass
复制代码

注解:
以上程序默认情况下,程序在循环内空等不执行任何指令。当检测到a,b按键中断按时,回调对应的函数。

button_a.irq(trigger=Pin.IRQ_FALLING, handler=ledon) 是调用的中断处理程序对应的函数。trigger 配置可以触发中断的事件,可能的值是:Pin.IRQ_FALLING 下降沿中断;Pin.IRQ_RISING 上升沿中断;Pin.IRQ_LOW_LEVEL 低电平中断;Pin.IRQ_HIGH_LEVEL 高电平中断。handler 是一个可选的函数,在中断触发时调用,返回一个回调对象。 详细使用可查阅 button_[a,b].irq。

注意

定义中断处理函数时,函数须包含任意 一个 参数,否则无法使用。ledon()、ledoff()函数中的参数为 _ 。

回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-4-21 15:11:43

8、双向开关灯

#MicroPython动手做(15)——掌控板之AB按键
#双向开关灯

  1. #MicroPython动手做(15)——掌控板之AB按键
  2. #双向开关灯
  3. from mpython import *
  4. import time
  5. def on_button_a_down(_):
  6.     global abc, variable
  7.     time.sleep_ms(10)
  8.     if button_a.value() == 1: return
  9.     variable = variable ^ 1
  10. def on_button_b_down(_):
  11.     global abc, variable
  12.     time.sleep_ms(10)
  13.     if button_b.value() == 1: return
  14.     variable = variable ^ 1
  15. button_a.irq(trigger=Pin.IRQ_FALLING, handler=on_button_a_down)
  16. button_b.irq(trigger=Pin.IRQ_FALLING, handler=on_button_b_down)
  17. variable = 0
  18. while True:
  19.     if variable == 1:
  20.         rgb.fill( (0, 0, 0) )
  21.         rgb.write()
  22.         time.sleep_ms(1)
  23.         oled.fill(0)
  24.         oled.DispChar("关灯", 50, 25, 1)
  25.     else:
  26.         rgb.fill((int(255), int(102), int(0)))
  27.         rgb.write()
  28.         time.sleep_ms(1)
  29.         oled.fill(0)
  30.         oled.DispChar("开灯", 50, 25, 1)
  31.     oled.show()
复制代码
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-4-21 15:18:42

mPython X 图形编程

MicroPython动手做(15)——掌控板之AB按键图1
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-4-21 16:19:25

注解:
在mPython X“数学”模组里找到异或运算符

MicroPython动手做(15)——掌控板之AB按键图1

在程序中我们A键和B键都可以控制灯的亮灭,即按下A键或B键灯亮,再按A键或B键灯灭,我们可以使用按位异或运算符“^”实现效果,按位异或运算是将两个运算分量的对应位按位遵照以下规则进行计算:

0 ^ 0 = 0, 0 ^ 1 = 1, 1 ^ 0 = 1, 1 ^ 1 = 0

即相应位的值相同的,结果为 0,不相同的结果为 1。

例如:

a = 60 (60 = 0011 1100)

b = 13 ( 13 = 0000 1101)

c = a ^ b( 49 = 0011 0001)

回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-4-21 16:27:57

每当按键A或者按键B被按下,将变量“variable”的值^1然后在赋值给变量“variable”

十进制的1和0在二进制也是1和0

初始化deng变量为0,按键按下执行异或运算deng^1=1;

此时deng变量为1,按键按下执行异或运算deng^1=0;

依此类推即可实现按键控制deng变量的数值。

a=60,b=13



贴士

按位运算符是以二进制数进行计算的,可以通过十进制转换位二进制运算,十进制与二进制的区别在于:

基数不同,前者满10进1,后者满2进1

有效字符不同,前者有效字符有10个:0,1,2,3,4,5,5,6,7,8,9;后者有效字符有2个:0,1

回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-4-21 16:47:17

MicroPython动手做(15)——掌控板之AB按键图1
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-4-21 18:16:10

艾伦·麦席森·图灵(Alan Mathison Turing):英国数学家、逻辑学家,被称为计算机科学之父,人工智能之父。图灵对于人工智能的发展有诸多贡献,提出了一种用于判定机器是否具有智能的试验方法,即图灵试验,至今,每年都有试验的比赛。此外,图灵提出的著名的图灵机模型为现代计算机的逻辑工作方式奠定了基础。

图灵1950年的时候发表一篇论文,《计算机器与智能》,提出了一个设想。测试者与被测试者(一个人和一台机器)隔开的情况下,通过一些装置(如键盘)向被测试者随意提问。进行多次测试后,如果有超过30%的测试者不能确定出被测试者是人还是机器,那么这台机器就通过了测试,并被认为具有人类智能。

MicroPython动手做(15)——掌控板之AB按键图1

回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-4-21 19:02:37

MicroPython动手做(15)——掌控板之AB按键图1
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-4-21 19:11:22

MicroPython动手做(15)——掌控板之AB按键图1
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2020-4-22 08:59:19

MicroPython动手做(15)——掌控板之AB按键图1
回复

使用道具 举报

12下一页
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail