Kevin Kent / Mbed 2 deprecated XVHE_HID

Dependencies:   USBDevice mbed

MCP4661.cpp

Committer:
kevinkent
Date:
2012-05-17
Revision:
0:960d250e49b2

File content as of revision 0:960d250e49b2:

/* mbed MCP4661 DigiPot Driver Library
 *
 * Copyright (c) 2012, Kevin Kent
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */


#include "MCP4661.h"
#include "mbed.h"

/*
 Constructor, pin names for I2C and the I2C address of the device
 */
MCP4661::MCP4661(PinName sda, PinName scl, int addr)
        : _i2c(sda, scl) {

    _i2c.frequency(1000000);

    _addr = addr;

}


int MCP4661::SetValue (int wiper, int value) {
    int reg;
    if (wiper == 0) {reg = MCP4661_VOL_WIPER0;}
    if (wiper == 1) {
        reg = MCP4661_VOL_WIPER1;
        reg = (reg << 4 );
    }
    reg |= MCP4661_WR;
    _write(reg, value);
    return(0);
}
int MCP4661::SetMode (int mask, int mode) {
    if ( (mode < 0) || (mode > 3) ) {
        return(1);
    } else {
        for (int i=0 ; i < 16 ; i++ ) {

            // if this matches, set the LED to the mode
            if (mask & (0x1 << i)) {
                SetLed(i,mode);
            }
        }
    }
    return(0);
}



/*
 led is in the range 0-15
 mode is inthe range 0-3
 */
int MCP4661::SetLed(int led, int mode) {

    int reg = 0;
    int offset = (led % 4);

    printf("\nSetLed(%d,%d)\n", led, mode);

    // makesure mode is within bounds
    if ( (mode < 0) || (mode > 3) ) {
        printf("Error : Invalid mode supplied\n");
        return(1);
    }

    // determine which register this is,
 

    // read the current status of the register
    char regval = _read(reg);

    // clear the two bit slice at the calculated offset
    regval &= ~(0x3 << (2 * offset));

    // now OR in the mode, shifted by 2*offset
    regval |= (mode << (2 * offset));

    // write the new value back
    _write(reg, regval);

    return(0);
}



// private functions for low level IO

void MCP4661::_write(int reg, int data) {
    char args[2];
    args[0] = reg;
    args[1] = data;
    _i2c.write(_addr, args,2);
}

int MCP4661::_read(int reg) {
    char args[2];
    args[0] = reg;
    _i2c.write(_addr, args, 1);
    _i2c.read(_addr, args, 1);
    return(args[0]);
}