【发布时间】:2019-03-11 16:01:56
【问题描述】:
我正在尝试将键盘功能调用到我自己的库中。在此之前,我有一个错误“无效使用非静态错误成员函数”并将该函数更改为静态函数。但是,由于键盘功能是非静态的,它仍然无法运行。
这是错误。
sketch\latch.cpp: In static member function 'static void latch::keypadEvent(KeypadEvent)': latch.cpp:18:11: error: invalid use of member 'latch::keypad' in static member function switch (keypad.getState()){ ^ In file included from sketch\latch.cpp:1:0: sketch\latch.h:20:12: note: declared here Keypad keypad; ^ exit status 1 invalid use of member 'latch::keypad' in static member function
我的代码
#include "latch.h"
latch doorlatch;
void setup(){
doorlatch.begin(9600);
}
void loop(){
doorlatch.main();
}
h 文件
#include <Keypad.h>
#ifndef _latch_
#define _latch_
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
class latch {
public:
latch();
void begin(int baudrate);
void main();
static void keypadEvent(KeypadEvent input);
Keypad keypad;
private:
const byte Rows = 4;
const byte Cols = 4;
char keys[4][4] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[4] = {7, 6, 5, 4};
byte colPins[4] = { 11, 10, 9, 8 };
};
#endif
cpp 文件
latch::latch():keypad( makeKeymap(keys), rowPins, colPins, Rows, Cols ) {
}
void latch::begin(int baudrate){
Serial.begin(baudrate);
keypad.addEventListener(keypadEvent);
}
void latch::main(){
keypad.getKey();
}
void latch::keypadEvent(KeypadEvent input){
switch (keypad.getState()){
case PRESSED:
Serial.print("Enter: ");
Serial.println(input);
delay(10);
}
}
有人可以帮我解决这个问题吗?还是我应该使用不同的方法?比如宣布为好友?
【问题讨论】:
标签: arduino