Theo/Ludo/Joe / ER2_Labyrinthe_V3

Dependencies:   mbed

Committer:
joehatier
Date:
Fri Feb 15 15:25:57 2019 +0000
Revision:
0:1a801a2a7b4b
suivi;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
joehatier 0:1a801a2a7b4b 1 /* mbed Microcontroller Library
joehatier 0:1a801a2a7b4b 2 * Copyright (c) 2006-2013 ARM Limited
joehatier 0:1a801a2a7b4b 3 *
joehatier 0:1a801a2a7b4b 4 * Licensed under the Apache License, Version 2.0 (the "License");
joehatier 0:1a801a2a7b4b 5 * you may not use this file except in compliance with the License.
joehatier 0:1a801a2a7b4b 6 * You may obtain a copy of the License at
joehatier 0:1a801a2a7b4b 7 *
joehatier 0:1a801a2a7b4b 8 * http://www.apache.org/licenses/LICENSE-2.0
joehatier 0:1a801a2a7b4b 9 *
joehatier 0:1a801a2a7b4b 10 * Unless required by applicable law or agreed to in writing, software
joehatier 0:1a801a2a7b4b 11 * distributed under the License is distributed on an "AS IS" BASIS,
joehatier 0:1a801a2a7b4b 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
joehatier 0:1a801a2a7b4b 13 * See the License for the specific language governing permissions and
joehatier 0:1a801a2a7b4b 14 * limitations under the License.
joehatier 0:1a801a2a7b4b 15 */
joehatier 0:1a801a2a7b4b 16 #ifndef MBED_DIGITALINOUT_H
joehatier 0:1a801a2a7b4b 17 #define MBED_DIGITALINOUT_H
joehatier 0:1a801a2a7b4b 18
joehatier 0:1a801a2a7b4b 19 #include "platform/platform.h"
joehatier 0:1a801a2a7b4b 20
joehatier 0:1a801a2a7b4b 21 #include "hal/gpio_api.h"
joehatier 0:1a801a2a7b4b 22 #include "platform/mbed_critical.h"
joehatier 0:1a801a2a7b4b 23
joehatier 0:1a801a2a7b4b 24 namespace mbed {
joehatier 0:1a801a2a7b4b 25 /** \addtogroup drivers */
joehatier 0:1a801a2a7b4b 26
joehatier 0:1a801a2a7b4b 27 /** A digital input/output, used for setting or reading a bi-directional pin
joehatier 0:1a801a2a7b4b 28 *
joehatier 0:1a801a2a7b4b 29 * @note Synchronization level: Interrupt safe
joehatier 0:1a801a2a7b4b 30 * @ingroup drivers
joehatier 0:1a801a2a7b4b 31 */
joehatier 0:1a801a2a7b4b 32 class DigitalInOut {
joehatier 0:1a801a2a7b4b 33
joehatier 0:1a801a2a7b4b 34 public:
joehatier 0:1a801a2a7b4b 35 /** Create a DigitalInOut connected to the specified pin
joehatier 0:1a801a2a7b4b 36 *
joehatier 0:1a801a2a7b4b 37 * @param pin DigitalInOut pin to connect to
joehatier 0:1a801a2a7b4b 38 */
joehatier 0:1a801a2a7b4b 39 DigitalInOut(PinName pin) : gpio()
joehatier 0:1a801a2a7b4b 40 {
joehatier 0:1a801a2a7b4b 41 // No lock needed in the constructor
joehatier 0:1a801a2a7b4b 42 gpio_init_in(&gpio, pin);
joehatier 0:1a801a2a7b4b 43 }
joehatier 0:1a801a2a7b4b 44
joehatier 0:1a801a2a7b4b 45 /** Create a DigitalInOut connected to the specified pin
joehatier 0:1a801a2a7b4b 46 *
joehatier 0:1a801a2a7b4b 47 * @param pin DigitalInOut pin to connect to
joehatier 0:1a801a2a7b4b 48 * @param direction the initial direction of the pin
joehatier 0:1a801a2a7b4b 49 * @param mode the initial mode of the pin
joehatier 0:1a801a2a7b4b 50 * @param value the initial value of the pin if is an output
joehatier 0:1a801a2a7b4b 51 */
joehatier 0:1a801a2a7b4b 52 DigitalInOut(PinName pin, PinDirection direction, PinMode mode, int value) : gpio()
joehatier 0:1a801a2a7b4b 53 {
joehatier 0:1a801a2a7b4b 54 // No lock needed in the constructor
joehatier 0:1a801a2a7b4b 55 gpio_init_inout(&gpio, pin, direction, mode, value);
joehatier 0:1a801a2a7b4b 56 }
joehatier 0:1a801a2a7b4b 57
joehatier 0:1a801a2a7b4b 58 /** Set the output, specified as 0 or 1 (int)
joehatier 0:1a801a2a7b4b 59 *
joehatier 0:1a801a2a7b4b 60 * @param value An integer specifying the pin output value,
joehatier 0:1a801a2a7b4b 61 * 0 for logical 0, 1 (or any other non-zero value) for logical 1
joehatier 0:1a801a2a7b4b 62 */
joehatier 0:1a801a2a7b4b 63 void write(int value)
joehatier 0:1a801a2a7b4b 64 {
joehatier 0:1a801a2a7b4b 65 // Thread safe / atomic HAL call
joehatier 0:1a801a2a7b4b 66 gpio_write(&gpio, value);
joehatier 0:1a801a2a7b4b 67 }
joehatier 0:1a801a2a7b4b 68
joehatier 0:1a801a2a7b4b 69 /** Return the output setting, represented as 0 or 1 (int)
joehatier 0:1a801a2a7b4b 70 *
joehatier 0:1a801a2a7b4b 71 * @returns
joehatier 0:1a801a2a7b4b 72 * an integer representing the output setting of the pin if it is an output,
joehatier 0:1a801a2a7b4b 73 * or read the input if set as an input
joehatier 0:1a801a2a7b4b 74 */
joehatier 0:1a801a2a7b4b 75 int read()
joehatier 0:1a801a2a7b4b 76 {
joehatier 0:1a801a2a7b4b 77 // Thread safe / atomic HAL call
joehatier 0:1a801a2a7b4b 78 return gpio_read(&gpio);
joehatier 0:1a801a2a7b4b 79 }
joehatier 0:1a801a2a7b4b 80
joehatier 0:1a801a2a7b4b 81 /** Set as an output
joehatier 0:1a801a2a7b4b 82 */
joehatier 0:1a801a2a7b4b 83 void output()
joehatier 0:1a801a2a7b4b 84 {
joehatier 0:1a801a2a7b4b 85 core_util_critical_section_enter();
joehatier 0:1a801a2a7b4b 86 gpio_dir(&gpio, PIN_OUTPUT);
joehatier 0:1a801a2a7b4b 87 core_util_critical_section_exit();
joehatier 0:1a801a2a7b4b 88 }
joehatier 0:1a801a2a7b4b 89
joehatier 0:1a801a2a7b4b 90 /** Set as an input
joehatier 0:1a801a2a7b4b 91 */
joehatier 0:1a801a2a7b4b 92 void input()
joehatier 0:1a801a2a7b4b 93 {
joehatier 0:1a801a2a7b4b 94 core_util_critical_section_enter();
joehatier 0:1a801a2a7b4b 95 gpio_dir(&gpio, PIN_INPUT);
joehatier 0:1a801a2a7b4b 96 core_util_critical_section_exit();
joehatier 0:1a801a2a7b4b 97 }
joehatier 0:1a801a2a7b4b 98
joehatier 0:1a801a2a7b4b 99 /** Set the input pin mode
joehatier 0:1a801a2a7b4b 100 *
joehatier 0:1a801a2a7b4b 101 * @param pull PullUp, PullDown, PullNone, OpenDrain
joehatier 0:1a801a2a7b4b 102 */
joehatier 0:1a801a2a7b4b 103 void mode(PinMode pull)
joehatier 0:1a801a2a7b4b 104 {
joehatier 0:1a801a2a7b4b 105 core_util_critical_section_enter();
joehatier 0:1a801a2a7b4b 106 gpio_mode(&gpio, pull);
joehatier 0:1a801a2a7b4b 107 core_util_critical_section_exit();
joehatier 0:1a801a2a7b4b 108 }
joehatier 0:1a801a2a7b4b 109
joehatier 0:1a801a2a7b4b 110 /** Return the output setting, represented as 0 or 1 (int)
joehatier 0:1a801a2a7b4b 111 *
joehatier 0:1a801a2a7b4b 112 * @returns
joehatier 0:1a801a2a7b4b 113 * Non zero value if pin is connected to uc GPIO
joehatier 0:1a801a2a7b4b 114 * 0 if gpio object was initialized with NC
joehatier 0:1a801a2a7b4b 115 */
joehatier 0:1a801a2a7b4b 116 int is_connected()
joehatier 0:1a801a2a7b4b 117 {
joehatier 0:1a801a2a7b4b 118 // Thread safe / atomic HAL call
joehatier 0:1a801a2a7b4b 119 return gpio_is_connected(&gpio);
joehatier 0:1a801a2a7b4b 120 }
joehatier 0:1a801a2a7b4b 121
joehatier 0:1a801a2a7b4b 122 /** A shorthand for write()
joehatier 0:1a801a2a7b4b 123 * \sa DigitalInOut::write()
joehatier 0:1a801a2a7b4b 124 * @code
joehatier 0:1a801a2a7b4b 125 * DigitalInOut inout(PIN);
joehatier 0:1a801a2a7b4b 126 * DigitalIn button(BUTTON1);
joehatier 0:1a801a2a7b4b 127 * inout.output();
joehatier 0:1a801a2a7b4b 128 *
joehatier 0:1a801a2a7b4b 129 * inout = button; // Equivalent to inout.write(button.read())
joehatier 0:1a801a2a7b4b 130 * @endcode
joehatier 0:1a801a2a7b4b 131 */
joehatier 0:1a801a2a7b4b 132 DigitalInOut &operator= (int value)
joehatier 0:1a801a2a7b4b 133 {
joehatier 0:1a801a2a7b4b 134 // Underlying write is thread safe
joehatier 0:1a801a2a7b4b 135 write(value);
joehatier 0:1a801a2a7b4b 136 return *this;
joehatier 0:1a801a2a7b4b 137 }
joehatier 0:1a801a2a7b4b 138
joehatier 0:1a801a2a7b4b 139 /**A shorthand for write() using the assignment operator which copies the
joehatier 0:1a801a2a7b4b 140 * state from the DigitalInOut argument.
joehatier 0:1a801a2a7b4b 141 * \sa DigitalInOut::write()
joehatier 0:1a801a2a7b4b 142 */
joehatier 0:1a801a2a7b4b 143 DigitalInOut &operator= (DigitalInOut &rhs)
joehatier 0:1a801a2a7b4b 144 {
joehatier 0:1a801a2a7b4b 145 core_util_critical_section_enter();
joehatier 0:1a801a2a7b4b 146 write(rhs.read());
joehatier 0:1a801a2a7b4b 147 core_util_critical_section_exit();
joehatier 0:1a801a2a7b4b 148 return *this;
joehatier 0:1a801a2a7b4b 149 }
joehatier 0:1a801a2a7b4b 150
joehatier 0:1a801a2a7b4b 151 /** A shorthand for read()
joehatier 0:1a801a2a7b4b 152 * \sa DigitalInOut::read()
joehatier 0:1a801a2a7b4b 153 * @code
joehatier 0:1a801a2a7b4b 154 * DigitalInOut inout(PIN);
joehatier 0:1a801a2a7b4b 155 * DigitalOut led(LED1);
joehatier 0:1a801a2a7b4b 156 *
joehatier 0:1a801a2a7b4b 157 * inout.input();
joehatier 0:1a801a2a7b4b 158 * led = inout; // Equivalent to led.write(inout.read())
joehatier 0:1a801a2a7b4b 159 * @endcode
joehatier 0:1a801a2a7b4b 160 */
joehatier 0:1a801a2a7b4b 161 operator int()
joehatier 0:1a801a2a7b4b 162 {
joehatier 0:1a801a2a7b4b 163 // Underlying call is thread safe
joehatier 0:1a801a2a7b4b 164 return read();
joehatier 0:1a801a2a7b4b 165 }
joehatier 0:1a801a2a7b4b 166
joehatier 0:1a801a2a7b4b 167 protected:
joehatier 0:1a801a2a7b4b 168 #if !defined(DOXYGEN_ONLY)
joehatier 0:1a801a2a7b4b 169 gpio_t gpio;
joehatier 0:1a801a2a7b4b 170 #endif //!defined(DOXYGEN_ONLY)
joehatier 0:1a801a2a7b4b 171 };
joehatier 0:1a801a2a7b4b 172
joehatier 0:1a801a2a7b4b 173 } // namespace mbed
joehatier 0:1a801a2a7b4b 174
joehatier 0:1a801a2a7b4b 175 #endif