diff --git a/.github/workflows/build-foobar.yml b/.github/workflows/build-arithmos.yml similarity index 79% rename from .github/workflows/build-foobar.yml rename to .github/workflows/build-arithmos.yml index 9da853f..dd6498b 100644 --- a/.github/workflows/build-foobar.yml +++ b/.github/workflows/build-arithmos.yml @@ -1,4 +1,4 @@ -name: Build libfoobar +name: Build arithmos-native on: push: @@ -16,12 +16,12 @@ jobs: - name: setup-toolchain run: chmod +rwx ./launch-sandbox-setup.sh && ./launch-sandbox-setup.sh - - name: compile-link-assemble libfoobar + - name: compile-link-assemble libarithmos run: chmod +rwx ./helper-scripts/project-impl/compile-all.sh && ./helper-scripts/project-impl/compile-all.sh - - name: compile-link-assemble hello_foobar.c + - name: compile-link-assemble hello_vector2d.c run: | chmod +rwx ./helper-scripts/project-impl/compile-examples.sh - ./helper-scripts/project-impl/compile-examples.sh atmega32 hello_foobar.c - ./helper-scripts/project-impl/compile-examples.sh atmega328p hello_foobar.c + ./helper-scripts/project-impl/compile-examples.sh atmega32 hello_vector2d.c + ./helper-scripts/project-impl/compile-examples.sh atmega328p hello_vector2d.c diff --git a/LICENSE b/LICENSE index fdddb29..5221018 100644 --- a/LICENSE +++ b/LICENSE @@ -1,24 +1,27 @@ -This is free and unencumbered software released into the public domain. +Copyright (c) 2022, Arithmos Project, arithmos-native +All rights reserved. -Anyone is free to copy, modify, publish, use, compile, sell, or -distribute this software, either in source code form or as a compiled -binary, for any purpose, commercial or non-commercial, and by any -means. +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: -In jurisdictions that recognize copyright laws, the author or authors -of this software dedicate any and all copyright interest in the -software to the public domain. We make this dedication for the benefit -of the public at large and to the detriment of our heirs and -successors. We intend this dedication to be an overt act of -relinquishment in perpetuity of all present and future rights to this -software under copyright law. +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR -OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -OTHER DEALINGS IN THE SOFTWARE. +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. -For more information, please refer to +3. Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file diff --git a/README.md b/README.md index a7db450..16f8052 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,7 @@ -# cmake-template +# arithmos-native -A template for embedded C libraries development. +The native portion for arithmos, a high performance maths algorithmic library written purely in C. -This template consists of the following modules: -1) foobar-core: for core code, this module gets compiled to a static library. -2) foobar-examples: for library examples (or your final runnable target). - -This template uses the avr toolchain, though you could edit it to use other toolchains, through editing the sandbox environment scripts. \ No newline at end of file +## This project has 2 modules: +- [x] arithmos-core: for core API header and source files. +- [x] arithmos-examples: for examples and techdemos. \ No newline at end of file diff --git a/arithmos-core/CMakeLists.txt b/arithmos-core/CMakeLists.txt index 955b018..87d6444 100644 --- a/arithmos-core/CMakeLists.txt +++ b/arithmos-core/CMakeLists.txt @@ -4,32 +4,64 @@ cmake_minimum_required(VERSION 3.18.1) project(arithmos VERSION 1.0) message(STATUS "Project: arithmos") -message(STATUS "Toolchain: ${TOOLCHAIN_HOME}") -message(STATUS "Target Hardware: ${TARGET}") +message(STATUS "C compiler: ${C_COMPILER}") +message(STATUS "CXX compiler: ${CXX_COMPILER}") +message(STATUS "Toolchain headers: ${TOOLCHAIN_HEADERS}") +message(STATUS "Compiler Options: ${COMPILER_OPTIONS}") +message(STATUS "Build command: ${BUILD_COMMAND}") # Cmake variables -set(library "arithmos") -set(CMAKE_C_COMPILER "${TOOLCHAIN_HOME}/bin/avr-gcc") -set(CMAKE_CXX_COMPILER "${TOOLCHAIN_HOME}/bin/avr-g++") -set(COMPILER_OPTIONS "-mmcu=${TARGET} -O3") +set(CMAKE_C_COMPILER "${C_COMPILER}") +set(CMAKE_CXX_COMPILER "${CXX_COMPILER}") +set(COMPILER_OPTIONS "${COMPILER_OPTIONS}") -set(toolchain_headers "${TOOLCHAIN_HOME}/toolchain/include/") -set(libfoobar "${CMAKE_CURRENT_SOURCE_DIR}/src/libfoo/do_foo.c" - "${CMAKE_CURRENT_SOURCE_DIR}/src/libfoo/init_foo.c" - "${CMAKE_CURRENT_SOURCE_DIR}/src/libfoo/destroy_foo.c") - -set(headers "${toolchain_headers}" +set(headers "${TOOLCHAIN_HEADERS}" "${CMAKE_CURRENT_SOURCE_DIR}/src/include/") -set(sources ${libfoobar}) -# add a library target -add_library(${library} STATIC ${sources}) +########################### libvector2d START ########################### +function(build_vector2d) + set(vector2d "vector2d") + set(libvector2d "${CMAKE_CURRENT_SOURCE_DIR}/src/libvector2d/vector2d_add.c" + "${CMAKE_CURRENT_SOURCE_DIR}/src/libvector2d/vector2d_angle.c" + "${CMAKE_CURRENT_SOURCE_DIR}/src/libvector2d/vector2d_dot_product.c" + "${CMAKE_CURRENT_SOURCE_DIR}/src/libvector2d/vector2d_product.c" + "${CMAKE_CURRENT_SOURCE_DIR}/src/libvector2d/vector2d_subtract.c" + "${CMAKE_CURRENT_SOURCE_DIR}/src/libvector2d/vector2d_distance.c" + "${CMAKE_CURRENT_SOURCE_DIR}/src/libvector2d/vector2d_length.c" + "${CMAKE_CURRENT_SOURCE_DIR}/src/libvector2d/vector2d_interpolate.c" + "${CMAKE_CURRENT_SOURCE_DIR}/src/libvector2d/vector2d_extrapolate.c" + "${CMAKE_CURRENT_SOURCE_DIR}/src/libvector2d/vector2d_are_parallel.c" + "${CMAKE_CURRENT_SOURCE_DIR}/src/libvector2d/vector2d_are_perpendicular.c" + "${CMAKE_CURRENT_SOURCE_DIR}/src/libvector2d/vector2d_normalize.c" + "${CMAKE_CURRENT_SOURCE_DIR}/src/libvector2d/vector2d_polar.c" + "${CMAKE_CURRENT_SOURCE_DIR}/src/libvector2d/vector2d_scalar_add.c" + "${CMAKE_CURRENT_SOURCE_DIR}/src/libvector2d/vector2d_scalar_divide.c" + "${CMAKE_CURRENT_SOURCE_DIR}/src/libvector2d/vector2d_scalar_multiply.c" + "${CMAKE_CURRENT_SOURCE_DIR}/src/libvector2d/vector2d_scalar_subtract.c" + "${CMAKE_CURRENT_SOURCE_DIR}/src/libvector2d/vector2d_scalar_moduluo.c") + + # add a library target + add_library(${vector2d} STATIC ${libvector2d}) + + # set both COMPILE_FLAGS and LINK_FLAGS to the specified binary architecture + set_target_properties(${vector2d} PROPERTIES COMPILE_FLAGS "${COMPILER_OPTIONS}" LINK_FLAGS "${COMPILER_OPTIONS}") + + # include headers for the target + target_include_directories(${vector2d} PUBLIC ${headers}) -# set both COMPILE_FLAGS and LINK_FLAGS to the specified binary architecture -set_target_properties(${library} PROPERTIES COMPILE_FLAGS "${COMPILER_OPTIONS}" LINK_FLAGS "${COMPILER_OPTIONS}") + # Start building the target + target_sources(${vector2d} PUBLIC ${libvector2d}) +endfunction() +########################### libvector2d END ########################### -# include headers for the target -target_include_directories(${library} PUBLIC ${headers}) +function(build_all) + build_vector2d() +endfunction() -# Start building the target -target_sources(${library} PUBLIC ${sources}) +########################### build process START ########################### +if(BUILD_COMMAND STREQUAL "vector2d") + build_vector2d() +else() + build_all() +endif() +########################### build process END ########################### \ No newline at end of file diff --git a/arithmos-core/README.md b/arithmos-core/README.md index da9f377..866f2c6 100644 --- a/arithmos-core/README.md +++ b/arithmos-core/README.md @@ -1,13 +1,9 @@ -# foobar-core +# arithmos-core -This is your core code module, it's compiled into a static library. +The Core of arithmos-native, this project consists of a couple of sub-projects: -To compile to a target mcu use the following script: -```bash -source "./helper-scripts/abstract/abstract-compile.sh" -source "./helper-scripts/project-impl/variables.sh" - -compile ${TOOLCHAIN_HOME} ${mcu_atmega32A} ${source_dir} -``` - -Edit the variables at `variables.sh` to satisfy your needs, to get more out of this, add your chip compilation script to `compile-all.sh`. \ No newline at end of file +- [x] libvector2d: A vector utility in the R(2) vector-space that provides powerful vector operations out of the box. +- [ ] libvector3d. +- [ ] libmatrix4x4. +- [ ] libgebra. +- [ ] libtrigo. \ No newline at end of file diff --git a/arithmos-core/src/include/foobar.h b/arithmos-core/src/include/foobar.h deleted file mode 100644 index 2607214..0000000 --- a/arithmos-core/src/include/foobar.h +++ /dev/null @@ -1,37 +0,0 @@ -#ifndef _FOOBAR_H_ -#define _FOOBAR_H_ - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * @brief Callbacks data structure. - */ -typedef struct { - void (*on_foo_created)(void**); - void (*on_foo_destroyed)(void**); -} foobar_callbacks; - -/** - * @brief Compilation unit code. - */ -extern void init_foo(); - -/** - * @brief Compilation unit code. - */ -extern void do_foo(); - -/** - * @brief Compilation unit code. - */ -extern void destroy_foo(); - -#ifdef __cplusplus -} -#endif - -#endif //_FOOBAR_H_ diff --git a/arithmos-core/src/include/vectorspaces/coordinate.h b/arithmos-core/src/include/vectorspaces/coordinate.h new file mode 100644 index 0000000..55213c1 --- /dev/null +++ b/arithmos-core/src/include/vectorspaces/coordinate.h @@ -0,0 +1,75 @@ +#if !(defined _vector2d_H_ || defined _vector3d_H_) +# error "Use vector2d or vector3d instead of this!" +#endif + +#ifndef _COORDINATE_H_ +#define _COORDINATE_H_ + +#include + +#if defined (_VECTOR2D_USE_UINT8_) +# define coordinate uint8_t +# define mod_coordinate coordinate +# define vector2d_pow(v, p) ((uint8_t) powf(v, p)) +# define vector2d_atan2(y, x) ((uint8_t) atan2f(y, x)) +# define vector2d_acos(v) ((uint8_t) acosf(v)) +#elif defined (_VECTOR2D_USE_INT8_) +# define coordinate int8_t +# define mod_coordinate coordinate +# define vector2d_pow(v, p) ((int8_t) powf(v, p)) +# define vector2d_atan2(y, x) ((int8_t) atan2f(y, x)) +# define vector2d_acos(v) ((int8_t) acosf(v)) +#elif defined (_VECTOR2D_USE_UINT16_) +# define coordinate uint16_t +# define mod_coordinate coordinate +# define vector2d_pow(v, p) ((uint16_t) powf(v, p)) +# define vector2d_atan2(y, x) ((uint16_t) atan2f(y, x)) +# define vector2d_acos(v) ((uint16_t) acosf(v)) +#elif defined (_VECTOR2D_USE_INT16_) +# define coordinate int16_t +# define mod_coordinate coordinate +# define vector2d_pow(v, p) ((int16_t) powf(v, p)) +# define vector2d_atan2(y, x) ((int16_t) atan2f(y, x)) +# define vector2d_acos(v) ((int16_t) acosf(v)) +#elif defined (_VECTOR2D_USE_UINT32_) +# define coordinate uint32_t +# define mod_coordinate coordinate +# define vector2d_pow(v, p) ((uint32_t) powf(v, p)) +# define vector2d_atan2(y, x) ((uint32_t) atan2f(y, x)) +# define vector2d_acos(v) ((uint32_t) acosf(v)) +#elif defined (_VECTOR2D_USE_INT32_) +# define coordinate int32_t +# define mod_coordinate coordinate +# define vector2d_pow(v, p) ((int32_t) powf(v, p)) +# define vector2d_atan2(y, x) ((int32_t) atan2f(y, x)) +# define vector2d_acos(v) ((int32_t) acosf(v)) +#elif defined (_VECTOR2D_USE_UINT64_) +# define coordinate uint64_t +# define mod_coordinate coordinate +# define vector2d_pow(v, p) ((uint64_t) powl(v, p)) +# define vector2d_atan2(y, x) ((uint64_t) atan2l(y, x)) +# define vector2d_acos(v) ((uint64_t) acosl(v)) +#elif defined (_VECTOR2D_USE_INT64_) +# define coordinate int64_t +# define mod_coordinate coordinate +# define vector2d_pow(v, p) ((int64_t) powl(v, p)) +# define vector2d_atan2(y, x) ((int64_t) atan2l(y, x)) +# define vector2d_acos(v) ((int64_t) acosl(v)) +#elif defined (_VECTOR2D_USE_DOUBLE_) +# define coordinate double +# define mod_coordinate int64_t +# define vector2d_pow(v, p) ((double) powl(v, p)) +# define vector2d_atan2(y, x) ((double) atan2l(y, x)) +# define vector2d_acos(v) ((double) acosl(v)) +#else +# define coordinate float +# define mod_coordinate int32_t +# define vector2d_pow(v, p) ((float) powf(v, p)) +# define vector2d_atan2(y, x) ((float) atan2f(y, x)) +# define vector2d_acos(v) ((float) acosf(v)) +#endif + +#define vector2d_sqrt(v) vector2d_pow(v, 0.5) +#define vector2d_abs(v) ((coordinate) vector2d_sqrt(vector2d_pow(v, 2))) + +#endif // _COORDINATE_H_ \ No newline at end of file diff --git a/arithmos-core/src/include/vectorspaces/vector2d/vector2d.h b/arithmos-core/src/include/vectorspaces/vector2d/vector2d.h new file mode 100644 index 0000000..cb48785 --- /dev/null +++ b/arithmos-core/src/include/vectorspaces/vector2d/vector2d.h @@ -0,0 +1,227 @@ +/** + * @brief The R(2) vectorspace to represent 2-dimensional vectors providing a + * powerful utility to operate on them. + * @author pavl_g. + * @copyright arithmos-native. + */ +#ifndef _vector2d_H_ +#define _vector2d_H_ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Represents a 2-dimensional (aka. R(2) space) vector point + * with a x-component and a y-component. + */ +typedef struct { + /** + * @brief The x-coordinate (Vx->) component of this vector in the R(2) space. + */ + coordinate x; + + /** + * @brief The y-coordinate (Vy->) component of this vector in the R(2) space. + */ + coordinate y; +} vector2d; + +/** + * @brief Adds a scalar value to the vector components and returns a new vector. + * + * @param vector2d the initial vector in R(2) space. + * @param coordinate the value to add to that vector. + * @return a new vector2d with x and y components. + */ +extern vector2d vector2d_scalar_add(vector2d, coordinate); + +/** + * @brief Subtracts a scalar value from the vector components and returns a new vector. + * + * @param vector2d the initial vector in R(2) space. + * @param coordinate the value to subtract from that vector. + * @return a new vector2d with x and y components. + */ +extern vector2d vector2d_scalar_subtract(vector2d, coordinate); + +/** + * @brief Multiplies a scalar value with the vector components and returns a new vector. + * + * @param vector2d the initial vector in R(2) space. + * @param coordinate the value to multiply to that vector. + * @return a new vector2d with x and y components. + */ +extern vector2d vector2d_scalar_multiply(vector2d, coordinate); + +/** + * @brief Divides a scalar value by the vector components and returns a new vector. + * + * @param vector2d the initial vector in R(2) space. + * @param coordinate the value to divide by that vector. + * @return a new vector2d with x and y components. + */ +extern vector2d vector2d_scalar_divide(vector2d, coordinate); + +/** + * @brief Finds the uint vector of a vector in R(2) space which represents the direction of that vector. + * + * @param vector2d the original vector in R(2) space. + * @return a new vector2d representing the uint vector of the input vector. + */ +extern vector2d vector2d_normalize(vector2d); + +/** + * @brief Adds two vector2d components together in a R(2) space returning a new vector. + * + * @param vector2d the first vector. + * @param vector2d the second vector. + * @return a new vector2d in the R(2) space. + */ +extern vector2d vector2d_add(vector2d, vector2d); + +/** + * @brief Divides the first vector by the second vector in a R(2) space returning a new vector. + * + * @param vector2d the first vector. + * @param vector2d the second vector. + * @return a new vector2d in the R(2) space. + */ +extern vector2d vector2d_divide(vector2d, vector2d); + +/** + * @brief Subtracts the second vector components from first vector components in a R(2) space returning a new vector. + * + * @param vector2d the first vector. + * @param vector2d the second vector. + * @return a new vector2d in the R(2) space. + */ +extern vector2d vector2d_subtract(vector2d, vector2d); + +/** + * @brief Interpolates between the first vector components and the second vector components with a scale in a R(2) space. + * + * @param vector2d the first vector. + * @param vector2d the second vector. + * @return a new vector2d in the R(2) space. + */ +extern vector2d vector2d_interpolate(vector2d, vector2d, coordinate); + +/** + * @brief Extrapolates from the first vector components and the second vector components with a scale in a R(2) space. + * + * @param vector2d the first vector. + * @param vector2d the second vector. + * @return a new vector2d in the R(2) space. + */ +extern vector2d vector2d_extrapolate(vector2d, vector2d, coordinate); + +/** + * @brief Performs a matrix multiplication operation on two vectors in the R(2) space. + * + * @param vector2d the first vector. + * @param vector2d the second vector. + * @return a new vector2d in the R(2) space. + */ +extern coordinate vector2d_dot_product(vector2d, vector2d); + +/** + * @brief Performs a scalar multiplication operation on the first vector components using the + * second vector components in a R(2) vector-space returning a new vector. + * + * @param vector2d the first vector. + * @param vector2d the second vector. + * @return a new vector2d in the R(2) space. + */ +extern vector2d vector2d_product(vector2d, vector2d); + +/** + * @brief Finds the angle between two vectors in the R(2) space by dividing their dot product by the product of their norms. + * @note The angle is in radians by default! + * + * @param vector2d the first vector. + * @param vector2d the second vector. + * @return a new vector2d in the R(2) space. + */ +extern coordinate vector2d_angle(vector2d, vector2d); + +/** + * @brief Finds the length (aka. norm) of a vector in the R(2) space by using the Pythagorean theorem on its components. + * + * @param vector2d a vector in the R(2) space to find its length. + * @return a scalar value representing the length of this vector in the R(2) space. + */ +extern coordinate vector2d_length(vector2d); + +/** + * @brief Converts the rectangular coordinate into a polar coordinate by finding + * the inverse of the tangent of Y/X. + * @note The angle is with the postive direction of x-axis and in rads! + * + * @param vector2d a rectangular coordinate vector. + * @return the polar coordinate value in radians. + */ +extern coordinate vector2d_polar(vector2d); + +/** + * @brief Applies a moduluo (integer division) on vector components, returing the remainder + * of the integer division in a new vector2d in a R(2) space. + * + * @param vector2d a rectangular coordinate vector. + * @return the polar coordinate value in radians. + */ +extern vector2d vector2f_scalar_moduluo(vector2d, coordinate); + +/** + * @brief Finds the metric distance between two vectors in a R(2) space by calculating the + * norm of the their subtraction. + * + * @param vector2d the first vector. + * @param vector2d the second vector. + * @return the metric distance between the specified vectors. + */ +extern coordinate vector2d_distance(vector2d, vector2d); + +/** + * @brief Tests whether two vectors are dependent in a R(2) vector-space. + * @note Two vectors are said to be dependent if they are multiplies of one another, + * such that, the [u = cv], where u and v are vectors and c is a scalar value not equal to zero. + * @note Linear dependent vectors are coincident vectors in a R(2) space. + * + * @param vector2d the first vector. + * @param vector2d the second vector. + * @return (1) for true if the two vectors are dependent or (0) for false otherwise. + */ +extern uint8_t vector2d_is_dependent(vector2d, vector2d); + +/** + * @brief Tests whether two vectors are perpendicular in a R(2) vector-space. + * @note Two vectors are said to be perpendicular if and only if their dot-product is zero + * because, cos(90) is zero, so u.v = |u|.|v|.cos(90) = 0. + * + * @param vector2d the first vector. + * @param vector2d the second vector. + * @return (1) for true if the two vectors are perpendicular or (0) for false otherwise. + */ +extern uint8_t vector2d_are_perpendicular(vector2d, vector2d); + +/** + * @brief Tests whether two vectors are parallel in a R(2) vector-space. + * @note Two vectors are said to be parallel if and only if their dot-product + * is equal to the product of their norms (lengths); + * because, cos(0) is 1, so u.v = |u|.|v|.cos(0) = |u|.|v|. + * + * @param vector2d the first vector. + * @param vector2d the second vector. + * @return (1) for true if the two vectors are parallel or (0) for false otherwise. + */ +extern uint8_t vector2d_are_parallel(vector2d, vector2d); + +#ifdef __cplusplus +} +#endif + +#endif //_vector2d_H_ diff --git a/arithmos-core/src/libfoo/destroy_foo.c b/arithmos-core/src/libfoo/destroy_foo.c deleted file mode 100644 index 96823e7..0000000 --- a/arithmos-core/src/libfoo/destroy_foo.c +++ /dev/null @@ -1,4 +0,0 @@ -#include - -void destroy_foo() { -} \ No newline at end of file diff --git a/arithmos-core/src/libfoo/do_foo.c b/arithmos-core/src/libfoo/do_foo.c deleted file mode 100644 index a713053..0000000 --- a/arithmos-core/src/libfoo/do_foo.c +++ /dev/null @@ -1,10 +0,0 @@ -/** - * @brief Represents a compilation unit defining a function definition. - * @author pavl_g. - * @copyright Unlicensed license. - */ -#include - -void do_foo() { - printf("Fetch me a tank of coffee!\n"); -} \ No newline at end of file diff --git a/arithmos-core/src/libfoo/init_foo.c b/arithmos-core/src/libfoo/init_foo.c deleted file mode 100644 index 99ff7de..0000000 --- a/arithmos-core/src/libfoo/init_foo.c +++ /dev/null @@ -1,4 +0,0 @@ -#include - -void init_foo() { -} \ No newline at end of file diff --git a/arithmos-core/src/libvector2d/vector2d_add.c b/arithmos-core/src/libvector2d/vector2d_add.c new file mode 100644 index 0000000..ef7b5ac --- /dev/null +++ b/arithmos-core/src/libvector2d/vector2d_add.c @@ -0,0 +1,8 @@ +#include + +vector2d vector2d_add(vector2d vec0, vector2d vec1) { + return (vector2d) { + vec0.x + vec1.x, + vec0.y + vec1.y + }; +} \ No newline at end of file diff --git a/arithmos-core/src/libvector2d/vector2d_angle.c b/arithmos-core/src/libvector2d/vector2d_angle.c new file mode 100644 index 0000000..ab559f2 --- /dev/null +++ b/arithmos-core/src/libvector2d/vector2d_angle.c @@ -0,0 +1,11 @@ +#include + +coordinate vector2d_angle(vector2d vec0, vector2d vec1) { + // 1) find the dot product + coordinate product = vector2d_dot_product(vec0, vec1); + // 2) find the norm of both vectors + coordinate vec0_norm = vector2d_length(vec0); + coordinate vec1_norm = vector2d_length(vec1); + // 3) find the angle of their inner product in this vector space + return vector2d_acos(product / (vec0_norm * vec1_norm)); +} \ No newline at end of file diff --git a/arithmos-core/src/libvector2d/vector2d_are_dependent.c b/arithmos-core/src/libvector2d/vector2d_are_dependent.c new file mode 100644 index 0000000..2ee3a1b --- /dev/null +++ b/arithmos-core/src/libvector2d/vector2d_are_dependent.c @@ -0,0 +1,6 @@ +#include + +uint8_t vector2d_are_dependent(vector2d vec0, vector2d vec1) { + vector2d constant = vector2d_divide(vec0, vec1); + return constant.x == constant.y != 0; +} \ No newline at end of file diff --git a/arithmos-core/src/libvector2d/vector2d_are_parallel.c b/arithmos-core/src/libvector2d/vector2d_are_parallel.c new file mode 100644 index 0000000..90ff1fb --- /dev/null +++ b/arithmos-core/src/libvector2d/vector2d_are_parallel.c @@ -0,0 +1,5 @@ +#include + +uint8_t vector2d_are_parallel(vector2d vec0, vector2d vec1) { + return vector2d_dot_product(vec0, vec1) == (vector2d_length(vec0) * vector2d_length(vec1)); +} \ No newline at end of file diff --git a/arithmos-core/src/libvector2d/vector2d_are_perpendicular.c b/arithmos-core/src/libvector2d/vector2d_are_perpendicular.c new file mode 100644 index 0000000..bea369a --- /dev/null +++ b/arithmos-core/src/libvector2d/vector2d_are_perpendicular.c @@ -0,0 +1,5 @@ +#include + +uint8_t vector2d_are_perpendicular(vector2d vec0, vector2d vec1) { + return vector2d_dot_product(vec0, vec1) == 0; +} \ No newline at end of file diff --git a/arithmos-core/src/libvector2d/vector2d_distance.c b/arithmos-core/src/libvector2d/vector2d_distance.c new file mode 100644 index 0000000..3114bd4 --- /dev/null +++ b/arithmos-core/src/libvector2d/vector2d_distance.c @@ -0,0 +1,5 @@ +#include + +coordinate vector2d_distance(vector2d vec0, vector2d vec1) { + return vector2d_length(vector2d_subtract(vec0, vec1)); +} \ No newline at end of file diff --git a/arithmos-core/src/libvector2d/vector2d_divide.c b/arithmos-core/src/libvector2d/vector2d_divide.c new file mode 100644 index 0000000..bc018a4 --- /dev/null +++ b/arithmos-core/src/libvector2d/vector2d_divide.c @@ -0,0 +1,8 @@ +#include + +vector2d vector2d_divide(vector2d vec0, vector2d vec1) { + return (vector2d) { + vec0.x / vec1.x, + vec0.y / vec1.y + }; +} \ No newline at end of file diff --git a/arithmos-core/src/libvector2d/vector2d_dot_product.c b/arithmos-core/src/libvector2d/vector2d_dot_product.c new file mode 100644 index 0000000..fc458fa --- /dev/null +++ b/arithmos-core/src/libvector2d/vector2d_dot_product.c @@ -0,0 +1,5 @@ +#include + +coordinate vector2d_dot_product(vector2d vec0, vector2d vec1) { + return vec0.x * vec1.x + vec0.y * vec1.y; +} \ No newline at end of file diff --git a/arithmos-core/src/libvector2d/vector2d_extrapolate.c b/arithmos-core/src/libvector2d/vector2d_extrapolate.c new file mode 100644 index 0000000..dbbb3fd --- /dev/null +++ b/arithmos-core/src/libvector2d/vector2d_extrapolate.c @@ -0,0 +1,8 @@ +#include + +vector2d vector2d_extrapolate(vector2d vec0, vector2d vec1, coordinate scale) { + return (vector2d) { + ((vec0.x + vec1.x) * scale) + vec0.x, + ((vec0.y + vec1.y) * scale) + vec0.y + }; +} \ No newline at end of file diff --git a/arithmos-core/src/libvector2d/vector2d_interpolate.c b/arithmos-core/src/libvector2d/vector2d_interpolate.c new file mode 100644 index 0000000..80b081f --- /dev/null +++ b/arithmos-core/src/libvector2d/vector2d_interpolate.c @@ -0,0 +1,12 @@ +#include + +vector2d vector2d_interpolate(vector2d vec0, vector2d vec1, coordinate scale) { + // 1) find the absolute x_distance and y_distance + coordinate x_distance = vector2d_abs(vec0.x - vec1.x); + coordinate y_distance = vector2d_abs(vec0.y - vec1.y); + // 2) interpolate between components with a scale starting from the first vector + return (vector2d) { + ((x_distance) * scale) + vec0.x, + ((y_distance) * scale) + vec0.y + };; +} \ No newline at end of file diff --git a/arithmos-core/src/libvector2d/vector2d_length.c b/arithmos-core/src/libvector2d/vector2d_length.c new file mode 100644 index 0000000..cb001ac --- /dev/null +++ b/arithmos-core/src/libvector2d/vector2d_length.c @@ -0,0 +1,5 @@ +#include + +coordinate vector2d_length(vector2d vec) { + return vector2d_sqrt(vector2d_pow(vec.x, 2) + vector2d_pow(vec.y, 2)); +} \ No newline at end of file diff --git a/arithmos-core/src/libvector2d/vector2d_normalize.c b/arithmos-core/src/libvector2d/vector2d_normalize.c new file mode 100644 index 0000000..64fd931 --- /dev/null +++ b/arithmos-core/src/libvector2d/vector2d_normalize.c @@ -0,0 +1,5 @@ +#include + +vector2d vector2d_normalize(vector2d vec) { + return vector2d_scalar_divide(vec, vector2d_length(vec)); +} \ No newline at end of file diff --git a/arithmos-core/src/libvector2d/vector2d_polar.c b/arithmos-core/src/libvector2d/vector2d_polar.c new file mode 100644 index 0000000..6c45a00 --- /dev/null +++ b/arithmos-core/src/libvector2d/vector2d_polar.c @@ -0,0 +1,5 @@ +#include + +coordinate vector2d_polar(vector2d vec) { + return vector2d_atan2(vec.y, vec.x); +} \ No newline at end of file diff --git a/arithmos-core/src/libvector2d/vector2d_product.c b/arithmos-core/src/libvector2d/vector2d_product.c new file mode 100644 index 0000000..36c633d --- /dev/null +++ b/arithmos-core/src/libvector2d/vector2d_product.c @@ -0,0 +1,8 @@ +#include + +vector2d vector2d_product(vector2d vec0, vector2d vec1) { + return (vector2d) { + vec0.x * vec1.x, + vec0.y * vec1.y + }; +} \ No newline at end of file diff --git a/arithmos-core/src/libvector2d/vector2d_scalar_add.c b/arithmos-core/src/libvector2d/vector2d_scalar_add.c new file mode 100644 index 0000000..aa06a1e --- /dev/null +++ b/arithmos-core/src/libvector2d/vector2d_scalar_add.c @@ -0,0 +1,7 @@ +#include + +vector2d vector2d_scalar_add(vector2d vec, coordinate scalar) { + vec.x += scalar; + vec.y += scalar; + return vec; +} \ No newline at end of file diff --git a/arithmos-core/src/libvector2d/vector2d_scalar_divide.c b/arithmos-core/src/libvector2d/vector2d_scalar_divide.c new file mode 100644 index 0000000..cdcb504 --- /dev/null +++ b/arithmos-core/src/libvector2d/vector2d_scalar_divide.c @@ -0,0 +1,7 @@ +#include + +vector2d vector2d_scalar_divide(vector2d vec, coordinate scalar) { + vec.x /= scalar; + vec.y /= scalar; + return vec; +} \ No newline at end of file diff --git a/arithmos-core/src/libvector2d/vector2d_scalar_moduluo.c b/arithmos-core/src/libvector2d/vector2d_scalar_moduluo.c new file mode 100644 index 0000000..04ee9a5 --- /dev/null +++ b/arithmos-core/src/libvector2d/vector2d_scalar_moduluo.c @@ -0,0 +1,7 @@ +#include + +vector2d vector2f_scalar_moduluo(vector2d vec, coordinate scalar) { + vec.x = ((mod_coordinate) vec.x) % ((mod_coordinate) scalar); + vec.y = ((mod_coordinate) vec.y) % ((mod_coordinate) scalar); + return vec; +} \ No newline at end of file diff --git a/arithmos-core/src/libvector2d/vector2d_scalar_multiply.c b/arithmos-core/src/libvector2d/vector2d_scalar_multiply.c new file mode 100644 index 0000000..7e8ea92 --- /dev/null +++ b/arithmos-core/src/libvector2d/vector2d_scalar_multiply.c @@ -0,0 +1,7 @@ +#include + +vector2d vector2d_scalar_multiply(vector2d vec, coordinate scalar) { + vec.x *= scalar; + vec.y *= scalar; + return vec; +} \ No newline at end of file diff --git a/arithmos-core/src/libvector2d/vector2d_scalar_subtract.c b/arithmos-core/src/libvector2d/vector2d_scalar_subtract.c new file mode 100644 index 0000000..1889426 --- /dev/null +++ b/arithmos-core/src/libvector2d/vector2d_scalar_subtract.c @@ -0,0 +1,7 @@ +#include + +vector2d vector2d_scalar_subtract(vector2d vec, coordinate scalar) { + vec.x -= scalar; + vec.y -= scalar; + return vec; +} \ No newline at end of file diff --git a/arithmos-core/src/libvector2d/vector2d_subtract.c b/arithmos-core/src/libvector2d/vector2d_subtract.c new file mode 100644 index 0000000..220b0e0 --- /dev/null +++ b/arithmos-core/src/libvector2d/vector2d_subtract.c @@ -0,0 +1,8 @@ +#include + +vector2d vector2d_subtract(vector2d vec0, vector2d vec1) { + return (vector2d) { + vec0.x - vec1.x, + vec0.y - vec1.y + }; +} \ No newline at end of file diff --git a/arithmos-examples/CMakeLists.txt b/arithmos-examples/CMakeLists.txt index d55784e..4865b3a 100644 --- a/arithmos-examples/CMakeLists.txt +++ b/arithmos-examples/CMakeLists.txt @@ -5,20 +5,22 @@ project(arithmos-examples VERSION 1.0) # display external options message(STATUS "Project: arithmos-examples") -message(STATUS "Toolchain: ${TOOLCHAIN_HOME}") -message(STATUS "Target Hardware: ${TARGET}") -message(STATUS "Included-library: ${ARITHMOS_LIB}") +message(STATUS "C compiler: ${C_COMPILER}") +message(STATUS "CXX compiler: ${CXX_COMPILER}") +message(STATUS "Toolchain headers: ${TOOLCHAIN_HEADERS}") +message(STATUS "Compiler Options: ${COMPILER_OPTIONS}") +message(STATUS "Included-library: ${STATIC_LIB_DIR}") message(STATUS "Runnable-example: ${EXAMPLE}") # Cmake variables set(elf "${EXAMPLE}.elf") -set(CMAKE_C_COMPILER "${TOOLCHAIN_HOME}/bin/avr-gcc") -set(CMAKE_CXX_COMPILER "${TOOLCHAIN_HOME}/bin/avr-g++") -set(COMPILER_OPTIONS "-mmcu=${TARGET}") +set(CMAKE_C_COMPILER "${C_COMPILER}") +set(CMAKE_CXX_COMPILER "${CXX_COMPILER}") +set(COMPILER_OPTIONS "${COMPILER_OPTIONS}") -set(headers "${ARITHMOS_LIB}/src/include") -set(avr_headers "${TOOLCHAIN_HOME}/include") +set(headers "${STATIC_LIB_DIR}/src/include/") +set(toolchain_headers "${TOOLCHAIN_HEADERS}") # C sources set(sources "${CMAKE_CURRENT_SOURCE_DIR}/src/${EXAMPLE}") @@ -30,9 +32,12 @@ add_executable(${elf}) set_target_properties(${elf} PROPERTIES COMPILE_FLAGS "${COMPILER_OPTIONS}" LINK_FLAGS "${COMPILER_OPTIONS}") # include headers for the target -target_include_directories(${elf} PUBLIC ${avr_headers} ${headers}) +target_include_directories(${elf} PUBLIC ${toolchain_headers} ${headers}) -target_link_libraries(${elf} "${ARITHMOS_LIB}/build/${TARGET}/libarithmos.a") +# add all arithmos libraries here +target_link_libraries(${elf} "${ARITHMOS_LIB}/build/${TARGET}/libvector2d.a") +# target_link_libraries(${elf} "${ARITHMOS_LIB}/build/${TARGET}/libvector3d.a") +# target_link_libraries(${elf} "${ARITHMOS_LIB}/build/${TARGET}/libmatrix4x4.a") # Start building the target -target_sources(${elf} PUBLIC ${sources}) +target_sources(${elf} PUBLIC ${sources}) \ No newline at end of file diff --git a/arithmos-examples/src/hello_foobar.c b/arithmos-examples/src/hello_foobar.c deleted file mode 100644 index c87bb50..0000000 --- a/arithmos-examples/src/hello_foobar.c +++ /dev/null @@ -1,14 +0,0 @@ -/** - * @brief An example showing an example :-). - * @author pavl_g. - * @copyright Unlicensed license. - */ -#include -#include - -int main() { - init_foo(); - do_foo(); - destroy_foo(); - return 0; -} diff --git a/arithmos-examples/src/hello_vector2d.c b/arithmos-examples/src/hello_vector2d.c new file mode 100644 index 0000000..da9cac9 --- /dev/null +++ b/arithmos-examples/src/hello_vector2d.c @@ -0,0 +1,31 @@ +/** + * @brief An example showing an example :-). + * @author pavl_g. + * @copyright Unlicensed license. + */ +#include +#include + +vector2d vec0 = { + 0, + 1 +}; + +vector2d vec1 = { + 2, + 3 +}; + +int main() { + printf("Summation = [%d, %d] \n", vector2d_add(vec0, vec1).x, vector2d_add(vec0, vec1).y); + printf("Subtraction = [%d, %d] \n", vector2d_subtract(vec0, vec1).x, vector2d_subtract(vec0, vec1).y); + printf("Product = [%d, %d] \n", vector2d_product(vec0, vec1).x, vector2d_product(vec0, vec1).y); + printf("Division = [%d, %d] \n", vector2d_divide(vec0, vec1).x, vector2d_divide(vec0, vec1).y); + printf("Inner Product = %d \n", vector2d_dot_product(vec0, vec1)); + printf("Inner Angle in radians = %d \n", vector2d_angle(vec0, vec1)); + printf("Are parallel = %d \n", vector2d_are_parallel(vec0, vec1)); + printf("Are perpendicular = %d \n", vector2d_are_perpendicular(vec0, vec1)); + printf("Extrapolate to 1/2 after the last vector = [%d, %d] \n", vector2d_extrapolate(vec0, vec1, 0.5).x, vector2d_extrapolate(vec0, vec1, 0.5).y); + + return 0; +} diff --git a/helper-scripts/abstract/abstract-compile-examples.sh b/helper-scripts/abstract/abstract-compile-examples.sh index df7bec6..f496df1 100644 --- a/helper-scripts/abstract/abstract-compile-examples.sh +++ b/helper-scripts/abstract/abstract-compile-examples.sh @@ -1,18 +1,22 @@ #!/bin/bash function compile() { - local TOOLCHAIN_HOME=${1} - local TARGET=${2} - local source_dir=${3} - local FOOBAR_LIB=${4} - local EXAMPLE=${5} + local C_COMPILER=${1} + local CXX_COMPILER=${2} + local TOOLCHAIN_HEADERS=${3} + local COMPILER_OPTIONS=${4} + local TARGET=${5} + local STATIC_LIB_DIR=${6} + local EXAMPLE=${7} + local source_dir=${8} local temp=$(pwd) cd ${source_dir} - - cmake "-DTOOLCHAIN_HOME=${TOOLCHAIN_HOME}" \ - "-DTARGET=${TARGET}" \ - "-DFOO_BAR=${FOOBAR_LIB}" \ + cmake "-DC_COMPILER=${C_COMPILER}" \ + "-DCXX_COMPILER=${CXX_COMPILER}" \ + "-DTOOLCHAIN_HEADERS=${TOOLCHAIN_HEADERS}" \ + "-DCOMPILER_OPTIONS=${COMPILER_OPTIONS}" \ + "-DSTATIC_LIB_DIR=${STATIC_LIB_DIR}" \ "-DEXAMPLE=${EXAMPLE}" \ -S . -B "./build/${TARGET}" diff --git a/helper-scripts/abstract/abstract-compile.sh b/helper-scripts/abstract/abstract-compile.sh index 2cfcdca..0abef52 100644 --- a/helper-scripts/abstract/abstract-compile.sh +++ b/helper-scripts/abstract/abstract-compile.sh @@ -1,16 +1,25 @@ #!/bin/bash function compile() { - local TOOLCHAIN_HOME=${1} - local TARGET=${2} - local source_dir=${3} + local C_COMPILER=${1} + local CXX_COMPILER=${2} + local TOOLCHAIN_HEADERS=${3} + local COMPILER_OPTIONS=${4} + local BUILD_COMMAND=${5} + local TARGET=${6} + local source_dir=${7} local temp=$(pwd) cd ${source_dir} - cmake "-DTOOLCHAIN_HOME=${TOOLCHAIN_HOME}" \ - "-DTARGET=${TARGET}" \ + cmake "-DC_COMPILER=${C_COMPILER}" \ + "-DCXX_COMPILER=${CXX_COMPILER}" \ + "-DTOOLCHAIN_HEADERS=${TOOLCHAIN_HEADERS}" \ + "-DCOMPILER_OPTIONS=${COMPILER_OPTIONS}" \ + "-DBUILD_COMMAND=${BUILD_COMMAND}" \ -S . -B "./build/${TARGET}" cmake --build "./build/${TARGET}" cd ${temp} + + return $? } diff --git a/helper-scripts/abstract/abstract-upload.sh b/helper-scripts/abstract/abstract-upload.sh index 8b4d55c..44be560 100644 --- a/helper-scripts/abstract/abstract-upload.sh +++ b/helper-scripts/abstract/abstract-upload.sh @@ -1,13 +1,13 @@ #!/bin/bash function convertToHex() { - local TOOLCHAIN_HOME=${1} + local objcopy=${1} local elf=${2} local directory=${3} temp=$(pwd) cd ${directory} - ${TOOLCHAIN_HOME}'/bin/avr-objcopy' -O ihex ${elf} ${elf}'.hex' + ${objcopy} -O ihex ${elf} ${elf}'.hex' cd $temp return $? } diff --git a/helper-scripts/project-impl/compile-atmega32.sh b/helper-scripts/project-impl/compile-atmega32.sh index 7f70815..5e207df 100755 --- a/helper-scripts/project-impl/compile-atmega32.sh +++ b/helper-scripts/project-impl/compile-atmega32.sh @@ -3,4 +3,10 @@ source "./helper-scripts/abstract/abstract-compile.sh" source "./helper-scripts/project-impl/variables.sh" -compile ${TOOLCHAIN_HOME} ${mcu_atmega32A} ${source_dir} +compile "${AVR_C_COMPILER}" \ + "${AVR_CXX_COMPILER}" \ + "${AVR_TOOLCHAIN_HEADERS}" \ + "-mmcu=${mcu_atmega32A} -O3" \ + "ALL" \ + "${mcu_atmega32A}" \ + ${source_dir} diff --git a/helper-scripts/project-impl/compile-atmega328p.sh b/helper-scripts/project-impl/compile-atmega328p.sh index 5fb7f03..17dda2e 100755 --- a/helper-scripts/project-impl/compile-atmega328p.sh +++ b/helper-scripts/project-impl/compile-atmega328p.sh @@ -3,4 +3,10 @@ source "./helper-scripts/abstract/abstract-compile.sh" source "./helper-scripts/project-impl/variables.sh" -compile ${TOOLCHAIN_HOME} ${mcu_atmega328p} ${source_dir} +compile "${AVR_C_COMPILER}" \ + "${AVR_CXX_COMPILER}" \ + "${AVR_TOOLCHAIN_HEADERS}" \ + "-mmcu=${mcu_atmega328p} -O3" \ + "ALL" \ + "${mcu_atmega328p}" \ + ${source_dir} diff --git a/helper-scripts/project-impl/compile-examples.sh b/helper-scripts/project-impl/compile-examples.sh index bb3697d..86b74f1 100755 --- a/helper-scripts/project-impl/compile-examples.sh +++ b/helper-scripts/project-impl/compile-examples.sh @@ -6,4 +6,11 @@ source "./helper-scripts/project-impl/variables.sh" target_mcu=${1} example=${2} -compile ${TOOLCHAIN_HOME} ${target_mcu} ${examples_dir} ${source_dir} ${example} \ No newline at end of file +compile "${AVR_C_COMPILER}" \ + "${AVR_CXX_COMPILER}" \ + "${AVR_TOOLCHAIN_HEADERS}" \ + "-mmcu=${target_mcu} -O3" \ + "${target_mcu}" \ + "${source_dir}" \ + "${example}" \ + "${examples_dir}" \ No newline at end of file diff --git a/helper-scripts/project-impl/upload-to-chip.sh b/helper-scripts/project-impl/upload-to-chip.sh index 581ead3..e1528e4 100755 --- a/helper-scripts/project-impl/upload-to-chip.sh +++ b/helper-scripts/project-impl/upload-to-chip.sh @@ -6,5 +6,5 @@ source "./helper-scripts/project-impl/variables.sh" elf=${1} directory=${2} -convertToHex ${TOOLCHAIN_HOME} ${elf} ${directory} +convertToHex ${AVR_OBJ_COPY} ${elf} ${directory} upload ${PROGRAMMER} ${BAUD_RATE} ${PORT} ${CHIP_ALIAS} ${elf} ${directory} \ No newline at end of file diff --git a/helper-scripts/project-impl/variables.sh b/helper-scripts/project-impl/variables.sh index a8b4ede..92c811c 100644 --- a/helper-scripts/project-impl/variables.sh +++ b/helper-scripts/project-impl/variables.sh @@ -1,8 +1,13 @@ #!/bin/bash project_root=$(pwd) -# home for toolchains and header files to link to sources -TOOLCHAIN_HOME="$project_root/avr8-gnu-toolchain-linux_x86_64" + +# AVR home for toolchains and header files to link to sources +AVR_TOOLCHAIN_HOME="$project_root/avr8-gnu-toolchain-linux_x86_64" +AVR_C_COMPILER="${AVR_TOOLCHAIN_HOME}/bin/avr-gcc" +AVR_CXX_COMPILER="${AVR_TOOLCHAIN_HOME}/bin/avr-g++" +AVR_OBJ_COPY="${AVR_TOOLCHAIN_HOME}/bin/avr-objcopy" +AVR_TOOLCHAIN_HEADERS="${AVR_TOOLCHAIN_HOME}/avr/include" # supported targets mcu_atmega32A="atmega32"