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