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