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_DIGITALOUT_H
juansal12 0:c792b17d9f78 17 #define MBED_DIGITALOUT_H
juansal12 0:c792b17d9f78 18
juansal12 0:c792b17d9f78 19 #include "platform.h"
juansal12 0:c792b17d9f78 20 #include "gpio_api.h"
juansal12 0:c792b17d9f78 21
juansal12 0:c792b17d9f78 22 namespace mbed {
juansal12 0:c792b17d9f78 23
juansal12 0:c792b17d9f78 24 /** A digital output, used for setting the state of a pin
juansal12 0:c792b17d9f78 25 *
juansal12 0:c792b17d9f78 26 * Example:
juansal12 0:c792b17d9f78 27 * @code
juansal12 0:c792b17d9f78 28 * // Toggle a LED
juansal12 0:c792b17d9f78 29 * #include "mbed.h"
juansal12 0:c792b17d9f78 30 *
juansal12 0:c792b17d9f78 31 * DigitalOut led(LED1);
juansal12 0:c792b17d9f78 32 *
juansal12 0:c792b17d9f78 33 * int main() {
juansal12 0:c792b17d9f78 34 * while(1) {
juansal12 0:c792b17d9f78 35 * led = !led;
juansal12 0:c792b17d9f78 36 * wait(0.2);
juansal12 0:c792b17d9f78 37 * }
juansal12 0:c792b17d9f78 38 * }
juansal12 0:c792b17d9f78 39 * @endcode
juansal12 0:c792b17d9f78 40 */
juansal12 0:c792b17d9f78 41 class DigitalOut {
juansal12 0:c792b17d9f78 42
juansal12 0:c792b17d9f78 43 public:
juansal12 0:c792b17d9f78 44 /** Create a DigitalOut connected to the specified pin
juansal12 0:c792b17d9f78 45 *
juansal12 0:c792b17d9f78 46 * @param pin DigitalOut pin to connect to
juansal12 0:c792b17d9f78 47 */
juansal12 0:c792b17d9f78 48 DigitalOut(PinName pin) : gpio() {
juansal12 0:c792b17d9f78 49 gpio_init_out(&gpio, pin);
juansal12 0:c792b17d9f78 50 }
juansal12 0:c792b17d9f78 51
juansal12 0:c792b17d9f78 52 /** Create a DigitalOut connected to the specified pin
juansal12 0:c792b17d9f78 53 *
juansal12 0:c792b17d9f78 54 * @param pin DigitalOut pin to connect to
juansal12 0:c792b17d9f78 55 * @param value the initial pin value
juansal12 0:c792b17d9f78 56 */
juansal12 0:c792b17d9f78 57 DigitalOut(PinName pin, int value) : gpio() {
juansal12 0:c792b17d9f78 58 gpio_init_out_ex(&gpio, pin, value);
juansal12 0:c792b17d9f78 59 }
juansal12 0:c792b17d9f78 60
juansal12 0:c792b17d9f78 61 /** Set the output, specified as 0 or 1 (int)
juansal12 0:c792b17d9f78 62 *
juansal12 0:c792b17d9f78 63 * @param value An integer specifying the pin output value,
juansal12 0:c792b17d9f78 64 * 0 for logical 0, 1 (or any other non-zero value) for logical 1
juansal12 0:c792b17d9f78 65 */
juansal12 0:c792b17d9f78 66 void write(int value) {
juansal12 0:c792b17d9f78 67 gpio_write(&gpio, value);
juansal12 0:c792b17d9f78 68 }
juansal12 0:c792b17d9f78 69
juansal12 0:c792b17d9f78 70 /** Return the output setting, represented as 0 or 1 (int)
juansal12 0:c792b17d9f78 71 *
juansal12 0:c792b17d9f78 72 * @returns
juansal12 0:c792b17d9f78 73 * an integer representing the output setting of the pin,
juansal12 0:c792b17d9f78 74 * 0 for logical 0, 1 for logical 1
juansal12 0:c792b17d9f78 75 */
juansal12 0:c792b17d9f78 76 int read() {
juansal12 0:c792b17d9f78 77 return gpio_read(&gpio);
juansal12 0:c792b17d9f78 78 }
juansal12 0:c792b17d9f78 79
juansal12 0:c792b17d9f78 80 /** Return the output setting, represented as 0 or 1 (int)
juansal12 0:c792b17d9f78 81 *
juansal12 0:c792b17d9f78 82 * @returns
juansal12 0:c792b17d9f78 83 * Non zero value if pin is connected to uc GPIO
juansal12 0:c792b17d9f78 84 * 0 if gpio object was initialized with NC
juansal12 0:c792b17d9f78 85 */
juansal12 0:c792b17d9f78 86 int is_connected() {
juansal12 0:c792b17d9f78 87 return gpio_is_connected(&gpio);
juansal12 0:c792b17d9f78 88 }
juansal12 0:c792b17d9f78 89
juansal12 0:c792b17d9f78 90 #ifdef MBED_OPERATORS
juansal12 0:c792b17d9f78 91 /** A shorthand for write()
juansal12 0:c792b17d9f78 92 */
juansal12 0:c792b17d9f78 93 DigitalOut& operator= (int value) {
juansal12 0:c792b17d9f78 94 write(value);
juansal12 0:c792b17d9f78 95 return *this;
juansal12 0:c792b17d9f78 96 }
juansal12 0:c792b17d9f78 97
juansal12 0:c792b17d9f78 98 DigitalOut& operator= (DigitalOut& rhs) {
juansal12 0:c792b17d9f78 99 write(rhs.read());
juansal12 0:c792b17d9f78 100 return *this;
juansal12 0:c792b17d9f78 101 }
juansal12 0:c792b17d9f78 102
juansal12 0:c792b17d9f78 103 /** A shorthand for read()
juansal12 0:c792b17d9f78 104 */
juansal12 0:c792b17d9f78 105 operator int() {
juansal12 0:c792b17d9f78 106 return read();
juansal12 0:c792b17d9f78 107 }
juansal12 0:c792b17d9f78 108 #endif
juansal12 0:c792b17d9f78 109
juansal12 0:c792b17d9f78 110 protected:
juansal12 0:c792b17d9f78 111 gpio_t gpio;
juansal12 0:c792b17d9f78 112 };
juansal12 0:c792b17d9f78 113
juansal12 0:c792b17d9f78 114 } // namespace mbed
juansal12 0:c792b17d9f78 115
juansal12 0:c792b17d9f78 116 #endif