Hello World example for PCA9532 Library

Dependencies:   mbed

Committer:
chris
Date:
Fri May 07 10:30:15 2010 +0000
Revision:
0:836d73d25007

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
chris 0:836d73d25007 1 /* mbed PCF9532 LED Driver Library
chris 0:836d73d25007 2 *
chris 0:836d73d25007 3 * Copyright (c) 2010, cstyles (http://mbed.org)
chris 0:836d73d25007 4 *
chris 0:836d73d25007 5 * Permission is hereby granted, free of charge, to any person obtaining a copy
chris 0:836d73d25007 6 * of this software and associated documentation files (the "Software"), to deal
chris 0:836d73d25007 7 * in the Software without restriction, including without limitation the rights
chris 0:836d73d25007 8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
chris 0:836d73d25007 9 * copies of the Software, and to permit persons to whom the Software is
chris 0:836d73d25007 10 * furnished to do so, subject to the following conditions:
chris 0:836d73d25007 11 *
chris 0:836d73d25007 12 * The above copyright notice and this permission notice shall be included in
chris 0:836d73d25007 13 * all copies or substantial portions of the Software.
chris 0:836d73d25007 14 *
chris 0:836d73d25007 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
chris 0:836d73d25007 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
chris 0:836d73d25007 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
chris 0:836d73d25007 18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
chris 0:836d73d25007 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
chris 0:836d73d25007 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
chris 0:836d73d25007 21 * THE SOFTWARE.
chris 0:836d73d25007 22 */
chris 0:836d73d25007 23
chris 0:836d73d25007 24
chris 0:836d73d25007 25 #include "PCA9532.h"
chris 0:836d73d25007 26 #include "mbed.h"
chris 0:836d73d25007 27
chris 0:836d73d25007 28 /*
chris 0:836d73d25007 29 Constructor, pin names for I2C and the I2C addrss of the device
chris 0:836d73d25007 30 */
chris 0:836d73d25007 31 PCA9532::PCA9532(PinName scl, PinName sda, int addr)
chris 0:836d73d25007 32 : _i2c(scl, sda) {
chris 0:836d73d25007 33
chris 0:836d73d25007 34 _i2c.frequency(1000000);
chris 0:836d73d25007 35
chris 0:836d73d25007 36 _addr = addr;
chris 0:836d73d25007 37
chris 0:836d73d25007 38 }
chris 0:836d73d25007 39
chris 0:836d73d25007 40
chris 0:836d73d25007 41
chris 0:836d73d25007 42
chris 0:836d73d25007 43
chris 0:836d73d25007 44 /*
chris 0:836d73d25007 45 Set the period
chris 0:836d73d25007 46 */
chris 0:836d73d25007 47 int PCA9532::Period (int channel, float period) {
chris 0:836d73d25007 48
chris 0:836d73d25007 49 char reg = 0;
chris 0:836d73d25007 50
chris 0:836d73d25007 51 if (channel == 0) {
chris 0:836d73d25007 52 reg = PCA9532_REG_PSC0;
chris 0:836d73d25007 53 } else if (channel == 1) {
chris 0:836d73d25007 54 reg = PCA9532_REG_PSC1;
chris 0:836d73d25007 55 } else {
chris 0:836d73d25007 56 return (1);
chris 0:836d73d25007 57 }
chris 0:836d73d25007 58
chris 0:836d73d25007 59 if (period > 1.0) {
chris 0:836d73d25007 60 period = 255;
chris 0:836d73d25007 61 } else if ( period < 0.0 ) {
chris 0:836d73d25007 62 period = 0;
chris 0:836d73d25007 63 } else {
chris 0:836d73d25007 64 period = 256 * period;
chris 0:836d73d25007 65 }
chris 0:836d73d25007 66
chris 0:836d73d25007 67 _write(reg, period);
chris 0:836d73d25007 68 return(0);
chris 0:836d73d25007 69
chris 0:836d73d25007 70 }
chris 0:836d73d25007 71
chris 0:836d73d25007 72
chris 0:836d73d25007 73 /*
chris 0:836d73d25007 74 Set the duty cycle
chris 0:836d73d25007 75 */
chris 0:836d73d25007 76 int PCA9532::Duty (int channel, float d) {
chris 0:836d73d25007 77
chris 0:836d73d25007 78 char duty = 0;
chris 0:836d73d25007 79 char reg = 0;
chris 0:836d73d25007 80
chris 0:836d73d25007 81 if (channel == 0) {
chris 0:836d73d25007 82 reg = PCA9532_REG_PWM0;
chris 0:836d73d25007 83 } else if (channel == 1) {
chris 0:836d73d25007 84 reg = PCA9532_REG_PWM1;
chris 0:836d73d25007 85 } else {
chris 0:836d73d25007 86 return (1);
chris 0:836d73d25007 87 }
chris 0:836d73d25007 88
chris 0:836d73d25007 89 if (d > 1.0) {
chris 0:836d73d25007 90 duty = 255;
chris 0:836d73d25007 91 } else if ( d < 0.0 ) {
chris 0:836d73d25007 92 duty = 0;
chris 0:836d73d25007 93 } else {
chris 0:836d73d25007 94 duty = 256 * d;
chris 0:836d73d25007 95 }
chris 0:836d73d25007 96
chris 0:836d73d25007 97 _write(reg, duty);
chris 0:836d73d25007 98 return(0);
chris 0:836d73d25007 99
chris 0:836d73d25007 100 }
chris 0:836d73d25007 101
chris 0:836d73d25007 102 /*
chris 0:836d73d25007 103 Set each of the LEDs in this mask to the give mode
chris 0:836d73d25007 104 Loop through the mask calling SetLed on each match
chris 0:836d73d25007 105 alt_mode specifies the mode of the non-Matches of SetMode
chris 0:836d73d25007 106 */
chris 0:836d73d25007 107 int PCA9532::SetMode (int mask, int mode) {
chris 0:836d73d25007 108 if ( (mode < 0) || (mode > 3) ) {
chris 0:836d73d25007 109 return(1);
chris 0:836d73d25007 110 } else {
chris 0:836d73d25007 111 for (int i=0 ; i < 16 ; i++ ) {
chris 0:836d73d25007 112
chris 0:836d73d25007 113 // if this matches, set the LED to the mode
chris 0:836d73d25007 114 if (mask & (0x1 << i)) {
chris 0:836d73d25007 115 SetLed(i,mode);
chris 0:836d73d25007 116 }
chris 0:836d73d25007 117 }
chris 0:836d73d25007 118 }
chris 0:836d73d25007 119 return(0);
chris 0:836d73d25007 120 }
chris 0:836d73d25007 121
chris 0:836d73d25007 122
chris 0:836d73d25007 123
chris 0:836d73d25007 124 /*
chris 0:836d73d25007 125 led is in the range 0-15
chris 0:836d73d25007 126 mode is inthe range 0-3
chris 0:836d73d25007 127 */
chris 0:836d73d25007 128 int PCA9532::SetLed(int led, int mode) {
chris 0:836d73d25007 129
chris 0:836d73d25007 130 int reg = 0;
chris 0:836d73d25007 131 int offset = (led % 4);
chris 0:836d73d25007 132
chris 0:836d73d25007 133 printf("\nSetLed(%d,%d)\n", led, mode);
chris 0:836d73d25007 134
chris 0:836d73d25007 135 // makesure mode is within bounds
chris 0:836d73d25007 136 if ( (mode < 0) || (mode > 3) ) {
chris 0:836d73d25007 137 printf("Error : Invalid mode supplied\n");
chris 0:836d73d25007 138 return(1);
chris 0:836d73d25007 139 }
chris 0:836d73d25007 140
chris 0:836d73d25007 141 // determine which register this is,
chris 0:836d73d25007 142 if (led < 4) {
chris 0:836d73d25007 143 reg = PCA9532_REG_LS0;
chris 0:836d73d25007 144 } else if ( (led > 3) && (led < 8) ) {
chris 0:836d73d25007 145 reg = PCA9532_REG_LS1;
chris 0:836d73d25007 146 } else if ( (led > 7) && (led < 12) ) {
chris 0:836d73d25007 147 reg = PCA9532_REG_LS2;
chris 0:836d73d25007 148 } else if ( (led > 11) && (led < 16) ) {
chris 0:836d73d25007 149 reg = PCA9532_REG_LS3;
chris 0:836d73d25007 150 } else {
chris 0:836d73d25007 151 return(1);
chris 0:836d73d25007 152 }
chris 0:836d73d25007 153
chris 0:836d73d25007 154 // read the current status of the register
chris 0:836d73d25007 155 char regval = _read(reg);
chris 0:836d73d25007 156
chris 0:836d73d25007 157 // clear the two bit slice at the calculated offset
chris 0:836d73d25007 158 regval &= ~(0x3 << (2 * offset));
chris 0:836d73d25007 159
chris 0:836d73d25007 160 // now OR in the mode, shifted by 2*offset
chris 0:836d73d25007 161 regval |= (mode << (2 * offset));
chris 0:836d73d25007 162
chris 0:836d73d25007 163 // write the new value back
chris 0:836d73d25007 164 _write(reg, regval);
chris 0:836d73d25007 165
chris 0:836d73d25007 166 return(0);
chris 0:836d73d25007 167 }
chris 0:836d73d25007 168
chris 0:836d73d25007 169
chris 0:836d73d25007 170
chris 0:836d73d25007 171 // private functions for low level IO
chris 0:836d73d25007 172
chris 0:836d73d25007 173 void PCA9532::_write(int reg, int data) {
chris 0:836d73d25007 174 char args[2];
chris 0:836d73d25007 175 args[0] = reg;
chris 0:836d73d25007 176 args[1] = data;
chris 0:836d73d25007 177 _i2c.write(_addr, args,2);
chris 0:836d73d25007 178 }
chris 0:836d73d25007 179
chris 0:836d73d25007 180 int PCA9532::_read(int reg) {
chris 0:836d73d25007 181 char args[2];
chris 0:836d73d25007 182 args[0] = reg;
chris 0:836d73d25007 183 _i2c.write(_addr, args, 1);
chris 0:836d73d25007 184 _i2c.read(_addr, args, 1);
chris 0:836d73d25007 185 return(args[0]);
chris 0:836d73d25007 186 }
chris 0:836d73d25007 187
chris 0:836d73d25007 188
chris 0:836d73d25007 189
chris 0:836d73d25007 190
chris 0:836d73d25007 191