lexus2k / lcdgfx

Driver for LCD displays running on Arduino/Avr/ESP32/Linux (including Rasperry) platforms
MIT License
389 stars 55 forks source link

[RPi 4][IC2] `Failed to write to the i2c bus: Bad file descriptor.` #84

Closed Bricklou closed 2 years ago

Bricklou commented 2 years ago

Describe the bug I'm trying to use the library on my Raspberry Pi, but for some reason I get the following error:

Failed to write to the i2c bus: Bad file descriptor.

To Reproduce Steps to reproduce the behavior:

  1. Create following files
    
    // src/main.cpp
    #include <iostream>
    #include <lcdgfx.h>
    #include <lcdgfx_gui.h>
    #include <cstdint>

int main() { DisplaySSD1306_128x64_I2C display(-1);

    display.clear();

}


```cmake
// CMakeLists.txt
cmake_minimum_required(VERSION 3.17)

project(screen_monitor
        VERSION 1.0.0
        LANGUAGES C CXX
)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++20 -Wall -Wextra -fPIC -rdynamic -pthread")

# ==========
# lcdgfx
# ==========
add_subdirectory(libs/lcdgfx)

# ==========
# screen_monitor
# ==========
add_executable(${PROJECT_NAME}
        src/main.cpp
)

target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_20)
set_target_properties(${PROJECT_NAME} PROPERTIES
        CXX_STANDARD 20
        CXX_STANDARD_REQUIRED ON
)

target_link_libraries(${PROJECT_NAME} PUBLIC lcdgfx)
target_include_directories(${PROJECT_NAME} PUBLIC
        libs/lcdgfx/src
)

if (CMAKE_BUILD_TYPE MATCHES "^[Rr]elease")
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s -02 -0s")
else()
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")
endif()
  1. Clone the lib into libs/lcdgfx
  2. Build the app
  3. See error

Expected behavior The program should run, but throws the error instead.

Please complete the following information:

Additional context I tried to run the demo program using this link: https://github.com/lexus2k/lcdgfx/wiki/FAQ#how-to-run-demos-on-raspberry-pi and it worked perfectly fine, but not with my own code. :confused:

lexus2k commented 2 years ago

Hi,

What about display.begin() in your code? Please check your code carefully.

Bricklou commented 2 years ago

Oh, yes ! Looks like it was missing. Thank you :sweat_smile: