Debian Codegraph @

bindings/rust/gpiosim-sys/src/lib.rs

raw symbols SCIP
1 // SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause
2 // SPDX-FileCopyrightText: 2022 Linaro Ltd.
3 // SPDX-FileCopyrightText: 2022 Viresh Kumar <[email protected]>
4
5 use libgpiod::{Error, OperationType, Result};
6
7 #[allow(non_camel_case_types, non_upper_case_globals)]
8 #[cfg_attr(test, allow(deref_nullptr, non_snake_case))]
9 #[allow(dead_code)]
10 mod bindings_raw {
11 include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
12 }
13 use bindings_raw::*;
14
15 mod sim;
16 pub use sim::*;
17
18 use crate::{
19 gpiosim_direction_GPIOSIM_DIRECTION_INPUT as GPIOSIM_DIRECTION_INPUT,
20 gpiosim_direction_GPIOSIM_DIRECTION_OUTPUT_HIGH as GPIOSIM_DIRECTION_OUTPUT_HIGH,
21 gpiosim_direction_GPIOSIM_DIRECTION_OUTPUT_LOW as GPIOSIM_DIRECTION_OUTPUT_LOW,
22 gpiosim_pull_GPIOSIM_PULL_DOWN as GPIOSIM_PULL_DOWN,
23 gpiosim_pull_GPIOSIM_PULL_UP as GPIOSIM_PULL_UP,
24 gpiosim_value_GPIOSIM_VALUE_ACTIVE as GPIOSIM_VALUE_ACTIVE,
25 gpiosim_value_GPIOSIM_VALUE_ERROR as GPIOSIM_VALUE_ERROR,
26 gpiosim_value_GPIOSIM_VALUE_INACTIVE as GPIOSIM_VALUE_INACTIVE,
27 };
28
29 /// Value settings.
30 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
31 pub enum Value {
32 /// Active
33 Active,
34 /// Inactive
35 InActive,
36 }
37
38 impl Value {
39 pub(crate) fn new(val: gpiosim_value) -> Result<Self> {
40 Ok(match val {
41 GPIOSIM_VALUE_INACTIVE => Value::InActive,
42 GPIOSIM_VALUE_ACTIVE => Value::Active,
43 GPIOSIM_VALUE_ERROR => {
44 return Err(Error::OperationFailed(
45 OperationType::SimBankGetVal,
46 errno::errno(),
47 ))
48 }
49 _ => return Err(Error::InvalidEnumValue("Value", val)),
50 })
51 }
52 }
53
54 /// Direction settings.
55 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
56 pub enum Direction {
57 /// Direction is input - for reading the value of an externally driven GPIO line.
58 Input,
59 /// Direction is output - for driving the GPIO line, value is high.
60 OutputHigh,
61 /// Direction is output - for driving the GPIO line, value is low.
62 OutputLow,
63 }
64
65 impl Direction {
66 fn val(self) -> gpiosim_direction {
67 match self {
68 Direction::Input => GPIOSIM_DIRECTION_INPUT,
69 Direction::OutputHigh => GPIOSIM_DIRECTION_OUTPUT_HIGH,
70 Direction::OutputLow => GPIOSIM_DIRECTION_OUTPUT_LOW,
71 }
72 }
73 }
74
75 /// Internal pull settings.
76 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
77 pub enum Pull {
78 /// The internal pull-up is enabled.
79 Up,
80 /// The internal pull-down is enabled.
81 Down,
82 }
83
84 impl Pull {
85 fn val(self) -> gpiosim_pull {
86 match self {
87 Pull::Up => GPIOSIM_PULL_UP,
88 Pull::Down => GPIOSIM_PULL_DOWN,
89 }
90 }
91 }