initial

Dependencies:   mbed

Committer:
yihui
Date:
Mon Jan 11 02:32:24 2016 +0000
Revision:
0:638edba3adf6
initial

Who changed what in which revision?

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