Library for "I2C 8x8 LED matrix board" from Switch Science https://www.switch-science.com/catalog/2071/

Dependents:   PCA9622_LED8x8_Demo PCA9622_LED8x8_Hello PCA9622_LED8x8_x6_Demo shake-shake-machine

0. What is this?

I2C interface single color LED matrix module with PCA9622(LED controller).

This library provides interface from memory array to LEDs, manages scan.
Brightness control can be done for each pixels by setting data as float value (from 0.0 to 1.0).

topview
bottomview

1. How to use

1.1 Sample code

Import programPCA9622_LED8x8_Hello

This is a very simple sample code for the PCA9622_LED8x8 library. arget hardware : "I2C 8x8 LED matrix board" from Switch Science https://www.switch-science.com/catalog/2071/

1.2 Very basic sample

1.2.1 Displaying static image

Next is simplified code sample that makes still image on the LEDs.
In the main() function, the library operation is started by start() function and image data is set by set_data().
The image data is stored in 2 dimensional float array.

#include "mbed.h"
#include "PCA9622_LED8x8.h"

PCA9622_LED8x8  matrix( p28, p27 );     //  I2C pins. SDA and SCL

int main()
{
    float   image[ 8 ][ 8 ];

    matrix.start();

    //  making 8x8 image to "image" array
    for ( int i = 0; i < 8; i++ )
        for ( int j = 0; j < 8; j++ )
            image[ i ][ j ]   = (i + 1) * (j + 1) / 64.0;

    //  set the image into library internal bufer
    matrix.set_data( image );

    while(1)
        ;
}


/media/uploads/nxp_ip/img_3560.jpg
result of upper code

1.2.2 Image array elements' physical position

Each element of "float image[8][8]" array has physical position of..

leftright
topimage[0][0]image[0][1]image[0][2]image[0][3]image[0][4]image[0][5]image[0][6]image[0][7]
image[1][0]image[1][1]image[1][2]image[1][3]image[1][4]image[1][5]image[1][6]image[1][7]
image[2][0]image[2][1]image[2][2]image[2][3]image[2][4]image[2][5]image[2][6]image[2][7]
image[3][0]image[3][1]image[3][2]image[3][3]image[3][4]image[3][5]image[3][6]image[3][7]
image[4][0]image[4][1]image[4][2]image[4][3]image[4][4]image[4][5]image[4][6]image[4][7]
image[5][0]image[5][1]image[5][2]image[5][3]image[5][4]image[5][5]image[5][6]image[5][7]
image[6][0]image[6][1]image[6][2]image[6][3]image[6][4]image[6][5]image[6][6]image[6][7]
bottomimage[7][0]image[7][1]image[7][2]image[7][3]image[7][4]image[7][5]image[7][6]image[7][7]

1.2.3 Making the image moving

If you want to perform animation on the LEDs, it can be done by simple loop.
Keep the loop doing (1) store image data into the array and () call set_data() function.
The set_data() function can be called any time.

The set_data() function does copy the data in in-active side of internal buffer.
This library has two 8x8 buffers internally to perform "ping-pong" buffering. One side of the buffer is "active" and other side is "inactive".
The active side is the buffer for displaying data to LEDs. The inactive side is for user access.
The set_data() always access to the inactive side and after the inactive side filling is done, the active/inactive side switched at next scan start.

    while(1)
    {
        //  do something for image array to store data;
        ....
        ...
        matrix.set_data( image );
        wait( 0.1 );
    }

2. Tips

2.1 Frame rate

To minimize the flicker, default setting of frame rate is set to 100Hz.
But it is bit heavy rate to maintain the scan by MCU. If you going to use multiple modules, you may need to lower the I2C clock frequency.

Since mbed's I2C write routine is blocking function, frequent I2C transfer consumes much CPU time (just to wait transfer complete). To draw a line, one I2C transfer is done (14 bytes transfer including slave address). If the frame rate is 100Hz, 800 times I2C transfer happens in a second (8 lines/frame * 100Hz). If your mbed is supporting "I2C Fast mode plus (Fm+)", this will be a good option to reduce the CPU load. PCA9622 supports Fm+ which can handle the clock upto 1MHz.

2.2 Start/Stop

After start() function call, a periodic interrupt is installed to maintain the scanning the LEDs. This will be kept until stop() function called.

3. Links

shop page (Japanese)
schematic
PCA9622

Component page is available

Please refer to the component page for for more information.

I2C 8x8 LED matrix board" from Switch Science

Information

If you want to try displaying image which is prepared as file (PNG, JPEG, GIF, BMP, etc.), this tool may be useful.
This tool converts the image data into float array (in C source format).
https://gist.github.com/toyowata/bf576ee091e75071bc66

Committer:
nxp_ip
Date:
Thu Feb 26 00:01:06 2015 +0000
Revision:
3:fb23972da0f3
Parent:
2:cd00372373e4
Version 1.1.1 : Fix in destructor. To dispose I2C instance if it has own.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nxp_ip 0:41234a8149bd 1 /**
nxp_ip 0:41234a8149bd 2 * PCA9622 LED 8x8 library
nxp_ip 0:41234a8149bd 3 *
nxp_ip 0:41234a8149bd 4 * @author Tedd OKANO
nxp_ip 3:fb23972da0f3 5 * @version 1.1.1
nxp_ip 3:fb23972da0f3 6 * @date 26-Feb-2015
nxp_ip 0:41234a8149bd 7 *
nxp_ip 0:41234a8149bd 8 * Library for "I2C 8x8 LED matrix board" from Switch Science
nxp_ip 0:41234a8149bd 9 * https://www.switch-science.com/catalog/2071/
nxp_ip 0:41234a8149bd 10 *
nxp_ip 1:a1bf164ff73b 11 * The I2C LED controller PCA9622 is used on this module
nxp_ip 1:a1bf164ff73b 12 * This LED controller chip enables brightness control by PWM.
nxp_ip 1:a1bf164ff73b 13 *
nxp_ip 0:41234a8149bd 14 * For more information about the PCA9622:
nxp_ip 0:41234a8149bd 15 * http://www.nxp.com/documents/data_sheet/PCA9622.pdf
nxp_ip 0:41234a8149bd 16 */
nxp_ip 0:41234a8149bd 17
nxp_ip 0:41234a8149bd 18 #ifndef MBED_PCA9622_LED8x8_H
nxp_ip 0:41234a8149bd 19 #define MBED_PCA9622_LED8x8_H
nxp_ip 0:41234a8149bd 20
nxp_ip 0:41234a8149bd 21 #include "mbed.h"
nxp_ip 0:41234a8149bd 22
nxp_ip 0:41234a8149bd 23 /** PCA9622_LED8x8 class
nxp_ip 0:41234a8149bd 24 *
nxp_ip 0:41234a8149bd 25 * PCA9622_LED8x8: A library to control LED matrix using the PCA9622
nxp_ip 0:41234a8149bd 26 *
nxp_ip 0:41234a8149bd 27 * PCA9622 is an I2C LED controller.
nxp_ip 0:41234a8149bd 28 * This library operates LED 8x8 matrix through the PCA9622.
nxp_ip 0:41234a8149bd 29 *
nxp_ip 0:41234a8149bd 30 * Example:
nxp_ip 0:41234a8149bd 31 * @code
nxp_ip 0:41234a8149bd 32 * #include "mbed.h"
nxp_ip 0:41234a8149bd 33 * #include "PCA9622_LED8x8.h"
nxp_ip 1:a1bf164ff73b 34 *
nxp_ip 0:41234a8149bd 35 * PCA9622_LED8x8 matrix( p28, p27 ); // for 40pin type mbed
nxp_ip 0:41234a8149bd 36 * //PCA9622_LED8x8 matrix( D14, D15 ); // for Arduino type mbed
nxp_ip 0:41234a8149bd 37 * //PCA9622_LED8x8 matrix( dp5, dp27 ); // for mbed LPC1114
nxp_ip 1:a1bf164ff73b 38 *
nxp_ip 0:41234a8149bd 39 * float func( float x, float y, float t ); // function to make 8x8 image
nxp_ip 1:a1bf164ff73b 40 *
nxp_ip 0:41234a8149bd 41 * int main()
nxp_ip 0:41234a8149bd 42 * {
nxp_ip 1:a1bf164ff73b 43 * float image[ 8 ][ 8 ]; //
nxp_ip 0:41234a8149bd 44 * int count = 0;
nxp_ip 1:a1bf164ff73b 45 *
nxp_ip 0:41234a8149bd 46 * matrix.start();
nxp_ip 1:a1bf164ff73b 47 *
nxp_ip 0:41234a8149bd 48 * while(1) {
nxp_ip 1:a1bf164ff73b 49 *
nxp_ip 0:41234a8149bd 50 * // making 8x8 image to "image" array
nxp_ip 0:41234a8149bd 51 * for ( int i = 0; i < 8; i++ )
nxp_ip 0:41234a8149bd 52 * for ( int j = 0; j < 8; j++ )
nxp_ip 0:41234a8149bd 53 * image[ i ][ j ] = func( i, j, count * 0.2 );
nxp_ip 1:a1bf164ff73b 54 *
nxp_ip 0:41234a8149bd 55 * matrix.set_data( image );
nxp_ip 1:a1bf164ff73b 56 *
nxp_ip 0:41234a8149bd 57 * count++;
nxp_ip 0:41234a8149bd 58 * wait( 0.05 );
nxp_ip 0:41234a8149bd 59 * }
nxp_ip 0:41234a8149bd 60 * }
nxp_ip 1:a1bf164ff73b 61 *
nxp_ip 0:41234a8149bd 62 * float func( float x, float y, float t )
nxp_ip 0:41234a8149bd 63 * {
nxp_ip 0:41234a8149bd 64 * //#define DISPLAY_OFFSET 3.5
nxp_ip 0:41234a8149bd 65 * #define DISPLAY_OFFSET 0
nxp_ip 0:41234a8149bd 66 * #define SIZE 0.3
nxp_ip 1:a1bf164ff73b 67 *
nxp_ip 0:41234a8149bd 68 * float s;
nxp_ip 1:a1bf164ff73b 69 *
nxp_ip 0:41234a8149bd 70 * x = (x - DISPLAY_OFFSET) * SIZE;
nxp_ip 0:41234a8149bd 71 * y = (y - DISPLAY_OFFSET) * SIZE;
nxp_ip 1:a1bf164ff73b 72 *
nxp_ip 0:41234a8149bd 73 * s = cos( powf( x * x + y * y, 0.5 ) - t );
nxp_ip 0:41234a8149bd 74 * return ( powf( s, 4.0 ) );
nxp_ip 0:41234a8149bd 75 * }
nxp_ip 0:41234a8149bd 76 * @endcode
nxp_ip 0:41234a8149bd 77 */
nxp_ip 0:41234a8149bd 78
nxp_ip 0:41234a8149bd 79 class PCA9622_LED8x8
nxp_ip 0:41234a8149bd 80 {
nxp_ip 0:41234a8149bd 81 public:
nxp_ip 1:a1bf164ff73b 82 enum FrameRate { DEFAULT_FRAMERATE = 50 };
nxp_ip 0:41234a8149bd 83
nxp_ip 1:a1bf164ff73b 84 /** Create a PCA9622_LED8x8 instance for the LED matrix board
nxp_ip 0:41234a8149bd 85 * which is connected to specified I2C pins with specified address
nxp_ip 0:41234a8149bd 86 *
nxp_ip 0:41234a8149bd 87 * @param sda I2C-bus SDA pin
nxp_ip 0:41234a8149bd 88 * @param scl I2C-bus SCL pin
nxp_ip 0:41234a8149bd 89 * @param slave_adr (option) I2C-bus address (default: 0xA0)
nxp_ip 0:41234a8149bd 90 * @param fr (option) frame rate (in "Hz". default: 100)
nxp_ip 0:41234a8149bd 91 */
nxp_ip 1:a1bf164ff73b 92 PCA9622_LED8x8( PinName sda, PinName scl, char slave_adr = 0xA0, float fr = DEFAULT_FRAMERATE );
nxp_ip 0:41234a8149bd 93
nxp_ip 1:a1bf164ff73b 94 /** Create a PCA9622_LED8x8 instance for the LED matrix board
nxp_ip 0:41234a8149bd 95 * which is connected to specified I2C pins with specified address
nxp_ip 0:41234a8149bd 96 *
nxp_ip 0:41234a8149bd 97 * @param i2c_obj I2C object (instance)
nxp_ip 0:41234a8149bd 98 * @param slave_adr (option) I2C-bus address (default: 0xA0)
nxp_ip 0:41234a8149bd 99 * @param fr (option) frame rate (in "Hz". default: 100)
nxp_ip 0:41234a8149bd 100 */
nxp_ip 1:a1bf164ff73b 101 PCA9622_LED8x8( I2C &i2c_obj, char slave_adr = 0xA0, float fr = DEFAULT_FRAMERATE );
nxp_ip 0:41234a8149bd 102
nxp_ip 0:41234a8149bd 103 /** Destructor of PCA9622_LED8x8
nxp_ip 0:41234a8149bd 104 */
nxp_ip 0:41234a8149bd 105 ~PCA9622_LED8x8();
nxp_ip 1:a1bf164ff73b 106
nxp_ip 0:41234a8149bd 107 /** Frame rate change
nxp_ip 0:41234a8149bd 108 *
nxp_ip 0:41234a8149bd 109 * Changes frame rate (scan rate of the matrix)
nxp_ip 0:41234a8149bd 110 *
nxp_ip 0:41234a8149bd 111 * @param rate Frame rate in "Hz"
nxp_ip 0:41234a8149bd 112 */
nxp_ip 0:41234a8149bd 113 void frame_rate( float rate );
nxp_ip 0:41234a8149bd 114
nxp_ip 0:41234a8149bd 115 /** Start operation
nxp_ip 0:41234a8149bd 116 *
nxp_ip 0:41234a8149bd 117 * The LED matrix operation need to be started with this function.
nxp_ip 1:a1bf164ff73b 118 * Scan line drawing by Ticker interrupt will be started.
nxp_ip 0:41234a8149bd 119 */
nxp_ip 0:41234a8149bd 120 void start( void );
nxp_ip 0:41234a8149bd 121
nxp_ip 0:41234a8149bd 122 /** Stop operation
nxp_ip 0:41234a8149bd 123 *
nxp_ip 1:a1bf164ff73b 124 * This stops the operation.
nxp_ip 0:41234a8149bd 125 * Ticker interrupt will be stopped
nxp_ip 0:41234a8149bd 126 */
nxp_ip 0:41234a8149bd 127 void stop( void );
nxp_ip 1:a1bf164ff73b 128
nxp_ip 0:41234a8149bd 129 /** Set the image to library internal buffer
nxp_ip 0:41234a8149bd 130 *
nxp_ip 1:a1bf164ff73b 131 * 8x8 pixel image will be copied into the library internal buffer.
nxp_ip 1:a1bf164ff73b 132 * User don't need to care the timing of this function call because
nxp_ip 1:a1bf164ff73b 133 * the library is using "ping-pong" buffering.
nxp_ip 1:a1bf164ff73b 134 * This function writes data into in-active side of the buffer.
nxp_ip 1:a1bf164ff73b 135 * The image will not be appear immediatly but from next scan start.
nxp_ip 0:41234a8149bd 136 *
nxp_ip 1:a1bf164ff73b 137 * Each elements of the array should be brightness information
nxp_ip 1:a1bf164ff73b 138 * in range of 0.0 to 1.0.
nxp_ip 0:41234a8149bd 139 *
nxp_ip 0:41234a8149bd 140 * The 2 dimensional 8x8 array will be handled as..
nxp_ip 1:a1bf164ff73b 141 *
nxp_ip 0:41234a8149bd 142 * p[0][0] : top-left
nxp_ip 0:41234a8149bd 143 * p[0][7] : top-right
nxp_ip 0:41234a8149bd 144 * p[7][0] : bottm-left
nxp_ip 0:41234a8149bd 145 * p[7][7] : bottom-right
nxp_ip 0:41234a8149bd 146 *
nxp_ip 0:41234a8149bd 147 * @param p image stored 2-dimensional float array
nxp_ip 0:41234a8149bd 148 */
nxp_ip 0:41234a8149bd 149 void set_data( float p[ 8 ][ 8 ] );
nxp_ip 1:a1bf164ff73b 150
nxp_ip 0:41234a8149bd 151 private:
nxp_ip 0:41234a8149bd 152 I2C *i2c_p;
nxp_ip 0:41234a8149bd 153 I2C &i2c;
nxp_ip 0:41234a8149bd 154 char address; // I2C slave address
nxp_ip 0:41234a8149bd 155 float framerate; // frame rate in "Hz"
nxp_ip 0:41234a8149bd 156 int in_operation; // ture, if scan is on-going
nxp_ip 1:a1bf164ff73b 157 int line_counter; // counting scan line
nxp_ip 0:41234a8149bd 158 int frame_counter; // counting frame number
nxp_ip 0:41234a8149bd 159 int buffer_switch_request; // flag to switch the ping-pong buffer
nxp_ip 0:41234a8149bd 160 int outgoing_buffer; // flag to select the buffer
nxp_ip 1:a1bf164ff73b 161 Ticker t;
nxp_ip 0:41234a8149bd 162 char pattern[ 2 ][ 8 ][ 8 ]; // having 2 sets of 8x8 array for pin-pong buffering
nxp_ip 0:41234a8149bd 163
nxp_ip 0:41234a8149bd 164 void draw_a_line( void ); // ISR called by Ticker
nxp_ip 0:41234a8149bd 165 void initialize( void ); // initialize function
nxp_ip 0:41234a8149bd 166 };
nxp_ip 0:41234a8149bd 167
nxp_ip 0:41234a8149bd 168 #endif // MBED_PCA9622_LED8x8_H