Example of sending interrupt using Rust mpsc channel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::sync::mpsc::{channel, Sender}; | |
pub struct PPU { | |
// data... | |
cycles: u8, | |
nmi_channel: Sender<bool>, | |
} | |
impl PPU { | |
pub fn new(chan: Sender<bool>) -> Self { | |
PPU { | |
cycles: 0, | |
nmi_channel: chan, | |
} | |
} | |
pub fn fire(&self) { | |
self.nmi_channel.send(true).unwrap(); | |
} | |
pub fn tick(&mut self) { | |
self.cycles += 1; | |
// send an interrupt every 3 cycles... | |
if self.cycles % 3 == 0 { | |
self.fire(); | |
} | |
} | |
} | |
fn main() { | |
let (tx, rx) = channel(); | |
let mut ppu = PPU::new(tx); | |
for _ in 1..=10 { | |
ppu.tick(); | |
match rx.try_recv() { | |
Err(err) => println!("... no such luck: {}", err), | |
Ok(_) => println!("Got an interrupt from PPU!"), | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment