aboutsummaryrefslogtreecommitdiffstats
path: root/src-riscv/led.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src-riscv/led.rs')
-rw-r--r--src-riscv/led.rs42
1 files changed, 42 insertions, 0 deletions
diff --git a/src-riscv/led.rs b/src-riscv/led.rs
new file mode 100644
index 0000000..536121b
--- /dev/null
+++ b/src-riscv/led.rs
@@ -0,0 +1,42 @@
+use gd32vf103xx_hal::{
+ gpio::{Floating, Input, Output, PushPull, Pxx, State, gpioa::PA7},
+};
+use embedded_hal::digital::v2::OutputPin;
+
+pub struct LED {
+ pin: Pxx<Output<PushPull>>,
+ state: State,
+}
+
+impl LED {
+ pub fn new(pin: PA7<Input<Floating>>) -> Self {
+ let mut p = pin.into_push_pull_output().downgrade();
+ p.set_low().ok();
+ Self { pin: p, state: State::Low }
+ }
+
+ pub fn is_on(&self) -> bool {
+ match self.state {
+ State::High => true,
+ State::Low => false,
+ }
+ }
+
+ pub fn on(&mut self) {
+ self.state = State::High;
+ self.pin.set_high().ok();
+ }
+
+ pub fn off(&mut self) {
+ self.state = State::Low;
+ self.pin.set_low().ok();
+ }
+
+ pub fn toggle(&mut self) {
+ if self.is_on() {
+ self.off();
+ } else {
+ self.on();
+ }
+ }
+}