From f12811a0a5e15b596a0cc06c095832a6b795172b Mon Sep 17 00:00:00 2001 From: Brian Cully Date: Sun, 28 Apr 2019 13:41:39 -0400 Subject: Working handler code. --- examples/dummy.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 examples/dummy.rs (limited to 'examples/dummy.rs') diff --git a/examples/dummy.rs b/examples/dummy.rs new file mode 100644 index 0000000..7e7c237 --- /dev/null +++ b/examples/dummy.rs @@ -0,0 +1,46 @@ +use clint::Handler; + +// Wrapper used to call through to `example_handler` via `closure` in +// `main`. `Handler::new()` places a do-nothing handler in this at +// compile-time, in case the interrupt using this handler is fired +// before being `replace`d in `main`. +static mut HANDLER: Handler = Handler::new(); + +fn main() { + let mut x: u32 = 0; + + // Create a closure to take a mutable reference to `x` for use in + // `example_handler`. + let closure = move || example_handler(&mut x); + + // Swap out the do-nothing handler with our closure that calls + // through to `example_handler`. Ideally, the interrupt which uses + // this handler would be disabled while this happens, but as this + // is a demo, and there aren't any actual interrupts firing, this + // is left as an exercise to the reader. + unsafe { HANDLER.replace(&closure) }; + + // Simulate firing the interrupt. + dummy_interrupt(); + dummy_interrupt(); + + // Because `x` is `Copy`, we still have access to the symbol, + // although its value won't be changed by `closure`. + println!("x(o): {}", x); +} + +// Not a real interrupt handler, but called like one. i.e.: simple +// function with no arguments. +// +// Calls through `HANDLER` to do its actual work. +fn dummy_interrupt() { + unsafe { HANDLER.call() }; +} + +// The meat of the interrupt handler, which does work with whatever +// params were passed in via `closure` in `main`. +fn example_handler(x: &mut u32) { + // Update our dummy value, just to show it works. + *x += 2; + println!("x: {}", x); +} -- cgit v1.2.3