Python等待按下按钮(多输入)

时间:2015-11-21 19:25:57

标签: python raspberry-pi

我目前正在寻找为我的脚本添加另一个按钮。目前,它会显示我的视频(相机)全屏输出,直到检测到按钮按下,然后它将执行某些操作。这看起来像我这样做了:

while True:
        camera.preview_fullscreen = True
        camera.preview_alpha = 128
        camera.start_preview()
        GPIO.wait_for_edge(picture_pin, GPIO.FALLING)
        action()

然而,我正处于舞台,我想引入另一个执行不同操作的按钮。因此,我正考虑加入这个:

import RPi.GPIO as GPIO

actionpin1 = 23
actionpin2 = 24

GPIO.setmode(GPIO.BCM)
GPIO.setup(actionpin1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(actionpin2, GPIO.IN, pull_up_down=GPIO.PUD_UP)

def action1():
        print "button pressed 1"

def action2():
        print "button pressed 2"

while True:
        print "waiting for button"
        GPIO.add_event_detect(actionpin1, GPIO.BOTH, callback=action1, bouncetime=800)
        GPIO.add_event_detect(actionpin2, GPIO.BOTH, callback=action2, bouncetime=800)

然而,它给了我这个错误:

  

Traceback(最近一次调用最后一次):文件“test.py”,第19行,in          GPIO.add_event_detect(actionpin1,GPIO.BOTH,callback = action1,bouncetime = 800)RuntimeError:已经发生冲突边缘检测   已启用此GPIO通道

这个错误与我在这个函数上读到的内容有冲突,所以我不确定为什么它会给我这个错误。有人能指出我正确的方向吗?

1 个答案:

答案 0 :(得分:3)

public class Contact{ JTextArea incoming; JTextField outgoing; BufferedReader reader; PrintWriter writer; Socket sock; JPanel mainP; JScrollPane scroll; JButton sendB; public static void main(String[] args){ Contact client = new Contact(); client.go(); } public void go(){ JFrame frame = new JFrame("Client"); mainP = new JPanel(); incoming = new JTextArea(15,50); incoming.setLineWrap(true); incoming.setWrapStyleWord(false); incoming.setEditable(false); scroll = new JScrollPane(incoming); scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); outgoing = new JTextField(20); sendB = new JButton("Send"); sendB.addActionListener(new SendButtonListener()); mainP.add(scroll); mainP.add(outgoing); mainP.add(sendB); Networking(); Thread readerThread = new Thread(new IncomingReader()); readerThread.start(); frame.setLocationRelativeTo(null); frame.getContentPane().add(BorderLayout.CENTER, mainP); frame.setSize(400,400); frame.setVisible(true); } private void Networking(){ try{ sock = new Socket("127.0.0.1",5000); InputStreamReader streamReader = new InputStreamReader(sock.getInputStream()); reader = new BufferedReader(streamReader); writer = new PrintWriter(sock.getOutputStream()); System.out.println("Connection Established"); }catch(IOException ex){ ex.printStackTrace(); } } public class SendButtonListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { try{ writer.println(outgoing.getText()); writer.close(); }catch(Exception ex){ ex.printStackTrace(); } outgoing.setText(""); outgoing.requestFocus(); } } public class IncomingReader implements Runnable{ @Override public void run() { String message; try{ while((message = reader.readLine()) != null){ System.out.println("read " + message); incoming.append(message + "\n"); } }catch(Exception ex){ ex.printStackTrace(); } } } } 方法放在While循环之外,这是无用的,因为在检测到事件时会调用回调函数:

add_event_detect