Simple wrapper object for LTC2606 1CH 16Bit DAC. Instantiate DacLtc2606 object instance passing reference to mbed.I2C object instance. Call write() method on DacLtc2606 instance passing 16Bit integer (0 [Off] to 32767/-32768 [Half-On] to -1 [Full-On]) to set DAC output.
DacLtc2606.cpp@2:d101ca4ae85e, 2012-09-14 (annotated)
- Committer:
- rkimble
- Date:
- Fri Sep 14 17:23:05 2012 +0000
- Revision:
- 2:d101ca4ae85e
- Parent:
- 1:5924632f3344
Improved numeric type usage/refined code.
; Added local storage and retrieval of DAC output value.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rkimble | 1:5924632f3344 | 1 | /* Copyright (c) 2012 Reed Kimble, MIT License |
rkimble | 1:5924632f3344 | 2 | * |
rkimble | 1:5924632f3344 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
rkimble | 1:5924632f3344 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
rkimble | 1:5924632f3344 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
rkimble | 1:5924632f3344 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
rkimble | 1:5924632f3344 | 7 | * furnished to do so, subject to the following conditions: |
rkimble | 1:5924632f3344 | 8 | * |
rkimble | 1:5924632f3344 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
rkimble | 1:5924632f3344 | 10 | * substantial portions of the Software. |
rkimble | 1:5924632f3344 | 11 | * |
rkimble | 1:5924632f3344 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
rkimble | 1:5924632f3344 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
rkimble | 1:5924632f3344 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
rkimble | 1:5924632f3344 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
rkimble | 1:5924632f3344 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
rkimble | 1:5924632f3344 | 17 | */ |
rkimble | 1:5924632f3344 | 18 | |
rkimble | 1:5924632f3344 | 19 | #include "DacLtc2606.h" |
rkimble | 1:5924632f3344 | 20 | #include "mbed.h" |
rkimble | 1:5924632f3344 | 21 | |
rkimble | 1:5924632f3344 | 22 | DacLtc2606::DacLtc2606(I2C i2c) : _i2c(i2c) |
rkimble | 1:5924632f3344 | 23 | { |
rkimble | 2:d101ca4ae85e | 24 | _currentValue = 0; |
rkimble | 1:5924632f3344 | 25 | } |
rkimble | 1:5924632f3344 | 26 | |
rkimble | 2:d101ca4ae85e | 27 | unsigned short DacLtc2606::getCurrentValue() |
rkimble | 2:d101ca4ae85e | 28 | { |
rkimble | 2:d101ca4ae85e | 29 | return _currentValue; |
rkimble | 2:d101ca4ae85e | 30 | } |
rkimble | 2:d101ca4ae85e | 31 | |
rkimble | 2:d101ca4ae85e | 32 | bool DacLtc2606::write(unsigned short value) |
rkimble | 1:5924632f3344 | 33 | { |
rkimble | 1:5924632f3344 | 34 | int opR; |
rkimble | 1:5924632f3344 | 35 | char args[3]; |
rkimble | 1:5924632f3344 | 36 | args[0] = _command; |
rkimble | 1:5924632f3344 | 37 | args[1] = (value >> 8) & 0xFF; |
rkimble | 1:5924632f3344 | 38 | args[2] = value & 0xFF; |
rkimble | 1:5924632f3344 | 39 | opR = _i2c.write(_slave, args, 3, false); |
rkimble | 2:d101ca4ae85e | 40 | if(opR == 0) {_currentValue = value;} |
rkimble | 1:5924632f3344 | 41 | return (opR == 0) ? true : false; |
rkimble | 1:5924632f3344 | 42 | } |
rkimble | 1:5924632f3344 | 43 | |
rkimble | 1:5924632f3344 | 44 | bool DacLtc2606::write(double percent) |
rkimble | 1:5924632f3344 | 45 | { |
rkimble | 2:d101ca4ae85e | 46 | if(percent < 0.0) {percent = 0.0;} |
rkimble | 2:d101ca4ae85e | 47 | if(percent > 1.0) {percent = 1.0;} |
rkimble | 2:d101ca4ae85e | 48 | unsigned short value = 0; |
rkimble | 2:d101ca4ae85e | 49 | value = 65535 * percent; |
rkimble | 2:d101ca4ae85e | 50 | _currentValue = value; |
rkimble | 2:d101ca4ae85e | 51 | return write(value); |
rkimble | 0:896aaf4b80d0 | 52 | } |