2022年7月13日 星期三

人機介面實作 – Digital Capacitive Touch Sensor(BS818A)

人機介面實作 – Digital Capacitive Touch Sensor(BS818A)

電路圖

 


程式列表

Python – ePy-Lite_BS818A.py

"""

  ePy-Lite_BS818A.py

  Capacitive Touch Sensor Module

 

  ePy-Lite  BS818A

  -----------------

  GND       1  GND: Ground

  3.3V      2  VCC: Module power supply – 2.2-5.5V

  P4        3  SDO: Output pin for Serial Data

  P5        4  CLK: Input pin for Serial Clock

"""

 

from machine import Switch  #Get button KEY library

from machine import Timer, I2C,LED,Pin

import utime

# from BS818A import Keypad

 

class Keypad:

  BS_KEYCODE_1 = 0x01

  BS_KEYCODE_2 = 0x02

  BS_KEYCODE_3 = 0x04

  BS_KEYCODE_4 = 0x08

  BS_KEYCODE_5 = 0x10

  BS_KEYCODE_6 = 0x20

  BS_KEYCODE_7 = 0x40

  BS_KEYCODE_8 = 0x80

  BS_Released  = 0xA0FF

 

  def __init__(self, scl, sdo):

    self._scl_pin = Pin(scl, Pin.OUT)

    self._sdo_pin = Pin(sdo, Pin.IN)

    self._scl_pin.on()

    utime.sleep_ms(1)

 

  def read(self):

    DATA = 0

 

    for i in range(16):

      self._scl_pin.off()

      utime.sleep_us(10)

      self._scl_pin.on()

      utime.sleep_us(10)

      DATA |= self._sdo_pin.value() << i

    utime.sleep_ms(6)

 

    return DATA & 0xFFFF;

 

  def PressBsButton(self,button):

    BsKey = self.read()

 

    if (BsKey != BS_Released):

      # print(BsKey)

      if ((BsKey & button) == button) :

        return False  # if bit set 1 is not button pressed

      else:

        return True

 

    return False

 

 # note  frequency

NOTE = [262,294,330,349,392,440,494,

        523,587,659,698,784,880,988,

        1046,1175,1318,1397,1568,1760,1976]

 

def buzzer_toggle(t):

  if buzzer_pin.value() == 0:

    buzzer_pin.value(1)

  else :

    buzzer_pin.value(0)

 

def tick3(timer):

  global TimerDone

  TimerDone = True

 

def key_int():

  global KeyDone

  KeyDone = True

 

# Start Function

if __name__ == '__main__':

  ledY = LED('ledy')

  ledY.off()

 

  KeyA = Switch('keya')    #Create button A

  KeyA.callback(key_int)

  KeyDone = False

 

  timer = Timer(3,freq=20)

  timer.callback(tick3)

  TimerDone = False

 

  buzzer_pin = Pin(Pin.epy.P22,Pin.OUT) #指定 P22 輸出, 推動buzzer

 

  scl_pin = Pin.board.P5

  sdo_pin = Pin.board.P4

 

  keypad = Keypad(scl=scl_pin, sdo=sdo_pin)

  OldKey=0xFFFF

 

  while True:

    if TimerDone == True:

      TimerDone = False

      pressed = keypad.read()

 

      if (pressed != 0xFFFF)and(pressed != OldKey) :

        print(hex(pressed))

        OldKey = pressed

 

        if (pressed & Keypad.BS_KEYCODE_1)==0:

          KeyNum = 0

        elif (pressed & Keypad.BS_KEYCODE_2)==0:

          KeyNum = 1

        elif (pressed & Keypad.BS_KEYCODE_3)==0:

          KeyNum = 2

        elif (pressed & Keypad.BS_KEYCODE_4)==0:

          KeyNum = 3

        elif (pressed & Keypad.BS_KEYCODE_5)==0:

          KeyNum = 4

        elif (pressed & Keypad.BS_KEYCODE_6)==0:

          KeyNum = 5

        elif (pressed & Keypad.BS_KEYCODE_7)==0:

          KeyNum = 6

        elif (pressed & Keypad.BS_KEYCODE_8)==0:

          KeyNum = 7

        tone = Timer(0,freq=(NOTE[KeyNum])*2)

 

        tone.callback(buzzer_toggle) # 回調函數掛載 每次 500Hz 會進去回調函數

        utime.sleep_ms(200)

        tone.callback(None) # 關閉呼叫回調函數

      elif (pressed == 0xFFFF):

        OldKey =  0xFFFF

 

    if KeyDone == True:      #Press A Key

      break

 

  KeyA.callback(None)

  timer.callback(None)

  timer.deinit()


執行結果

https://www.facebook.com/groups/richlink/permalink/551904826440077/


沒有留言:

張貼留言