Skip to content

Commit

Permalink
implement persistence
Browse files Browse the repository at this point in the history
  • Loading branch information
astraw committed Nov 22, 2021
1 parent c429ed9 commit c1ffd91
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
20 changes: 20 additions & 0 deletions examples/feature-persistence.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
fn main() -> anyhow::Result<()> {
// Before using any pylon methods, the pylon runtime must be initialized.
let pylon = pylon_cxx::Pylon::new();

// Create an instant camera object with the camera device found first.
let camera = pylon_cxx::TlFactory::instance(&pylon).create_first_device()?;

camera.open()?;

let filename = "NodeMap.pfs";

println!("Saving camera's node map to file.");
pylon_cxx::NodeMap::save(&camera, filename)?;

println!("Reading file back to camera's node map.");
pylon_cxx::NodeMap::load(&camera, filename, true)?;

println!("Ok.");
Ok(())
}
6 changes: 6 additions & 0 deletions include/pylon-cxx-rs.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ namespace Pylon
void instant_camera_open(const std::unique_ptr<CInstantCamera> &);
bool instant_camera_is_open(const std::unique_ptr<CInstantCamera> &);
void instant_camera_close(const std::unique_ptr<CInstantCamera> &);
void node_map_load(const std::unique_ptr<CInstantCamera> &, rust::String, bool);
void node_map_save(const std::unique_ptr<CInstantCamera> &, rust::String);

void node_map_load_from_string(const std::unique_ptr<CInstantCamera> &, rust::String, bool);
rust::String node_map_save_to_string(const std::unique_ptr<CInstantCamera> &);

void instant_camera_start_grabbing(const std::unique_ptr<CInstantCamera> &);
void instant_camera_start_grabbing_with_count(const std::unique_ptr<CInstantCamera> &, uint32_t);
void instant_camera_stop_grabbing(const std::unique_ptr<CInstantCamera> &);
Expand Down
44 changes: 44 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,19 @@ mod ffi {
timeout_handling: TimeoutHandling,
) -> Result<bool>;

fn node_map_load(
camera: &UniquePtr<CInstantCamera>,
filename: String,
validate: bool,
) -> Result<()>;
fn node_map_save(camera: &UniquePtr<CInstantCamera>, filename: String) -> Result<()>;
fn node_map_load_from_string(
camera: &UniquePtr<CInstantCamera>,
features: String,
validate: bool,
) -> Result<()>;
fn node_map_save_to_string(camera: &UniquePtr<CInstantCamera>) -> Result<String>;

fn node_map_get_boolean_parameter(
camera: &UniquePtr<CInstantCamera>,
name: &str,
Expand Down Expand Up @@ -390,6 +403,11 @@ impl CommandNode {
}

pub trait NodeMap {
fn load<P: AsRef<std::path::Path>>(&self, path: P, validate: bool) -> PylonResult<()>;
fn save<P: AsRef<std::path::Path>>(&self, path: P) -> PylonResult<()>;
fn load_from_string(&self, features: String, validate: bool) -> PylonResult<()>;
fn save_to_string(&self) -> PylonResult<String>;

fn boolean_node(&self, name: &str) -> PylonResult<BooleanNode>;
fn integer_node(&self, name: &str) -> PylonResult<IntegerNode>;
fn float_node(&self, name: &str) -> PylonResult<FloatNode>;
Expand All @@ -400,6 +418,21 @@ pub trait NodeMap {
unsafe impl<'a> Send for InstantCamera<'a> {}

impl<'a> NodeMap for InstantCamera<'a> {
fn load<P: AsRef<std::path::Path>>(&self, path: P, validate: bool) -> PylonResult<()> {
let filename = path_to_string(path)?;
ffi::node_map_load(&self.inner, filename, validate).into_rust()
}
fn save<P: AsRef<std::path::Path>>(&self, path: P) -> PylonResult<()> {
let filename = path_to_string(path)?;
ffi::node_map_save(&self.inner, filename).into_rust()
}
fn load_from_string(&self, features: String, validate: bool) -> PylonResult<()> {
ffi::node_map_load_from_string(&self.inner, features, validate).into_rust()
}
fn save_to_string(&self) -> PylonResult<String> {
ffi::node_map_save_to_string(&self.inner).into_rust()
}

fn boolean_node(&self, name: &str) -> PylonResult<BooleanNode> {
let inner = ffi::node_map_get_boolean_parameter(&self.inner, name)?;
Ok(BooleanNode { inner })
Expand Down Expand Up @@ -615,3 +648,14 @@ impl Clone for DeviceInfo {
}

unsafe impl Send for DeviceInfo {}

fn path_to_string<P: AsRef<std::path::Path>>(path: P) -> PylonResult<String> {
match path.as_ref().to_str() {
Some(filename) => Ok(filename.into()),
None => Err(PylonError {
msg: "Cannot convert path to UTF-8".to_string(),
#[cfg(feature = "backtrace")]
backtrace: Backtrace::capture(),
}),
}
}
25 changes: 25 additions & 0 deletions src/pylon-cxx-rs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,31 @@ namespace Pylon
camera->Close();
}

void node_map_load(const std::unique_ptr<CInstantCamera> &camera, rust::String filename, bool validate)
{
const char *filename_c = filename.c_str();
CFeaturePersistence::Load(filename_c, &camera->GetNodeMap(), validate);
}

void node_map_save(const std::unique_ptr<CInstantCamera> &camera, rust::String filename)
{
const char *filename_c = filename.c_str();
CFeaturePersistence::Save(filename_c, &camera->GetNodeMap());
}

void node_map_load_from_string(const std::unique_ptr<CInstantCamera> &camera, rust::String features, bool validate)
{
const char *features_c = features.c_str();
CFeaturePersistence::LoadFromString(features_c, &camera->GetNodeMap(), validate);
}

rust::String node_map_save_to_string(const std::unique_ptr<CInstantCamera> &camera)
{
Pylon::String_t result;
CFeaturePersistence::SaveToString(result, &camera->GetNodeMap());
return rust::String(result.c_str(), result.length());
}

void instant_camera_start_grabbing(const std::unique_ptr<CInstantCamera> &camera)
{
camera->StartGrabbing();
Expand Down

0 comments on commit c1ffd91

Please sign in to comment.