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