Arduino - passing values by reference from lamda to singleton

Viewed 13

Hello i am bigginer in programing and i have specific problem. I have been learning a new ways to write a code in small Arduino project. that project have multiple objects like distance measuring Senzor, led diods , temperature senzor, etc. And all this objects have its own menu where you can, for example, start a calibration or just get values. What i need is singleton class that has a function enter_esc() that need a int (*funct)() parameter basically function pointer. That enter_esc(int (*funct)()) function just looping function until you press escape pin which is defined. function Calibration() have inside some private: object data types like value or cali_value.

so i tried to insert function Calibration() right into enter_esc(Calibration) but it won't compile becouse i didnt pass that vlaues by reference or copy.

but what i found is lambda. i made a lamda similar to a Calibration() function and i passed values by reference &{//domething;} but i had to use enter_esc(std::function<int()>& funct) whitch is only int C++ standard library and not in Arduino C/C++ so my qestion is:

[is there some way how to pass values by reference by using lambda to a singleton class in Arduino ?]

(i konw it can be done differently but like i said i want to learn some new ways to program, also if you have some different way to make it i will by very happy to see it)

10Q for your time :)

//Class.h     
#pragma once


class events {
private:
    static events e_instance;
    int p_menu, p_enter, p_esc, p_up, p_down;
    int menuValue;
    events();
public:
    events(const events&) = delete;
    static events& Get();
    


    int ArrowUpDown(int maxVal);
    int ArrowUpDown(int p_up, int p_down, int maxVal);

  
    int enter_esc(const std::function<int()>& funct);
};
events events::e_instance;

class deviceBase : public Printables
{
public:
    const char* a_pin;
    int d_pin;
    String type;
    String deviceName;
    bool inUse;
    int actualCount;

public:
    String getType() override;
    int getActualCount() override;
    String getName() override;
    String getInUse() override;
};

class senzor : public deviceBase
{
private:

    int Value;
    int triggValue;
public:
    int p_triggValue = 10;
    static int allSenzors;
    friend events;
    senzor();
    ~senzor();
public:

    int getValue();
    int Calibration();
    void changeTriggVal(int x);
    void Reset();
    void nullCalibration();

    void Menu(int x);
    void setName(String deviceName);
    void setInUse(bool x);

    int getPin();
};
int senzor::allSenzors = 0;

if you have some good advice to my code writing i will be also very glad

    //Class.cpp
        #include <iostream>
        #include <string>
        #include <functional>
        
        #define LOG(x) std::cout << x << std::endl;
        
        #define PINMENU 12
        #define PINENTER 8
        #define PINESC 9
        #define PINUP 11
        #define PINDOWN 13
        
        using String = std::string;
        struct Printables
        {
            virtual String getType() = 0;
            virtual int getActualCount() = 0; ;
            virtual String getName() = 0;
            virtual String getInUse() = 0;
        };
        
        #include "Class.h"
         events& events::Get() {
            return e_instance;
        }
        
        int events::ArrowUpDown(int maxVal) {
            if (maxVal) {
                menuValue = menuValue < maxVal ? menuValue++ : menuValue;
            }
            if (maxVal) {
                menuValue = menuValue > 0 ? menuValue-- : menuValue;
            }
            return menuValue;
        }
        
        
        int events::enter_esc(const std::function<int()>&funct) {
            if (1) {
                while (!p_esc) {
                    auto f = funct;
                   
                }
            }
                    return 1;
        }
        int events::ArrowUpDown(int p_up, int p_down, int maxVal) { return 666; }
        
        events::events() {};
        
        String deviceBase::getType() { return type; }
        int deviceBase::getActualCount() { return actualCount; }
        String deviceBase::getName() { return deviceName; }
        String deviceBase::getInUse() {
            String Status;
            Status = inUse == 1 ? "Active" : "Deactive";
            return Status;
        }
        senzor::senzor() : Value(0), triggValue(1) {
            a_pin = "xx";
            type = "[SENZOR]";
            deviceName = "[UNKNOWN]";
            inUse = 0;
            allSenzors++;
            actualCount = allSenzors;
            a_pin = 0;
        }
        
        senzor::~senzor() {
            allSenzors = 0;
        }
        
        int senzor::getValue() {
            Value = 4;
            return Value;
        }
        
            int senzor::Calibration() {
            triggValue = triggValue < getValue() ? getValue() : triggValue;
            p_triggValue = triggValue;
            return p_triggValue;
        }
        void senzor::changeTriggVal(int x) {
            p_triggValue = x;
        }
        void senzor::Reset() {
            p_triggValue = triggValue;
        }
        void senzor::nullCalibration() {
            triggValue = 1;
        }
        void senzor::setName(String deviceName) {
            this->deviceName = deviceName;
        }
        
        void senzor::setInUse(bool x) {
            inUse = x;
        }
        int senzor::getPin() {
            return 4;
        }
        int printsss() {
            return 1;
        }
////////////////////////////////this what i was writing about//////////////////////////////
        void senzor::Menu(int x) {
            
            events::Get().enter_esc([&]() { triggValue = triggValue < getValue() ? getValue() : triggValue;
            p_triggValue = triggValue;
            return p_triggValue; });
        
        }

but if i use lambda in arduino with enter_esc(int (*funct)()) i get this kind of error

no matching function for call to 'events::enter_esc(senzor::Menu(int)::<lambda()>)'
0 Answers
Related