Creating a Library for an Arduino

醉酒当歌 提交于 2019-12-23 03:24:09

问题


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

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();
}

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

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.

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

Thanks in advance for you helpful comments.


回答1:


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
}

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.



来源:https://stackoverflow.com/questions/13169714/creating-a-library-for-an-arduino

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!