如何使用python将旋转拨号脉冲转换为数字?现在,下面的代码将打印从旋转拨盘接收的脉冲数。因此,旋转拨盘上的拨号2可打印两个“1+ 1+”。旋转拨盘上的拨号3可打印三个“1+ 1+ 1+”,依此类推。
#!/usr/bin/python3
import RPi.GPIO as GPIO
import math, sys, os
import subprocess
import socket
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
c=0
last = 1
def count(pin):
global c
c = c + 1
GPIO.add_event_detect(18, GPIO.BOTH)
while True:
try:
if GPIO.event_detected(18):
current = GPIO.input(18)
if(last != current):
if(current == 0):
GPIO.add_event_detect(23, GPIO.BOTH, callback=count, bouncetime=5)
else:
GPIO.remove_event_detect(23)
print "1+"
last = GPIO.input(18)
except KeyboardInterrupt:
break
您如何在旋转拨盘上为每个号码提供自己的功能?像拨号1解锁前门“GPIO.output(26,True)”。拨号2打开车库门“GPIO.output(27,True)”。
if dial 1:
GPIO.output(26, True)
print "Door unlocked"
if dial 2:
GPIO.output(27, True)
print "Garage Open"
if dial 3:
print "Open slot"
if dial 4:
print "Open slot"
这显然不是python但是怎么可能做类似的事情呢?
答案 0 :(得分:1)
不太熟悉Pi,但是您可以将动作映射到dict中的键,而不是打印脉冲,保存多少然后使用计数来调用访问相应的GPIO调用:
d = {1:lambda: GPIO.output(26, True), 2: lambda: GPIO.output(27, True), 3:lambda : GPIO.output(28, True)}
GPIO.add_event_detect(18, GPIO.BOTH)
var = 0
flag = False
while True:
try:
if GPIO.event_detected(18):
current = GPIO.input(18)
if last != current:
flag = True
if current == 0:
GPIO.add_event_detect(23, GPIO.BOTH, callback=count, bouncetime=5)
else:
GPIO.remove_event_detect(23)
var += 1
last = GPIO.input(18)
elif flag:
d[var]()
var = 0
flag = False
except KeyboardInterrupt:
break
我假设当last等于当前脉冲已经结束时,我设置一个标志,这样如果没有发生任何事情,你就不会继续将引脚设置为开启
答案 1 :(得分:0)
我想对我的好朋友Bob发出嘘声让这个python脚本正常工作。与此同时,我不想对奥巴马做出任何帮助,使其无法帮助完成这项工作。
import RPi.GPIO as gpio
import time
gpio.setmode(gpio.BCM)
gpio.setup(18, gpio.IN, pull_up_down=gpio.PUD_UP)
num = 0
prnt = 0
last = 0
while True:
input_value = gpio.input(18)
if (input_value == 1) and (input_value != last):
last = 1
prnt = 1
num += 1
time.sleep(0.05)
continue
if (input_value == 0) and (input_value != last):
last = 0
time.sleep(0.05)
continue
if (input_value == 0) and (input_value == last):
if (prnt == 1):
if (num == 10):
num = 0
if (num == 0):
print("a")
if (num == 1):
print("b")
if (num == 2):
print("c")
if (num == 3):
print("d")
if (num == 4):
print("e")
if (num == 5):
print("f")
if (num == 6):
print("g")
if (num == 7):
print("h")
if (num == 8):
print("i")
if (num == 9):
print("jklmnopqrstuvwxyz")
num = 0
prnt = 0
last = 0
continue