mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BusIn.cpp Source File

BusIn.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 #include "drivers/BusIn.h"
00018 #include "platform/mbed_assert.h"
00019 
00020 namespace mbed {
00021 
00022 BusIn::BusIn(PinName p0, PinName p1, PinName p2, PinName p3, PinName p4, PinName p5, PinName p6, PinName p7, PinName p8, PinName p9, PinName p10, PinName p11, PinName p12, PinName p13, PinName p14, PinName p15)
00023 {
00024     PinName pins[16] = {p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15};
00025 
00026     // No lock needed in the constructor
00027     _nc_mask = 0;
00028     for (int i = 0; i < 16; i++) {
00029         _pin[i] = (pins[i] != NC) ? new DigitalIn(pins[i]) : 0;
00030         if (pins[i] != NC) {
00031             _nc_mask |= (1 << i);
00032         }
00033     }
00034 }
00035 
00036 BusIn::BusIn(PinName pins[16])
00037 {
00038     // No lock needed in the constructor
00039     _nc_mask = 0;
00040     for (int i = 0; i < 16; i++) {
00041         _pin[i] = (pins[i] != NC) ? new DigitalIn(pins[i]) : 0;
00042         if (pins[i] != NC) {
00043             _nc_mask |= (1 << i);
00044         }
00045     }
00046 }
00047 
00048 BusIn::~BusIn()
00049 {
00050     // No lock needed in the destructor
00051     for (int i = 0; i < 16; i++) {
00052         if (_pin[i] != 0) {
00053             delete _pin[i];
00054         }
00055     }
00056 }
00057 
00058 int BusIn::read()
00059 {
00060     int v = 0;
00061     lock();
00062     for (int i = 0; i < 16; i++) {
00063         if (_pin[i] != 0) {
00064             v |= _pin[i]->read() << i;
00065         }
00066     }
00067     unlock();
00068     return v;
00069 }
00070 
00071 void BusIn::mode(PinMode pull)
00072 {
00073     lock();
00074     for (int i = 0; i < 16; i++) {
00075         if (_pin[i] != 0) {
00076             _pin[i]->mode(pull);
00077         }
00078     }
00079     unlock();
00080 }
00081 
00082 void BusIn::lock()
00083 {
00084     _mutex.lock();
00085 }
00086 
00087 void BusIn::unlock()
00088 {
00089     _mutex.unlock();
00090 }
00091 
00092 BusIn::operator int()
00093 {
00094     // Underlying read is thread safe
00095     return read();
00096 }
00097 
00098 DigitalIn &BusIn::operator[](int index)
00099 {
00100     // No lock needed since _pin is not modified outside the constructor
00101     MBED_ASSERT(index >= 0 && index < 16);
00102     MBED_ASSERT(_pin[index]);
00103     return *_pin[index];
00104 }
00105 
00106 } // namespace mbed