Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

usb_hub: split powered state and request to prevent glitches #21

Merged
merged 1 commit into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions src/ui/screens/usb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ impl UsbScreen {

struct Active {
widgets: WidgetContainer,
port_enables: [Arc<Topic<bool>>; 3],
port_requests: [Arc<Topic<bool>>; 3],
port_states: [Arc<Topic<bool>>; 3],
highlighted: Arc<Topic<usize>>,
}

Expand All @@ -78,19 +79,19 @@ impl ActivatableScreen for UsbScreen {
(
0,
"Port 1",
&ui.res.usb_hub.port1.powered,
&ui.res.usb_hub.port1.status,
&ui.res.adc.usb_host1_curr.topic,
),
(
1,
"Port 2",
&ui.res.usb_hub.port2.powered,
&ui.res.usb_hub.port2.status,
&ui.res.adc.usb_host2_curr.topic,
),
(
2,
"Port 3",
&ui.res.usb_hub.port3.powered,
&ui.res.usb_hub.port3.status,
&ui.res.adc.usb_host3_curr.topic,
),
];
Expand Down Expand Up @@ -147,16 +148,22 @@ impl ActivatableScreen for UsbScreen {
});
}

let port_enables = [
ui.res.usb_hub.port1.powered.clone(),
ui.res.usb_hub.port2.powered.clone(),
ui.res.usb_hub.port3.powered.clone(),
let port_requests = [
ui.res.usb_hub.port1.request.clone(),
ui.res.usb_hub.port2.request.clone(),
ui.res.usb_hub.port3.request.clone(),
];
let port_states = [
ui.res.usb_hub.port1.status.clone(),
ui.res.usb_hub.port2.status.clone(),
ui.res.usb_hub.port3.status.clone(),
];
let highlighted = self.highlighted.clone();

let active = Active {
widgets,
port_enables,
port_requests,
port_states,
highlighted,
};

Expand All @@ -182,7 +189,10 @@ impl ActiveScreen for Active {
InputEvent::ToggleAction(_) => {
self.highlighted.set((highlighted + 1) % 3);
}
InputEvent::PerformAction(_) => self.port_enables[highlighted].toggle(false),
InputEvent::PerformAction(_) => {
let status = self.port_states[highlighted].try_get().unwrap_or(false);
self.port_requests[highlighted].set(!status);
}
}
}
}
17 changes: 11 additions & 6 deletions src/usb_hub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ pub struct UsbDevice {

#[derive(Clone)]
pub struct UsbPort {
pub powered: Arc<Topic<bool>>,
pub request: Arc<Topic<bool>>,
pub status: Arc<Topic<bool>>,
pub device: Arc<Topic<Option<UsbDevice>>>,
}

Expand All @@ -157,30 +158,34 @@ pub struct UsbHub {

fn handle_port(bb: &mut BrokerBuilder, name: &'static str, base: &'static str) -> UsbPort {
let port = UsbPort {
powered: bb.topic_rw(format!("/v1/usb/host/{name}/powered").as_str(), None),
request: bb.topic_wo(format!("/v1/usb/host/{name}/powered").as_str(), None),
status: bb.topic_ro(format!("/v1/usb/host/{name}/powered").as_str(), None),
device: bb.topic_ro(format!("/v1/usb/host/{name}/device").as_str(), Some(None)),
};

let powered = port.powered.clone();
let request = port.request.clone();
let status = port.status.clone();
let device = port.device.clone();
let disable_path = Path::new(base).join("disable");

// Spawn a task that turns USB port power on or off upon request.
// Also clears the device info upon power off so it does not contain stale
// information until the next poll.
spawn(async move {
let (mut src, _) = powered.subscribe_unbounded();
let (mut src, _) = request.subscribe_unbounded();

while let Some(ev) = src.next().await {
write(&disable_path, if ev { b"0" } else { b"1" }).unwrap();

if !ev {
device.set(None);
}

status.set(ev);
}
});

let powered = port.powered.clone();
let status = port.status.clone();
let device = port.device.clone();
let disable_path = Path::new(base).join("disable");
let (id_product_path, id_vendor_path, manufacturer_path, product_path) = {
Expand All @@ -204,7 +209,7 @@ fn handle_port(bb: &mut BrokerBuilder, name: &'static str, base: &'static str) -
_ => panic!("Read unexpected value for USB port disable state"),
};

powered.set_if_changed(is_powered);
status.set_if_changed(is_powered);
}

let id_product = read_to_string(&id_product_path).ok();
Expand Down