Skip to content

Instantly share code, notes, and snippets.

@nmarley
Last active Jan 7, 2021
Embed
What would you like to do?
Example of sending interrupt using Rust mpsc channel
use std::sync::mpsc::channel;
use std::sync::mpsc::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