Home Alert System
Dependencies: PWM_Tone_Library DHT
mbed/DigitalOut.h@5:569a4894abc1, 2019-03-08 (annotated)
- Committer:
- aziz111
- Date:
- Fri Mar 08 17:15:02 2019 +0000
- Revision:
- 5:569a4894abc1
- Parent:
- 3:78f223d34f36
Final
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ethaderu | 3:78f223d34f36 | 1 | /* mbed Microcontroller Library |
ethaderu | 3:78f223d34f36 | 2 | * Copyright (c) 2006-2013 ARM Limited |
ethaderu | 3:78f223d34f36 | 3 | * |
ethaderu | 3:78f223d34f36 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
ethaderu | 3:78f223d34f36 | 5 | * you may not use this file except in compliance with the License. |
ethaderu | 3:78f223d34f36 | 6 | * You may obtain a copy of the License at |
ethaderu | 3:78f223d34f36 | 7 | * |
ethaderu | 3:78f223d34f36 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
ethaderu | 3:78f223d34f36 | 9 | * |
ethaderu | 3:78f223d34f36 | 10 | * Unless required by applicable law or agreed to in writing, software |
ethaderu | 3:78f223d34f36 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
ethaderu | 3:78f223d34f36 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
ethaderu | 3:78f223d34f36 | 13 | * See the License for the specific language governing permissions and |
ethaderu | 3:78f223d34f36 | 14 | * limitations under the License. |
ethaderu | 3:78f223d34f36 | 15 | */ |
ethaderu | 3:78f223d34f36 | 16 | #ifndef MBED_DIGITALOUT_H |
ethaderu | 3:78f223d34f36 | 17 | #define MBED_DIGITALOUT_H |
ethaderu | 3:78f223d34f36 | 18 | |
ethaderu | 3:78f223d34f36 | 19 | #include "platform.h" |
ethaderu | 3:78f223d34f36 | 20 | #include "gpio_api.h" |
ethaderu | 3:78f223d34f36 | 21 | |
ethaderu | 3:78f223d34f36 | 22 | namespace mbed { |
ethaderu | 3:78f223d34f36 | 23 | |
ethaderu | 3:78f223d34f36 | 24 | /** A digital output, used for setting the state of a pin |
ethaderu | 3:78f223d34f36 | 25 | * |
ethaderu | 3:78f223d34f36 | 26 | * Example: |
ethaderu | 3:78f223d34f36 | 27 | * @code |
ethaderu | 3:78f223d34f36 | 28 | * // Toggle a LED |
ethaderu | 3:78f223d34f36 | 29 | * #include "mbed.h" |
ethaderu | 3:78f223d34f36 | 30 | * |
ethaderu | 3:78f223d34f36 | 31 | * DigitalOut led(LED1); |
ethaderu | 3:78f223d34f36 | 32 | * |
ethaderu | 3:78f223d34f36 | 33 | * int main() { |
ethaderu | 3:78f223d34f36 | 34 | * while(1) { |
ethaderu | 3:78f223d34f36 | 35 | * led = !led; |
ethaderu | 3:78f223d34f36 | 36 | * wait(0.2); |
ethaderu | 3:78f223d34f36 | 37 | * } |
ethaderu | 3:78f223d34f36 | 38 | * } |
ethaderu | 3:78f223d34f36 | 39 | * @endcode |
ethaderu | 3:78f223d34f36 | 40 | */ |
ethaderu | 3:78f223d34f36 | 41 | class DigitalOut { |
ethaderu | 3:78f223d34f36 | 42 | |
ethaderu | 3:78f223d34f36 | 43 | public: |
ethaderu | 3:78f223d34f36 | 44 | /** Create a DigitalOut connected to the specified pin |
ethaderu | 3:78f223d34f36 | 45 | * |
ethaderu | 3:78f223d34f36 | 46 | * @param pin DigitalOut pin to connect to |
ethaderu | 3:78f223d34f36 | 47 | */ |
ethaderu | 3:78f223d34f36 | 48 | DigitalOut(PinName pin) : gpio() { |
ethaderu | 3:78f223d34f36 | 49 | gpio_init_out(&gpio, pin); |
ethaderu | 3:78f223d34f36 | 50 | } |
ethaderu | 3:78f223d34f36 | 51 | |
ethaderu | 3:78f223d34f36 | 52 | /** Create a DigitalOut connected to the specified pin |
ethaderu | 3:78f223d34f36 | 53 | * |
ethaderu | 3:78f223d34f36 | 54 | * @param pin DigitalOut pin to connect to |
ethaderu | 3:78f223d34f36 | 55 | * @param value the initial pin value |
ethaderu | 3:78f223d34f36 | 56 | */ |
ethaderu | 3:78f223d34f36 | 57 | DigitalOut(PinName pin, int value) : gpio() { |
ethaderu | 3:78f223d34f36 | 58 | gpio_init_out_ex(&gpio, pin, value); |
ethaderu | 3:78f223d34f36 | 59 | } |
ethaderu | 3:78f223d34f36 | 60 | |
ethaderu | 3:78f223d34f36 | 61 | /** Set the output, specified as 0 or 1 (int) |
ethaderu | 3:78f223d34f36 | 62 | * |
ethaderu | 3:78f223d34f36 | 63 | * @param value An integer specifying the pin output value, |
ethaderu | 3:78f223d34f36 | 64 | * 0 for logical 0, 1 (or any other non-zero value) for logical 1 |
ethaderu | 3:78f223d34f36 | 65 | */ |
ethaderu | 3:78f223d34f36 | 66 | void write(int value) { |
ethaderu | 3:78f223d34f36 | 67 | gpio_write(&gpio, value); |
ethaderu | 3:78f223d34f36 | 68 | } |
ethaderu | 3:78f223d34f36 | 69 | |
ethaderu | 3:78f223d34f36 | 70 | /** Return the output setting, represented as 0 or 1 (int) |
ethaderu | 3:78f223d34f36 | 71 | * |
ethaderu | 3:78f223d34f36 | 72 | * @returns |
ethaderu | 3:78f223d34f36 | 73 | * an integer representing the output setting of the pin, |
ethaderu | 3:78f223d34f36 | 74 | * 0 for logical 0, 1 for logical 1 |
ethaderu | 3:78f223d34f36 | 75 | */ |
ethaderu | 3:78f223d34f36 | 76 | int read() { |
ethaderu | 3:78f223d34f36 | 77 | return gpio_read(&gpio); |
ethaderu | 3:78f223d34f36 | 78 | } |
ethaderu | 3:78f223d34f36 | 79 | |
ethaderu | 3:78f223d34f36 | 80 | /** Return the output setting, represented as 0 or 1 (int) |
ethaderu | 3:78f223d34f36 | 81 | * |
ethaderu | 3:78f223d34f36 | 82 | * @returns |
ethaderu | 3:78f223d34f36 | 83 | * Non zero value if pin is connected to uc GPIO |
ethaderu | 3:78f223d34f36 | 84 | * 0 if gpio object was initialized with NC |
ethaderu | 3:78f223d34f36 | 85 | */ |
ethaderu | 3:78f223d34f36 | 86 | int is_connected() { |
ethaderu | 3:78f223d34f36 | 87 | return gpio_is_connected(&gpio); |
ethaderu | 3:78f223d34f36 | 88 | } |
ethaderu | 3:78f223d34f36 | 89 | |
ethaderu | 3:78f223d34f36 | 90 | #ifdef MBED_OPERATORS |
ethaderu | 3:78f223d34f36 | 91 | /** A shorthand for write() |
ethaderu | 3:78f223d34f36 | 92 | */ |
ethaderu | 3:78f223d34f36 | 93 | DigitalOut& operator= (int value) { |
ethaderu | 3:78f223d34f36 | 94 | write(value); |
ethaderu | 3:78f223d34f36 | 95 | return *this; |
ethaderu | 3:78f223d34f36 | 96 | } |
ethaderu | 3:78f223d34f36 | 97 | |
ethaderu | 3:78f223d34f36 | 98 | DigitalOut& operator= (DigitalOut& rhs) { |
ethaderu | 3:78f223d34f36 | 99 | write(rhs.read()); |
ethaderu | 3:78f223d34f36 | 100 | return *this; |
ethaderu | 3:78f223d34f36 | 101 | } |
ethaderu | 3:78f223d34f36 | 102 | |
ethaderu | 3:78f223d34f36 | 103 | /** A shorthand for read() |
ethaderu | 3:78f223d34f36 | 104 | */ |
ethaderu | 3:78f223d34f36 | 105 | operator int() { |
ethaderu | 3:78f223d34f36 | 106 | return read(); |
ethaderu | 3:78f223d34f36 | 107 | } |
ethaderu | 3:78f223d34f36 | 108 | #endif |
ethaderu | 3:78f223d34f36 | 109 | |
ethaderu | 3:78f223d34f36 | 110 | protected: |
ethaderu | 3:78f223d34f36 | 111 | gpio_t gpio; |
ethaderu | 3:78f223d34f36 | 112 | }; |
ethaderu | 3:78f223d34f36 | 113 | |
ethaderu | 3:78f223d34f36 | 114 | } // namespace mbed |
ethaderu | 3:78f223d34f36 | 115 | |
ethaderu | 3:78f223d34f36 | 116 | #endif |