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

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

问题描述

我有一些code,我想投入​​它使用另一个库出库, 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私有字段(preferably静态但不是必要的),但我试过很多是它声明,但似乎没有任何工作。我不断收到错误,如呼叫为SoftwareSerial :: SoftwareSerial()无效使用合格的名'在MyLibrary :: _系列'不匹配函数

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'.

我把它编译罚款一次通过声明静态SoftwareSerial _serial; 在我的.h文件中,而 SoftwareSerial在MyLibrary :: _串行(4, 5,假); 我的.cpp文件的顶部。关键是,我想设置 _serial 的港口在我的构造在MyLibrary(这样我就可以创建一个使用特定在SoftwareSerial /输出引脚在MyLibrary),而不是让他们明确在.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
}

本语法可能看起来很奇怪在第一,但虽然它并不像pretty,它明确区分的初始化的变量VS的操作的有关变量,是什么,将初始化在构造函数体内可以稍微模糊。作为一项规则,除非你使用这个语法来初始化成员变量,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天全站免登陆