Skip to content
This repository was archived by the owner on Apr 17, 2023. It is now read-only.

Commit cbfebf9

Browse files
authored
Merge pull request #39 from arduino-cmake/feature/find-lib-alternative
Added support for libraries that don't have a properties file
2 parents 67307b6 + a7eda7f commit cbfebf9

File tree

12 files changed

+610
-42
lines changed

12 files changed

+610
-42
lines changed

cmake/Platform/Libraries/LibrariesFinder.cmake

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
# _library_name - Name of the Arduino library to find.
88
# _board_id - Board ID associated with the linked Core Lib.
99
# [3RD_PARTY] - Whether library should be treated as a 3rd Party library.
10+
# [HEADER_ONLY] - Whether library should be treated as header-only library.
1011
#=============================================================================#
1112
function(find_arduino_library _target_name _library_name _board_id)
1213

@@ -17,22 +18,24 @@ function(find_arduino_library _target_name _library_name _board_id)
1718
convert_string_to_pascal_case(${_library_name} _library_name)
1819
endif ()
1920

20-
find_file(library_properties_file library.properties
21-
PATHS ${ARDUINO_SDK_LIBRARIES_PATH} ${ARDUINO_CMAKE_SKETCHBOOK_PATH}/libraries
22-
PATH_SUFFIXES ${_library_name}
21+
find_file(library_path
22+
NAMES ${_library_name}
23+
PATHS ${ARDUINO_SDK_LIBRARIES_PATH} ${ARDUINO_CMAKE_SKETCHBOOK_PATH}
24+
${CMAKE_CURRENT_SOURCE_DIR} ${PROJECT_SOURCE_DIR}
25+
PATH_SUFFIXES libraries dependencies
2326
NO_DEFAULT_PATH
2427
NO_CMAKE_FIND_ROOT_PATH)
2528

2629
if (${library_properties_file} MATCHES "NOTFOUND")
2730
message(SEND_ERROR "Couldn't find library named ${_library_name}")
28-
else () # Library is found
2931

30-
get_filename_component(library_path ${library_properties_file} DIRECTORY)
32+
else () # Library is found
3133

3234
find_library_header_files("${library_path}" library_headers)
3335

3436
if (NOT library_headers)
3537
message(SEND_ERROR "Couldn't find any header files for the ${_library_name} library")
38+
3639
else ()
3740

3841
if (parsed_args_HEADER_ONLY)
@@ -47,18 +50,21 @@ function(find_arduino_library _target_name _library_name _board_id)
4750
"${_library_name} library - Is it a header-only library?"
4851
"If so, please pass the HEADER_ONLY option "
4952
"as an argument to the function")
53+
5054
else ()
55+
5156
set(sources ${library_headers} ${library_sources})
5257

53-
add_arduino_library(${_target_name} ${_board_id}
54-
LIB_PROPS_FILE ${library_properties_file}
55-
${sources})
58+
add_arduino_library(${_target_name} ${_board_id} ${sources})
59+
5660
endif ()
5761

5862
endif ()
63+
5964
endif ()
65+
6066
endif ()
6167

62-
unset(library_properties_file CACHE)
68+
unset(library_path CACHE)
6369

6470
endfunction()

cmake/Platform/Libraries/LibrarySourcesArchitectureResolver.cmake

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,8 @@ function(resolve_library_architecture _library_sources _return_var)
3838
else ()
3939

4040
# Warn user and assume library is arch-agnostic
41-
message(STATUS "Library's properties file can't be found "
42-
"under its' root directory - Assuming the library "
43-
"is architecture-agnostic (supports all architectures)")
41+
message(STATUS "Library's properties file can't be found under its' root directory.\n\t"
42+
"Assuming the library is architecture-agnostic (supports all architectures)")
4443
set(${_return_var} "${_library_sources}" PARENT_SCOPE)
4544
return()
4645

cmake/Platform/Sources/ArduinoLibrarySourcesSeeker.cmake

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,18 @@
88
function(find_library_header_files _base_path _return_var)
99

1010
if (EXISTS ${_base_path}/src) # 'src' sub-dir exists and should contain sources
11+
1112
# Headers are always searched recursively under the 'src' sub-dir
1213
find_header_files(${_base_path}/src headers RECURSE)
14+
1315
else ()
14-
find_header_files(${_base_path} headers)
16+
17+
# Both root-dir and 'utility' sub-dir are searched when 'src' doesn't exist
18+
find_header_files(${_base_path} root_headers)
19+
find_header_files(${_base_path}/utility utility_headers)
20+
21+
set(headers ${root_headers} ${utility_headers})
22+
1523
endif ()
1624

1725
set(${_return_var} "${headers}" PARENT_SCOPE)
@@ -28,10 +36,18 @@ endfunction()
2836
function(find_library_source_files _base_path _return_var)
2937

3038
if (EXISTS ${_base_path}/src)
39+
3140
# Sources are always searched recursively under the 'src' sub-dir
3241
find_source_files(${_base_path}/src sources RECURSE)
42+
3343
else ()
34-
find_source_files(${_base_path} sources)
44+
45+
# Both root-dir and 'utility' sub-dir are searched when 'src' doesn't exist
46+
find_source_files(${_base_path} root_sources)
47+
find_source_files(${_base_path}/utility utility_sources)
48+
49+
set(sources ${root_sources} ${utility_sources})
50+
3551
endif ()
3652

3753
set(${_return_var} "${sources}" PARENT_SCOPE)

cmake/Platform/Targets/ArduinoLibraryTarget.cmake

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,22 @@
33
# As it's an Arduino library, it also finds and links all dependent platform libraries (if any).
44
# _target_name - Name of the library target to be created. Usually library's real name.
55
# _board_id - Board ID associated with the linked Core Lib.
6-
# [LIB_PROPS_FILE] - Full path to the library's properties file. Optional.
76
# [Sources] - List of source files (Could also be headers for code-inspection in some IDEs)
87
# to create the executable from, similar to CMake's built-in add_executable.
98
#=============================================================================#
109
function(add_arduino_library _target_name _board_id)
1110

12-
cmake_parse_arguments(parsed_args "" "LIB_PROPS_FILE" "" ${ARGN})
13-
parse_sources_arguments(parsed_sources "" "LIB_PROPS_FILE" "" "${ARGN}")
11+
parse_sources_arguments(parsed_sources "" "" "" "${ARGN}")
1412

15-
if (parsed_args_LIB_PROPS_FILE)
16-
resolve_library_architecture("${parsed_sources}" arch_resolved_sources
17-
LIB_PROPS_FILE ${parsed_args_LIB_PROPS_FILE})
18-
else ()
19-
20-
get_sources_root_directory("${parsed_sources}" library_root_dir)
13+
get_sources_root_directory("${parsed_sources}" library_root_dir)
2114

22-
get_library_properties_file(${library_root_dir} library_properties_file)
23-
24-
if (library_properties_file) # Properties file has been found
25-
resolve_library_architecture("${parsed_sources}" arch_resolved_sources
26-
LIB_PROPS_FILE ${library_properties_file})
27-
else ()
28-
resolve_library_architecture("${parsed_sources}" arch_resolved_sources)
29-
endif ()
15+
get_library_properties_file(${library_root_dir} library_properties_file)
3016

17+
if (library_properties_file) # Properties file has been found
18+
resolve_library_architecture("${parsed_sources}" arch_resolved_sources
19+
LIB_PROPS_FILE ${library_properties_file})
20+
else ()
21+
resolve_library_architecture("${parsed_sources}" arch_resolved_sources)
3122
endif ()
3223

3324
_add_arduino_cmake_library(${_target_name} ${_board_id} "${arch_resolved_sources}")

cmake/Platform/Utilities/LibraryUtils.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function(get_library_properties_file _library_root_directory _return_var)
1919
set(lib_props_file ${absolute_lib_root_dir}/${ARDUINO_CMAKE_LIBRARY_PROPERTIES_FILE_NAME})
2020

2121
if (NOT EXISTS ${lib_props_file})
22-
message(WARNING "Library's properties file doesn't exist under the given root directory.\n"
22+
message(STATUS "Library's properties file doesn't exist under the given root directory.\n\t"
2323
"Root directory: ${absolute_lib_root_dir}")
2424
return()
2525
endif ()

examples/3rd-party-library/CMakeLists.txt

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,19 @@ add_arduino_executable(3rd_Party_Arduino_Library ${board_id} 3rd_party.cpp
99
target_include_directories(3rd_Party_Arduino_Library PRIVATE include)
1010

1111
# Add the "NeoPixel" library manually using the library addition API
12-
add_arduino_library(Adafruit_NeoPixel ${board_id} libraries/Adafruit_NeoPixel/Adafruit_NeoPixel.cpp)
13-
target_include_directories(Adafruit_NeoPixel PUBLIC libraries/Adafruit_NeoPixel)
12+
add_arduino_library(adafruit_NeoPixel ${board_id} libraries/Adafruit_NeoPixel/Adafruit_NeoPixel.cpp)
13+
target_include_directories(adafruit_NeoPixel PUBLIC libraries/Adafruit_NeoPixel)
1414

15-
# Find the "GFX" library by 'tricking' the framework to use current directory as the Sketchbook path,
16-
# allowing us to use the 'find' API
17-
set(ARDUINO_CMAKE_SKETCHBOOK_PATH "${CMAKE_CURRENT_LIST_DIR}")
18-
find_arduino_library(Adafruit_GFX Adafruit-GFX-Library ${board_id} 3RD_PARTY)
15+
# Find the "GFX" library - It's located under the 'libraries' sub-dir, which is a valid search path
16+
find_arduino_library(adafruit_GFX Adafruit-GFX-Library ${board_id} 3RD_PARTY)
1917
# We can also explicitly add additional directories to the target,
20-
# as only root dir and 'src' sub-dir are added by default
21-
target_source_directories(Adafruit_GFX DIRS libraries/Adafruit-GFX-Library/Fonts)
18+
# as only root dir and 'src' and 'utility' sub-dirs are added by default
19+
target_source_directories(adafruit_GFX DIRS libraries/Adafruit-GFX-Library/Fonts)
20+
21+
# We can even automatically find a library that doesn't have a properties file!
22+
find_arduino_library(sky_writer Skywriter ${board_id} 3RD_PARTY)
2223

2324
# Link all libraries to our previously created target
24-
link_arduino_library(3rd_Party_Arduino_Library Adafruit_NeoPixel ${board_id})
25-
link_arduino_library(3rd_Party_Arduino_Library Adafruit_GFX ${board_id})
25+
link_arduino_library(3rd_Party_Arduino_Library adafruit_NeoPixel ${board_id})
26+
link_arduino_library(3rd_Party_Arduino_Library adafruit_GFX ${board_id})
27+
link_arduino_library(3rd_Party_Arduino_Library sky_writer ${board_id})
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#include <Adafruit_NeoPixel.h>
2+
#include <Wire.h>
3+
4+
#define PIN 8
5+
6+
Adafruit_NeoPixel Neopixel = Adafruit_NeoPixel(64, PIN, NEO_GRB + NEO_KHZ800);
7+
8+
unsigned long brightness = 50;
9+
unsigned char color = 0;
10+
11+
// Include string names of gestures/touches for testing
12+
//#define SKYWRITER_INC_DEBUG_STRINGS
13+
#include "skywriter.h"
14+
15+
unsigned int max_x, max_y, max_z;
16+
unsigned int min_x, min_y, min_z;
17+
18+
void setup() {
19+
Serial.begin(9600);
20+
while(!Serial){};
21+
Serial.println("Hello world!");
22+
23+
Neopixel.begin();
24+
Neopixel.setBrightness(brightness);
25+
Neopixel.show(); // Initialize all pixels to 'off'
26+
27+
28+
Skywriter.begin(12, 13);
29+
Skywriter.onTouch(touch);
30+
//Skywriter.onAirwheel(airwheel);
31+
Skywriter.onGesture(gesture);
32+
Skywriter.onXYZ(xyz);
33+
}
34+
35+
void loop() {
36+
Skywriter.poll();
37+
/*int x;
38+
for(x = 0; x < 64; x++){
39+
Neopixel.setPixelColor(x, Wheel(color));
40+
}*/
41+
Neopixel.show();
42+
}
43+
44+
void xyz(unsigned int x, unsigned int y, unsigned int z){
45+
if (x < min_x) min_x = x;
46+
if (y < min_y) min_y = y;
47+
if (z < min_z) min_z = z;
48+
if (x > max_x) max_x = x;
49+
if (y > max_y) max_y = y;
50+
if (z > max_z) max_z = z;
51+
52+
unsigned char pixel_x = map(x, min_x, max_x, 0, 7);
53+
unsigned char pixel_y = map(y, min_y, max_y, 0, 7);
54+
uint32_t color = Wheel(map(z, min_z, max_z, 0, 255));
55+
56+
57+
Neopixel.setPixelColor(pixel_x*8 + pixel_y, color);
58+
59+
char buf[64];
60+
sprintf(buf, "%05u:%05u:%05u gest:%02u touch:%02u", x, y, z, Skywriter.last_gesture, Skywriter.last_touch);
61+
Serial.println(buf);
62+
}
63+
64+
void gesture(unsigned char type){
65+
Serial.println("Got gesture ");
66+
Serial.print(type,DEC);
67+
Serial.print('\n');
68+
69+
if( type == SW_FLICK_WEST_EAST ){
70+
Neopixel.clear();
71+
}
72+
}
73+
74+
void touch(unsigned char type){
75+
Serial.println("Got touch ");
76+
Serial.print(type,DEC);
77+
Serial.print('\n');
78+
79+
if( type == SW_TOUCH_CENTER ){
80+
Neopixel.setBrightness(map(Skywriter.x, min_x, max_x, 0, 255));
81+
//color = map(Skywriter.y, min_y, max_y, 0, 255);
82+
}
83+
/*else if( type == SW_TOUCH_EAST ){
84+
Neopixel.setBrightness(0);
85+
}
86+
else if( type == SW_TOUCH_WEST ){
87+
Neopixel.setBrightness(255);
88+
}*/
89+
}
90+
91+
void airwheel(int delta){
92+
Serial.println("Got airwheel ");
93+
Serial.print(delta);
94+
Serial.print('\n');
95+
96+
brightness += (delta/100.0);
97+
Neopixel.setBrightness(brightness % 255);
98+
}
99+
100+
uint32_t Wheel(byte WheelPos) {
101+
if(WheelPos < 85) {
102+
return Neopixel.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
103+
} else if(WheelPos < 170) {
104+
WheelPos -= 85;
105+
return Neopixel.Color(255 - WheelPos * 3, 0, WheelPos * 3);
106+
} else {
107+
WheelPos -= 170;
108+
return Neopixel.Color(0, WheelPos * 3, 255 - WheelPos * 3);
109+
}
110+
}
111+

0 commit comments

Comments
 (0)