Christian Dupaty / Mbed 2 deprecated CHARLIEPLEX-IS31FL3731

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002 Christian Dupaty 10/2021
00003 IS31FL3731 with CHARLEPLEX LED MATRIX adaptation from Adafruit project to ARM MBEB, tested on NUCLEO L073RZ
00004 for original project on Arduino see https://learn.adafruit.com/animated-flame-pendant/overview
00005 
00006 The program reads the data from an image and places it alternately
00007 on pages 0 and 1 of IS31FL3731
00008 
00009 Data structure in data.h
00010 first byte x1 PF and y1 pf
00011 second byte x2 PF and y2 pf
00012 loop from x1 to x2
00013      loop from y1 to y2
00014          copy from the third byte into the img buffer (144 bytes)
00015 
00016 the I2C write method sends the write address then a series of data, the number of data is then specified
00017 ex: Wire.write (I2C_ADDR, cmd, s); I2C_ADDR address on 8bits, cmd pointer to data to send, s number of data
00018 */
00019 
00020 //--------------------------------------------------------------------------
00021 // Animated flame for Adafruit Pro Trinket.  Uses the following parts:
00022 //   - Pro Trinket microcontroller (adafruit.com/product/2010 or 2000)
00023 //     (#2010 = 3V/12MHz for longest battery life, but 5V/16MHz works OK)
00024 //   - Charlieplex LED Matrix Driver (2946)
00025 //   - Charlieplex LED Matrix (2947, 2948, 2972, 2973 or 2974)
00026 //   - 350 mAh LiPoly battery (2750)
00027 //   - LiPoly backpack (2124)
00028 //   - SPDT Slide Switch (805)
00029 //
00030 // This is NOT good "learn from" code for the IS31FL3731; it is "squeeze
00031 // every last byte from the Pro Trinket" code.  If you're starting out,
00032 // download the Adafruit_IS31FL3731 and Adafruit_GFX libraries, which
00033 // provide functions for drawing pixels, lines, etc.  This sketch also
00034 // uses some ATmega-specific tricks and will not run as-is on other chips.
00035 //--------------------------------------------------------------------------
00036 
00037 #include "mbed.h"
00038 #include "data.h"           // Flame animation data
00039 //#define debug             // for some printf
00040 #define I2C_ADDR 0xE8       // I2C address of Charlieplex matrix 0x74
00041 I2C Wire(I2C_SDA, I2C_SCL);
00042 uint8_t        page = 0;    // Front/back buffer control
00043 const uint8_t *ptr  = anim; // Current pointer into animation data
00044 uint8_t        img[9 * 16]; // Buffer for rendering image
00045 
00046 //Buffer for I2C write method
00047 char cmd[200];
00048 
00049 // to select register on IS31FL3731, see datasheet page 9
00050 void pageSelect(uint8_t n)
00051 {
00052     cmd[0]=0xFD;
00053     cmd[1]=n;
00054     Wire.write(I2C_ADDR,cmd,2);  // attention write method close communications (stop condition is issued)
00055 }
00056 
00057 // SETUP FUNCTION - RUNS ONCE AT STARTUP -----------------------------------
00058 
00059 void setup()
00060 {
00061     uint8_t i, p, s;
00062 
00063     s=0;                // s is a counter for I2C buffer (cmd) write method
00064     pageSelect(0x0B);               // Access to the Function Registers (page Night)
00065     cmd[0]=0;            // adress first internal register 
00066     s++;                                       
00067     for(i=0; i<=0x0C; i++) 
00068     {
00069         cmd[i+1]=0x00;
00070         if (i==0) cmd[i+1]=0x00; //  0x08 for Auto Frame Play Mode   0x00 for picture mode
00071         if (i==0x0A) cmd[i+1]=0xFF;     // all to 0 except shutdown register
00072         s++;
00073     }
00074     Wire.write(I2C_ADDR,cmd,s);  
00075 
00076     for(p=0; p<2; p++) 
00077     {
00078         s=0;
00079         // For each page used (0 & 1)...
00080         pageSelect(p);                 // Access the Frame Registers
00081         cmd[0]=0;                      // Start from 1st LED control reg
00082         s++;
00083         for(i=0; i<0xB4; i++) 
00084         {
00085             if (i<18) cmd[i+1]=0xFF;          // Enable all LEDs (18*8=144)
00086             else cmd[i+1]=0x00;             // desable blink and PWM
00087             s++;
00088         }
00089         Wire.write(I2C_ADDR,cmd,s);
00090     }
00091 }
00092 
00093 void loop()
00094 {
00095     uint8_t  a, x1, y1, x2, y2, x, y,s;
00096 #ifdef debug
00097     printf("-------------------------------------------------------------\n");
00098     printf("start loop\n");
00099     printf("-------------------------------------------------------------\n");
00100 #endif
00101     pageSelect(0x0B);    // Function registers
00102     cmd[0]=0x01; // Picture Display reg
00103     cmd[1]=page;
00104     Wire.write(I2C_ADDR,cmd,2);
00105 
00106     page ^= 1; // Flip front/back buffer index
00107 
00108     // Then render NEXT frame.  Start by getting bounding rect for new frame:
00109     a = *ptr++;     // New frame X1/Y1
00110     if(a >= 144)   // 0x90
00111     {
00112         // EOD marker? (valid X1 never exceeds 8)
00113         ptr = anim;                 // Reset animation data pointer to start
00114         a   = *ptr++; // and take first value
00115     }
00116     x1 = a >> 4;                  // X1 = high 4 bits
00117     y1 = a & 0x0F;                // Y1 = low 4 bits
00118     a  = *ptr++;                // New frame X2/Y2
00119     x2 = a >> 4;                  // X2 = high 4 bits
00120     y2 = a & 0x0F;                // Y2 = low 4 bits
00121 
00122     // Read rectangle of data from anim[] into portion of img[] buffer
00123     #ifdef debug
00124     printf("x1= %d y1= %d x2= %d y2 = %d \n",x1,y1,x2,y2);
00125     #endif
00126     for(x=x1; x<=x2; x++) {
00127         // Column-major
00128         for(y=y1; y<=y2; y++) 
00129         {
00130         img[(x << 4) + y] = *ptr++;
00131         #ifdef debug
00132         printf("x= %d y= %d data= %d \n",x,y,img[(x << 4) + y]);
00133         #endif
00134         }
00135     }
00136 
00137     // Write img[] to matrix (not actually displayed until next pass)
00138     pageSelect(page);    // Select background buffer
00139     s=0;
00140     cmd[0]=0x24; // First byte of PWM data
00141     s++;
00142     // copy img buffer to IS31FL3731
00143     for(uint8_t j=0; j<144; j++) 
00144     {
00145             cmd[j+1]=img[j];
00146             s++;
00147             #ifdef debug
00148             printf("j= %d img[j]= %d \n",j,img[j]);
00149             #endif
00150     }
00151     Wire.write(I2C_ADDR,cmd,s);
00152     wait_ms(30);            // can be adjuted for speed annimation
00153 }
00154 
00155 int main()
00156 {
00157     #ifdef debug
00158     printf("-------------------------------------------------------------\n");
00159     printf("printf("Simulation flame with IS31FL3731\n\n");
00160     printf("-------------------------------------------------------------\n");
00161     #endif
00162     // arduino like
00163     setup();
00164     while(1) loop();
00165 }