Basic coding question on Classes

Status
Not open for further replies.

AdmiralCrunch

Well-known member
Hi

I do initialize some classes and a button called "btShift":

Code:
#include <Arduino.h>

#include <cfg.h>
cfg cfg;

#include <Bounce.h>
Bounce btnShift = Bounce(cfg.btnShiftPin, 10);  // 10 ms debounce

#include <ctrlMain.h>
ctrlMain ctrlMain;

now I need to access a variable from cfg in a function of ctrlMain.
also I need to access btnShift in a function of ctrlMain.

Is this possible? If so, how would I do that?
 
@AdmiralCrunch - After 156 messages, you've *still* not noticed the "Forum Rule"? It's in red at the top of every page.

Look, you've asked many questions here, even cases of the same problem, and we always try to help you. The least you could do is put a little more effect into your questions, to follow this important rule. Posting a complete program (so anyone can copy the code into Arduino and run it on a real board) saves everyone a lot of time and lets us help you better and faster. Please, follow the Forum Rule. Do not keep posting these low-effort questions without complete code, which waste everyone's time by needing many followup questions just to get enough info you help you.
 
@AdmiralCrunch
If I were you,
I would make a very simple sketch that focuses on the C++ programming.
so we can see and play with the code.

Usually, by reducing code to bare minimum, you will
a) solve most issues yourself
b) have code that can be used to formulate a precise question and allows to search documentation.


Edit:

Even if I do not know what you wanted to do, here is an example I just did

file A.h
Code:
#ifndef _A_H_
#define _A_H_
class A
{ int var1;

  public: 
  static int var2;
};
#endif

file B.h
Code:
#ifndef _B_H_
#define _B_H_
class B
{
  public:
  static void test(void) { Serial.println("A.test");}
};
#endif

file C.h
Code:
#ifndef _C_H_
#define _C_H_
class C
{
  public:
  void test(void) 
  { Serial.println(A::var2);
    B::test();
  }
};
#endif

sketch
Code:
#include "A.h"
A a;
int A::var2 = 1;

#include "B.h"
B b;
#include "C.h"
C c;

void setup() {
  // put your setup code here, to run once:
  while(!Serial);
  c.test();
}

void loop() {
  // put your main code here, to run repeatedly:

}

I do not know if it running, but it compiles.
BTW, the compiler told me about static and initalization
 
Last edited:
@Paul
.. yes I know, sorry :/ .. I'll try to follow better. I am really thankfull for the help here. Without this forum I would be able to do nothing :) I try to learn as much as I can, but unfortunately between wife+kids, job, life, I really have little time for my projects. Sometimes just, like in this case, I am so noob I just don't know what to post to describe the problem accurately..

I promise to do better :)

The example from @WMXZ works but I don't get it managed to adapt it to my needs :/
.. I thought pointers or references would be the right way (which I also don't know how to make)

so I have this main.cpp
Code:
#include <Arduino.h>
#include <Bounce.h>

/* ### CONFIG ### */
#include <cfg.h>
cfg cfg; 

/* ### UTILS ### */
#include <utils.h>
utils utils;


Bounce btnShift = Bounce(cfg.btnShiftPin, 10);

void setup() {
    
}

void loop() {
    utils.pollShift();  
}
.. I want a function from utils-class to check if btnShift was pressed.


this utils.h
Code:
#ifndef utils_h
#define utils_h
#include "Arduino.h"

class utils {
    
  public:      
    utils();  

    void pollShift();   

  private:    
    
};
#endif

with this utils.cpp
Code:
#include "Arduino.h"
#include "utils.h"

utils::utils() { }

/* Polls Shift-Button and sets Status-Var */
void utils::pollShift() {
    if (btnShift::update()) {
        if (btnShift::fallingEdge()) {
            cfg::shiftStatus = true;
            if(cfg::debug == true) {
                Serial.println("Shift pressed..");
            }          
        } else {
            cfg::shiftStatus = false;
            if(cfg::debug == true) {
                Serial.println("Shift released..");
            }  
        }
    }
}

.. this utils.cpp fails because btnShift and cfg are not declared.


this is the cfg.h
Code:
#ifndef cfg_h
#define cfg_h
#include "Arduino.h"

class cfg {
    
  public:  
    
    cfg();          
  
    int btnShiftPin = 14;

    int shiftStatus;
    bool debug = true;  

  private:    
    
};
#endif
 
Status
Not open for further replies.
Back
Top