Adding a new component - ble peripheral
Now that the setup is complete, let’s talk about components. Code modularity is crucial, and in ESP-IDF, we achieve that by creating components. However, creating a component requires some knowledge of CMake and C. A component is essentially a standalone set of code that can be used in the main file or other components. Here’s a quick look at my code structure.
1 | ├── CMakeLists.txt |
The common
directory holds essential files frequently utilized alongside a module. Meanwhile, the nimble-ble-prph
component is centered around the nimble-ble-prph.c
file, housing the bulk of the code.
CMake structure for the project
Starting with the high-level CMakeLists.txt
:
1 | cmake_minimum_required(VERSION 3.16) |
cmake_minimum_required
: Specifying the CMake version.set
: Defining the location for the essentialcommon
component.include
: Incorporating essential configurations fromproject.cmake
.
Now, the core CMakeLists.txt
in the main folder:
1 | idf_component_register(SRCS "main.c" |
REQUIRES
: Integrating thenimble-ble-prph
component as a dependency.
Lastly, the pivotal CMakeLists.txt
for the nimble-ble-prph
component:
1 | set(srcs "main.c") |
SRCS
: Enumerating source files for the compilation process.INCLUDE_DIRS
: Specifying directories for header files.REQUIRES
: Listing dependencies, including components likenvs_flash
,nimble_peripheral_utils
, andbt
. CMake kindly reminds us if we miss one.Awww!🥺I want to talk more about what I am trying to do with1
2
3Compilation failed because nimble-ble-prph.c (in "nimble-ble-prph" component) includes nvs_flash.h, provided by nvs_flash component(s).
However, nvs_flash component(s) is not in the requirements list of "nimble-ble-prph".
To fix this, add nvs_flash to PRIV_REQUIRES list of idf_component_register call in /home/thedevmanek/Documents/Group Projects/assistive-shoulder-firmware/components/nimble-ble-prph/CMakeLists.txt.bluetooth
but I will do that in the next post.I know it is sad but I am hungry. BYE!