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

Dependents:   HT1632MsgScroller SMS_LEDMatrixPrinter

Files at this revision

API Documentation at this revision

Comitter:
SomeRandomBloke
Date:
Wed Nov 28 14:03:35 2012 +0000
Parent:
8:61130f9b5b79
Child:
10:af973a9c48b2
Commit message:
Added documentation

Changed in this revision

HT1632_LedMatrix.cpp Show annotated file Show diff for this revision Revisions of this file
HT1632_LedMatrix.h Show annotated file Show diff for this revision Revisions of this file
font_5x7_p.h Show annotated file Show diff for this revision Revisions of this file
--- a/HT1632_LedMatrix.cpp	Sat Nov 24 21:58:03 2012 +0000
+++ b/HT1632_LedMatrix.cpp	Wed Nov 28 14:03:35 2012 +0000
@@ -75,6 +75,8 @@
 uint8_t numYDevices = 1;   // Number of vertical devices
 uint8_t xMax = 32 * numXDevices-1;
 uint8_t yMax = 8 * numYDevices-1;
+uint8_t displayWidth = 32;
+uint8_t displayHeight = 8;
 
 // Variables used to keep track of cursor position
 int cursorX = 0;
@@ -109,15 +111,24 @@
 }
 
 
-void HT1632_LedMatrix::init( uint8_t xDevices, uint8_t yDevices )
+void HT1632_LedMatrix::init( uint8_t xDevices, uint8_t yDevices, uint8_t displayType )
 {
     // Set up the display size based on number of devices both horizontal and vertical
+    
     numXDevices = xDevices;
-    xMax = 32 * numXDevices-1;
     numYDevices = yDevices;
-    yMax = 8 * numYDevices-1;
     numDevices = numXDevices * numYDevices;
 
+    if( displayType == HT1632_24x16 ) {
+        displayWidth = 24;
+        displayHeight = 16;
+    } else {
+        displayWidth = 32;
+        displayHeight = 8;
+    }    
+    xMax = displayWidth * numXDevices-1;
+    yMax = displayHeight * numYDevices-1;
+
     // Disable all display CS lines by taking high
     for( uint8_t i = 0; i < 4; i++ )
         ht1632_cs[i] = HIGH;
@@ -129,8 +140,13 @@
         sendcmd(chipno, HT1632_CMD_LEDON);    // LEDs on 
         sendcmd(chipno, HT1632_CMD_BLOFF);    // Blink Off 
         sendcmd(chipno, HT1632_CMD_MSTMD);    // Master Mode 
-        sendcmd(chipno, HT1632_CMD_RCCLK);    // Internal Oscillator 
-        sendcmd(chipno, HT1632_CMD_COMS00);  // 08*32, NMOS drivers
+        sendcmd(chipno, HT1632_CMD_RCCLK);    // Internal Oscillator
+        if( displayType == HT1632_24x16 ) {
+// TODO - check
+            sendcmd(chipno, HT1632_CMD_COMS10);  // 16*24, NMOS drivers
+        } else {    
+            sendcmd(chipno, HT1632_CMD_COMS00);  // 08*32, NMOS drivers
+        }
         sendcmd(chipno, HT1632_CMD_PWM | 0x0c);   // PWM Duty
 
         for (uint8_t i=0; i<96; i++)
@@ -176,9 +192,9 @@
     ht1632_cs[chipno] = HIGH;
 }
 
-/*
+/**
  * writebits
- * Write bits to h1632 on pins HT1632_DATA, HT1632_WRCLK
+ * Write bits to HT1632 on pins HT1632_DATA, HT1632_WRCLK
  * Chip is assumed to already be chip-selected
  * Bits are shifted out from MSB to LSB, with the first bit sent
  * being (bits & firstbit), shifted till firsbit is zero.
@@ -199,7 +215,7 @@
 
 /*
  * writedatabits
- * Write databits to h1632 on pins HT1632_DATA, HT1632_WRCLK
+ * Write databits to HT1632 on pins HT1632_DATA, HT1632_WRCLK
  * Chip is assumed to already be chip-selected
  * Bits are shifted out from LSB to MSB
  */
@@ -245,7 +261,7 @@
         chipselect(chipno);  // Select chip
         writebits(HT1632_ID_WR, 0x04);   // send ID: WRITE to RAM
         writebits(0, 0x40);              // Send address
-        for (i = 0; i < 32; i++)         // Clear entire display
+        for (i = 0; i < displayWidth; i++)         // Clear entire display
             writedatabits(0, 8);         // send 8 bits of data
         chipfree(chipno); // done
         for (i=0; i < 64; i++)
@@ -313,7 +329,7 @@
 }
 
 /*
- * Copy a character glyph from the smallFont data structure to
+ * Copy a character glyph from the font data structure to
  * display memory, with its upper left at the given coordinate
  * This is unoptimized and simply uses plot() to draw each dot.
  */
@@ -323,7 +339,7 @@
 }
 
 /*
- * Copy a character glyph from the myfont data structure to
+ * Copy a character glyph from the font data structure to
  * display memory, with its upper left at the given coordinate
  * This is unoptimized and simply uses plot() to draw each dot.
  * returns number of columns that didn't fit
@@ -358,7 +374,7 @@
             chipno = chip_number(x,y);
             addr = chip_byte_address(x,y); // compute which memory byte this is in
             if (x <= xMax && y <= yMax) {
-                shadowram[(addr>>1)+32*chipno] = colData;
+                shadowram[(addr>>1) + displayWidth * chipno] = colData;
                 sendcol(chipno,addr,colData);
                 x++;
             } else {
@@ -375,7 +391,7 @@
             chipno = chip_number(x,y);
             addr = chip_byte_address(x,y); // compute which memory byte this is in
             if (x <= xMax && y <= yMax) {
-                shadowram[(addr>>1)+32*chipno] = colData;
+                shadowram[(addr>>1) + displayWidth * chipno] = colData;
                 sendcol(chipno,addr,colData);
                 x++;
             } else {
@@ -435,12 +451,12 @@
     char bitval = 1<<(y&7);  // compute which bit will need set
 
     if (val) {  // Modify the shadow memory
-        shadowram[shadowAddress +32*chipno] |= bitval;
+        shadowram[shadowAddress + displayWidth * chipno] |= bitval;
     } else {
-        shadowram[shadowAddress +32*chipno] &= ~bitval;
+        shadowram[shadowAddress + displayWidth * chipno] &= ~bitval;
     }
     // Now copy the new memory value to the display
-    sendcol(chipno, addr, shadowram[shadowAddress +32*chipno]);
+    sendcol(chipno, addr, shadowram[shadowAddress + displayWidth * chipno]);
 }
 
 
@@ -493,7 +509,7 @@
 void HT1632_LedMatrix::putShadowRam(uint8_t chipno)
 {
     for (int i=0; i<64; i+=2) {
-        sendcol(chipno,i,shadowram[(i>>1)+32*chipno]);
+        sendcol(chipno,i,shadowram[(i>>1)+ displayWidth * chipno]);
     }
 }
 
--- a/HT1632_LedMatrix.h	Sat Nov 24 21:58:03 2012 +0000
+++ b/HT1632_LedMatrix.h	Wed Nov 28 14:03:35 2012 +0000
@@ -45,10 +45,14 @@
 #ifndef _HT1632_LEDMATRIX_H
 #define _HT1632_LEDMATRIX_H
 
+#include <inttypes.h>
+
 // To include the graphic functions use the following. Comment out to exclude them 
 #define USE_GRAPHIC
 
-#include <inttypes.h>
+// Defines for display sizes
+#define HT1632_32x08  1 
+#define HT1632_24x16  2
 
 /*
  * commands written to the chip consist of a 3 bit "ID", followed by
@@ -79,53 +83,226 @@
 
 /** class HT1632_LedMatrix
 */
-class HT1632_LedMatrix  //: public Print
+class HT1632_LedMatrix
 {
 private:
-    /** Select a specific display
+    /** Select a specific display, numbers as 0 to 3
      *
      * @param chip number
      */
     void chipselect( uint8_t );
+    /** Deselect a specific display, numbers as 0 to 3
+     *
+     * @param chip number
+     */
     void chipfree( uint8_t );
+    
+    /** Write bits to HT1632 on pins HT1632_DATA, HT1632_WRCLK
+     * Chip is assumed to already be chip-selected
+     * Bits are shifted out from MSB to LSB, with the first bit sent
+     * being (bits & firstbit), shifted till firsbit is zero.
+     * @param bits
+     * @param firstbit
+     */ 
     void writebits( uint8_t, uint8_t );
+    
+    /** Write databits to HT1632 on pins HT1632_DATA, HT1632_WRCLK
+     * Chip is assumed to already be chip-selected
+     * Bits are shifted out from LSB to MSB
+     * @param bits
+     * @param count
+     */
     void writedatabits( uint8_t, uint8_t );
+    
+    /**  * Send a command to the ht1632 chip.
+     * A command consists of a 3-bit "CMD" ID, an 8bit command, and
+     * one "don't care bit".
+     *   Select 1 0 0 c7 c6 c5 c4 c3 c2 c1 c0 xx Free
+     * @param chipno the number of the chip/display to write command to
+     * @param command to send
+     */
     void sendcmd( uint8_t, uint8_t );
+    
+    /** Send a nibble (4 bits) of data to a particular memory location of the
+     * ht1632.  The command has 3 bit ID, 7 bits of address, and 4 bits of data.
+     *    Select 1 0 1 A6 A5 A4 A3 A2 A1 A0 D0 D1 D2 D3 Free
+     * Note that the address is sent MSB first, while the data is sent LSB first!
+     * This means that somewhere a bit reversal will have to be done to get
+     * zero-based addressing of words and dots within words.
+     * @param chipno
+     * @param address
+     * @param data
+     */
     void senddata( uint8_t, uint8_t, uint8_t );
+
+    /** Send a byte of data to a particular memory location of the
+     * ht1632.  The command has 3 bit ID, 7 bits of address, and 8 bits of data.
+     *    Select 1 0 1 A6 A5 A4 A3 A2 A1 A0 D0 D1 D2 D3 D4 D5 D6 D7 D8 Free
+     * Note that the address is sent MSB first, while the data is sent LSB first!
+     * This means that somewhere a bit reversal will have to be done to get
+     * zero-based addressing of words and dots within words.
+     * @param chipno
+     * @param address
+     * @param data
+     */
     void sendcol( uint8_t, uint8_t, uint8_t );
 
 public:
-    /** Default Constructor
+    /** Default Constructor for the display driver
      */
     HT1632_LedMatrix( );
 
-    // Init/Clear/position functions
-    void init( uint8_t, uint8_t );
-    void displayOff( void ) ;
-    void displayOn( void ) ;
+    /** Initialise the library with the display configuration
+     * @param Number of horizontal displays, max 4
+     * @param Number of vertical displays, max 4
+     * @param Type of displays being used, either 32x8 or 24x16
+     */
+    void init( uint8_t, uint8_t, uint8_t displayType = HT1632_32x08 );
+    
+    /** Turn off the display
+     */
+    void displayOff( void );
+    
+    /** Turn on the display
+     */
+    void displayOn( void );
+    
+    /** Clear the display, and the shadow memory, and the snapshot
+     * memory.  This uses the "write multiple words" capability of
+     * the chipset by writing all 96 words of memory without raising
+     * the chipselect signal.
+     */
     void clear(void);
+    
+    /** Set display brightness. Brighness is from 0 to 15
+     * @param brightness value, range x-y
+     */
     void setBrightness( unsigned char );
+    
+    /** Copy a character glyph from the font data structure to
+     * display memory, with its upper left at the given coordinate
+     * This is unoptimized and simply uses plot() to draw each dot.
+     * returns number of columns that didn't fit
+     * @param column
+     * @param row
+     * @param character to write
+     * @returns the number of columns that couldnt be displayed on current matrix
+     */
     uint8_t putChar( int, int, char );
+
+    /** Copy a character glyph from the font data structure to
+     * display memory, with its upper left at the given coordinate
+     * This is a wrapper for putChar that uses current cursor position
+     */
     void write( uint8_t );
+
+    /** Write a string at the position specified
+     * x and y start from 0 and count number of pixels, 2nd row on a 2 row display is y=8
+     * @param x position
+     * @param y position
+     * @param pointer to character data to display
+     */
     void putString( int, int, char* );
+    
+    /** Plot a point on the display, with the upper left hand corner
+     * being (0,0), and the lower right hand corner being (xMax-1, yMax-1).
+     * Note that Y increases going "downward" in contrast with most
+     * mathematical coordiate systems, but in common with many displays
+     * basic bounds checking used.
+     * @param column
+     * @param row
+     * @param point on or off
+     */
     void plot( int, int, char );
+    
+    /** Set cursor position
+     * @param x or column position, 0 is to left
+     * @param y or row position, 0 is at top
+     */
     void gotoXY(int , int);
+    
+    /** Return the current cursor position
+     * @param column pointer
+     * @param row pointer
+     */
     void getXY(int* , int*);
+
+    /** Return the maximum size of the display
+     * @param column pointer
+     * @param row pointer
+     */
     void getXYMax(int*, int*);
+    
+    /** Shift cursor X position a number of positions either left or right.
+     * @param increment, -1 for one position left, 1 for right
+     */
     void shiftCursorX(int );
+    
+    /** Create a custom character. 8 character slots are availabe.
+     * @param character number, 0 to 7
+     * @param array of character bit maps
+     */
     void setCustomChar( int, unsigned char[]);
+    
+    /** Create a custom character. 8 character slots are availabe.
+     * @param character number, 0 to 7
+     * @param array of character bit maps
+     * @param width of chaaracter in columns
+     */
     void setCustomChar( int, unsigned char[], uint8_t );
+    
+    /** Scroll row to the left
+     * @param number fo columns to scroll
+     * @param row number to scroll, used when displays are stacked vertically or when more than 8 high displays are used
+     */
     void scrollLeft(uint8_t, uint8_t);
+    
+    /** Write shaddow ram to display
+     */
     void putShadowRam();
+    
+    /** Write shadow ram to a specific dasipaly board
+     * @param chip or display number
+     */
     void putShadowRam(uint8_t);
     // Graphic functions
 #ifdef  USE_GRAPHIC
+
+    /** Draws a line between two points on the display
+     * @param start column
+     * @param start row
+     * @param end column
+     * @param end row
+     * @param point on or off, off to erase, on to draw
+     */
     void drawLine(unsigned char x1, unsigned char y1,
                   unsigned char x2, unsigned char y2, unsigned char c);
+
+    /** Draw a rectangle given to top left and bottom right points
+     * @param top left column
+     * @param top left row
+     * @param bottom right column
+     * @param bottom right row
+     * @param point on or off, off to erase, on to draw
+     */                  
     void drawRectangle(unsigned char x1, unsigned char y1,
                        unsigned char x2, unsigned char y2, unsigned char c);
+    /** Draw a filled rectangle given to top left and bottom right points
+     * @param top left column
+     * @param top left row
+     * @param bottom right column
+     * @param bottom right row
+     * @param point on or off, off to erase, on to draw
+     */                  
     void drawFilledRectangle(unsigned char x1, unsigned char y1,
                              unsigned char x2, unsigned char y2, unsigned char c);
+    /** Draw a circle using Bresenham's algorithm.
+     * Some small circles will look like squares!!
+     * @param centre column
+     * @param centre row
+     * @param radius
+     * @param point on or off, off to erase, on to draw
+     */
     void drawCircle(unsigned char xc, unsigned char yc,
                     unsigned char r, unsigned char c);
 #endif
--- a/font_5x7_p.h	Sat Nov 24 21:58:03 2012 +0000
+++ b/font_5x7_p.h	Wed Nov 28 14:03:35 2012 +0000
@@ -1,5 +1,4 @@
-// Font definition for LCD 3110 library
-// 5 x 7 font
+// Font definition for basic proportinal 5x7 font
 // 1 pixel space at left and bottom
 // index = ASCII - 32