0

我遇到了关于 Arduino 中的 Protothreading 库的问题。我创建了一个Button类,它代表一个硬件按钮。现在的想法是你可以附加一个ButtonListener来监听按钮。如果按下按钮,则clicked()调用该函数。

#include <Arduino.h>
#include <pt.h>

class ButtonListener {
    public:
        virtual void clicked() = 0;
        virtual void longClicked() = 0;
        virtual void tapped(int) = 0;
};

class Button {

    static const int RECOIL_TIME = 200;
    static const int LONG_CLICK_LENGTH = 1000;

    private:
        int _pin;
        ButtonListener *_listener;
        struct pt _thread;
        unsigned long _timestamp = 0;

        int listenerHook(struct pt *pt) {
            PT_BEGIN(pt);
            this->_timestamp = 0;
            while (true) {
                PT_WAIT_UNTIL(pt, millis() - _timestamp > 1);
                _timestamp = millis();
                if (&this->_listener != NULL) {
                    this->listenForClick();
                }
            }
            PT_END(pt);
        }

        void listenForClick() {
            boolean longClicked = true;
            int state = digitalRead(this->_pin);
            if (state == HIGH) {
                unsigned long timestamp = millis();
                while (true) {
                    longClicked = millis() - timestamp > LONG_CLICK_LENGTH;
                    state = digitalRead(this->_pin);
                    if (state == LOW) {
                        break;
                    }
                }
                if (&this->_listener != NULL) {
                    if (longClicked) {
                        (*this->_listener).longClicked();
                    }
                    else {
                        (*this->_listener).clicked();
                    }
                }
            }
        }

    public:
        Button(int pin) {
            this->_pin = pin;
        }

        void init() {
            pinMode(this->_pin, OUTPUT);
            PT_INIT(&this->_thread);
        }

        void setListener(ButtonListener *listener) {
            this->_listener = listener;
        }

        void listen() {
            this->listenerHook(&this->_thread);
        }
};

现在我创建了两个实现ButtonListener

class Button12Listener : public ButtonListener {
    public:
        void clicked() {
            Serial.println("Button 12 clicked!");
        }
}

另一个实现是 aButton13Listener并打印“Button 13 clicked!”

然后让我们运行代码:

// Instantiate the buttons
Button button12(12);
Button button13(13);

void setup() {
    Serial.begin(9600);

    button12.init();
    button13.init();

    // Add listeners to the buttons
    button12.setListener(new Button12Listener());
    button13.setListener(new Button13Listener());
}

void loop() {
    while (true) {
        // Listen for button clicks
        button12.listen();
        button13.listen();
    }
    Serial.println("Loop ended.");
    delay(60000);
}

我期待“点击按钮 12!” 当我单击针脚 12 上的按钮时,“单击了按钮 13!” 当我单击引脚 13 上的按钮时。

但是当我尝试点击任何按钮时,它会随机打印“Button 12 clicked!” 或“点击了按钮 13!” 无论我按什么按钮。

看起来原型线程在按钮或其他东西之间共享。

如果我检查按钮的调用顺序,如下所示:

button12.listen();
Serial.println("listen12");
button13.listen();
Serial.println("listen13");

然后是以下输出:

12
13
12
13
12
12

这似乎没问题。

所以有什么问题?我错过了什么?

4

1 回答 1

1

通过在listenForClick 中使用while(true) 循环,您完全消除了protothreads 的全部意义。我会这样做:

PT_BEGIN(thr); 
while(1){
  // ensure that the pin is low when you start
  PT_WAIT_UNTIL(thr, digitalRead(pin) == LOW); 
  // wait until pin goes high
  PT_WAIT_UNTIL(thr, digitalRead(pin) == HIGH); 
  // insert delay here for minimum time the pin must be high
  this->timeout = millis() + 20; // 20 ms
  // wait until the delay has expired
  PT_WAIT_UNTIL(thr, this->timeout - millis() > 0); 
  // wait until the pin goes low again
  PT_WAIT_UNTIL(thr, digitalRead(pin) == LOW); 
  // call the click callback
  this->clicked(); 
}
PT_END(thr); 

然后重复调用这个线程。

注意:当您连接了按钮时,您通常会在引脚上拉上拉并在引脚和地之间连接按钮 - 因此当按钮按下时引脚为低电平,当按钮未被按下时为高电平。在 arduino 上肯定会出现这种情况。因此,您必须更改上面的代码以等待负脉冲而不是正脉冲。:)

于 2015-02-21T08:11:28.630 回答