驴友花雕 发表于 2020-4-24 11:41:51

MicroPython动手做(17)——掌控板之触摸引脚

1、触摸开关
是科技发展进步的一种新兴产品。他一般是指应用触摸感应芯片原理设计的一种控制开关,是传统机械按键式开关的换代产品。能实现更智能化、操作更方便的触摸开关有传统开关不可比拟的优势,是智能产品的非常流行的一种发展性开关。触摸开关是电子取代机械的又一成功应用。触摸开关没有金属触点,不放电不打火,大量的节约铜合金材料,同时对于机械结构的要求大大减少。它直接取代传统开关,操作舒适、手感极佳、控制精准且没有机械磨损。同时,触摸开关更有人性化的关怀,可以自己选择开关上的文字提示,个性化的文字标签呈现出液晶显示的效果,水晶面板发出淡淡的微光,让深夜不再是完全的漆黑,足以让人形成方位和轮廓感。



驴友花雕 发表于 2020-6-6 17:02:16

10、六个触摸键控制的RGB颜色灯

#MicroPython动手做(17)——掌控板之触摸引脚
#六个触摸键控制的RGB颜色灯

from mpython import *
import network
import time
import music
from yeelight import *
from machine import Timer

my_wifi = wifi()

my_wifi.connectWiFi("zh", "zy1567")

_status_p = _status_y = _status_t = _status_h = _status_o = _status_n = 0
def on_touchpad_P_pressed():pass
def on_touchpad_P_unpressed():pass
def on_touchpad_Y_pressed():pass
def on_touchpad_Y_unpressed():pass
def on_touchpad_T_pressed():pass
def on_touchpad_T_unpressed():pass
def on_touchpad_H_pressed():pass
def on_touchpad_H_unpressed():pass
def on_touchpad_O_pressed():pass
def on_touchpad_O_unpressed():pass
def on_touchpad_N_pressed():pass
def on_touchpad_N_unpressed():pass

tim12 = Timer(12)

def timer12_tick(_):
    global _status_p, _status_y, _status_t, _status_h, _status_o, _status_n
    try:
      touchPad_P.read();pass
    except:
      return
    if touchPad_P.read() < 400:
      if 1 != _status_p:_status_p = 1;on_touchpad_P_pressed()
    elif 0 != _status_p:_status_p = 0;on_touchpad_P_unpressed()
    if touchPad_Y.read() < 400:
      if 1 != _status_y:_status_y = 1;on_touchpad_Y_pressed()
    elif 0 != _status_y:_status_y = 0;on_touchpad_Y_unpressed()
    if touchPad_T.read() < 400:
      if 1 != _status_t:_status_t = 1;on_touchpad_T_pressed()
    elif 0 != _status_t:_status_t = 0;on_touchpad_T_unpressed()
    if touchPad_H.read() < 400:
      if 1 != _status_h:_status_h = 1;on_touchpad_H_pressed()
    elif 0 != _status_h:_status_h = 0;on_touchpad_H_unpressed()
    if touchPad_O.read() < 400:
      if 1 != _status_o:_status_o = 1;on_touchpad_O_pressed()
    elif 0 != _status_o:_status_o = 0;on_touchpad_O_unpressed()
    if touchPad_N.read() < 400:
      if 1 != _status_n:_status_n = 1;on_touchpad_N_pressed()
    elif 0 != _status_n:_status_n = 0;on_touchpad_N_unpressed()

tim12.init(period=100, mode=Timer.PERIODIC, callback=timer12_tick)

def on_touchpad_P_pressed():
    global i
    time.sleep_ms(500)
    bulb.set_rgb(153, 0, 0)
    oled.DispChar("P键红色", 38, 32, 1)
    oled.show()
    rgb.fill((int(153), int(0), int(0)))
    rgb.write()
    time.sleep_ms(1)

def on_touchpad_Y_pressed():
    global i
    time.sleep_ms(500)
    bulb.set_rgb(0, 153, 0)
    oled.DispChar("Y键绿色", 38, 32, 1)
    oled.show()
    rgb.fill((int(0), int(153), int(0)))
    rgb.write()
    time.sleep_ms(1)

def on_touchpad_T_pressed():
    global i
    time.sleep_ms(500)
    bulb.set_rgb(51, 51, 255)
    oled.DispChar("T键蓝色", 38, 32, 1)
    oled.show()
    rgb.fill((int(51), int(51), int(255)))
    rgb.write()
    time.sleep_ms(1)

def on_touchpad_H_pressed():
    global i
    time.sleep_ms(500)
    bulb.set_rgb(255, 102, 0)
    oled.DispChar("H键橙色", 38, 32, 1)
    oled.show()
    rgb.fill((int(153), int(51), int(0)))
    rgb.write()
    time.sleep_ms(1)

def on_touchpad_O_pressed():
    global i
    time.sleep_ms(500)
    bulb.set_rgb(204, 51, 204)
    oled.DispChar("O键紫色", 38, 32, 1)
    oled.show()
    rgb.fill((int(102), int(51), int(102)))
    rgb.write()
    time.sleep_ms(1)

def on_touchpad_N_pressed():
    global i
    time.sleep_ms(500)
    bulb.set_rgb(255, 204, 51)
    oled.DispChar("N键黄色", 38, 32, 1)
    oled.show()
    rgb.fill((int(153), int(102), int(51)))
    rgb.write()
    time.sleep_ms(1)


rgb = (int(51), int(51), int(51))
rgb.write()
time.sleep_ms(1)
music.play('G5:1')
bulb = Bulb(discover_bulbs()["ip"])
time.sleep_ms(500)
bulb.turn_on()
oled.fill(0)
oled.DispChar("触摸键控制RGB灯", 18, 16, 1)
oled.show()

驴友花雕 发表于 2020-4-25 17:56:58

7、6个触摸按键控制RGB灯并显示按键名

#MicroPython动手做(17)——掌控板之触摸引脚
#6个触摸按键控制RGB灯并显示按键名

from mpython import *

from machine import Timer

import time

_status_p = _status_y = _status_t = _status_h = _status_o = _status_n = 0
def on_touchpad_P_pressed():pass
def on_touchpad_P_unpressed():pass
def on_touchpad_Y_pressed():pass
def on_touchpad_Y_unpressed():pass
def on_touchpad_T_pressed():pass
def on_touchpad_T_unpressed():pass
def on_touchpad_H_pressed():pass
def on_touchpad_H_unpressed():pass
def on_touchpad_O_pressed():pass
def on_touchpad_O_unpressed():pass
def on_touchpad_N_pressed():pass
def on_touchpad_N_unpressed():pass

tim12 = Timer(12)

def timer12_tick(_):
    global _status_p, _status_y, _status_t, _status_h, _status_o, _status_n
    try:
      touchPad_P.read();pass
    except:
      return
    if touchPad_P.read() < 400:
      if 1 != _status_p:_status_p = 1;on_touchpad_P_pressed()
    elif 0 != _status_p:_status_p = 0;on_touchpad_P_unpressed()
    if touchPad_Y.read() < 400:
      if 1 != _status_y:_status_y = 1;on_touchpad_Y_pressed()
    elif 0 != _status_y:_status_y = 0;on_touchpad_Y_unpressed()
    if touchPad_T.read() < 400:
      if 1 != _status_t:_status_t = 1;on_touchpad_T_pressed()
    elif 0 != _status_t:_status_t = 0;on_touchpad_T_unpressed()
    if touchPad_H.read() < 400:
      if 1 != _status_h:_status_h = 1;on_touchpad_H_pressed()
    elif 0 != _status_h:_status_h = 0;on_touchpad_H_unpressed()
    if touchPad_O.read() < 400:
      if 1 != _status_o:_status_o = 1;on_touchpad_O_pressed()
    elif 0 != _status_o:_status_o = 0;on_touchpad_O_unpressed()
    if touchPad_N.read() < 400:
      if 1 != _status_n:_status_n = 1;on_touchpad_N_pressed()
    elif 0 != _status_n:_status_n = 0;on_touchpad_N_unpressed()

tim12.init(period=100, mode=Timer.PERIODIC, callback=timer12_tick)

def on_touchpad_P_pressed():
    global i
    oled.fill(0)
    oled.DispChar('P', 60, 22, 1)
    oled.show()
    rgb = (int(255), int(0), int(0))
    rgb.write()
    time.sleep_ms(1)
    time.sleep(1)
    rgb.fill( (0, 0, 0) )
    rgb.write()
    time.sleep_ms(1)

def on_touchpad_H_pressed():
    global i
    oled.fill(0)
    oled.DispChar('H', 60, 22, 1)
    oled.show()
    rgb = (int(255), int(102), int(0))
    rgb.write()
    time.sleep_ms(1)
    time.sleep(1)
    rgb.fill( (0, 0, 0) )
    rgb.write()
    time.sleep_ms(1)

def on_touchpad_Y_pressed():
    global i
    oled.fill(0)
    oled.DispChar('Y', 60, 22, 1)
    oled.show()
    rgb = (int(0), int(153), int(0))
    rgb.write()
    time.sleep_ms(1)
    time.sleep(1)
    rgb.fill( (0, 0, 0) )
    rgb.write()
    time.sleep_ms(1)

def on_touchpad_O_pressed():
    global i
    oled.fill(0)
    oled.DispChar('O', 60, 22, 1)
    oled.show()
    rgb = (int(255), int(102), int(0))
    rgb.write()
    time.sleep_ms(1)
    rgb = (int(255), int(102), int(0))
    rgb.write()
    time.sleep_ms(1)
    time.sleep(1)
    rgb.fill( (0, 0, 0) )
    rgb.write()
    time.sleep_ms(1)

def on_touchpad_T_pressed():
    global i
    oled.fill(0)
    oled.DispChar('T', 60, 22, 1)
    oled.show()
    rgb = (int(51), int(51), int(255))
    rgb.write()
    time.sleep_ms(1)
    time.sleep(1)
    rgb.fill( (0, 0, 0) )
    rgb.write()
    time.sleep_ms(1)

def on_touchpad_N_pressed():
    global i
    oled.fill(0)
    oled.DispChar('N', 60, 22, 1)
    oled.show()
    rgb.fill((int(255), int(102), int(0)))
    rgb.write()
    time.sleep_ms(1)
    time.sleep(1)
    rgb.fill( (0, 0, 0) )
    rgb.write()
    time.sleep_ms(1)

驴友花雕 发表于 2020-4-29 14:43:54

9、触摸按键点播六首曲子

#MicroPython动手做(17)——掌控板之触摸引脚
# 触摸按键点播六首曲子

from mpython import *
import time
import music
from machine import Timer

def on_button_a_down(_):
    time.sleep_ms(10)
    if button_a.value() == 1: return
    music.stop()

_status_p = _status_y = _status_t = _status_h = _status_o = _status_n = 0
def on_touchpad_P_pressed():pass
def on_touchpad_P_unpressed():pass
def on_touchpad_Y_pressed():pass
def on_touchpad_Y_unpressed():pass
def on_touchpad_T_pressed():pass
def on_touchpad_T_unpressed():pass
def on_touchpad_H_pressed():pass
def on_touchpad_H_unpressed():pass
def on_touchpad_O_pressed():pass
def on_touchpad_O_unpressed():pass
def on_touchpad_N_pressed():pass
def on_touchpad_N_unpressed():pass

tim12 = Timer(12)

def timer12_tick(_):
    global _status_p, _status_y, _status_t, _status_h, _status_o, _status_n
    try:
      touchPad_P.read();pass
    except:
      return
    if touchPad_P.read() < 400:
      if 1 != _status_p:_status_p = 1;on_touchpad_P_pressed()
    elif 0 != _status_p:_status_p = 0;on_touchpad_P_unpressed()
    if touchPad_Y.read() < 400:
      if 1 != _status_y:_status_y = 1;on_touchpad_Y_pressed()
    elif 0 != _status_y:_status_y = 0;on_touchpad_Y_unpressed()
    if touchPad_T.read() < 400:
      if 1 != _status_t:_status_t = 1;on_touchpad_T_pressed()
    elif 0 != _status_t:_status_t = 0;on_touchpad_T_unpressed()
    if touchPad_H.read() < 400:
      if 1 != _status_h:_status_h = 1;on_touchpad_H_pressed()
    elif 0 != _status_h:_status_h = 0;on_touchpad_H_unpressed()
    if touchPad_O.read() < 400:
      if 1 != _status_o:_status_o = 1;on_touchpad_O_pressed()
    elif 0 != _status_o:_status_o = 0;on_touchpad_O_unpressed()
    if touchPad_N.read() < 400:
      if 1 != _status_n:_status_n = 1;on_touchpad_N_pressed()
    elif 0 != _status_n:_status_n = 0;on_touchpad_N_unpressed()

tim12.init(period=100, mode=Timer.PERIODIC, callback=timer12_tick)

def on_touchpad_P_pressed():
    music.play(music.DONG_FANG_HONG, wait=False, loop=False)

def on_touchpad_Y_pressed():
    music.play(music.BIRTHDAY, wait=False, loop=False)

def on_touchpad_T_pressed():
    music.play(music.MO_LI_HUA, wait=False, loop=False)

def on_touchpad_H_pressed():
    music.play(music.ODE, wait=False, loop=False)

def on_touchpad_O_pressed():
    music.play(music.PRELUDE, wait=False, loop=False)

def on_touchpad_N_pressed():
    music.play(music.CAI_YUN_ZHUI_YUE, wait=False, loop=False)

button_a.irq(trigger=Pin.IRQ_FALLING, handler=on_button_a_down)


while True:
    oled.fill(0)
    oled.DispChar("A键:停止", 35, 0, 1)
    oled.DispChar("P:东方红Y:生日快乐", 6, 20, 1)
    oled.DispChar("T:茉莉花H:欢乐颂", 11, 35, 1)
    oled.DispChar("O:婚宴 N:彩云追月", 13, 50, 1)
    oled.show()

驴友花雕 发表于 2020-4-24 12:46:47

2、在掌控板正面金手指处拓展6个触摸按键,依次P、Y、T、H、O、N。


驴友花雕 发表于 2020-4-24 13:46:00



程序中依次从左到右分别为touchPad_P、touchPad_Y、touchPad_T、touchPad_H、touchPad_O、touchPad_N,可以通过它们下发命令。

touchPad_.read()¶描述:返回触摸引脚 touchPad_P/Y/T/H/O/N 的值

驴友花雕 发表于 2020-4-24 14:56:53


3、P键控制OLED显示

#MicroPython动手做(17)——掌控板之触摸引脚
#P键控制OLED显示

from mpython import *

import time
while True:
    if touchPad_P.read() < 400:
      oled.fill(0)
      oled.DispChar('Hello, world!', 25, 22, 1)
      oled.show()
      time.sleep(1)
    else:
      oled.fill(0)
      oled.DispChar('拜拜!', 50, 22, 1)
      oled.show()
      time.sleep(1)

驴友花雕 发表于 2020-4-24 15:05:20

mPython 图形编程


驴友花雕 发表于 2020-4-24 15:18:51


驴友花雕 发表于 2020-4-24 15:42:25

4、测量P键的实际触摸数值

#MicroPython动手做(17)——掌控板之触摸引脚
#测量P键的实际触摸数值

from mpython import *

import time
while True:
    if touchPad_P.read() < 400:
      oled.fill(0)
      oled.DispChar('---触摸---', 40, 22, 1)
      oled.DispChar((str(touchPad_P.read())), 60, 33, 1)
      oled.show()
      time.sleep(1)
    else:
      oled.fill(0)
      oled.DispChar('---释放---', 40, 22, 1)
      oled.DispChar((str(touchPad_P.read())), 57, 33, 1)
      oled.show()
      time.sleep(1)

驴友花雕 发表于 2020-4-24 15:45:31

mPython 图形编程


驴友花雕 发表于 2020-4-24 15:48:40


驴友花雕 发表于 2020-4-25 14:51:47

5、用单触摸按键实现开关(P键)

#MicroPython动手做(17)——掌控板之触摸引脚
#用单触摸按键实现开关(P键)

from mpython import *#导入掌控库
import time#引入时间库

#初始化布尔变量为真
item = True
while True:
    if touchPad_P.read() < 100: # 触摸p
      if item == True:    #如果变量为真
            rgb.fill((80, 0, 0))# 设置为红色
            rgb.write()   
            item = False # 变量设为假
      else:      # 否则变量为假
            rgb.fill((0, 0, 0))# 设为0
            rgb.write()   
            item = True      #变量设为真
    time.sleep_ms(220)    #延时防抖

驴友花雕 发表于 2020-4-25 15:47:30

6、OLED屏幕同时显示每个触摸引脚的值

#MicroPython动手做(17)——掌控板之触摸引脚
#OLED屏幕同时显示每个触摸引脚的值

from mpython import *

while True:
    oled.fill(0)
    oled.DispChar((str("P") + str(touchPad_P.read())), 0, 0, 1)
    oled.DispChar((str("Y") + str(touchPad_Y.read())), 93, 0, 1)
    oled.DispChar((str("T") + str(touchPad_T.read())), 0, 16, 1)
    oled.DispChar((str("H") + str(touchPad_H.read())), 93, 16, 1)
    oled.DispChar((str("O") + str(touchPad_O.read())), 0, 32, 1)
    oled.DispChar((str("N") + str(touchPad_N.read())), 93, 32, 1)
    oled.show()

驴友花雕 发表于 2020-4-25 15:54:16

mPython X 图形编程

驴友花雕 发表于 2020-4-25 16:44:13


驴友花雕 发表于 2020-4-25 18:07:06

mPython 图形编程



驴友花雕 发表于 2020-4-25 19:26:36

8、简易触摸按键电子琴(6键)


驴友花雕 发表于 2020-4-25 19:30:59

#MicroPython动手做(17)——掌控板之触摸引脚
#简易触摸按键电子琴(6键)

from mpython import *

import music
while True:
    music.stop()
    if touchPad_P.read() < 400:
      music.pitch(262, 500)
    else:
      if touchPad_Y.read() < 400:
            music.pitch(294, 500)
      else:
            if touchPad_T.read() < 400:
                music.pitch(330, 500)
            else:
                if touchPad_H.read() < 400:
                  music.pitch(349, 500)
                else:
                  if touchPad_O.read() < 400:
                        music.pitch(392, 500)
                  else:
                        if touchPad_N.read() < 400:
                            music.pitch(440, 500)

驴友花雕 发表于 2020-4-25 20:03:53

mPython 图形编程


驴友花雕 发表于 2020-4-25 20:08:52

视频:掌控板模拟简易触摸按键电子琴(6键)
https://v.youku.com/v_show/id_XNDY0ODEwMjcxMg==.html?spm=a2hbt.13141534.app.5~5!2~5!2~5~5~5!2~5~5!2~5!2~5!2~5~5~A

https://v.youku.com/v_show/id_XNDY0ODEwMjcxMg==.html?spm=a2hbt.13141534.app.5~5!2~5!2~5~5~5!2~5~5!2~5!2~5!2~5~5~A



驴友花雕 发表于 2020-4-29 14:47:18

mPython X 图形编程


页: [1] 2
查看完整版本: MicroPython动手做(17)——掌控板之触摸引脚