Library for Sure Electronics HT1632 based LED matrix displays. Supports multiple displays connected together.

Dependents:   HT1632MsgScroller SMS_LEDMatrixPrinter

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HT1632_LedMatrix.h Source File

HT1632_LedMatrix.h

00001 /** Library for Holtek HT1632 LED driver chip,
00002  * As implemented on the Sure Electronics DE-DP10X or and DE-DP13X1X display board
00003  * 8 x 32 dot matrix LED module.)
00004  *
00005  * Original code by:
00006  * Nov, 2008 by Bill Westfield ("WestfW")
00007  *   Copyrighted and distributed under the terms of the Berkely license
00008  *   (copy freely, but include this notice of original author.)
00009  *
00010  * Adapted for 8x32 display by FlorinC.
00011  *
00012  * Arduino Library Created and updated by Andrew Lindsay October/November 2009
00013  *
00014  * Ported to Mbed platform by Andrew Lindsay, November 2012
00015  *
00016  * @author Andrew Lindsay
00017  *
00018  * @section LICENSE
00019  *
00020  * Copyright (c) 2012 Andrew Lindsay (andrew [at] thiseldo [dot] co [dot] uk)
00021  *
00022  * Permission is hereby granted, free of charge, to any person obtaining a copy
00023  * of this software and associated documentation files (the "Software"), to deal
00024  * in the Software without restriction, including without limitation the rights
00025  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00026  * copies of the Software, and to permit persons to whom the Software is
00027  * furnished to do so, subject to the following conditions:
00028 
00029  * The above copyright notice and this permission notice shall be included in
00030  * all copies or substantial portions of the Software.
00031  * 
00032  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00033  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00034  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00035  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00036  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00037  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00038  * THE SOFTWARE.
00039  * 
00040  * @section DESCRIPTION
00041  *  Definitions for Holtek HT1632 LED driver
00042  * 
00043  */
00044 
00045 #ifndef _HT1632_LEDMATRIX_H
00046 #define _HT1632_LEDMATRIX_H
00047 
00048 #include "mbed.h"
00049 #include "inttypes.h"
00050 
00051 // To include the graphic functions use the following. Comment out to exclude them 
00052 #undef USE_GRAPHIC
00053 
00054 // Defines for display sizes
00055 #define HT1632_08x32  1 
00056 #define HT1632_16x24  2
00057 
00058 /*
00059  * commands written to the chip consist of a 3 bit "ID", followed by
00060  * either 9 bits of "Command code" or 7 bits of address + 4 bits of data.
00061  */
00062 #define HT1632_ID_CMD 4     /* ID = 100 - Commands */
00063 #define HT1632_ID_RD  6     /* ID = 110 - Read RAM */
00064 #define HT1632_ID_WR  5     /* ID = 101 - Write RAM */
00065 
00066 #define HT1632_CMD_SYSDIS 0x00  /* CMD= 0000-0000-x Turn off oscil */
00067 #define HT1632_CMD_SYSON  0x01  /* CMD= 0000-0001-x Enable system oscil */
00068 #define HT1632_CMD_LEDOFF 0x02  /* CMD= 0000-0010-x LED duty cycle gen off */
00069 #define HT1632_CMD_LEDON  0x03  /* CMD= 0000-0011-x LEDs ON */
00070 #define HT1632_CMD_BLOFF  0x08  /* CMD= 0000-1000-x Blink ON */
00071 #define HT1632_CMD_BLON   0x09  /* CMD= 0000-1001-x Blink Off */
00072 #define HT1632_CMD_SLVMD  0x10  /* CMD= 0001-00xx-x Slave Mode */
00073 #define HT1632_CMD_MSTMD  0x14  /* CMD= 0001-01xx-x Master Mode */
00074 #define HT1632_CMD_RCCLK  0x18  /* CMD= 0001-10xx-x Use on-chip clock */
00075 #define HT1632_CMD_EXTCLK 0x1C  /* CMD= 0001-11xx-x Use external clock */
00076 #define HT1632_CMD_COMS00 0x20  /* CMD= 0010-ABxx-x commons options */
00077 #define HT1632_CMD_COMS01 0x24  /* CMD= 0010-ABxx-x commons options */
00078 #define HT1632_CMD_COMS10 0x28  /* CMD= 0010-ABxx-x commons options */
00079 #define HT1632_CMD_COMS11 0x2C  /* CMD= 0010-ABxx-x commons options */
00080 #define HT1632_CMD_PWM    0xA0  /* CMD= 101x-PPPP-x PWM duty cycle */
00081 
00082 #define PIXEL_OFF 0
00083 #define PIXEL_ON  1
00084 
00085 /** class HT1632_LedMatrix
00086 */
00087 class HT1632_LedMatrix : public Stream
00088 {
00089 public:
00090     /** Default Constructor for the display driver
00091      */
00092 //    HT1632_LedMatrix( );
00093     HT1632_LedMatrix( PinName clk, PinName dat, PinName cs1, PinName cs2 , PinName cs3, PinName cs4);
00094 
00095 
00096     /** Initialise the library with the display configuration
00097      * @param Number of horizontal displays, max 4
00098      * @param Number of vertical displays, max 4
00099      * @param Type of displays being used, either 32x8 or 24x16
00100      */
00101     void init( uint8_t, uint8_t, uint8_t displayType = HT1632_08x32 );
00102     
00103     /** Turn off the display
00104      */
00105     void displayOff( void );
00106     
00107     /** Turn on the display
00108      */
00109     void displayOn( void );
00110     
00111     /** Clear the display, and the shadow memory, and the snapshot
00112      * memory.  This uses the "write multiple words" capability of
00113      * the chipset by writing all 96 words of memory without raising
00114      * the chipselect signal.
00115      */
00116     void clear(void);
00117     
00118     /** Set display brightness. Brighness is from 0 to 15
00119      * @param brightness value, range x-y
00120      */
00121     void setBrightness( unsigned char );
00122     
00123     /** Copy a character glyph from the font data structure to
00124      * display memory, with its upper left at the given coordinate
00125      * This is unoptimized and simply uses plot() to draw each dot.
00126      * returns number of columns that didn't fit
00127      * @param column
00128      * @param row
00129      * @param character to write
00130      * @returns the number of columns that couldnt be displayed on current matrix
00131      */
00132     uint8_t putChar( int, int, char );
00133 
00134     /** Copy a character glyph from the font data structure to
00135      * display memory, with its upper left at the given coordinate
00136      * This is a wrapper for putChar that uses current cursor position
00137      */
00138     void write( uint8_t );
00139 
00140     /** Write a string at the position specified
00141      * x and y start from 0 and count number of pixels, 2nd row on a 2 row display is y=8
00142      * @param x position
00143      * @param y position
00144      * @param pointer to character data to display
00145      */
00146     void putString( int, int, char* );
00147     
00148     /** Plot a point on the display, with the upper left hand corner
00149      * being (0,0), and the lower right hand corner being (xMax-1, yMax-1).
00150      * Note that Y increases going "downward" in contrast with most
00151      * mathematical coordiate systems, but in common with many displays
00152      * basic bounds checking used.
00153      * @param column
00154      * @param row
00155      * @param point on or off
00156      */
00157     void plot( int, int, char );
00158     
00159     /** Set cursor position
00160      * @param x or column position, 0 is to left
00161      * @param y or row position, 0 is at top
00162      */
00163     void gotoXY(int , int);
00164     
00165     /** Return the current cursor position
00166      * @param column pointer
00167      * @param row pointer
00168      */
00169     void getXY(int* , int*);
00170 
00171     /** Return the maximum size of the display
00172      * @param column pointer
00173      * @param row pointer
00174      */
00175     void getXYMax(int*, int*);
00176     
00177     /** Shift cursor X position a number of positions either left or right.
00178      * @param increment, -1 for one position left, 1 for right
00179      */
00180     void shiftCursorX(int );
00181     
00182     /** Create a custom character. 8 character slots are availabe.
00183      * @param character number, 0 to 7
00184      * @param array of character bit maps
00185      */
00186     void setCustomChar( int, unsigned char[]);
00187     
00188     /** Create a custom character. 8 character slots are availabe.
00189      * @param character number, 0 to 7
00190      * @param array of character bit maps
00191      * @param width of chaaracter in columns
00192      */
00193     void setCustomChar( int, unsigned char[], uint8_t );
00194     
00195     /** Scroll row to the left
00196      * @param number fo columns to scroll
00197      * @param row number to scroll, used when displays are stacked vertically or when more than 8 high displays are used
00198      */
00199     void scrollLeft(uint8_t, uint8_t);
00200     
00201     /** Write shaddow ram to display
00202      */
00203     void putShadowRam();
00204     
00205     /** Write shadow ram to a specific dasipaly board
00206      * @param chip or display number
00207      */
00208     void putShadowRam(uint8_t);
00209     // Graphic functions
00210 #ifdef  USE_GRAPHIC
00211 
00212     /** Draws a line between two points on the display
00213      * @param start column
00214      * @param start row
00215      * @param end column
00216      * @param end row
00217      * @param point on or off, off to erase, on to draw
00218      */
00219     void drawLine(unsigned char x1, unsigned char y1,
00220                   unsigned char x2, unsigned char y2, unsigned char c);
00221 
00222     /** Draw a rectangle given to top left and bottom right points
00223      * @param top left column
00224      * @param top left row
00225      * @param bottom right column
00226      * @param bottom right row
00227      * @param point on or off, off to erase, on to draw
00228      */                  
00229     void drawRectangle(unsigned char x1, unsigned char y1,
00230                        unsigned char x2, unsigned char y2, unsigned char c);
00231     /** Draw a filled rectangle given to top left and bottom right points
00232      * @param top left column
00233      * @param top left row
00234      * @param bottom right column
00235      * @param bottom right row
00236      * @param point on or off, off to erase, on to draw
00237      */                  
00238     void drawFilledRectangle(unsigned char x1, unsigned char y1,
00239                              unsigned char x2, unsigned char y2, unsigned char c);
00240     /** Draw a circle using Bresenham's algorithm.
00241      * Some small circles will look like squares!!
00242      * @param centre column
00243      * @param centre row
00244      * @param radius
00245      * @param point on or off, off to erase, on to draw
00246      */
00247     void drawCircle(unsigned char xc, unsigned char yc,
00248                     unsigned char r, unsigned char c);
00249 #endif
00250 
00251 private:
00252     /** Select a specific display, numbers as 0 to 3
00253      *
00254      * @param chip number
00255      */
00256     void chipselect( uint8_t );
00257     /** Deselect a specific display, numbers as 0 to 3
00258      *
00259      * @param chip number
00260      */
00261     void chipfree( uint8_t );
00262     
00263     /** Write bits to HT1632 on pins HT1632_DATA, HT1632_WRCLK
00264      * Chip is assumed to already be chip-selected
00265      * Bits are shifted out from MSB to LSB, with the first bit sent
00266      * being (bits & firstbit), shifted till firsbit is zero.
00267      * @param bits
00268      * @param firstbit
00269      */ 
00270     void writebits( uint8_t, uint8_t );
00271     
00272     /** Write databits to HT1632 on pins HT1632_DATA, HT1632_WRCLK
00273      * Chip is assumed to already be chip-selected
00274      * Bits are shifted out from LSB to MSB
00275      * @param bits
00276      * @param count
00277      */
00278     void writedatabits( uint8_t, uint8_t );
00279     
00280     /**  * Send a command to the ht1632 chip.
00281      * A command consists of a 3-bit "CMD" ID, an 8bit command, and
00282      * one "don't care bit".
00283      *   Select 1 0 0 c7 c6 c5 c4 c3 c2 c1 c0 xx Free
00284      * @param chipno the number of the chip/display to write command to
00285      * @param command to send
00286      */
00287     void sendcmd( uint8_t, uint8_t );
00288     
00289     /** Send a nibble (4 bits) of data to a particular memory location of the
00290      * ht1632.  The command has 3 bit ID, 7 bits of address, and 4 bits of data.
00291      *    Select 1 0 1 A6 A5 A4 A3 A2 A1 A0 D0 D1 D2 D3 Free
00292      * Note that the address is sent MSB first, while the data is sent LSB first!
00293      * This means that somewhere a bit reversal will have to be done to get
00294      * zero-based addressing of words and dots within words.
00295      * @param chipno
00296      * @param address
00297      * @param data
00298      */
00299     void senddata( uint8_t, uint8_t, uint8_t );
00300 
00301     /** Send a byte of data to a particular memory location of the
00302      * ht1632.  The command has 3 bit ID, 7 bits of address, and 8 bits of data.
00303      *    Select 1 0 1 A6 A5 A4 A3 A2 A1 A0 D0 D1 D2 D3 D4 D5 D6 D7 D8 Free
00304      * Note that the address is sent MSB first, while the data is sent LSB first!
00305      * This means that somewhere a bit reversal will have to be done to get
00306      * zero-based addressing of words and dots within words.
00307      * @param chipno
00308      * @param address
00309      * @param data
00310      */
00311     void sendcol( uint8_t, uint8_t, uint8_t );
00312     
00313     DigitalOut _wrclk;
00314     DigitalOut _data;
00315     DigitalOut _cs1;
00316     DigitalOut _cs2;
00317     DigitalOut _cs3;
00318     DigitalOut _cs4;
00319     
00320 //protected:
00321     virtual int _putc(int value);
00322     virtual int _getc();
00323 
00324 };
00325 
00326 #endif //_HT1632_LEDMATRIX_H