[C++] Klassen aus anderer Datei

Gast #6087886
Lesenswert?

Hallo,

wie kann man in C++ (Arduino) Klassen aus einer anderen Datei verwenden?

z.B.:
dispay_Utils.h
1
#ifndef DISPLAYUTILS_H
2
#define DISPLAYUTILS_H
3

4
#include <Arduino.h>
5
#include "config.h"
6

7
class displayUtils {
8
  public:
9
    displayUtils();
10
    
11
};
12

13
#endif

display_Utils.cpp
1
#include "display_utils.h"
2
#include <LiquidCrystal_I2C.h>
3
#include <LCDMenuLib2.h> //https://github.com/Jomelo/LCDMenuLib2
4

5
//LCDMenuLib2_menu LCDML_0 (255, 0, 0, NULL, NULL); // root menu element (do not change)
6
//extern LCDMenuLib2 LCDML(LCDML_0, _LCDML_DISP_rows, _LCDML_DISP_cols, lcdml_menu_display, lcdml_menu_clear, lcdml_menu_control);
7
LiquidCrystal_I2C lcd(0x27,_LCDML_DISP_cols,_LCDML_DISP_rows);
8
//extern LCDML;
9
displayUtils::displayUtils(){
10
}
11

12
// =====================================================================
13
//
14
// Output function
15
//
16
// =====================================================================
17

18
void lcdml_menu_control(void)
19
{
20
  // If something must init, put in in the setup condition
21
  if(LCDML.BT_setup()) {
22
    // runs only once
23
  }
24

25
  // check if new serial input is available
26
  if (Serial.available()) {
27
    // read one char from input buffer
28
    /*switch (Serial.read())
29
    {
30
      case _LCDML_CONTROL_serial_enter:  LCDML.BT_enter(); break;
31
      case _LCDML_CONTROL_serial_up:     LCDML.BT_up();    break;
32
      case _LCDML_CONTROL_serial_down:   LCDML.BT_down();  break;
33
      case _LCDML_CONTROL_serial_left:   LCDML.BT_left();  break;
34
      case _LCDML_CONTROL_serial_right:  LCDML.BT_right(); break;
35
      case _LCDML_CONTROL_serial_quit:   LCDML.BT_quit();  break;
36
      default: break;
37
    }*/
38
  }
39
}
40

41
/* ******************************************************************** */
42
void lcdml_menu_clear()
43
/* ******************************************************************** */
44
{
45
  lcd.clear();
46
  lcd.setCursor(0, 0);
47
}
48

49
/* ******************************************************************** */
50
void lcdml_menu_display()
51
/* ******************************************************************** */
52
{
53
  // update content
54
  // ***************
55
  if (LCDML.DISP_checkMenuUpdate()) {
56
    // clear menu
57
    // ***************
58
    LCDML.DISP_clear();
59

60
    // declaration of some variables
61
    // ***************
62
    // content variable
63
    char content_text[_LCDML_DISP_cols];  // save the content text of every menu element
64
    // menu element object
65
    LCDMenuLib2_menu *tmp;
66
    // some limit values
67
    uint8_t i = LCDML.MENU_getScroll();
68
    uint8_t maxi = _LCDML_DISP_rows + i;
69
    uint8_t n = 0;
70

71
    // check if this element has children
72
    if ((tmp = LCDML.MENU_getDisplayedObj()) != NULL)
73
    {
74
      // loop to display lines
75
      do
76
      {
77
        // check if a menu element has a condition and if the condition be true
78
        if (tmp->checkCondition())
79
        {
80
          // check the type off a menu element
81
          if(tmp->checkType_menu() == true)
82
          {
83
            // display normal content
84
            LCDML_getContent(content_text, tmp->getID());
85
            lcd.setCursor(1, n);
86
            lcd.print(content_text);
87
          }
88
          else
89
          {
90
            if(tmp->checkType_dynParam()) {
91
              tmp->callback(n);
92
            }
93
          }
94
          // increment some values
95
          i++;
96
          n++;
97
        }
98
      // try to go to the next sibling and check the number of displayed rows
99
      } while (((tmp = tmp->getSibling(1)) != NULL) && (i < maxi));
100
    }
101
  }
102

103
  if(LCDML.DISP_checkMenuCursorUpdate())
104
  {
105
    // init vars
106
    uint8_t n_max             = (LCDML.MENU_getChilds() >= _LCDML_DISP_rows) ? _LCDML_DISP_rows : (LCDML.MENU_getChilds());
107
    uint8_t scrollbar_min     = 0;
108
    uint8_t scrollbar_max     = LCDML.MENU_getChilds();
109
    uint8_t scrollbar_cur_pos = LCDML.MENU_getCursorPosAbs();
110
    uint8_t scroll_pos        = ((1.*n_max * _LCDML_DISP_rows) / (scrollbar_max - 1) * scrollbar_cur_pos);
111

112

113
    // display rows
114
    for (uint8_t n = 0; n < n_max; n++)
115
    {
116
      //set cursor
117
      lcd.setCursor(0, n);
118

119
      //set cursor char
120
      if (n == LCDML.MENU_getCursorPos()) {
121
        lcd.write(_LCDML_DISP_cfg_cursor);
122
      } else {
123
        lcd.write(' ');
124
      }
125

126
      // delete or reset scrollbar
127
      if (_LCDML_DISP_cfg_scrollbar == 1) {
128
        if (scrollbar_max > n_max) {
129
          lcd.setCursor((_LCDML_DISP_cols - 1), n);
130
          lcd.write((uint8_t)0);
131
        }
132
        else {
133
          lcd.setCursor((_LCDML_DISP_cols - 1), n);
134
          lcd.print(' ');
135
        }
136
      }
137
    }
138

139
    // display scrollbar
140
    if (_LCDML_DISP_cfg_scrollbar == 1) {
141
      if (scrollbar_max > n_max) {
142
        //set scroll position
143
        if (scrollbar_cur_pos == scrollbar_min) {
144
          // min pos
145
          lcd.setCursor((_LCDML_DISP_cols - 1), 0);
146
          lcd.write((uint8_t)1);
147
        } else if (scrollbar_cur_pos == (scrollbar_max - 1)) {
148
          // max pos
149
          lcd.setCursor((_LCDML_DISP_cols - 1), (n_max - 1));
150
          lcd.write((uint8_t)4);
151
        } else {
152
          // between
153
          lcd.setCursor((_LCDML_DISP_cols - 1), scroll_pos / n_max);
154
          lcd.write((uint8_t)(scroll_pos % n_max) + 1);
155
        }
156
      }
157
    }
158
  }
159
}

Ein paar Zeilen aus der main.cpp
1
#include <LCDMenuLib2.h> //https://github.com/Jomelo/LCDMenuLib2
2
#include <Wire.h>
3
#include <LiquidCrystal_I2C.h>
4
#include "display_utils.h"
5

6
displayUtils dpUtils();
7

8
LCDMenuLib2_menu LCDML_0 (255, 0, 0, NULL, NULL); // root menu element (do not change)
9
LCDMenuLib2 LCDML(LCDML_0, _LCDML_DISP_rows, _LCDML_DISP_cols, lcdml_menu_display, lcdml_menu_clear, lcdml_menu_control);
Gast #6088218
Lesenswert?

Ich verstehe das Problem auch nicht.
Erst erzeugst du locker 100 Zeilen komplexen Code, und dann weisst du 
nicht, wie man den aufruft?

Vielleicht doch erst einmal ein gutes C++ Buch lesen?
Bücher sind doch gerade zur Weihnachtszeit hoch im Kurs.
Beitrag #6155298 wurde von einem Moderator gelöscht.

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren