-
Notifications
You must be signed in to change notification settings - Fork 5
/
lib.rs
34 lines (31 loc) · 1.02 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
use pd_ext::builder::ControlExternalBuilder;
use pd_ext::external::ControlExternal;
use pd_ext::outlet::{OutletSend, OutletType};
use pd_ext_macros::external;
//based on https://github.com/pure-data/externals-howto#a-simple-external-counter
external! {
#[repr(packed)]
pub struct Counter {
count: isize,
outlet: Box<dyn OutletSend>
}
impl ControlExternal for Counter {
fn new(builder: &mut dyn ControlExternalBuilder<Self>) -> Result<Self, String> {
let count = if let Some(atom) = builder.creation_args().iter().next() {
atom.get_int().unwrap_or(0) as isize
} else {
0isize
};
let outlet = builder.new_message_outlet(OutletType::Float);
Ok(Self { count, outlet })
}
}
impl Counter {
#[bang]
pub fn bang(&mut self) {
let f = self.count as pd_sys::t_float;
self.count = self.count.wrapping_add(1);
self.outlet.send_float(f);
}
}
}