Appendix B: Function Block Reference
AutoCore provides standard function blocks inspired by IEC 61131-3. Import them from autocore_std::fb.
RTrig — Rising Edge Detector
Detects false to true transitions. Equivalent to R_TRIG in IEC 61131-3.
#![allow(unused)]
fn main() {
use autocore_std::fb::RTrig;
let mut trig = RTrig::new();
trig.call(false); // returns false
trig.call(true); // returns true (rising edge detected)
trig.call(true); // returns false (no transition)
trig.call(false); // returns false
trig.call(true); // returns true (another rising edge)
}
FTrig — Falling Edge Detector
Detects true to false transitions. Equivalent to F_TRIG in IEC 61131-3.
#![allow(unused)]
fn main() {
use autocore_std::fb::FTrig;
let mut trig = FTrig::new();
trig.call(true); // returns false
trig.call(false); // returns true (falling edge detected)
trig.call(false); // returns false (no transition)
trig.call(true); // returns false
trig.call(false); // returns true (another falling edge)
}
Ton — Timer On Delay
Output becomes true after input has been true for the specified duration. Equivalent to TON in IEC 61131-3.
#![allow(unused)]
fn main() {
use autocore_std::fb::Ton;
use std::time::Duration;
let mut timer = Ton::new();
// In process_tick:
let done = timer.call(input_signal, Duration::from_secs(5));
// done = true after input_signal has been true for 5 seconds continuously
// timer.et = elapsed time
// timer.q = same as the return value (done)
// If input_signal becomes false at any time, the timer resets
}
Fields:
| Field | Type | Description |
|---|---|---|
q | bool | Output — true when timer has elapsed |
et | Duration | Elapsed time since input became true |
BitResetOnDelay — Auto-Reset Timer
Sets output to false after a delay. Useful for pulse outputs.
#![allow(unused)]
fn main() {
use autocore_std::fb::BitResetOnDelay;
use std::time::Duration;
let mut reset = BitResetOnDelay::new(Duration::from_millis(500));
// When you set the bit to true, it automatically resets to false after 500ms
reset.set(); // Output becomes true
// ... 500ms later, in process_tick ...
reset.call(); // Call every cycle to update
// reset.q becomes false after the delay
}
RunningAverage — Online Averaging
Computes a running average of values.
#![allow(unused)]
fn main() {
use autocore_std::fb::RunningAverage;
let mut avg = RunningAverage::new();
avg.add(10.0);
avg.add(20.0);
avg.add(30.0);
let mean = avg.average(); // 20.0
let count = avg.count(); // 3
avg.reset(); // Start over
}