kernel/hil/
led.rs

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.
4
5//! Interface for LEDs that abstract away polarity and pin.
6//!
7//!  Author: Philip Levis <pal@cs.stanford.edu>
8//!  Date: July 31, 2015
9//!
10
11use crate::hil::gpio;
12
13/// 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.
19    fn init(&self);
20
21    /// Turn the LED on.
22    fn on(&self);
23
24    /// Turn the LED off.
25    fn off(&self);
26
27    /// Toggle the LED.
28    fn toggle(&self);
29
30    /// Return the on/off state of the LED. `true` if the LED is on, `false` if
31    /// it is off.
32    fn read(&self) -> bool;
33}
34
35/// For LEDs in which on is when GPIO is high.
36pub struct LedHigh<'a, P: gpio::Pin> {
37    pub pin: &'a P,
38}
39
40/// For LEDs in which on is when GPIO is low.
41pub struct LedLow<'a, P: gpio::Pin> {
42    pub pin: &'a P,
43}
44
45impl<'a, P: gpio::Pin> LedHigh<'a, P> {
46    pub fn new(p: &'a P) -> Self {
47        Self { pin: p }
48    }
49}
50
51impl<'a, P: gpio::Pin> LedLow<'a, P> {
52    pub fn new(p: &'a P) -> Self {
53        Self { pin: p }
54    }
55}
56
57impl<P: gpio::Pin> Led for LedHigh<'_, P> {
58    fn init(&self) {
59        self.pin.make_output();
60    }
61
62    fn on(&self) {
63        self.pin.set();
64    }
65
66    fn off(&self) {
67        self.pin.clear();
68    }
69
70    fn toggle(&self) {
71        self.pin.toggle();
72    }
73
74    fn read(&self) -> bool {
75        self.pin.read()
76    }
77}
78
79impl<P: gpio::Pin> Led for LedLow<'_, P> {
80    fn init(&self) {
81        self.pin.make_output();
82    }
83
84    fn on(&self) {
85        self.pin.clear();
86    }
87
88    fn off(&self) {
89        self.pin.set();
90    }
91
92    fn toggle(&self) {
93        self.pin.toggle();
94    }
95
96    fn read(&self) -> bool {
97        !self.pin.read()
98    }
99}