Hi,
I tried in Arduino IDE2 sketch with Adafruit GFX library. It works Ok. Then I made also header and cpp file with this function. It come error: 'display' was not declared in this scope;. If I rename the cpp extetion to ino compilation is OK. Can you hlep me please, what is proble?
Please explain.
Show your program; make sure to use code tags.
Also make sure to clearly mark the code (files) when posting them.
PS
It's considered impolite to modify your opening post after somebody replied.
at compile time .ino files are combined into one file and compiled together - hence your functions in the other .ino file(s) are found OK
.cpp files are compiled separately and and then linked at link time
if you have definitions (data and functions) in a .cpp file you need to declare them extern and declare function prototypes in your .ino files
extern declarations and function protoypes are generally in a header file
For a basic guide to using multiple .cpp / .h files, see My Reply #5 in this Thread.
Hi, thank you very much for answers.
I am new in C, C++ and Arduino. I worked before as hardware developer. Now I am 70 and I say it is long way to 100, then I start with programming. Before I made some programs in assembler I8080, I8051,PIC.
I tray made in my header file extern for may new function, but this doesn't work:
extern void test_func(void);
I hoped in header file in library Adafruit GFX have the function extern declaration. Than I have to for every used function made extern in header file?
extern applies to variables, not functions. It's time to post your real, complete code, otherwise no further help is possible.
Hi, I sent my files
OLED_SSD1306_GFX.ino (1,8 KB)
Menu_Oled.h (2,2 KB)
OLED_SSD1306_GFX.ino (1,8 KB)
OK, now post them properly. Inline. With Code Tags:
Not quite what I meant
menu_Oled.h
#ifndef Menu_Oled_H
#define Menu_Oled_H
//#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
//display oled constants
#define I2C_SDA 5
#define I2C_SCL 4
//gpio for buttons
#define BUTTON_UP 15
#define BUTTON_ENTER 7
#define BUTTON_DW 6
//uncomment type desired of display OLED
//#define SSD1306_OLED_TYPE
//#define SSD1309_OLED_TYPE
/*
#ifdef SSD1306_OLED_TYPE
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
#endif
*/
//#ifdef SSD1309_OLED_TYPE
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
//#endif
#define CHAR_WIDTH 6
#define CHAR_IN_LINE SCREEN_WIDTH / CHAR_WIDTH //char on one line
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library.
// On an arduino UNO: A4(SDA), A5(SCL)
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
// On an arduino LEONARDO: 2(SDA), 3(SCL), ...
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
//constants for funtion nameWrite
#define CUR_ENTER -1 // cursor position Enter text button
#define CUR_BACK -2 // cursor position Backslash text button
#define CUR_EXIT -3 // cursor position Exit text button
#define CUR_CYKLE -4 // cursor position Cyckle, go to end of text
#define BUF_VAL 1 // buffer is valid
#define BUF_NON 0 // buffer not valid
#define GLYPH_HIGHT 8 //high of character
#define GLYPH_WIDTH 5 + 1 // with of character + 1 pixel space
//declaration of object display of class Adafruit_SSD1306
//by parametrical constructor by declaration display have to call constructor.
//or shorted notation Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Adafruit_SSD1306 display = Adafruit_SSD1306(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const char text[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPRSTUVWXYZ1234567890-=`[]';,./!@#$%^&*()_+{}:|<>?";
int nameWrite(const char *p_STR, char buf[], uint8_t bufLength);
#endif
OLED_SSD_GFX.ino
/**************************************************************************
This is an example for our Monochrome OLEDs based on SSD1306 drivers
Pick one up today in the adafruit shop!
------> http://www.adafruit.com/category/63_98
This example is for a 128x32 pixel display using I2C to communicate
3 pins are required to interface (two I2C and one reset).
Adafruit invests time and resources providing this open
source code, please support Adafruit and open-source
hardware by purchasing products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries,
with contributions from the open source community.
BSD license, check license.txt for more information
All text above, and the splash screen below must be
included in any redistribution.
**************************************************************************/
#pragma GCC optimize("O0")
//#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "Menu_Oled.h"
uint8_t xLoc = 0;
uint8_t yLoc = 0;
uint8_t curLoc = 0;
void setup() {
Serial.begin(57600);
delay(1000);
while (!Serial)
;
Serial.flush();
Serial.println();
Serial.println("nic nefunguje");
//init buttons
pinMode(BUTTON_UP, INPUT);
pinMode(BUTTON_ENTER, INPUT);
pinMode(BUTTON_DW, INPUT);
Wire.begin(I2C_SDA, I2C_SCL); //define I2C pins
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;)
; // Don't proceed, loop forever
}
}
void loop() {
// Clear the buffer
display.clearDisplay();
display.setCursor(0, 0);
display.setTextSize(1);
yLoc = 0;
char nameBuf[CHAR_IN_LINE + 1]; //temporaly buffer for name string
nameWrite(text, nameBuf, CHAR_IN_LINE);
while (1)
;
}
Why do you have a second ino file with the same name?
Hi, thanks with help correct show my files.The ino file main is OLED_SSD1306_GFX.ino and tag is Menu_Oled.
if I use for Menu_Oled extention .cpp come error 'display' was not declared in this scope;
file: OLED_test_tag.ino
#pragma GCC optimize("O0")
//#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "Menu_Oled.h"
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
//display oled constants
#define I2C_SDA 5
#define I2C_SCL 4
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
//declaration of object display class type Adafruit_SSD1306
//by parametrical constructor by declaration display have to call constructor.
//or shorted notation Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Adafruit_SSD1306 display = Adafruit_SSD1306(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
Serial.begin(57600);
delay(1000);
while (!Serial)
;
Wire.begin(I2C_SDA, I2C_SCL); //define I2C pins
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;)
; // Don't proceed, loop forever
}
}
void loop() {
// Clear the buffer
display.clearDisplay();
display.setCursor(0, 0);
display.setTextSize(1);
display.setTextColor(WHITE, BLACK);
//display.println("print from main");
test_func();
display.display();
delay(200);
while (1)
;
}
file: Menu_Oled.h
#ifndef Menu_Oled_H
#define Menu_Oled_H
//#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
extern void test_func(void);
#endif
file:Menu_Oled.cpp
```cpp
#include "Menu_Oled.h"
void test_func(void) {
display.setTextColor(WHITE, BLACK);
display.print("println from cpp");
}
```cpp
#include "Menu_Oled.h"
void test_func(void) {
display.setTextColor(WHITE, BLACK);
display.print("println from cpp");
}
For which board are you compiling?
I now have two files; does the below reflect your setup?
OLED_SSD306_GFX.ino
Menu_Oled.cpp
I use some cheep Alliexpres ESP32_S3_DEVKITC_1
The code in your .cpp file has no idea about the existence of the ''Adafruit_SSD1306object nameddisplay`` that was defined in your main .ino file.
You could pass it to the test_func() as a C++ reference:
//#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
void test_func(Adafruit_SSD1306 &display);
void test_func(Adafruit_SSD1306 &display) {
}
Call it as:
test_func(display);
OK, I will download your new files.
The basic problem is that what is known in your ino file is not known in other files (.and vice versa); hence display is unknown in the cpp file.
I thought enough there are all include files in header file.
But that doesn't tell the compiler that there is a display object somewhere.
You need to tell the compiler when it compiles the Menu_Oled.cpp that it exists.
One way is to modify Menu_Oled.cpp
#include "Menu_Oled.h"
// tell the compiler that the display variable exists
extern Adafruit_SSD1306 display;
void test_func(void)
{
display.setTextColor(WHITE, BLACK);
display.print("println from cpp");
}
This differs from the approach by gfvalvo; he passed it as an argument to the function.
Thank you very much it works fine.
What you mean: This differs from the approach by gfvalvo; he passed it as an argument to the function?
Can you explain me please the other solution.