Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: STM32F031_blink_LED_2
api/DigitalIn.h@0:38ccae254a29, 2014-10-18 (annotated)
- Committer:
- mega64
- Date:
- Sat Oct 18 02:40:17 2014 +0000
- Revision:
- 0:38ccae254a29
only for STM32F030F4
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mega64 | 0:38ccae254a29 | 1 | /* mbed Microcontroller Library |
mega64 | 0:38ccae254a29 | 2 | * Copyright (c) 2006-2013 ARM Limited |
mega64 | 0:38ccae254a29 | 3 | * |
mega64 | 0:38ccae254a29 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mega64 | 0:38ccae254a29 | 5 | * you may not use this file except in compliance with the License. |
mega64 | 0:38ccae254a29 | 6 | * You may obtain a copy of the License at |
mega64 | 0:38ccae254a29 | 7 | * |
mega64 | 0:38ccae254a29 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mega64 | 0:38ccae254a29 | 9 | * |
mega64 | 0:38ccae254a29 | 10 | * Unless required by applicable law or agreed to in writing, software |
mega64 | 0:38ccae254a29 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mega64 | 0:38ccae254a29 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mega64 | 0:38ccae254a29 | 13 | * See the License for the specific language governing permissions and |
mega64 | 0:38ccae254a29 | 14 | * limitations under the License. |
mega64 | 0:38ccae254a29 | 15 | */ |
mega64 | 0:38ccae254a29 | 16 | #ifndef MBED_DIGITALIN_H |
mega64 | 0:38ccae254a29 | 17 | #define MBED_DIGITALIN_H |
mega64 | 0:38ccae254a29 | 18 | |
mega64 | 0:38ccae254a29 | 19 | #include "platform.h" |
mega64 | 0:38ccae254a29 | 20 | |
mega64 | 0:38ccae254a29 | 21 | #include "gpio_api.h" |
mega64 | 0:38ccae254a29 | 22 | |
mega64 | 0:38ccae254a29 | 23 | namespace mbed { |
mega64 | 0:38ccae254a29 | 24 | |
mega64 | 0:38ccae254a29 | 25 | /** A digital input, used for reading the state of a pin |
mega64 | 0:38ccae254a29 | 26 | * |
mega64 | 0:38ccae254a29 | 27 | * Example: |
mega64 | 0:38ccae254a29 | 28 | * @code |
mega64 | 0:38ccae254a29 | 29 | * // Flash an LED while a DigitalIn is true |
mega64 | 0:38ccae254a29 | 30 | * |
mega64 | 0:38ccae254a29 | 31 | * #include "mbed.h" |
mega64 | 0:38ccae254a29 | 32 | * |
mega64 | 0:38ccae254a29 | 33 | * DigitalIn enable(p5); |
mega64 | 0:38ccae254a29 | 34 | * DigitalOut led(LED1); |
mega64 | 0:38ccae254a29 | 35 | * |
mega64 | 0:38ccae254a29 | 36 | * int main() { |
mega64 | 0:38ccae254a29 | 37 | * while(1) { |
mega64 | 0:38ccae254a29 | 38 | * if(enable) { |
mega64 | 0:38ccae254a29 | 39 | * led = !led; |
mega64 | 0:38ccae254a29 | 40 | * } |
mega64 | 0:38ccae254a29 | 41 | * wait(0.25); |
mega64 | 0:38ccae254a29 | 42 | * } |
mega64 | 0:38ccae254a29 | 43 | * } |
mega64 | 0:38ccae254a29 | 44 | * @endcode |
mega64 | 0:38ccae254a29 | 45 | */ |
mega64 | 0:38ccae254a29 | 46 | class DigitalIn { |
mega64 | 0:38ccae254a29 | 47 | |
mega64 | 0:38ccae254a29 | 48 | public: |
mega64 | 0:38ccae254a29 | 49 | /** Create a DigitalIn connected to the specified pin |
mega64 | 0:38ccae254a29 | 50 | * |
mega64 | 0:38ccae254a29 | 51 | * @param pin DigitalIn pin to connect to |
mega64 | 0:38ccae254a29 | 52 | */ |
mega64 | 0:38ccae254a29 | 53 | DigitalIn(PinName pin) : gpio() { |
mega64 | 0:38ccae254a29 | 54 | gpio_init_in(&gpio, pin); |
mega64 | 0:38ccae254a29 | 55 | } |
mega64 | 0:38ccae254a29 | 56 | |
mega64 | 0:38ccae254a29 | 57 | /** Create a DigitalIn connected to the specified pin |
mega64 | 0:38ccae254a29 | 58 | * |
mega64 | 0:38ccae254a29 | 59 | * @param pin DigitalIn pin to connect to |
mega64 | 0:38ccae254a29 | 60 | * @param mode the initial mode of the pin |
mega64 | 0:38ccae254a29 | 61 | */ |
mega64 | 0:38ccae254a29 | 62 | DigitalIn(PinName pin, PinMode mode) : gpio() { |
mega64 | 0:38ccae254a29 | 63 | gpio_init_in_ex(&gpio, pin, mode); |
mega64 | 0:38ccae254a29 | 64 | } |
mega64 | 0:38ccae254a29 | 65 | /** Read the input, represented as 0 or 1 (int) |
mega64 | 0:38ccae254a29 | 66 | * |
mega64 | 0:38ccae254a29 | 67 | * @returns |
mega64 | 0:38ccae254a29 | 68 | * An integer representing the state of the input pin, |
mega64 | 0:38ccae254a29 | 69 | * 0 for logical 0, 1 for logical 1 |
mega64 | 0:38ccae254a29 | 70 | */ |
mega64 | 0:38ccae254a29 | 71 | int read() { |
mega64 | 0:38ccae254a29 | 72 | return gpio_read(&gpio); |
mega64 | 0:38ccae254a29 | 73 | } |
mega64 | 0:38ccae254a29 | 74 | |
mega64 | 0:38ccae254a29 | 75 | /** Set the input pin mode |
mega64 | 0:38ccae254a29 | 76 | * |
mega64 | 0:38ccae254a29 | 77 | * @param mode PullUp, PullDown, PullNone, OpenDrain |
mega64 | 0:38ccae254a29 | 78 | */ |
mega64 | 0:38ccae254a29 | 79 | void mode(PinMode pull) { |
mega64 | 0:38ccae254a29 | 80 | gpio_mode(&gpio, pull); |
mega64 | 0:38ccae254a29 | 81 | } |
mega64 | 0:38ccae254a29 | 82 | |
mega64 | 0:38ccae254a29 | 83 | #ifdef MBED_OPERATORS |
mega64 | 0:38ccae254a29 | 84 | /** An operator shorthand for read() |
mega64 | 0:38ccae254a29 | 85 | */ |
mega64 | 0:38ccae254a29 | 86 | operator int() { |
mega64 | 0:38ccae254a29 | 87 | return read(); |
mega64 | 0:38ccae254a29 | 88 | } |
mega64 | 0:38ccae254a29 | 89 | #endif |
mega64 | 0:38ccae254a29 | 90 | |
mega64 | 0:38ccae254a29 | 91 | protected: |
mega64 | 0:38ccae254a29 | 92 | gpio_t gpio; |
mega64 | 0:38ccae254a29 | 93 | }; |
mega64 | 0:38ccae254a29 | 94 | |
mega64 | 0:38ccae254a29 | 95 | } // namespace mbed |
mega64 | 0:38ccae254a29 | 96 | |
mega64 | 0:38ccae254a29 | 97 | #endif |