test

Dependents:   robotic_fish_6

Committer:
juansal12
Date:
Fri Dec 03 23:00:34 2021 +0000
Revision:
0:c792b17d9f78
uploaded sofi code ;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
juansal12 0:c792b17d9f78 1 /* mbed Microcontroller Library
juansal12 0:c792b17d9f78 2 * Copyright (c) 2006-2013 ARM Limited
juansal12 0:c792b17d9f78 3 *
juansal12 0:c792b17d9f78 4 * Licensed under the Apache License, Version 2.0 (the "License");
juansal12 0:c792b17d9f78 5 * you may not use this file except in compliance with the License.
juansal12 0:c792b17d9f78 6 * You may obtain a copy of the License at
juansal12 0:c792b17d9f78 7 *
juansal12 0:c792b17d9f78 8 * http://www.apache.org/licenses/LICENSE-2.0
juansal12 0:c792b17d9f78 9 *
juansal12 0:c792b17d9f78 10 * Unless required by applicable law or agreed to in writing, software
juansal12 0:c792b17d9f78 11 * distributed under the License is distributed on an "AS IS" BASIS,
juansal12 0:c792b17d9f78 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
juansal12 0:c792b17d9f78 13 * See the License for the specific language governing permissions and
juansal12 0:c792b17d9f78 14 * limitations under the License.
juansal12 0:c792b17d9f78 15 */
juansal12 0:c792b17d9f78 16 #ifndef MBED_DIGITALINOUT_H
juansal12 0:c792b17d9f78 17 #define MBED_DIGITALINOUT_H
juansal12 0:c792b17d9f78 18
juansal12 0:c792b17d9f78 19 #include "platform.h"
juansal12 0:c792b17d9f78 20
juansal12 0:c792b17d9f78 21 #include "gpio_api.h"
juansal12 0:c792b17d9f78 22
juansal12 0:c792b17d9f78 23 namespace mbed {
juansal12 0:c792b17d9f78 24
juansal12 0:c792b17d9f78 25 /** A digital input/output, used for setting or reading a bi-directional pin
juansal12 0:c792b17d9f78 26 */
juansal12 0:c792b17d9f78 27 class DigitalInOut {
juansal12 0:c792b17d9f78 28
juansal12 0:c792b17d9f78 29 public:
juansal12 0:c792b17d9f78 30 /** Create a DigitalInOut connected to the specified pin
juansal12 0:c792b17d9f78 31 *
juansal12 0:c792b17d9f78 32 * @param pin DigitalInOut pin to connect to
juansal12 0:c792b17d9f78 33 */
juansal12 0:c792b17d9f78 34 DigitalInOut(PinName pin) : gpio() {
juansal12 0:c792b17d9f78 35 gpio_init_in(&gpio, pin);
juansal12 0:c792b17d9f78 36 }
juansal12 0:c792b17d9f78 37
juansal12 0:c792b17d9f78 38 /** Create a DigitalInOut connected to the specified pin
juansal12 0:c792b17d9f78 39 *
juansal12 0:c792b17d9f78 40 * @param pin DigitalInOut pin to connect to
juansal12 0:c792b17d9f78 41 * @param direction the initial direction of the pin
juansal12 0:c792b17d9f78 42 * @param mode the initial mode of the pin
juansal12 0:c792b17d9f78 43 * @param value the initial value of the pin if is an output
juansal12 0:c792b17d9f78 44 */
juansal12 0:c792b17d9f78 45 DigitalInOut(PinName pin, PinDirection direction, PinMode mode, int value) : gpio() {
juansal12 0:c792b17d9f78 46 gpio_init_inout(&gpio, pin, direction, mode, value);
juansal12 0:c792b17d9f78 47 }
juansal12 0:c792b17d9f78 48
juansal12 0:c792b17d9f78 49 /** Set the output, specified as 0 or 1 (int)
juansal12 0:c792b17d9f78 50 *
juansal12 0:c792b17d9f78 51 * @param value An integer specifying the pin output value,
juansal12 0:c792b17d9f78 52 * 0 for logical 0, 1 (or any other non-zero value) for logical 1
juansal12 0:c792b17d9f78 53 */
juansal12 0:c792b17d9f78 54 void write(int value) {
juansal12 0:c792b17d9f78 55 gpio_write(&gpio, value);
juansal12 0:c792b17d9f78 56 }
juansal12 0:c792b17d9f78 57
juansal12 0:c792b17d9f78 58 /** Return the output setting, represented as 0 or 1 (int)
juansal12 0:c792b17d9f78 59 *
juansal12 0:c792b17d9f78 60 * @returns
juansal12 0:c792b17d9f78 61 * an integer representing the output setting of the pin if it is an output,
juansal12 0:c792b17d9f78 62 * or read the input if set as an input
juansal12 0:c792b17d9f78 63 */
juansal12 0:c792b17d9f78 64 int read() {
juansal12 0:c792b17d9f78 65 return gpio_read(&gpio);
juansal12 0:c792b17d9f78 66 }
juansal12 0:c792b17d9f78 67
juansal12 0:c792b17d9f78 68 /** Set as an output
juansal12 0:c792b17d9f78 69 */
juansal12 0:c792b17d9f78 70 void output() {
juansal12 0:c792b17d9f78 71 gpio_dir(&gpio, PIN_OUTPUT);
juansal12 0:c792b17d9f78 72 }
juansal12 0:c792b17d9f78 73
juansal12 0:c792b17d9f78 74 /** Set as an input
juansal12 0:c792b17d9f78 75 */
juansal12 0:c792b17d9f78 76 void input() {
juansal12 0:c792b17d9f78 77 gpio_dir(&gpio, PIN_INPUT);
juansal12 0:c792b17d9f78 78 }
juansal12 0:c792b17d9f78 79
juansal12 0:c792b17d9f78 80 /** Set the input pin mode
juansal12 0:c792b17d9f78 81 *
juansal12 0:c792b17d9f78 82 * @param mode PullUp, PullDown, PullNone, OpenDrain
juansal12 0:c792b17d9f78 83 */
juansal12 0:c792b17d9f78 84 void mode(PinMode pull) {
juansal12 0:c792b17d9f78 85 gpio_mode(&gpio, pull);
juansal12 0:c792b17d9f78 86 }
juansal12 0:c792b17d9f78 87
juansal12 0:c792b17d9f78 88 /** Return the output setting, represented as 0 or 1 (int)
juansal12 0:c792b17d9f78 89 *
juansal12 0:c792b17d9f78 90 * @returns
juansal12 0:c792b17d9f78 91 * Non zero value if pin is connected to uc GPIO
juansal12 0:c792b17d9f78 92 * 0 if gpio object was initialized with NC
juansal12 0:c792b17d9f78 93 */
juansal12 0:c792b17d9f78 94 int is_connected() {
juansal12 0:c792b17d9f78 95 return gpio_is_connected(&gpio);
juansal12 0:c792b17d9f78 96 }
juansal12 0:c792b17d9f78 97
juansal12 0:c792b17d9f78 98 #ifdef MBED_OPERATORS
juansal12 0:c792b17d9f78 99 /** A shorthand for write()
juansal12 0:c792b17d9f78 100 */
juansal12 0:c792b17d9f78 101 DigitalInOut& operator= (int value) {
juansal12 0:c792b17d9f78 102 write(value);
juansal12 0:c792b17d9f78 103 return *this;
juansal12 0:c792b17d9f78 104 }
juansal12 0:c792b17d9f78 105
juansal12 0:c792b17d9f78 106 DigitalInOut& operator= (DigitalInOut& rhs) {
juansal12 0:c792b17d9f78 107 write(rhs.read());
juansal12 0:c792b17d9f78 108 return *this;
juansal12 0:c792b17d9f78 109 }
juansal12 0:c792b17d9f78 110
juansal12 0:c792b17d9f78 111 /** A shorthand for read()
juansal12 0:c792b17d9f78 112 */
juansal12 0:c792b17d9f78 113 operator int() {
juansal12 0:c792b17d9f78 114 return read();
juansal12 0:c792b17d9f78 115 }
juansal12 0:c792b17d9f78 116 #endif
juansal12 0:c792b17d9f78 117
juansal12 0:c792b17d9f78 118 protected:
juansal12 0:c792b17d9f78 119 gpio_t gpio;
juansal12 0:c792b17d9f78 120 };
juansal12 0:c792b17d9f78 121
juansal12 0:c792b17d9f78 122 } // namespace mbed
juansal12 0:c792b17d9f78 123
juansal12 0:c792b17d9f78 124 #endif