endail / hx711-pico-c

Implementation of single and multiple HX711 use via RP2040's state machine
https://endail.github.io/hx711-pico-c/
MIT License
32 stars 7 forks source link

Move PIO init function out of .pio file #31

Closed endail closed 2 years ago

endail commented 2 years ago

https://github.com/endail/hx711-pico-c/blob/ba506a442e5c5b53ad48c8e1009de55d0e97c119/src/hx711_noblock.pio#L158-L291

#include "../include/hx711.h" is not playing nice with the CMake build system when this repo needs to be included in another (re: #30).

It seems that after calling pico_generate_pio_header in CMakeLists.txt, the c-sdk portion of the pio file is merely dropped in-place in the resulting generated header file. That header file then sits in the CMake build directory (eg. extern/hx711-pico-c/hx711_noblock.pio.h) with the relative #include to a non-existent hx711.h header file.

Solution: move the init function to a new source file, hx711_noblock.c.

endail commented 2 years ago

It seems this is not so simple to decouple the PIO program from the embedded init function, as the latter uses the hx711_noblock_program_get_default_config function generated from the program.

However, from reading the pico_generate_pio_header() CMake function source, it seems that there is a pico_generate_pio_header_OUTPUT_DIR argument which can be given. The important lines are here.

endail commented 2 years ago

I have solved this as follows. In the project's CMakeLists.txt, rather than generating the PIO header file to the CMake build directory, I have it now pointing to the include directory like so:

pico_generate_pio_header(hx711-pico-c
    "${CMAKE_CURRENT_LIST_DIR}/src/hx711_noblock.pio"
    OUTPUT_DIR "${PROJECT_SOURCE_DIR}/include"
    )

I currently have the above function call wrapped a file-existence check. ie. if the PIO header does not exist, generate it locally.

# if, for whatever reason, the pio file does not exist, generate it
if(NOT "${PROJECT_SOURCE_DIR}/include/hx711_noblock.pio.h")
        message("PIO files does not exist; generating now...")
        pico_generate_pio_header(hx711-pico-c
                "${CMAKE_CURRENT_LIST_DIR}/src/hx711_noblock.pio"
                OUTPUT_DIR "${PROJECT_SOURCE_DIR}/include"
                )
endif()

This probably should be removed because I have moved the generation of the PIO header to a Github action. On push, the header will be created and committed to the include directory (assuming modification of course...).

In any case, with the PIO header now sitting in the include dir, its relative #include "hx711.h" should now work. Including this repository as a submodule, for example, should also now work with a simple #include "extern/hx711-pico-c/include/hx711_noblock.pio.h" or wherever it is.