use cameleon::genapi::{
CacheStore, DefaultCacheStore, DefaultGenApiCtxt, DefaultNodeStore, DefaultValueStore,
GenApiCtxt, NodeId, ValueCtxt,
};
use cameleon::{u3v, Camera};
struct MyCacheStore {
store: DefaultCacheStore,
use_cache: bool,
}
impl CacheStore for MyCacheStore {
fn cache(&mut self, nid: NodeId, address: i64, length: i64, data: &[u8]) {
if self.use_cache {
self.store.cache(nid, address, length, data)
}
}
fn get_cache(&self, nid: NodeId, address: i64, length: i64) -> Option<&[u8]> {
if self.use_cache {
self.store.get_cache(nid, address, length)
} else {
None
}
}
fn invalidate_by(&mut self, nid: NodeId) {
if self.use_cache {
self.store.invalidate_by(nid)
}
}
fn invalidate_of(&mut self, nid: NodeId) {
if self.use_cache {
self.store.invalidate_of(nid)
}
}
fn clear(&mut self) {
if self.use_cache {
self.store.clear();
}
}
}
struct MyGenApiCtxt {
node_store: DefaultNodeStore,
value_ctxt: ValueCtxt<DefaultValueStore, MyCacheStore>,
}
impl GenApiCtxt for MyGenApiCtxt {
type NS = DefaultNodeStore;
type VS = DefaultValueStore;
type CS = MyCacheStore;
fn enter<F, R>(&mut self, f: F) -> R
where
F: FnOnce(&Self::NS, &mut ValueCtxt<Self::VS, Self::CS>) -> R,
{
f(&self.node_store, &mut self.value_ctxt)
}
fn node_store(&self) -> &Self::NS {
&self.node_store
}
fn clear_cache(&mut self) {
self.value_ctxt.clear_cache()
}
}
impl MyGenApiCtxt {
fn enable_cache(&mut self) {
self.value_ctxt.cache_store.use_cache = true;
}
fn disable_cache(&mut self) {
self.value_ctxt.cache_store.use_cache = false;
}
}
impl From<DefaultGenApiCtxt> for MyGenApiCtxt {
fn from(from: DefaultGenApiCtxt) -> Self {
let value_ctxt = ValueCtxt::new(
from.value_ctxt.value_store,
MyCacheStore {
store: from.value_ctxt.cache_store,
use_cache: true,
},
);
Self {
node_store: from.node_store,
value_ctxt,
}
}
}
fn main() {
let mut cameras = u3v::enumerate_cameras().unwrap();
if cameras.is_empty() {
println!("no camera found!");
return;
}
let mut camera = cameras.pop().unwrap();
camera.open().unwrap();
camera.load_context().unwrap();
let mut camera: Camera<u3v::ControlHandle, u3v::StreamHandle, MyGenApiCtxt> =
camera.convert_into();
let mut params_ctxt = camera.params_ctxt().unwrap();
params_ctxt.ctxt.enable_cache();
let gain_node = params_ctxt
.node("Gain")
.unwrap()
.as_float(¶ms_ctxt)
.unwrap();
gain_node.set_value(&mut params_ctxt, 0.1_f64).unwrap();
params_ctxt.ctxt.disable_cache();
let value = gain_node.value(&mut params_ctxt).unwrap();
println!("Gain: {}", value);
camera.close().unwrap();
}