繁体   English   中英

Arduino:将函数传递给类,返回String

[英]Arduino: pass function to class, returning String

我正在尝试让我的Arduino类返回带有各种信息的String消息以进行记录。 经过大量的试验和错误,我设法将对日志记录函数的引用传递给类,但是只能获取char *而不是String,并且我希望能够发送Strings,从而使发送所有内容变得更加容易各种数据。

我已经开始工作了。 草图:

#include <Test.h>
#include <string.h>

void setup() {
  Serial.begin(115200);
  Test t;
  t.setLogging(writeLog);
  writeLog("Test message!" + String(" .... "));
  t.doSomething("This is useful.");
  t.doSomething("This as well.\n");
  t.doSomething("This is even more useful.\n");
  bool b = true;

}

void loop() {
}

void writeLog (char* message) {
  Serial.print("char function: ");
  Serial.print(message);
}

void writeLog (String message) {
  Serial.print("String function: ");
  Serial.println(message);
}

头文件:

#ifndef TEST_h
#define TEST_h

class Test
{
  public:
    Test();       // The constructor.
    void setLogging(void (*)(char*)); // Takes function setting where to log.
    void doSomething(char*);
};

#endif

班级:

#include <Test.h>

typedef void (*LogFunction)(char*);
LogFunction writeLog;

Test::Test () { 
}

void Test::doSomething (char* s) {

  // Do something useful and log the result.
  writeLog(s);
}

void Test::setLogging (void (*f)(char*) ) {
  writeLog = f;
  return;
}

现在,我希望我的班级能够做的是像String这样发送信息,而不是char *(我也没有找到一种简单的方法来将“ anything”转换为char *然后串联两个或多个字符串):

writeLog ("HydroMonitorECSensor::setCalibration  Receiving calibration - haveCalibration = " + String(haveCalibration));
writeLog ("HydroMonitorECSensor::setCalibration  calibratedSlope = " + String(calibratedSlope));
writeLog ("HydroMonitorECSensor::setPins  capPos set to " + String(capPos));

haveCalibration是一个bool (当String变为“ true”或“ false”)时, calibratedSlopedoublecapPosint 这样,我可以轻松干净地将完整的行发送到记录器。 在主脚本中效果很好-并非来自类。

我尝试简单地将char*更改为String并将#include <string.h>添加到库文件中,但是它不起作用。

然后在Test.cpp中,我得到void Test::setLogging (void (*f)(String) ) {和在Test.h中, void setLogging(void (*)(String)); 现在我收到错误消息:

In file included from /home/wouter/Arduino/libraries/Test/Test.cpp:1:0:
/home/wouter/Arduino/libraries/Test/Test.h:10:29: error: expected ',' or '...' before '(' token
     void setLogging(void (*)(String)); // Takes function setting where to log.
                             ^
/home/wouter/Arduino/libraries/Test/Test.cpp:16:40: error: variable or field 'setLogging' declared void
 void Test::setLogging (void (*f)(String) ) {
                                        ^
/home/wouter/Arduino/libraries/Test/Test.cpp:16:31: error: 'f' was not declared in this scope
 void Test::setLogging (void (*f)(String) ) {
                               ^
/home/wouter/Arduino/libraries/Test/Test.cpp:16:34: error: 'String' was not declared in this scope
 void Test::setLogging (void (*f)(String) ) {
                                  ^
exit status 1
Error compiling for board NodeMCU 1.0 (ESP-12E Module).

有什么建议吗?

其他信息,也许很重要:我正在使用Arduino IDE并针对ESP8266进行编译。

您正在使用的Arduino提供的String类,但不包括Arduino.h在头test.h头文件。 这导致它找不到String类,并且编译失败。

以下作品:

main.cpp

#include <Arduino.h>
#include <test.hpp>

void writeLog (char* message);
void writeLog (String message);

void setup() {
  Serial.begin(115200);
  Test t;
  t.setLogging(writeLog);
  writeLog("Test message!" + String(" .... "));
  t.doSomething("This is useful.");
  t.doSomething("This as well.\n");
  t.doSomething("This is even more useful.\n");
  bool b = true;

}

void loop() {
}

void writeLog (char* message) {
  Serial.print("char function: ");
  Serial.print(message);
}

void writeLog (String message) {
  Serial.print("String function: ");
  Serial.println(message);
}

test.hpp

#ifndef TEST_h
#define TEST_h

#include <Arduino.h> //for "String" class

//Typdef for the log function. Takes a String, returns nothing
typedef void (*LogFunction)(String);

class Test
{
  public:
    Test();       // The constructor.
  //  void setLogging(void (*)(char*)); // Takes function setting where to log.
    void setLogging(LogFunction); //use the typedef here
    void doSomething(char*);
};

#endif

test.cpp

#include <test.hpp>


LogFunction writeLog;

Test::Test () {
}

void Test::doSomething (char* s) {

  // Do something useful and log the result.
  writeLog(s);
}

//void Test::setLogging (void (*f)(char*) ) {
void Test::setLogging (LogFunction f) { //also use typedef here
  writeLog = f;
  return;
}

在可能出现的其他情况中,编译器告诉您它无法解析标识符String 这可能有几个原因:首先,您编写String ,而不是string (请注意您在写作中使用大写字母)。 其次,如果您编写的是string而不是std::string ,则除非您using namespace std (出于多种原因而并非首选变量)或using std::string进行声明,否则无法解析该using std::string 第三,类std::string在标头<string>声明,这与<string.h>不同。

所以我会写#include <string>然后使用std::string

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM