The prosthetic control(MIT)

Committer:
ganlikun
Date:
Thu Jun 23 05:23:34 2022 +0000
Revision:
0:20e0c61e0684
01

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ganlikun 0:20e0c61e0684 1 /* mbed Microcontroller Library
ganlikun 0:20e0c61e0684 2 * Copyright (c) 2006-2013 ARM Limited
ganlikun 0:20e0c61e0684 3 *
ganlikun 0:20e0c61e0684 4 * Licensed under the Apache License, Version 2.0 (the "License");
ganlikun 0:20e0c61e0684 5 * you may not use this file except in compliance with the License.
ganlikun 0:20e0c61e0684 6 * You may obtain a copy of the License at
ganlikun 0:20e0c61e0684 7 *
ganlikun 0:20e0c61e0684 8 * http://www.apache.org/licenses/LICENSE-2.0
ganlikun 0:20e0c61e0684 9 *
ganlikun 0:20e0c61e0684 10 * Unless required by applicable law or agreed to in writing, software
ganlikun 0:20e0c61e0684 11 * distributed under the License is distributed on an "AS IS" BASIS,
ganlikun 0:20e0c61e0684 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ganlikun 0:20e0c61e0684 13 * See the License for the specific language governing permissions and
ganlikun 0:20e0c61e0684 14 * limitations under the License.
ganlikun 0:20e0c61e0684 15 */
ganlikun 0:20e0c61e0684 16 #ifndef MBED_PORTINOUT_H
ganlikun 0:20e0c61e0684 17 #define MBED_PORTINOUT_H
ganlikun 0:20e0c61e0684 18
ganlikun 0:20e0c61e0684 19 #include "platform/platform.h"
ganlikun 0:20e0c61e0684 20
ganlikun 0:20e0c61e0684 21 #if defined (DEVICE_PORTINOUT) || defined(DOXYGEN_ONLY)
ganlikun 0:20e0c61e0684 22
ganlikun 0:20e0c61e0684 23 #include "hal/port_api.h"
ganlikun 0:20e0c61e0684 24 #include "platform/mbed_critical.h"
ganlikun 0:20e0c61e0684 25
ganlikun 0:20e0c61e0684 26 namespace mbed {
ganlikun 0:20e0c61e0684 27 /** \addtogroup drivers */
ganlikun 0:20e0c61e0684 28
ganlikun 0:20e0c61e0684 29 /** A multiple pin digital in/out used to set/read multiple bi-directional pins
ganlikun 0:20e0c61e0684 30 *
ganlikun 0:20e0c61e0684 31 * @note Synchronization level: Interrupt safe
ganlikun 0:20e0c61e0684 32 * @ingroup drivers
ganlikun 0:20e0c61e0684 33 */
ganlikun 0:20e0c61e0684 34 class PortInOut {
ganlikun 0:20e0c61e0684 35 public:
ganlikun 0:20e0c61e0684 36
ganlikun 0:20e0c61e0684 37 /** Create an PortInOut, connected to the specified port
ganlikun 0:20e0c61e0684 38 *
ganlikun 0:20e0c61e0684 39 * @param port Port to connect to (Port0-Port5)
ganlikun 0:20e0c61e0684 40 * @param mask A bitmask to identify which bits in the port should be included (0 - ignore)
ganlikun 0:20e0c61e0684 41 */
ganlikun 0:20e0c61e0684 42 PortInOut(PortName port, int mask = 0xFFFFFFFF) {
ganlikun 0:20e0c61e0684 43 core_util_critical_section_enter();
ganlikun 0:20e0c61e0684 44 port_init(&_port, port, mask, PIN_INPUT);
ganlikun 0:20e0c61e0684 45 core_util_critical_section_exit();
ganlikun 0:20e0c61e0684 46 }
ganlikun 0:20e0c61e0684 47
ganlikun 0:20e0c61e0684 48 /** Write the value to the output port
ganlikun 0:20e0c61e0684 49 *
ganlikun 0:20e0c61e0684 50 * @param value An integer specifying a bit to write for every corresponding port pin
ganlikun 0:20e0c61e0684 51 */
ganlikun 0:20e0c61e0684 52 void write(int value) {
ganlikun 0:20e0c61e0684 53 port_write(&_port, value);
ganlikun 0:20e0c61e0684 54 }
ganlikun 0:20e0c61e0684 55
ganlikun 0:20e0c61e0684 56 /** Read the value currently output on the port
ganlikun 0:20e0c61e0684 57 *
ganlikun 0:20e0c61e0684 58 * @returns
ganlikun 0:20e0c61e0684 59 * An integer with each bit corresponding to associated port pin setting
ganlikun 0:20e0c61e0684 60 */
ganlikun 0:20e0c61e0684 61 int read() {
ganlikun 0:20e0c61e0684 62 return port_read(&_port);
ganlikun 0:20e0c61e0684 63 }
ganlikun 0:20e0c61e0684 64
ganlikun 0:20e0c61e0684 65 /** Set as an output
ganlikun 0:20e0c61e0684 66 */
ganlikun 0:20e0c61e0684 67 void output() {
ganlikun 0:20e0c61e0684 68 core_util_critical_section_enter();
ganlikun 0:20e0c61e0684 69 port_dir(&_port, PIN_OUTPUT);
ganlikun 0:20e0c61e0684 70 core_util_critical_section_exit();
ganlikun 0:20e0c61e0684 71 }
ganlikun 0:20e0c61e0684 72
ganlikun 0:20e0c61e0684 73 /** Set as an input
ganlikun 0:20e0c61e0684 74 */
ganlikun 0:20e0c61e0684 75 void input() {
ganlikun 0:20e0c61e0684 76 core_util_critical_section_enter();
ganlikun 0:20e0c61e0684 77 port_dir(&_port, PIN_INPUT);
ganlikun 0:20e0c61e0684 78 core_util_critical_section_exit();
ganlikun 0:20e0c61e0684 79 }
ganlikun 0:20e0c61e0684 80
ganlikun 0:20e0c61e0684 81 /** Set the input pin mode
ganlikun 0:20e0c61e0684 82 *
ganlikun 0:20e0c61e0684 83 * @param mode PullUp, PullDown, PullNone, OpenDrain
ganlikun 0:20e0c61e0684 84 */
ganlikun 0:20e0c61e0684 85 void mode(PinMode mode) {
ganlikun 0:20e0c61e0684 86 core_util_critical_section_enter();
ganlikun 0:20e0c61e0684 87 port_mode(&_port, mode);
ganlikun 0:20e0c61e0684 88 core_util_critical_section_exit();
ganlikun 0:20e0c61e0684 89 }
ganlikun 0:20e0c61e0684 90
ganlikun 0:20e0c61e0684 91 /** A shorthand for write()
ganlikun 0:20e0c61e0684 92 * \sa PortInOut::write()
ganlikun 0:20e0c61e0684 93 */
ganlikun 0:20e0c61e0684 94 PortInOut& operator= (int value) {
ganlikun 0:20e0c61e0684 95 write(value);
ganlikun 0:20e0c61e0684 96 return *this;
ganlikun 0:20e0c61e0684 97 }
ganlikun 0:20e0c61e0684 98
ganlikun 0:20e0c61e0684 99 /** A shorthand for write()
ganlikun 0:20e0c61e0684 100 * \sa PortInOut::write()
ganlikun 0:20e0c61e0684 101 */
ganlikun 0:20e0c61e0684 102 PortInOut& operator= (PortInOut& rhs) {
ganlikun 0:20e0c61e0684 103 write(rhs.read());
ganlikun 0:20e0c61e0684 104 return *this;
ganlikun 0:20e0c61e0684 105 }
ganlikun 0:20e0c61e0684 106
ganlikun 0:20e0c61e0684 107 /** A shorthand for read()
ganlikun 0:20e0c61e0684 108 * \sa PortInOut::read()
ganlikun 0:20e0c61e0684 109 */
ganlikun 0:20e0c61e0684 110 operator int() {
ganlikun 0:20e0c61e0684 111 return read();
ganlikun 0:20e0c61e0684 112 }
ganlikun 0:20e0c61e0684 113
ganlikun 0:20e0c61e0684 114 private:
ganlikun 0:20e0c61e0684 115 port_t _port;
ganlikun 0:20e0c61e0684 116 };
ganlikun 0:20e0c61e0684 117
ganlikun 0:20e0c61e0684 118 } // namespace mbed
ganlikun 0:20e0c61e0684 119
ganlikun 0:20e0c61e0684 120 #endif
ganlikun 0:20e0c61e0684 121
ganlikun 0:20e0c61e0684 122 #endif