dhgdh

Dependencies:   MAX44000 PWM_Tone_Library nexpaq_mdk

Fork of LED_Demo by joey shelton

Committer:
cyberjoey
Date:
Sat Oct 22 01:31:58 2016 +0000
Revision:
9:6bb35cef007d
Parent:
1:55a6170b404f
WORKING

Who changed what in which revision?

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