Arduino + cmake + netbeans = success !


The Arduino IDE is very easy to use, but it lacks features.
Fortunately, there is a very nice project to use cmake with the arduino very easily.

I've used the following fork  on github

In particular, it can sort of automatically import most of the config directly from the the .ino file
(most is the critical part)

First some pits &traps  :

Library naming

The library names must strictly follow the arduino naming rule. i.e. it must contain MYMODULE/MYMODULE.h.
Layout  like
    MYMODULE-0.1/MYMODULE.h
or
   MYMODULE_library/MYMODULE.h

will fail. Create symlinks as needed.

Includes

The libs to include are autoprobed from the #include in the ino file. The parser is picky, make sure you dont have comments / space/ tabs AFTER the include.
This will fail
#include <foo.h> // some comment

Nano
For my nano i had to add the following set
set(ARDUINO_CPUMENU .menu.cpu.${ARDUINO_CPU})

Libs
Simple libs are automatically probed and built
Complicated ones, like u8glib need the following scheme
    # u8glib is complicated and needs special care
    link_directories(${CUSTOM_LIB_DIR}/U8glib)
    set(U8glib_RECURSE True)

Linking

With my setup i had to explictely name the libs i was using


You can then import the result withing netbeans
(make upload will upload the resulting binary to the arduino)



The end result is as follow, taking the battery capacity checker as an example :



CMakelists.txt

set(CMAKE_TOOLCHAIN_FILE cmake/ArduinoToolchain.cmake) # Arduino Toolchain

cmake_minimum_required(VERSION 2.8)
#====================================================================#
#  Setup Project                                                     #
#====================================================================#
project(ArduinoExample C CXX)
#
set(ARDUINO_DEFAULT_BOARD nano)        # Default Board ID, when not specified
set(ARDUINO_DEFAULT_PORT /dev/ttyUSB0) # Default Port, when not specified
set(ARDUINO_CPU          atmega328)

# this is needed for the nano, it should be set automatically, maybe something changed with sdk 1.8.x ?
set(ARDUINO_CPUMENU .menu.cpu.${ARDUINO_CPU})
#
print_board_list()
print_programmer_list()

PRINT_SETTINGS(${ARDUINO_DEFAULT_BOARD})

# Where you store your downloaded libs or manually installed
SET(CUSTOM_LIB_DIR /home/fx/Arduino/libraries)

link_directories( ${CUSTOM_LIB_DIR})

# u8glib is complicated and needs special care
link_directories(${CUSTOM_LIB_DIR}/U8glib)
set(U8glib_RECURSE True)
#
# Be careful with the includes in the ino files
# #include <foo.h> // this is a comment
# will fail
# same with extra tabs or space after the #include "blah.h"
#
SET(libPrefix ${ARDUINO_DEFAULT_BOARD}_)



generate_arduino_firmware(ampmeter 
                SKETCH .              # Folder to get the .ino from, must follow DIR_NAME/DIR_NAME.ino patten !
                LIBS ${libPrefix}Wire 
                LIBS ${libPrefix}U8glib 
                LIBS ${libPrefix}Adafruit_Sensor  # # Must be the folder name exactly, must no contain vx.y or _Libray or _master, do symlinks if necessar
                LIBS ${libPrefix}Adafruit_BMP280 
                LIBS ${libPrefix}Adafruit_INA219 
                LIBS ${libPrefix}SPI 
                PORT ${ARDUINO_DEFAULT_PORT}
                )



Comments

Popular posts from this blog

Component tester with STM32 : Part 1 ADC, Resistor

Fixing the INA3221

INA3221, weird wiring