Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bryantaylor 0:eafc3fd41f75 1 /* mbed Microcontroller Library
bryantaylor 0:eafc3fd41f75 2 * Copyright (c) 2006-2013 ARM Limited
bryantaylor 0:eafc3fd41f75 3 *
bryantaylor 0:eafc3fd41f75 4 * Licensed under the Apache License, Version 2.0 (the "License");
bryantaylor 0:eafc3fd41f75 5 * you may not use this file except in compliance with the License.
bryantaylor 0:eafc3fd41f75 6 * You may obtain a copy of the License at
bryantaylor 0:eafc3fd41f75 7 *
bryantaylor 0:eafc3fd41f75 8 * http://www.apache.org/licenses/LICENSE-2.0
bryantaylor 0:eafc3fd41f75 9 *
bryantaylor 0:eafc3fd41f75 10 * Unless required by applicable law or agreed to in writing, software
bryantaylor 0:eafc3fd41f75 11 * distributed under the License is distributed on an "AS IS" BASIS,
bryantaylor 0:eafc3fd41f75 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
bryantaylor 0:eafc3fd41f75 13 * See the License for the specific language governing permissions and
bryantaylor 0:eafc3fd41f75 14 * limitations under the License.
bryantaylor 0:eafc3fd41f75 15 */
bryantaylor 0:eafc3fd41f75 16 #include "BusIn.h"
bryantaylor 0:eafc3fd41f75 17 #include "mbed_assert.h"
bryantaylor 0:eafc3fd41f75 18
bryantaylor 0:eafc3fd41f75 19 namespace mbed {
bryantaylor 0:eafc3fd41f75 20
bryantaylor 0:eafc3fd41f75 21 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) {
bryantaylor 0:eafc3fd41f75 22 PinName pins[16] = {p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15};
bryantaylor 0:eafc3fd41f75 23
bryantaylor 0:eafc3fd41f75 24 // No lock needed in the constructor
bryantaylor 0:eafc3fd41f75 25 _nc_mask = 0;
bryantaylor 0:eafc3fd41f75 26 for (int i=0; i<16; i++) {
bryantaylor 0:eafc3fd41f75 27 _pin[i] = (pins[i] != NC) ? new DigitalIn(pins[i]) : 0;
bryantaylor 0:eafc3fd41f75 28 if (pins[i] != NC) {
bryantaylor 0:eafc3fd41f75 29 _nc_mask |= (1 << i);
bryantaylor 0:eafc3fd41f75 30 }
bryantaylor 0:eafc3fd41f75 31 }
bryantaylor 0:eafc3fd41f75 32 }
bryantaylor 0:eafc3fd41f75 33
bryantaylor 0:eafc3fd41f75 34 BusIn::BusIn(PinName pins[16]) {
bryantaylor 0:eafc3fd41f75 35 // No lock needed in the constructor
bryantaylor 0:eafc3fd41f75 36 _nc_mask = 0;
bryantaylor 0:eafc3fd41f75 37 for (int i=0; i<16; i++) {
bryantaylor 0:eafc3fd41f75 38 _pin[i] = (pins[i] != NC) ? new DigitalIn(pins[i]) : 0;
bryantaylor 0:eafc3fd41f75 39 if (pins[i] != NC) {
bryantaylor 0:eafc3fd41f75 40 _nc_mask |= (1 << i);
bryantaylor 0:eafc3fd41f75 41 }
bryantaylor 0:eafc3fd41f75 42 }
bryantaylor 0:eafc3fd41f75 43 }
bryantaylor 0:eafc3fd41f75 44
bryantaylor 0:eafc3fd41f75 45 BusIn::~BusIn() {
bryantaylor 0:eafc3fd41f75 46 // No lock needed in the destructor
bryantaylor 0:eafc3fd41f75 47 for (int i=0; i<16; i++) {
bryantaylor 0:eafc3fd41f75 48 if (_pin[i] != 0) {
bryantaylor 0:eafc3fd41f75 49 delete _pin[i];
bryantaylor 0:eafc3fd41f75 50 }
bryantaylor 0:eafc3fd41f75 51 }
bryantaylor 0:eafc3fd41f75 52 }
bryantaylor 0:eafc3fd41f75 53
bryantaylor 0:eafc3fd41f75 54 int BusIn::read() {
bryantaylor 0:eafc3fd41f75 55 int v = 0;
bryantaylor 0:eafc3fd41f75 56 lock();
bryantaylor 0:eafc3fd41f75 57 for (int i=0; i<16; i++) {
bryantaylor 0:eafc3fd41f75 58 if (_pin[i] != 0) {
bryantaylor 0:eafc3fd41f75 59 v |= _pin[i]->read() << i;
bryantaylor 0:eafc3fd41f75 60 }
bryantaylor 0:eafc3fd41f75 61 }
bryantaylor 0:eafc3fd41f75 62 unlock();
bryantaylor 0:eafc3fd41f75 63 return v;
bryantaylor 0:eafc3fd41f75 64 }
bryantaylor 0:eafc3fd41f75 65
bryantaylor 0:eafc3fd41f75 66 void BusIn::mode(PinMode pull) {
bryantaylor 0:eafc3fd41f75 67 lock();
bryantaylor 0:eafc3fd41f75 68 for (int i=0; i<16; i++) {
bryantaylor 0:eafc3fd41f75 69 if (_pin[i] != 0) {
bryantaylor 0:eafc3fd41f75 70 _pin[i]->mode(pull);
bryantaylor 0:eafc3fd41f75 71 }
bryantaylor 0:eafc3fd41f75 72 }
bryantaylor 0:eafc3fd41f75 73 unlock();
bryantaylor 0:eafc3fd41f75 74 }
bryantaylor 0:eafc3fd41f75 75
bryantaylor 0:eafc3fd41f75 76 void BusIn::lock() {
bryantaylor 0:eafc3fd41f75 77 _mutex.lock();
bryantaylor 0:eafc3fd41f75 78 }
bryantaylor 0:eafc3fd41f75 79
bryantaylor 0:eafc3fd41f75 80 void BusIn::unlock() {
bryantaylor 0:eafc3fd41f75 81 _mutex.unlock();
bryantaylor 0:eafc3fd41f75 82 }
bryantaylor 0:eafc3fd41f75 83
bryantaylor 0:eafc3fd41f75 84 BusIn::operator int() {
bryantaylor 0:eafc3fd41f75 85 // Underlying read is thread safe
bryantaylor 0:eafc3fd41f75 86 return read();
bryantaylor 0:eafc3fd41f75 87 }
bryantaylor 0:eafc3fd41f75 88
bryantaylor 0:eafc3fd41f75 89 DigitalIn& BusIn::operator[] (int index) {
bryantaylor 0:eafc3fd41f75 90 // No lock needed since _pin is not modified outside the constructor
bryantaylor 0:eafc3fd41f75 91 MBED_ASSERT(index >= 0 && index <= 16);
bryantaylor 0:eafc3fd41f75 92 MBED_ASSERT(_pin[index]);
bryantaylor 0:eafc3fd41f75 93 return *_pin[index];
bryantaylor 0:eafc3fd41f75 94 }
bryantaylor 0:eafc3fd41f75 95
bryantaylor 0:eafc3fd41f75 96 } // namespace mbed