mbed library for NZ32-SC151

Committer:
modtronix-com
Date:
Fri Aug 19 15:46:42 2016 +1000
Revision:
17:639ed60ce759
Parent:
1:71204b8406f2
Added tag v1.1 for changeset 076cbe3e55be

Who changed what in which revision?

UserRevisionLine numberNew contents of line
modtronix 1:71204b8406f2 1 /* mbed Microcontroller Library
modtronix 1:71204b8406f2 2 * Copyright (c) 2006-2013 ARM Limited
modtronix 1:71204b8406f2 3 *
modtronix 1:71204b8406f2 4 * Licensed under the Apache License, Version 2.0 (the "License");
modtronix 1:71204b8406f2 5 * you may not use this file except in compliance with the License.
modtronix 1:71204b8406f2 6 * You may obtain a copy of the License at
modtronix 1:71204b8406f2 7 *
modtronix 1:71204b8406f2 8 * http://www.apache.org/licenses/LICENSE-2.0
modtronix 1:71204b8406f2 9 *
modtronix 1:71204b8406f2 10 * Unless required by applicable law or agreed to in writing, software
modtronix 1:71204b8406f2 11 * distributed under the License is distributed on an "AS IS" BASIS,
modtronix 1:71204b8406f2 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
modtronix 1:71204b8406f2 13 * See the License for the specific language governing permissions and
modtronix 1:71204b8406f2 14 * limitations under the License.
modtronix 1:71204b8406f2 15 */
modtronix 1:71204b8406f2 16 #ifndef MBED_DIGITALIN_H
modtronix 1:71204b8406f2 17 #define MBED_DIGITALIN_H
modtronix 1:71204b8406f2 18
modtronix 1:71204b8406f2 19 #include "platform.h"
modtronix 1:71204b8406f2 20
modtronix 1:71204b8406f2 21 #include "gpio_api.h"
modtronix 1:71204b8406f2 22
modtronix 1:71204b8406f2 23 namespace mbed {
modtronix 1:71204b8406f2 24
modtronix 1:71204b8406f2 25 /** A digital input, used for reading the state of a pin
modtronix 1:71204b8406f2 26 *
modtronix 1:71204b8406f2 27 * Example:
modtronix 1:71204b8406f2 28 * @code
modtronix 1:71204b8406f2 29 * // Flash an LED while a DigitalIn is true
modtronix 1:71204b8406f2 30 *
modtronix 1:71204b8406f2 31 * #include "mbed.h"
modtronix 1:71204b8406f2 32 *
modtronix 1:71204b8406f2 33 * DigitalIn enable(p5);
modtronix 1:71204b8406f2 34 * DigitalOut led(LED1);
modtronix 1:71204b8406f2 35 *
modtronix 1:71204b8406f2 36 * int main() {
modtronix 1:71204b8406f2 37 * while(1) {
modtronix 1:71204b8406f2 38 * if(enable) {
modtronix 1:71204b8406f2 39 * led = !led;
modtronix 1:71204b8406f2 40 * }
modtronix 1:71204b8406f2 41 * wait(0.25);
modtronix 1:71204b8406f2 42 * }
modtronix 1:71204b8406f2 43 * }
modtronix 1:71204b8406f2 44 * @endcode
modtronix 1:71204b8406f2 45 */
modtronix 1:71204b8406f2 46 class DigitalIn {
modtronix 1:71204b8406f2 47
modtronix 1:71204b8406f2 48 public:
modtronix 1:71204b8406f2 49 /** Create a DigitalIn connected to the specified pin
modtronix 1:71204b8406f2 50 *
modtronix 1:71204b8406f2 51 * @param pin DigitalIn pin to connect to
modtronix 1:71204b8406f2 52 */
modtronix 1:71204b8406f2 53 DigitalIn(PinName pin) : gpio() {
modtronix 1:71204b8406f2 54 gpio_init_in(&gpio, pin);
modtronix 1:71204b8406f2 55 }
modtronix 1:71204b8406f2 56
modtronix 1:71204b8406f2 57 /** Create a DigitalIn connected to the specified pin
modtronix 1:71204b8406f2 58 *
modtronix 1:71204b8406f2 59 * @param pin DigitalIn pin to connect to
modtronix 1:71204b8406f2 60 * @param mode the initial mode of the pin
modtronix 1:71204b8406f2 61 */
modtronix 1:71204b8406f2 62 DigitalIn(PinName pin, PinMode mode) : gpio() {
modtronix 1:71204b8406f2 63 gpio_init_in_ex(&gpio, pin, mode);
modtronix 1:71204b8406f2 64 }
modtronix 1:71204b8406f2 65 /** Read the input, represented as 0 or 1 (int)
modtronix 1:71204b8406f2 66 *
modtronix 1:71204b8406f2 67 * @returns
modtronix 1:71204b8406f2 68 * An integer representing the state of the input pin,
modtronix 1:71204b8406f2 69 * 0 for logical 0, 1 for logical 1
modtronix 1:71204b8406f2 70 */
modtronix 1:71204b8406f2 71 int read() {
modtronix 1:71204b8406f2 72 return gpio_read(&gpio);
modtronix 1:71204b8406f2 73 }
modtronix 1:71204b8406f2 74
modtronix 1:71204b8406f2 75 /** Set the input pin mode
modtronix 1:71204b8406f2 76 *
modtronix 1:71204b8406f2 77 * @param mode PullUp, PullDown, PullNone, OpenDrain
modtronix 1:71204b8406f2 78 */
modtronix 1:71204b8406f2 79 void mode(PinMode pull) {
modtronix 1:71204b8406f2 80 gpio_mode(&gpio, pull);
modtronix 1:71204b8406f2 81 }
modtronix 1:71204b8406f2 82
modtronix 1:71204b8406f2 83 /** Return the output setting, represented as 0 or 1 (int)
modtronix 1:71204b8406f2 84 *
modtronix 1:71204b8406f2 85 * @returns
modtronix 1:71204b8406f2 86 * Non zero value if pin is connected to uc GPIO
modtronix 1:71204b8406f2 87 * 0 if gpio object was initialized with NC
modtronix 1:71204b8406f2 88 */
modtronix 1:71204b8406f2 89 int is_connected() {
modtronix 1:71204b8406f2 90 return gpio_is_connected(&gpio);
modtronix 1:71204b8406f2 91 }
modtronix 1:71204b8406f2 92
modtronix 1:71204b8406f2 93 #ifdef MBED_OPERATORS
modtronix 1:71204b8406f2 94 /** An operator shorthand for read()
modtronix 1:71204b8406f2 95 */
modtronix 1:71204b8406f2 96 operator int() {
modtronix 1:71204b8406f2 97 return read();
modtronix 1:71204b8406f2 98 }
modtronix 1:71204b8406f2 99 #endif
modtronix 1:71204b8406f2 100
modtronix 1:71204b8406f2 101 protected:
modtronix 1:71204b8406f2 102 gpio_t gpio;
modtronix 1:71204b8406f2 103 };
modtronix 1:71204b8406f2 104
modtronix 1:71204b8406f2 105 } // namespace mbed
modtronix 1:71204b8406f2 106
modtronix 1:71204b8406f2 107 #endif