mbed-os

Dependents:   cobaLCDJoyMotor_Thread odometry_omni_3roda_v3 odometry_omni_3roda_v1 odometry_omni_3roda_v2 ... more

Committer:
be_bryan
Date:
Mon Dec 11 17:54:04 2017 +0000
Revision:
0:b74591d5ab33
motor ++

Who changed what in which revision?

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