1// Licensed under the Apache License, Version 2.0 or the MIT License.
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3// Copyright Tock Contributors 2022.
45//! Interface for LEDs that abstract away polarity and pin.
6//!
7//! Author: Philip Levis <pal@cs.stanford.edu>
8//! Date: July 31, 2015
9//!
1011use crate::hil::gpio;
1213/// Simple on/off interface for LED pins.
14///
15/// Since GPIO pins are synchronous in Tock the LED interface is synchronous as
16/// well.
17pub trait Led {
18/// Initialize the LED. Must be called before the LED is used.
19fn init(&self);
2021/// Turn the LED on.
22fn on(&self);
2324/// Turn the LED off.
25fn off(&self);
2627/// Toggle the LED.
28fn toggle(&self);
2930/// Return the on/off state of the LED. `true` if the LED is on, `false` if
31 /// it is off.
32fn read(&self) -> bool;
33}
3435/// For LEDs in which on is when GPIO is high.
36pub struct LedHigh<'a, P: gpio::Pin> {
37pub pin: &'a P,
38}
3940/// For LEDs in which on is when GPIO is low.
41pub struct LedLow<'a, P: gpio::Pin> {
42pub pin: &'a P,
43}
4445impl<'a, P: gpio::Pin> LedHigh<'a, P> {
46pub fn new(p: &'a P) -> Self {
47Self { pin: p }
48 }
49}
5051impl<'a, P: gpio::Pin> LedLow<'a, P> {
52pub fn new(p: &'a P) -> Self {
53Self { pin: p }
54 }
55}
5657impl<P: gpio::Pin> Led for LedHigh<'_, P> {
58fn init(&self) {
59self.pin.make_output();
60 }
6162fn on(&self) {
63self.pin.set();
64 }
6566fn off(&self) {
67self.pin.clear();
68 }
6970fn toggle(&self) {
71self.pin.toggle();
72 }
7374fn read(&self) -> bool {
75self.pin.read()
76 }
77}
7879impl<P: gpio::Pin> Led for LedLow<'_, P> {
80fn init(&self) {
81self.pin.make_output();
82 }
8384fn on(&self) {
85self.pin.clear();
86 }
8788fn off(&self) {
89self.pin.set();
90 }
9192fn toggle(&self) {
93self.pin.toggle();
94 }
9596fn read(&self) -> bool {
97 !self.pin.read()
98 }
99}