首先,一些背景。我已经购买了一些Avago HCMS-29xx LED 显示器。有一个用于控制它们的 Arduino 库,但我想使用树莓派。所以我已经fork了原库的 GitHub 并开始移植。
库端口主要用于基本的打印示例,但我将字符串传递给库的方式充其量是被破解的。所以我做了一些搜索,找到了一个示例,展示了如何使用 Stream 类作为我的库类的基础,并使用 printf() 将字符打印到我的显示器上。
这是示例:
//New class setup to drive a display
class NEWLCDCLASS : public Stream //use Stream base class
{
……
// lots of code for the new LCD class - not shown here to keep it simple
.....
//and add this to the end of the class
protected
//used by printf - supply a new _putc virtual function for the new device
virtual int _putc(int c) {
myLCDputc(c); //your new LCD put to print an ASCII character on LCD
return 0;
};
//assuming no reads from LCD
virtual int _getc() {
return -1;
}
和我的代码:
#ifndef LedDisplay_h
#define LedDisplay_h
#include <cstring>
#include <cstdint>
#include <cstdio>
namespace LedDisplay
{
class LedDisplay : public Stream
{
//lots of code to drive the display
.....
protected:
//used by printf - supply a new _putc virtual function for the new device
virtual int _putc(int c) {
write(c); //your new LCD put to print an ASCII character on LCD
return 0;
};
//assuming no reads from LCD
virtual int _getc() {
return -1;
}
};
}
#endif
但是当我尝试编译时,它给出了一个错误
In file included from print.cpp:1:0:
LedDisplayPi.h:20:1: error: expected class-name before ‘{’ token
{
^
print.cpp: In function ‘int main()’:
print.cpp:38:13: error: ‘class LedDisplayNs::LedDisplay’ has no member named ‘printf’; did you mean ‘write’?
myDisplay.printf("%s",helloWorldstring.c_str());
^~~~~~
我究竟做错了什么?这甚至是使用标准 printf() 函数的明智方法吗?