What is the correct way to use a library inside a class?

Hey all,

I'd like to use a library inside of a library I'm creating. Specifically, the ESP32 'Preferences' library. I have my external .h and .cpp files which contain my class, but I'm not sure where in my code I should instantiate the 'preferences' object, and where I should call the .begin() method? I have included the library I want to use in both the .h and .cpp, is that correct? Obviously I don't want to place any of this code within the main sketch so I can reuse this library.

Not sure a code snippet helps but can do if required.

Cheers!

They don't. But a small, complete example would.

Header file (.h)
Writes class declarations (including declarations of members and methods inside the class), function prototypes, #define constants, etc., but generally does not write out the concrete implementation.
It is important to note when writing header files that pre-compiled statements (as follows) must be added at the beginning and at the end in the following style.

 #ifndef CIRCLE_H
 #define CIRCLE_H

 // Your code is written here

 #endif

This is done to prevent duplicate compilation, without which there is a risk of errors.
As for the name CIRCLE_H, it doesn't really matter, you can call it whatever you want, as long as it conforms to the specification. In principle, it is highly recommended to write it in this form, because it is easier to correspond to the name of the header file.

Source file (.cpp)
The source file mainly writes the specific code that implements those functions that have been declared in the header file. It is important to note that you must #include the implemented header file at the beginning, as well as the header file to be used. Then when you need to use the classes in the header file you wrote, you just need to #include them.

I'm not sure my question is being grasped, here it is worded differently:

In the example below, where do I put preferences.begin();? And is the location of

#include <Preferences.h>
Preferences preferences;

correct?

Thanks

Code:

/* file: menu-option.h */

#include "Arduino.h"

#ifndef MenuOption_h
#define MenuOption_h

class MenuOption {
  public:
    void Init(int pos, String title, String systemName, String type, float defaultVal, float maxVal, float minVal, bool showInMenu, bool saveInFlash); //bool, int, or float
    void adjustVal(bool increment);
    int pos();
    String title();
    String getType();
    float defaultVal();

    void setBoolVal(bool value);
    void setIntVal(int value);
    void setFloatVal(float value);
    bool boolVal();
    int intVal();
    float floatVal();
    
    bool showInMenu();
    bool saveInFlash();
    
  private:
    int _pos;
    String _title;
    String _systemName;
    String _type;
    float _defaultVal;
    bool _boolVal;
    int _intVal;
    float _floatVal;
    float _maxVal;
    float _minVal;
    bool _showInMenu;
    bool _saveInFlash;
};

#endif
/* file: menu-option.cpp */

#include "Arduino.h"
#include "menu-option.h"

// is this valid here?
#include <Preferences.h>
Preferences preferences;

//class constructor
void MenuOption::MenuOption(int pos, String title, String systemName, String type, float defaultVal, float maxVal, float minVal, bool showInMenu, bool saveInFlash) {
  _pos = pos;
  _title = title;
  _systemName = systemName;
  _type = type;
  _defaultVal = defaultVal;
  _maxVal = maxVal;
  _minVal = minVal;
  _showInMenu = showInMenu;
  _saveInFlash = saveInFlash;
  
  // load the last known setting from flash
  if (_type == "bool") {
    _boolVal = preferences.getBool(_systemName, _defaultVal == 1 ? true : false);
  } else if (_type == "int") {
    _intVal = preferences.getUInt(_systemName, Int(_defaultVal));
  } else if (_type == "float") {
    _floatVal = preferences.getFloat(_systemName, _defaultVal);
  }  
}

void MenuOption::adjustVal(bool increment) {
  if (_type == "bool") {
    _boolVal = !_boolVal;
    preferences.putBool(_systemName, _boolVal) //save to flash
  }
  else if (_type == "int") {
    if (increment) {
      _intVal++;
      if (_intVal > int(_maxVal)) {
        _intVal = int(_minVal);
      }
    } else {
      _intVal--;
      if (_intVal < int(_minVal)) {
        _intVal = int(_maxVal);
      }
    }
    preferences.putUInt(_systemName, _intVal) //save to flash
  }
  else if (_type == "float") {
    if (increment) {
      _floatVal = _floatVal + 0.5;
      if (_floatVal > _maxVal) {
        _floatVal = _minVal;
      }
    } else {
      _floatVal = _floatVal - 0.5;
      if (_floatVal < _minVal) {
        _floatVal = _maxVal;
      }
    }
    preferences.putFloat(_systemName, _floatVal) //save to flash
  }
}

int MenuOption::pos() {
  return _pos;
}
String MenuOption::title() {
  return _title;
}
String MenuOption::getType() {
  return _type;
}
bool MenuOption::defaultVal() {
  return _defaultVal;
}
void MenuOption::setBoolVal(bool value) {
  _boolVal = value;
}
void MenuOption::setIntVal(int value) {
  _intVal = value;
}
void MenuOption::setFloatVal(float value) {
  _floatVal = value;
}
bool MenuOption::boolVal() {
  return _boolVal;
}
int MenuOption::intVal() {
  return _intVal;
}
float MenuOption::floatVal() {
  return _floatVal;
}
bool MenuOption::showInMenu() {
  return _showInMenu;
}
bool MenuOption::saveInFlash() {
  return _saveInFlash;
}

You should list dependencies in your library.properties file.

To get the best help your example code should be complete. It should compile or at least show the same compile errors as you are seeing.

Your code is incomplete. Where is the implementation of the Init() function?

Also, show an example main .ino file that uses the new class as intended.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.