Peter Cooper / Mbed 2 deprecated Dome

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers i2c.c Source File

i2c.c

00001 #include "mbed.h"
00002 
00003 #include "main.h"
00004 #include "useful.h"
00005 #include "i2c.h"
00006 #include "pca9685_reg.h"    /* Light Driver Chip */
00007 
00008 I2C i2c(p9, p10); // sda, scl
00009 I2C i2c_1(p28, p27); // sda, scl
00010 
00011 
00012 #define PIO          0x40
00013 #define RELAYS       0x40
00014 
00015 
00016 
00017 /******************************************/
00018 /*                                        */
00019 /*   Probe the I2C bus, and show the      */
00020 /*   user what we have found              */
00021 /*                                        */
00022 /*                                        */
00023 /*                                        */
00024 /******************************************/
00025 void i2c_probe(void) 
00026 {
00027     lprintf("Searching for I2C devices...\n");
00028     
00029     int count = 0;
00030     for (int address=4; address<256; address+=2) {
00031         if (!i2c.write(address, NULL, 0)) { // 0 returned is ok
00032             lprintf(" - I2C device found at address 0x%02X\n", address);
00033             count++;
00034         }
00035     }
00036     lprintf("%d devices found\n", count);
00037 }
00038 void i2c_probe2(void) 
00039 {
00040     lprintf("Searching for I2C devices... (seconday bus)\n");
00041     
00042     int count = 0;
00043     for (int address=4; address<256; address+=2) {
00044         if (!i2c_1.write(address, NULL, 0)) { // 0 returned is ok
00045             lprintf(" - I2C device found at address 0x%02X\n", address);
00046             count++;
00047         }
00048     }
00049     lprintf("%d devices found\n", count);
00050 }
00051 
00052 
00053 
00054 /******************************************/
00055 /*                                        */
00056 /* 1 - 8, Relays On,                      */
00057 /*                                        */
00058 /******************************************/
00059 void relay_operate(char r)
00060 {
00061     char buf[0x60];
00062         
00063     switch(r){
00064         case    0    :    /* Turn off the relays */
00065             buf[0]=0x00; 
00066             break;
00067         case    1    :
00068             buf[0]=0x01; 
00069             break;
00070         case    2    :
00071             buf[0]=0x02; 
00072             break;
00073         case    3    :
00074             buf[0]=0x04; 
00075             break;
00076         case    4    :
00077             buf[0]=0x08; 
00078             break;
00079         case    5    :
00080             buf[0]=0x10; 
00081             break;
00082         case    6    :
00083             buf[0]=0x20; 
00084             break;
00085         case    7    :
00086             buf[0]=0x40; 
00087             break;
00088         case    8    :
00089             buf[0]=0x80; 
00090             break;
00091         default      :
00092             lprintf("Unknown Relay %d\n\r",r);
00093             return;
00094     }
00095     i2c.write(RELAYS,buf,1);
00096 }
00097 
00098 /******************************************/
00099 /*                                        */
00100 /* Read and Write the PIO latch           */
00101 /*                                        */
00102 /******************************************/
00103 void pio_write(unsigned char r, unsigned char d)
00104 {
00105     unsigned char buf[0x60];
00106     
00107     buf[0]=d;
00108     i2c.write(r,(char *)buf,1);
00109 }
00110 void pio_read(unsigned char d)
00111 {
00112    unsigned char r;
00113    unsigned char buf[0x60];
00114     
00115    i2c.read(d,(char *)buf,1);
00116    r = buf[0];
00117    
00118    lprintf("Returned value from the PIO was 0x%02x\n\r",r);
00119 }
00120 
00121 /******************************************/
00122 /*                                        */
00123 /* Philips PCA9685 I2C Driver, 16 channel */
00124 /* Lighting controler chip, we have 4     */
00125 /* running in this system, so we need to  */
00126 /* think how the channels map ??          */
00127 /*                                        */
00128 /******************************************/
00129 
00130 /******************************************/
00131 /*                                        */
00132 /* Init code for the PCA9685              */
00133 /*                                        */
00134 /******************************************/
00135 
00136 void init_pca9685(unsigned char address)
00137 {
00138     unsigned char buf[30];
00139     
00140     lprintf("Setting up channel %d\n\r",address);
00141     
00142     buf[0] = PCA9685_MODE1;
00143     buf[1] = PCA9685_AI;
00144     buf[2] = PCA9685_OUTDRV;
00145     i2c.write(address,(char *) buf, 3);
00146 }
00147 
00148 /******************************************/
00149 /*                                        */
00150 /* Send data to a given channle of a      */
00151 /* given PCA9685 chip                     */
00152 /*                                        */
00153 /******************************************/
00154 
00155 void pca9685_led(unsigned char addr, int led, unsigned char *values) 
00156 {
00157     unsigned char buf[5];
00158    
00159     if (led == PCA9685_ALL_LEDS) {
00160         buf[0] = PCA9685_ALL_LED_ON_L;
00161     } else {
00162         buf[0] = PCA9685_BASE(led);
00163     }
00164 
00165     buf[1] = values[0];
00166     buf[2] = values[1];
00167     buf[3] = values[2];
00168     buf[4] = values[3];
00169     i2c.write(addr, (char *)buf, 5);
00170 }
00171 
00172 /******************************************/
00173 /*                                        */
00174 /* Calculate the register values for a    */
00175 /* givern brightness percentage           */
00176 /*                                        */
00177 /******************************************/
00178 
00179 void pca9685_brightness(int percent, unsigned char *values) 
00180 {
00181     unsigned int on, off;
00182 
00183     if (percent == 0) {
00184     values[PCA9685_LED_ON_H] = 0;
00185     values[PCA9685_LED_OFF_H] = PCA9685_LED_OFF;
00186     return;
00187     }
00188     if (percent == 100) {
00189     values[PCA9685_LED_ON_H] = PCA9685_LED_ON;
00190     values[PCA9685_LED_OFF_H] = 0;
00191     return;
00192     }
00193     on = 0;
00194     off = (4096 * percent) / 100;
00195     values[PCA9685_LED_ON_L] = on & 0xff;
00196     values[PCA9685_LED_ON_H] = (on >> 8) & 0xf;
00197     values[PCA9685_LED_OFF_L] = off & 0xff;
00198     values[PCA9685_LED_OFF_H] = (off >> 8) & 0xf;
00199 }
00200 
00201 /******************************************/
00202 /*                                        */
00203 /* Set a given channel to a given level   */
00204 /*                                        */
00205 /******************************************/
00206 void channel_light(unsigned char ch, unsigned char lev)
00207 {
00208     char            chip,led;     /* Chip Number, channel number */
00209     unsigned char   v[4];         /* register data for givern level */
00210     
00211     led = ch%16;
00212     v[0]=0;
00213     v[1]=0;
00214     v[2]=0;
00215     v[3]=0;
00216     
00217     if(lev > 100){
00218         lprintf("Level percentage range 0 - 100 (Trying for %d)\n\r",lev);
00219         return;
00220     }
00221     
00222     switch(ch/16){
00223         case    0    :
00224             chip=LEDDRV1;
00225             break;
00226         case    1    :
00227             chip=LEDDRV2;
00228             break;
00229         case    2    :
00230             chip=LEDDRV3;
00231             break;
00232         case    3    :
00233             chip=LEDDRV4;
00234             break;
00235         case    4    :
00236             chip=LEDDRV5;
00237             break;
00238         case    5    :
00239             chip=LEDDRV6;
00240             break;
00241         default      :
00242             lprintf("Error unknown chip %d\n\r",ch/16);
00243             return;
00244     }
00245     
00246     lprintf("Setting channel %d to brightness leven %d chip = %d(%d),%d\n\r",
00247         ch,lev,chip,ch/16,led);
00248     pca9685_brightness(lev,v);    /* Calculate the brightness level */
00249     lprintf("Brightness level is %02x,%02x,%02x,%02x\n\r",v[0],v[1],v[2],v[3]);
00250     pca9685_led(chip,led,v);      /* Send to chip */
00251 }
00252