Wim Huiskamp / PT6964

Dependents:   mbed_PT6964

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PT6964.h Source File

PT6964.h

00001 /* mbed PT6964 Library, for PT6964 LED controller
00002  * Copyright (c) 2015, v01: WH, Initial version
00003  *               2015, v02: WH, rename Digit/Grid 
00004  *               2016, v03: WH, updated Icon handling, UDCs and _putc()  
00005  *               2016, v04: WH, Refactored display and keyboard defines   
00006  *
00007  * Permission is hereby granted, free of charge, to any person obtaining a copy
00008  * of this software and associated documentation files (the "Software"), to deal
00009  * in the Software without restriction, including without limitation the rights
00010  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00011  * copies of the Software, and to permit persons to whom the Software is
00012  * furnished to do so, subject to the following conditions:
00013  *
00014  * The above copyright notice and this permission notice shall be included in
00015  * all copies or substantial portions of the Software.
00016  *
00017  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00018  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00019  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00020  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00021  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00022  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00023  * THE SOFTWARE.
00024  */
00025 
00026 #ifndef PT6964_H
00027 #define PT6964_H
00028 
00029 #include "Font_7Seg.h"
00030 
00031 /** An interface for driving PT6964 LED controller
00032  *
00033  * @code
00034  * #include "mbed.h"
00035  * #include "PT6964.h" 
00036  * 
00037  * DisplayData_t size is  8 bytes (4 grids @ 13 segments) OR 10 bytes (5 grids @ 12 segments) OR
00038  *                       12 bytes (6 grids @ 11 segments) OR 14 bytes (7 grids @ 10 segments) 
00039  * PT6964::DisplayData_t mbed_str = {0xDA,0x00, 0x7C,0x00, 0x3C,0x01, 0xF6,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00};  
00040  * PT6964::DisplayData_t all_str  = {0xFF,0x0F, 0xFF,0x0F, 0xFF,0x0F, 0xFF,0x0F, 0xFF,0x0F, 0xFF,0x0F, 0xFF,0x0F};  
00041  *
00042  * // KeyData_t size is 5 bytes  
00043  * PT6964::KeyData_t keydata; 
00044  *
00045  * // PT6964 declaration, Default setting 7 Grids @ 10 Segments
00046  * PT6964 PT6964(p5,p6,p7, p8);
00047  *
00048  * int main() {
00049  *   PT6964.cls(); 
00050  *   PT6964.writeData(all_str);
00051  *   wait(4);
00052  *   PT6964.writeData(mbed_str);    
00053  *   wait(1);
00054  *   PT6964.setBrightness(PT6964_BRT0);
00055  *   wait(1);
00056  *   PT6964.setBrightness(PT6964_BRT3);
00057  *
00058  *   while (1) {
00059  *    // Check and read keydata
00060  *    if (PT6964.getKeys(&keydata)) {
00061  *      pc.printf("Keydata 0..4 = 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\r\n", keydata[0], keydata[1], keydata[2], keydata[3], keydata[4]);
00062  *
00063  *      if (keydata[0] == 0x10) { //sw2   
00064  *        PT6964.cls(); 
00065  *        PT6964.writeData(all_str);
00066  *      }  
00067  *    } 
00068  *   }   
00069  * }
00070  * @endcode
00071  */
00072 
00073 //PT6964 Display and Keymatrix data
00074 #define PT6964_MAX_NR_GRIDS    7
00075 #define PT6964_BYTES_PER_GRID  2
00076 //Significant bits Keymatrix data
00077 #define PT6964_KEY_MSK      0x1B 
00078 
00079 //Memory size in bytes for Display and Keymatrix
00080 #define PT6964_DISPLAY_MEM  (PT6964_MAX_NR_GRIDS * PT6964_BYTES_PER_GRID)
00081 #define PT6964_KEY_MEM         5
00082 
00083 //Reserved bits for commands
00084 #define PT6964_CMD_MSK      0xC0
00085 
00086 //Mode setting command
00087 #define PT6964_MODE_SET_CMD 0x00
00088 #define PT6964_GR4_SEG13    0x00
00089 #define PT6964_GR5_SEG12    0x01
00090 #define PT6964_GR6_SEG11    0x02
00091 #define PT6964_GR7_SEG10    0x03 //default
00092 
00093 //Data setting commands
00094 #define PT6964_DATA_SET_CMD 0x40
00095 #define PT6964_DATA_WR      0x00
00096 #define PT6964_KEY_RD       0x02
00097 #define PT6964_ADDR_INC     0x00
00098 #define PT6964_ADDR_FIXED   0x04
00099 #define PT6964_MODE_NORM    0x00
00100 #define PT6964_MODE_TEST    0x08
00101 
00102 //Address setting commands
00103 #define PT6964_ADDR_SET_CMD 0xC0
00104 #define PT6964_ADDR_MSK     0x0F
00105 
00106 //Display control commands
00107 #define PT6964_DSP_CTRL_CMD 0x80
00108 #define PT6964_BRT_MSK      0x07
00109 #define PT6964_BRT0         0x00 //Pulsewidth 1/16
00110 #define PT6964_BRT1         0x01
00111 #define PT6964_BRT2         0x02
00112 #define PT6964_BRT3         0x03
00113 #define PT6964_BRT4         0x04
00114 #define PT6964_BRT5         0x05
00115 #define PT6964_BRT6         0x06
00116 #define PT6964_BRT7         0x07 //Pulsewidth 14/16
00117 
00118 #define PT6964_BRT_DEF      PT6964_BRT3
00119 
00120 #define PT6964_DSP_OFF      0x00
00121 #define PT6964_DSP_ON       0x08
00122 
00123 
00124 /** A class for driving Princeton PT6964 LED controller
00125  *  Note: the PT6964 is also available from other chipvendors eg AIP1628, HT1628, CM1628, SM1628
00126  *
00127  * @brief Supports 4 Grids @ 13 Segments or 5 Grids @ 12 Segments or 6 Grids @ 11 Segments or 7 Grids @ 10 Segments. 
00128  *        Also supports a scanned keyboard of upto 20 keys.
00129  *        SPI bus interface device. 
00130  */
00131 class PT6964 {
00132  public:
00133 
00134   /** Enums for display mode */
00135   enum Mode {
00136     Grid4_Seg13 = PT6964_GR4_SEG13,
00137     Grid5_Seg12 = PT6964_GR5_SEG12,   
00138     Grid6_Seg11 = PT6964_GR6_SEG11,
00139     Grid7_Seg10 = PT6964_GR7_SEG10
00140   };
00141   
00142   /** Datatypes for display and keymatrix data */
00143   typedef char DisplayData_t[PT6964_DISPLAY_MEM];
00144   typedef char KeyData_t[PT6964_KEY_MEM];
00145     
00146  /** Constructor for class for driving Princeton PT6964 LED controller
00147   *
00148   * @brief Supports 4 Grids @ 13 segments or 5 Grids @ 12 segments or 6 Grids @ 11 Segments or 7 Grids @ 10 Segments. 
00149   *        Also supports a scanned keyboard of upto 20 keys.
00150   *        SPI bus interface device. 
00151   *
00152   *  @param  PinName mosi, miso, sclk, cs SPI bus pins
00153   *  @param  Mode selects either Grids/Segments (default 7 Grids @ 10 Segments)
00154   */
00155   PT6964(PinName mosi, PinName miso, PinName sclk, PinName cs, Mode mode=Grid7_Seg10);
00156       
00157   /** Clear the screen and locate to 0
00158    */ 
00159   void cls();  
00160 
00161   /** Write databyte to PT6964
00162    *  @param  int address display memory location to write byte
00163    *  @param  char data byte written at given address
00164    *  @return none
00165    */ 
00166   void writeData(int address, char data); 
00167  
00168  /** Write Display datablock to PT6964
00169    *  @param  DisplayData_t data Array of PT6964_DISPLAY_MEM (=14) bytes for displaydata (starting at address 0)
00170    *  @param  length number bytes to write (valid range 0..PT6964_DISPLAY_MEM (=14), starting at address 0)   
00171    *  @return none
00172    */   
00173   void writeData(DisplayData_t data, int length = PT6964_DISPLAY_MEM);
00174 
00175   /** Read keydata block from PT6964
00176    *  @param  *keydata Ptr to Array of PT6964_KEY_MEM (=5) bytes for keydata
00177    *  @return bool keypress True when at least one key was pressed
00178    *
00179    * Note: Due to the hardware configuration the PT6964 key matrix scanner will detect multiple keys pressed at same time,
00180    *       but this may result in some spurious keys also being set in keypress data array.
00181    *       It may be best to ignore all keys in those situations. That option is implemented in this method depending on #define setting.
00182    */   
00183   bool getKeys(KeyData_t *keydata);
00184 
00185   /** Set Brightness
00186     *
00187     * @param  char brightness (3 significant bits, valid range 0..7 (1/16 .. 14/14 dutycycle)  
00188     * @return none
00189     */
00190   void setBrightness(char brightness = PT6964_BRT_DEF);
00191   
00192   /** Set the Display mode On/off
00193     *
00194     * @param bool display mode
00195     */
00196   void setDisplay(bool on);
00197   
00198  private:  
00199   SPI _spi;
00200   DigitalOut _cs;
00201   Mode _mode;
00202   char _display;
00203   char _bright; 
00204   
00205   /** Init the SPI interface and the controller
00206     * @param  none
00207     * @return none
00208     */ 
00209   void _init();
00210 
00211   /** Helper to reverse all command or databits. The PT6964 expects LSB first, whereas SPI is MSB first
00212     *  @param  char data
00213     *  @return bitreversed data
00214     */ 
00215   char _flip(char data);
00216 
00217   /** Write command and parameter to PT6964
00218     *  @param  int cmd Command byte
00219     *  &Param  int data Parameters for command
00220     *  @return none
00221     */ 
00222   void _writeCmd(int cmd, int data);  
00223 };
00224 
00225 
00226 
00227 // Derived class for PT6964 used in DVD-538A front display unit
00228 //
00229 
00230 #define DVD538A_NR_GRIDS  5
00231 #define DVD538A_NR_DIGITS 4
00232 #define DVD538A_NR_UDC    8
00233 
00234 /** Constructor for class for driving Princeton PT6964 controller as used in DVD538A
00235   *
00236   *  @brief Supports 4 Digits of 7 Segments, 1 Grid of 9 Icons. Also supports a scanned keyboard of 4 keys.
00237   *  
00238   *  @param  PinName mosi, miso, sclk, cs SPI bus pins
00239   */
00240 class PT6964_DVD538A : public PT6964, public Stream {
00241  public:
00242 
00243   /** Enums for Icons */
00244   //  Grid encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
00245   enum Icon {
00246     LD1  = (1<<24) | S7_LD1,
00247     LD2  = (1<<24) | S7_LD2,
00248     CD   = (1<<24) | S7_CD,
00249     DVD  = (1<<24) | S7_DVD,
00250     PSE  = (1<<24) | S7_PSE,
00251     PLY  = (1<<24) | S7_PLY,    
00252     COL2 = (1<<24) | S7_COL2,
00253     MP4  = (1<<24) | S7_MP4,
00254     MP3  = (1<<24) | S7_MP3    
00255   };
00256   
00257   typedef short UDCData_t[DVD538A_NR_UDC];
00258   
00259  /** Constructor for class for driving Princeton PT6964 VFD controller as used in DVD538A
00260    *
00261    * @brief Supports 4 Digits of 7 Segments, 1 Grid of 9 Icons. Also supports a scanned keyboard of 4 keys.
00262    *  
00263    * @param  PinName mosi, miso, sclk, cs SPI bus pins
00264    */
00265   PT6964_DVD538A(PinName mosi, PinName miso, PinName sclk, PinName cs);
00266 
00267 #if DOXYGEN_ONLY
00268     /** Write a character to the Display
00269      *
00270      * @param c The character to write to the display
00271      */
00272     int putc(int c);
00273 
00274     /** Write a formatted string to the Display
00275      *
00276      * @param format A printf-style format string, followed by the
00277      *               variables to use in formatting the string.
00278      */
00279     int printf(const char* format, ...);   
00280 #endif
00281 
00282      /** Locate cursor to a screen column
00283      *
00284      * @param column  The horizontal position from the left, indexed from 0
00285      */
00286     void locate(int column);
00287     
00288     /** Clear the screen and locate to 0
00289      * @param bool clrAll Clear Icons also (default = false)
00290      */
00291     void cls(bool clrAll = false);   
00292 
00293     /** Set Icon
00294      *
00295      * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
00296      * @return none
00297      */
00298     void setIcon(Icon icon);
00299 
00300     /** Clr Icon
00301      *
00302      * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
00303      * @return none
00304      */
00305     void clrIcon(Icon icon);
00306 
00307    /** Set User Defined Characters (UDC)
00308      *
00309      * @param unsigned char udc_idx   The Index of the UDC (0..7)
00310      * @param int udc_data            The bitpattern for the UDC (16 bits)       
00311      */
00312     void setUDC(unsigned char udc_idx, int udc_data);
00313 
00314 
00315    /** Number of screen columns
00316     *
00317     * @param none
00318     * @return columns
00319     */
00320     int columns();   
00321 
00322    /** Write Display datablock to PT6964
00323     *  @param  DisplayData_t data Array of PT6964_DISPLAY_MEM (=14) bytes for displaydata (starting at address 0)
00324     *  @param  length number bytes to write (valid range 0..(DVD538A_NR_GRIDS*2) (=14), starting at address 0)   
00325     *  @return none
00326     */   
00327     void writeData(DisplayData_t data, int length = (DVD538A_NR_GRIDS*2)) {
00328       PT6964::writeData(data, length);
00329     }  
00330 
00331 protected:  
00332     // Stream implementation functions
00333     virtual int _putc(int value);
00334     virtual int _getc();
00335 
00336 private:
00337     int _column;
00338     int _columns;   
00339     
00340     DisplayData_t _displaybuffer;
00341     UDCData_t _UDC_7S; 
00342 };
00343 
00344 
00345 #endif