mbed

Dependents:   DHTSensor_Test K64F_eCompass_OneNET_JW

Committer:
mbotkinl
Date:
Wed Feb 25 20:22:22 2015 +0000
Revision:
0:2cc6bb4d7fea
Working code to read Temperature and Humidity readings

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbotkinl 0:2cc6bb4d7fea 1 /* mbed Microcontroller Library
mbotkinl 0:2cc6bb4d7fea 2 * Copyright (c) 2006-2013 ARM Limited
mbotkinl 0:2cc6bb4d7fea 3 *
mbotkinl 0:2cc6bb4d7fea 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbotkinl 0:2cc6bb4d7fea 5 * you may not use this file except in compliance with the License.
mbotkinl 0:2cc6bb4d7fea 6 * You may obtain a copy of the License at
mbotkinl 0:2cc6bb4d7fea 7 *
mbotkinl 0:2cc6bb4d7fea 8 * http://www.apache.org/licenses/LICENSE-2.0
mbotkinl 0:2cc6bb4d7fea 9 *
mbotkinl 0:2cc6bb4d7fea 10 * Unless required by applicable law or agreed to in writing, software
mbotkinl 0:2cc6bb4d7fea 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbotkinl 0:2cc6bb4d7fea 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbotkinl 0:2cc6bb4d7fea 13 * See the License for the specific language governing permissions and
mbotkinl 0:2cc6bb4d7fea 14 * limitations under the License.
mbotkinl 0:2cc6bb4d7fea 15 */
mbotkinl 0:2cc6bb4d7fea 16 #ifndef MBED_PORTIN_H
mbotkinl 0:2cc6bb4d7fea 17 #define MBED_PORTIN_H
mbotkinl 0:2cc6bb4d7fea 18
mbotkinl 0:2cc6bb4d7fea 19 #include "platform.h"
mbotkinl 0:2cc6bb4d7fea 20
mbotkinl 0:2cc6bb4d7fea 21 #if DEVICE_PORTIN
mbotkinl 0:2cc6bb4d7fea 22
mbotkinl 0:2cc6bb4d7fea 23 #include "port_api.h"
mbotkinl 0:2cc6bb4d7fea 24
mbotkinl 0:2cc6bb4d7fea 25 namespace mbed {
mbotkinl 0:2cc6bb4d7fea 26
mbotkinl 0:2cc6bb4d7fea 27 /** A multiple pin digital input
mbotkinl 0:2cc6bb4d7fea 28 *
mbotkinl 0:2cc6bb4d7fea 29 * Example:
mbotkinl 0:2cc6bb4d7fea 30 * @code
mbotkinl 0:2cc6bb4d7fea 31 * // Switch on an LED if any of mbed pins 21-26 is high
mbotkinl 0:2cc6bb4d7fea 32 *
mbotkinl 0:2cc6bb4d7fea 33 * #include "mbed.h"
mbotkinl 0:2cc6bb4d7fea 34 *
mbotkinl 0:2cc6bb4d7fea 35 * PortIn p(Port2, 0x0000003F); // p21-p26
mbotkinl 0:2cc6bb4d7fea 36 * DigitalOut ind(LED4);
mbotkinl 0:2cc6bb4d7fea 37 *
mbotkinl 0:2cc6bb4d7fea 38 * int main() {
mbotkinl 0:2cc6bb4d7fea 39 * while(1) {
mbotkinl 0:2cc6bb4d7fea 40 * int pins = p.read();
mbotkinl 0:2cc6bb4d7fea 41 * if(pins) {
mbotkinl 0:2cc6bb4d7fea 42 * ind = 1;
mbotkinl 0:2cc6bb4d7fea 43 * } else {
mbotkinl 0:2cc6bb4d7fea 44 * ind = 0;
mbotkinl 0:2cc6bb4d7fea 45 * }
mbotkinl 0:2cc6bb4d7fea 46 * }
mbotkinl 0:2cc6bb4d7fea 47 * }
mbotkinl 0:2cc6bb4d7fea 48 * @endcode
mbotkinl 0:2cc6bb4d7fea 49 */
mbotkinl 0:2cc6bb4d7fea 50 class PortIn {
mbotkinl 0:2cc6bb4d7fea 51 public:
mbotkinl 0:2cc6bb4d7fea 52
mbotkinl 0:2cc6bb4d7fea 53 /** Create an PortIn, connected to the specified port
mbotkinl 0:2cc6bb4d7fea 54 *
mbotkinl 0:2cc6bb4d7fea 55 * @param port Port to connect to (Port0-Port5)
mbotkinl 0:2cc6bb4d7fea 56 * @param mask A bitmask to identify which bits in the port should be included (0 - ignore)
mbotkinl 0:2cc6bb4d7fea 57 */
mbotkinl 0:2cc6bb4d7fea 58 PortIn(PortName port, int mask = 0xFFFFFFFF) {
mbotkinl 0:2cc6bb4d7fea 59 port_init(&_port, port, mask, PIN_INPUT);
mbotkinl 0:2cc6bb4d7fea 60 }
mbotkinl 0:2cc6bb4d7fea 61
mbotkinl 0:2cc6bb4d7fea 62 /** Read the value currently output on the port
mbotkinl 0:2cc6bb4d7fea 63 *
mbotkinl 0:2cc6bb4d7fea 64 * @returns
mbotkinl 0:2cc6bb4d7fea 65 * An integer with each bit corresponding to associated port pin setting
mbotkinl 0:2cc6bb4d7fea 66 */
mbotkinl 0:2cc6bb4d7fea 67 int read() {
mbotkinl 0:2cc6bb4d7fea 68 return port_read(&_port);
mbotkinl 0:2cc6bb4d7fea 69 }
mbotkinl 0:2cc6bb4d7fea 70
mbotkinl 0:2cc6bb4d7fea 71 /** Set the input pin mode
mbotkinl 0:2cc6bb4d7fea 72 *
mbotkinl 0:2cc6bb4d7fea 73 * @param mode PullUp, PullDown, PullNone, OpenDrain
mbotkinl 0:2cc6bb4d7fea 74 */
mbotkinl 0:2cc6bb4d7fea 75 void mode(PinMode mode) {
mbotkinl 0:2cc6bb4d7fea 76 port_mode(&_port, mode);
mbotkinl 0:2cc6bb4d7fea 77 }
mbotkinl 0:2cc6bb4d7fea 78
mbotkinl 0:2cc6bb4d7fea 79 /** A shorthand for read()
mbotkinl 0:2cc6bb4d7fea 80 */
mbotkinl 0:2cc6bb4d7fea 81 operator int() {
mbotkinl 0:2cc6bb4d7fea 82 return read();
mbotkinl 0:2cc6bb4d7fea 83 }
mbotkinl 0:2cc6bb4d7fea 84
mbotkinl 0:2cc6bb4d7fea 85 private:
mbotkinl 0:2cc6bb4d7fea 86 port_t _port;
mbotkinl 0:2cc6bb4d7fea 87 };
mbotkinl 0:2cc6bb4d7fea 88
mbotkinl 0:2cc6bb4d7fea 89 } // namespace mbed
mbotkinl 0:2cc6bb4d7fea 90
mbotkinl 0:2cc6bb4d7fea 91 #endif
mbotkinl 0:2cc6bb4d7fea 92
mbotkinl 0:2cc6bb4d7fea 93 #endif