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