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