Cmake build system for esp32 #1

Merged
franchioping merged 8 commits from cmake_esp32 into master 2026-04-02 16:55:09 +01:00
7 changed files with 336 additions and 107 deletions
Showing only changes of commit c3a3a356ed - Show all commits

17
.nvim.lua Normal file
View File

@ -0,0 +1,17 @@
-- You must enable the exrc setting in neovim for this config file to be used.
local rust_analyzer = {
cargo = {
target = "xtensa-esp32-none-elf",
allTargets = false,
},
}
rust_analyzer.cargo.extraEnv = { RUST_TOOLCHAIN = "esp" }
rust_analyzer.check = { extraEnv = { RUST_TOOLCHAIN = "esp" } }
rust_analyzer.server = { extraEnv = { RUST_TOOLCHAIN = "stable" } }
-- Note the neovim name of the language server is rust_analyzer with an underscore.
vim.lsp.config("rust_analyzer", {
settings = {
["rust-analyzer"] = rust_analyzer
},
})

View File

@ -5,7 +5,7 @@ version = "0.1.0"
edition = "2024"
[lib]
crate-type = ["staticlib"]
crate-type = ["staticlib", "lib"]
[build-dependencies]
cbindgen = "0.29"

View File

@ -1,5 +1,5 @@
use std::env;
use std::path::{Path, PathBuf};
use std::{env, time};
fn main() {
let cargo_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
@ -19,4 +19,11 @@ fn run_cbindgen(cargo_dir: &Path, target_dir: &Path) {
.write_to_file(&out);
println!("cargo:rerun-if-changed={}", out.display());
println!(
"cargo:rerun-if-changed={}",
time::SystemTime::now()
.duration_since(time::UNIX_EPOCH)
.unwrap()
.as_secs()
);
}

137
cbindgen.toml Normal file
View File

@ -0,0 +1,137 @@
# This is a template cbindgen.toml file with all of the default values.
# Some values are commented out because their absence is the real default.
#
# See https://github.com/mozilla/cbindgen/blob/main/docs.md#cbindgentoml
# for detailed documentation of every option here.
language = "C++"
############## Options for Wrapping the Contents of the Header #################
# header = "/* Text to put at the beginning of the generated file. Probably a license. */"
# trailer = "/* Text to put at the end of the generated file */"
# include_guard = "my_bindings_h"
# pragma_once = true
# autogen_warning = "/* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */"
include_version = false
namespace = "dcont"
# namespaces = []
using_namespaces = []
sys_includes = []
includes = []
no_includes = false
# cpp_compat = true
after_includes = ""
############################ Code Style Options ################################
braces = "SameLine"
line_length = 100
tab_width = 2
documentation = true
documentation_style = "auto"
documentation_length = "full"
line_endings = "LF" # also "CR", "CRLF", "Native"
############################# Codegen Options ##################################
style = "both"
sort_by = "Name" # default for `fn.sort_by` and `const.sort_by`
usize_is_size_t = true
[defines]
# "target_os = freebsd" = "DEFINE_FREEBSD"
# "feature = serde" = "DEFINE_SERDE"
[export]
include = []
exclude = []
# prefix = "CAPI_"
item_types = []
renaming_overrides_prefixing = false
[export.rename]
[export.body]
[export.mangle]
[fn]
rename_args = "None"
# must_use = "MUST_USE_FUNC"
# deprecated = "DEPRECATED_FUNC"
# deprecated_with_note = "DEPRECATED_FUNC_WITH_NOTE"
# no_return = "NO_RETURN"
# prefix = "START_FUNC"
# postfix = "END_FUNC"
args = "auto"
sort_by = "Name"
[struct]
rename_fields = "None"
# must_use = "MUST_USE_STRUCT"
# deprecated = "DEPRECATED_STRUCT"
# deprecated_with_note = "DEPRECATED_STRUCT_WITH_NOTE"
rename_associated_constant = "None"
derive_constructor = false
derive_eq = false
derive_neq = false
derive_lt = false
derive_lte = false
derive_gt = false
derive_gte = false
[enum]
rename_variants = "None"
# must_use = "MUST_USE_ENUM"
# deprecated = "DEPRECATED_ENUM"
# deprecated_with_note = "DEPRECATED_ENUM_WITH_NOTE"
add_sentinel = false
prefix_with_name = false
derive_helper_methods = false
derive_const_casts = false
derive_mut_casts = false
# cast_assert_name = "ASSERT"
derive_tagged_enum_destructor = false
derive_tagged_enum_copy_constructor = false
enum_class = true
private_default_tagged_enum_constructor = false
[const]
allow_static_const = true
allow_constexpr = false
sort_by = "Name"
[macro_expansion]
bitflags = false
############## Options for How Your Rust library Should Be Parsed ##############
[parse]
parse_deps = false
# include = []
exclude = []
clean = false
extra_bindings = []
[parse.expand]
crates = []
all_features = false
default_features = true
features = []

96
src/ccont.rs Normal file
View File

@ -0,0 +1,96 @@
use crate::*;
use nalgebra as na;
#[unsafe(no_mangle)]
pub extern "C" fn create(c: config::ControllerConfig) -> *mut StackedController {
return Box::into_raw(Box::new(StackedController::new(c)));
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn destroy(ptr: *mut StackedController) {
assert!(!ptr.is_null());
let _x = unsafe { Box::from_raw(ptr) };
}
#[repr(C)]
pub struct MotorThrottles {
pub values: [f32; 4],
}
#[unsafe(no_mangle)]
pub extern "C" fn get_throttles(ptr: *mut StackedController) -> MotorThrottles {
let controller = unsafe {
assert!(!ptr.is_null());
&mut *ptr
};
return MotorThrottles {
values: controller.get_motor_throttles(),
};
}
#[unsafe(no_mangle)]
pub extern "C" fn set_cur_time(ptr: *mut StackedController, time: f32) {
let controller = unsafe {
assert!(!ptr.is_null());
&mut *ptr
};
controller.set_time(time);
}
#[unsafe(no_mangle)]
pub extern "C" fn set_input(ptr: *mut StackedController, inp: Input) {
let controller = unsafe {
assert!(!ptr.is_null());
&mut *ptr
};
controller.set_input(inp);
}
#[unsafe(no_mangle)]
pub extern "C" fn set_cur_rot(ptr: *mut StackedController, parts: Vec3C, scalar: f32) {
let controller = unsafe {
assert!(!ptr.is_null());
&mut *ptr
};
let quat: na::UnitQuaternion<f32> =
na::UnitQuaternion::from_quaternion(na::Quaternion::from_parts(scalar, parts.into()));
controller.set_rotation(quat);
}
#[unsafe(no_mangle)]
pub extern "C" fn set_cur_angvel(ptr: *mut StackedController, angvel: Vec3C) {
let controller = unsafe {
assert!(!ptr.is_null());
&mut *ptr
};
controller.set_angular_velocity(angvel.into());
}
#[unsafe(no_mangle)]
pub extern "C" fn set_cur_linvel(ptr: *mut StackedController, vel: Vec3C) {
let controller = unsafe {
assert!(!ptr.is_null());
&mut *ptr
};
controller.set_linvel(vel.into());
}
#[unsafe(no_mangle)]
pub extern "C" fn set_cur_pos(ptr: *mut StackedController, pos: Vec3C) {
let controller = unsafe {
assert!(!ptr.is_null());
&mut *ptr
};
controller.set_pos(pos.into());
}

73
src/cmath.rs Normal file
View File

@ -0,0 +1,73 @@
use nalgebra as na;
#[repr(C)]
pub struct Vec3C {
pub x: f32,
pub y: f32,
pub z: f32,
}
#[repr(C)]
pub struct QuatC {
pub i: f32,
pub j: f32,
pub k: f32,
pub w: f32,
}
impl Into<na::UnitQuaternion<f32>> for QuatC {
fn into(self) -> na::UnitQuaternion<f32> {
na::UnitQuaternion::from_quaternion(na::Quaternion::from_parts(
self.w,
na::vector![self.i, self.j, self.k],
))
}
}
impl Into<na::Vector3<f32>> for Vec3C {
fn into(self) -> na::Vector3<f32> {
na::vector![self.x, self.y, self.z]
}
}
impl From<na::Vector3<f32>> for Vec3C {
fn from(value: na::Vector3<f32>) -> Self {
Vec3C { x: value.x, y: value.y, z: value.z }
}
}
#[unsafe(no_mangle)]
pub extern "C" fn apply_rot(vec_ptr: *const Vec3C, quat_ptr: *const QuatC) -> Vec3C{
assert!(!vec_ptr.is_null());
assert!(!quat_ptr.is_null());
unsafe {
let quat: na::UnitQuaternion<f32> = std::ptr::read(quat_ptr).into();
let v: na::Vector3<f32> = std::ptr::read(vec_ptr).into();
return quat.transform_vector(&v).into();
}
}
#[unsafe(no_mangle)]
pub extern "C" fn multiply_vec_by(vec_ptr: *mut Vec3C, k: f32) {
assert!(!vec_ptr.is_null());
unsafe {
(*vec_ptr).x *= k;
(*vec_ptr).y *= k;
(*vec_ptr).z *= k;
}
}
#[unsafe(no_mangle)]
pub extern "C" fn add_to_vec(vec_ptr: *mut Vec3C, to_add_ptr: *const Vec3C){
assert!(!vec_ptr.is_null());
assert!(!to_add_ptr.is_null());
unsafe {
(*vec_ptr).x += (*to_add_ptr).x;
(*vec_ptr).y += (*to_add_ptr).y;
(*vec_ptr).z += (*to_add_ptr).z;
}
}

View File

@ -1,5 +1,3 @@
use nalgebra::{self as na, Quaternion, Unit};
pub mod config;
pub mod controller;
pub mod input;
@ -10,106 +8,7 @@ pub use controller::*;
pub use input::*;
pub use stacked::*;
#[unsafe(no_mangle)]
pub extern "C" fn create(c: config::ControllerConfig) -> *mut StackedController {
return Box::into_raw(Box::new(StackedController::new(c)));
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn destroy(ptr: *mut StackedController) {
assert!(!ptr.is_null());
let _x = unsafe { Box::from_raw(ptr) };
}
#[repr(C)]
pub struct MotorThrottles {
pub values: [f32; 4],
}
#[repr(C)]
pub struct Vec3C {
pub x: f32,
pub y: f32,
pub z: f32,
}
impl Into<na::Vector3<f32>> for Vec3C {
fn into(self) -> na::Vector3<f32> {
na::vector![self.x, self.y, self.z]
}
}
#[unsafe(no_mangle)]
pub extern "C" fn get_throttles(ptr: *mut StackedController) -> MotorThrottles {
let controller = unsafe {
assert!(!ptr.is_null());
&mut *ptr
};
return MotorThrottles {
values: controller.get_motor_throttles(),
};
}
#[unsafe(no_mangle)]
pub extern "C" fn set_cur_time(ptr: *mut StackedController, time: f32) {
let controller = unsafe {
assert!(!ptr.is_null());
&mut *ptr
};
controller.set_time(time);
}
#[unsafe(no_mangle)]
pub extern "C" fn set_input(ptr: *mut StackedController, inp: Input) {
let controller = unsafe {
assert!(!ptr.is_null());
&mut *ptr
};
controller.set_input(inp);
}
#[unsafe(no_mangle)]
pub extern "C" fn set_cur_rot(ptr: *mut StackedController, parts: Vec3C, scalar: f32) {
let controller = unsafe {
assert!(!ptr.is_null());
&mut *ptr
};
let quat: na::UnitQuaternion<f32> =
na::UnitQuaternion::from_quaternion(Quaternion::from_parts(scalar, parts.into()));
controller.set_rotation(quat);
}
#[unsafe(no_mangle)]
pub extern "C" fn set_cur_angvel(ptr: *mut StackedController, angvel: Vec3C) {
let controller = unsafe {
assert!(!ptr.is_null());
&mut *ptr
};
controller.set_angular_velocity(angvel.into());
}
#[unsafe(no_mangle)]
pub extern "C" fn set_cur_linvel(ptr: *mut StackedController, vel: Vec3C) {
let controller = unsafe {
assert!(!ptr.is_null());
&mut *ptr
};
controller.set_linvel(vel.into());
}
#[unsafe(no_mangle)]
pub extern "C" fn set_cur_pos(ptr: *mut StackedController, pos: Vec3C) {
let controller = unsafe {
assert!(!ptr.is_null());
&mut *ptr
};
controller.set_pos(pos.into());
}
pub mod ccont;
pub mod cmath;
pub use ccont::*;
pub use cmath::*;