Skip to content

Commit

Permalink
lib: extend event handling
Browse files Browse the repository at this point in the history
  • Loading branch information
JerwuQu committed Oct 12, 2024
1 parent 5216591 commit 0c385ab
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 13 deletions.
10 changes: 7 additions & 3 deletions ggoled_app/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,15 @@ fn main() {
dialog_unwrap(config.save());
}

// Handle draw events
// Handle events
while let Some(event) = dev.try_event() {
println!("event: {:?}", event);
match event {
DrawEvent::Disconnected => update_connection(false),
DrawEvent::Reconnected => update_connection(true),
DrawEvent::DeviceDisconnected => update_connection(false),
DrawEvent::DeviceReconnected => update_connection(true),
DrawEvent::DeviceEvent(event) => match event {
_ => {}
},
}
}

Expand Down
29 changes: 24 additions & 5 deletions ggoled_draw/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// This is a wrapper around `ggoled_lib` that has high-level draw functions and additional events.
// Heavily specialised for `ggoled_cli` and `ggoled_app`, and is therefore not recommended for general use.

use anyhow::bail;
use ggoled_lib::{bitmap::BitVec, Bitmap, Device};
use ggoled_lib::{bitmap::BitVec, Bitmap, Device, DeviceEvent};
use image::{codecs::gif::GifDecoder, io::Reader as ImageReader, AnimationDecoder, ImageFormat};
use rusttype::{point, Font, Scale};
use std::{
Expand Down Expand Up @@ -157,9 +160,12 @@ enum DrawCommand {
SetShiftMode(ShiftMode),
Stop,
}

#[derive(Debug)]
pub enum DrawEvent {
Disconnected,
Reconnected,
DeviceDisconnected,
DeviceReconnected,
DeviceEvent(DeviceEvent),
}

struct AnimState {
Expand Down Expand Up @@ -223,7 +229,7 @@ fn run_draw_device_thread(
last_connect_attempt = time;
if dev.reconnect().is_ok() {
connected = true;
event_sender.send(DrawEvent::Reconnected).unwrap();
event_sender.send(DrawEvent::DeviceReconnected).unwrap();
}
}

Expand Down Expand Up @@ -292,7 +298,7 @@ fn run_draw_device_thread(
if let Err(_err) = dev.draw(&screen, 0, 0) {
if connected {
connected = false;
event_sender.send(DrawEvent::Disconnected).unwrap();
event_sender.send(DrawEvent::DeviceDisconnected).unwrap();
}
} else {
prev_screen = screen;
Expand All @@ -301,6 +307,19 @@ fn run_draw_device_thread(
drop(layers);
}

// Get device events and pass back to DrawDevice
if connected {
let events = dev.get_events().unwrap_or_else(|_| {
connected = false;
event_sender.send(DrawEvent::DeviceDisconnected).unwrap();
vec![]
});
for event in events {
event_sender.send(DrawEvent::DeviceEvent(event)).unwrap();
}
}

// Stop
if stop_after_frame {
break;
}
Expand Down
29 changes: 24 additions & 5 deletions ggoled_lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ struct ReportDrawable<'a> {
src_y: usize,
}

#[derive(Debug)]
pub enum DeviceEvent {
VolumeChanged { volume: u8 },
BatteryChanged { headset: u8, charging: u8 },
Expand Down Expand Up @@ -199,13 +200,14 @@ impl Device {
}

fn parse_event(buf: &[u8; 64]) -> Option<DeviceEvent> {
println!("got: {:x?}", buf);
#[cfg(debug_assertions)]
println!("parse_event: {:x?}", buf);
if buf[0] != 7 {
return None;
}
Some(match buf[1] {
0x25 => DeviceEvent::VolumeChanged {
volume: 38u8.saturating_sub(buf[3]),
volume: 0x38u8.saturating_sub(buf[2]),
},
// Connection/Disconnection: 0xb5 => {}
0xb7 => DeviceEvent::BatteryChanged {
Expand All @@ -217,10 +219,27 @@ impl Device {
})
}

/// Poll events from the device.
pub fn poll(&self) -> anyhow::Result<Option<DeviceEvent>> {
/// Poll events from the device. This blocks until an event is returned.
pub fn poll_event(&self) -> anyhow::Result<Option<DeviceEvent>> {
let mut buf = [0u8; 64];
_ = self.info_dev.read(&mut buf).unwrap();
self.info_dev.set_blocking_mode(true)?;
_ = self.info_dev.read(&mut buf)?;
Ok(Self::parse_event(&buf))
}

/// Return any pending events from the device. Non-blocking.
pub fn get_events(&self) -> anyhow::Result<Vec<DeviceEvent>> {
self.info_dev.set_blocking_mode(false)?;
let mut events = vec![];
loop {
let mut buf = [0u8; 64];
let len = self.info_dev.read(&mut buf)?;
if len == 0 {
break;
} else if let Some(event) = Self::parse_event(&buf) {
events.push(event);
}
}
Ok(events)
}
}

0 comments on commit 0c385ab

Please sign in to comment.