Erste version der Software für der Prototyp

Committer:
borlanic
Date:
Thu Mar 29 07:02:09 2018 +0000
Revision:
0:380207fcb5c1
Encoder, IMU --> OK; Controller --> in bearbeitung

Who changed what in which revision?

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