为 Arduino 创建库 [英] Creating a Library for an Arduino

查看:32
本文介绍了为 Arduino 创建库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一些代码放入使用另一个库的库中,SoftwareSerial.现在我将 SoftwareSerial.h 和 SoftwareSerial.cpp 文件添加到与我正在创建的库相同的文件夹中.

I have some code that I wanted to put into a library which uses another library, SoftwareSerial. Now I added the SoftwareSerial.h and SoftwareSerial.cpp files to the same folder as the library I'm creating.

我的头文件看起来像这样:

My header file looks something like this:

#ifndef MyLibrary_h
#define MyLibrary_h

#include "Arduino.h"
#include "SoftwareSerial.h"

#define MyLibrary_VERSION       1       // software version of this library


//DEFINE ALL CLASS VARIABLES

#define DATA_BUFFER_SIZE 50  //soft serial has 63 byte buffer.

class MyLibrary
{
    public:
        MyLibrary(uint8_t port_in, uint8_t port_out);
        float getSomeValue(uint8_t some_index);
    private:
        SoftwareSerial _serial;
                //Not sure if I should add the constructors below to the above declaration.
                //(uint8_t in_pin=4, uint8_t out_pin=5, bool logic_reversed = false);
        float convertSomeValue(byte upperbyte, byte lowerbyte);
        void flushSerialBuffer();
}; 

#endif

我的 .cpp 文件如下所示:

My .cpp file looks like this:

#include "Arduino.h"
#include "MyLibrary.h"
#include "SoftwareSerial.h"


MyLibrary::MyLibrary(uint8_t in_pin, uint8_t out_pin)
{

    bool logic_reversed = false;
    this->_serial(in_pin*, out_pin*, logic_reversed);
        //I tried the declaration below as well.
    //SoftwareSerial _serial(in_pin*, out_pin*, logic_reversed);
}

float MyLibrary::getSomeValue(uint8_t sensor_index) {
    float someValue = 1.1;
    return someValue;
}

float MyLibrary::convertSome(byte upperbyte, byte lowerbyte) {
    float someValue = 0.9;
    return someValue;
}

void MyLibrary::flushSerialBuffer() {
    //Flush serial buffer
    while(_serial.available())
        char c = _serial.read();
}

我希望 SoftwareSerial 成为 MyLibrary 中的私有字段(最好是静态的,但不是必要的),但我已经尝试了很多声明它,但似乎没有任何效果.我不断收到诸如 no matching function for call to 'SoftwareSerial::SoftwareSerial()invalid use ofqualified-name 'MyLibrary::_serial' 之类的错误.

I would like SoftwareSerial to be a private field in MyLibrary (preferably static but not necessary) but I've tried many was of declaring it but nothing seems to work. I keep getting errors like no matching function for call to 'SoftwareSerial::SoftwareSerial() or invalid use of qualified-name 'MyLibrary::_serial'.

我通过在我的 .h 文件中声明 static SoftwareSerial _serial;SoftwareSerial MyLibrary::_serial(4,5,false);在我的 .cpp 文件的顶部.问题是,我想在 MyLibrary 的构造函数中设置 _serial 的端口(这样我就可以创建一个 MyLibrary,它使用 SoftwareSerial 的特定输入/输出引脚)并且没有在.cpp 文件的顶部.

I got it to compile fine once by declaring static SoftwareSerial _serial; in my .h file, and SoftwareSerial MyLibrary::_serial(4,5,false); at the top of my .cpp file. The thing is, I would like to set the ports of _serial in my constructor for MyLibrary (so I can create a MyLibrary that uses specific in/out pins for SoftwareSerial) and not have them explicitly declared at the top of the .cpp file.

我对 C 编码和 Arduino 不太熟悉,所以如果有人能向我解释如何在 .h 文件中正确声明这些并在 MyLibrary 构造函数或MyLibrary.begin() 函数(或类似的东西).

I'm not so familiar with C coding and Arduino so it would be a great help if someone could explain to me how to declare these properly in the .h file and instanciate them with the correct ports in the MyLibrary constructor or a MyLibrary.begin() function (or something of the like).

预先感谢您的有用评论.

推荐答案

你需要的是让你的构造函数进行如下初始化:

What you need is to make your constructor do the initialization as follows:

class MyLibrary{
public:
   MyLibrary(uint8_t, uint8_t);
   //...
private:
   SoftwareSerial _serial;
   //...
};

MyLibrary::MyLibrary(uint8_t in, uint8_t out)
   : _serial(in, out)
{
   //do initialization
}

这种语法乍一看似乎很奇怪,但虽然它并不那么漂亮,但它清楚地区分了变量的初始化和对变量的操作,这是将初始化放在构造函数的主体中可能会稍微模糊.通常,除非您使用此语法来初始化成员变量,否则 C++ 将调用默认构造函数,如果该成员没有可调用的默认构造函数,则会导致编译错误.

This syntax might seem strange at first, but although it isn't quite as pretty, it clearly differentiates initialization of variables vs operations on variables, which is something that placing the initialization in the body of the constructor can make slightly fuzzy. As a rule, UNLESS you use this syntax to initialize a member variable, C++ will call the default constructor, which will cause a compile error iff the member does not have a callable default constructor.

这篇关于为 Arduino 创建库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆