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

查看:335
本文介绍了为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中的私有字段(最好是静态但不是必需的),但我已经尝试过许多声明它,但似乎没有什么工作。我不断收到错误像没有匹配的函数调用'SoftwareSerial :: SoftwareSerial()无效使用qualified-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'.

我通过声明 static SoftwareSerial _serial; 我的.h文件和 SoftwareSerial MyLibrary :: _ serial(4,5,false); 在我的.cpp文件的顶部。事情是,我想在MyLibrary的构造函数中设置 _serial 的端口(所以我可以创建一个MyLibrary,它使用SoftwareSerial的特定in / out引脚),而不是他们明确声明在.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天全站免登陆