Wim Huiskamp / PT6311

Dependents:   mbed_PT6311

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PT6311.h Source File

PT6311.h

00001 /* mbed PT6311 Library, for Princeton PT6311 VFD controller
00002  * Copyright (c) 2016, v01: WH, Initial version for VFDEM2
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, including without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020  * THE SOFTWARE.
00021  */
00022 
00023 #ifndef PT6311_H
00024 #define PT6311_H
00025 
00026 // Select one of the testboards for Princeton PT6311 VFD controller
00027 #include "PT6311_Config.h"
00028 
00029 /** An interface for driving Princeton PT6311 VFD controller
00030  *
00031  * @code
00032  *
00033  * #if (PT6311_TEST == 1)  
00034  * // Direct driving of PT6311 Test
00035  *
00036  * #include "mbed.h"
00037  * #include "PT6311.h" 
00038  * 
00039  * DisplayData_t size is 24 bytes (8 Grids @ 20 Segments) ... 48 bytes (16 Grids @ 12 Segments) 
00040  * DisplayData_t size default is 48 bytes (16 Grids @ 12 Segments) 
00041  * PT6311::DisplayData_t all_str  = {0xFF,0x0F,0x00 0xFF,0x0F,0x00, 0xFF,0x0F,0x00, 0xFF,0x0F,0x00, 0xFF,0x0F,0x00, 0xFF,0x0F,0x00, 0xFF,0x0F,0x00, 0xFF,0x0F,0x00,
00042  *                                   0xFF,0x0F,0x00 0xFF,0x0F,0x00, 0xFF,0x0F,0x00, 0xFF,0x0F,0x00, 0xFF,0x0F,0x00, 0xFF,0x0F,0x00, 0xFF,0x0F,0x00, 0xFF,0x0F,0x00};  
00043  *
00044  * // KeyData_t size is 6 bytes  
00045  * PT6311::KeyData_t keydata; 
00046  *
00047  * // PT6311 declaration, Default setting 16 Grids @ 12 Segments
00048  * PT6311 PT6311(p5,p6,p7, p8);
00049  *
00050  * int main() {
00051  *   PT6311.cls(); 
00052  *   PT6311.writeData(all_str);
00053  *   wait(4);
00054  *   PT6311.setBrightness(PT6311_BRT0);
00055  *   wait(1);
00056  *   PT6311.setBrightness(PT6311_BRT3);
00057  *
00058  *   while (1) {
00059  *    // Check and read keydata
00060  *    if (PT6311.getKeys(&keydata)) {
00061  *      pc.printf("Keydata 0..5 = 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\r\n", keydata[0], keydata[1], keydata[2], keydata[3], keydata[4], keydata[5]);
00062  *
00063  *      if (keydata[0] == 0x10) { //sw2   
00064  *        PT6311.cls(); 
00065  *        wait(1);
00066  *        PT6311.writeData(all_str);
00067  *      }  
00068  *    } 
00069  *   }   
00070  * }
00071  * #endif
00072  *
00073  * @endcode
00074  */
00075 
00076 //PT6311 Display and Keymatrix data
00077 #define PT6311_MAX_NR_GRIDS   16
00078 #define PT6311_BYTES_PER_GRID  3
00079 //Significant bits Keymatrix data
00080 #define PT6311_KEY_MSK      0xFF 
00081 
00082 //Memory size in bytes for Display and Keymatrix
00083 #define PT6311_DISPLAY_MEM  (PT6311_MAX_NR_GRIDS * PT6311_BYTES_PER_GRID)
00084 #define PT6311_KEY_MEM         6
00085 
00086 //Reserved bits for commands
00087 #define PT6311_CMD_MSK      0xC0
00088 
00089 //Mode setting command
00090 #define PT6311_MODE_SET_CMD 0x00
00091 #define PT6311_GR8_SEG20    0x00
00092 #define PT6311_GR9_SEG19    0x08
00093 #define PT6311_GR10_SEG18   0x09
00094 #define PT6311_GR11_SEG17   0x0A
00095 #define PT6311_GR12_SEG16   0x0B
00096 #define PT6311_GR13_SEG15   0x0C
00097 #define PT6311_GR14_SEG14   0x0D
00098 #define PT6311_GR15_SEG13   0x0E
00099 #define PT6311_GR16_SEG12   0x0F //default
00100 
00101 //Data setting commands
00102 #define PT6311_DATA_SET_CMD 0x40
00103 #define PT6311_DATA_WR      0x00
00104 #define PT6311_LED_WR       0x01
00105 #define PT6311_KEY_RD       0x02
00106 #define PT6311_SW_RD        0x03
00107 #define PT6311_ADDR_INC     0x00
00108 #define PT6311_ADDR_FIXED   0x04
00109 #define PT6311_MODE_NORM    0x00
00110 #define PT6311_MODE_TEST    0x08
00111 
00112 //LED settings data
00113 #define PT6311_LED_MSK      0x1F
00114 #define PT6311_LED1         0x01
00115 #define PT6311_LED2         0x02
00116 #define PT6311_LED3         0x04
00117 #define PT6311_LED4         0x08
00118 #define PT6311_LED5         0x10
00119 
00120 //Switch settings data
00121 #define PT6311_SW_MSK       0x0F
00122 #define PT6311_SW1          0x01
00123 #define PT6311_SW2          0x02
00124 #define PT6311_SW3          0x04
00125 #define PT6311_SW4          0x08
00126 
00127 //Address setting commands
00128 #define PT6311_ADDR_SET_CMD 0xC0
00129 #define PT6311_ADDR_MSK     0x3F
00130 
00131 //Display control commands
00132 #define PT6311_DSP_CTRL_CMD 0x80
00133 #define PT6311_BRT_MSK      0x07
00134 #define PT6311_BRT0         0x00 //Pulsewidth 1/16, Default
00135 #define PT6311_BRT1         0x01
00136 #define PT6311_BRT2         0x02
00137 #define PT6311_BRT3         0x03
00138 #define PT6311_BRT4         0x04
00139 #define PT6311_BRT5         0x05
00140 #define PT6311_BRT6         0x06
00141 #define PT6311_BRT7         0x07 //Pulsewidth 14/16
00142 
00143 #define PT6311_BRT_DEF      PT6311_BRT3
00144 
00145 #define PT6311_DSP_OFF      0x00 //Default
00146 #define PT6311_DSP_ON       0x08
00147 
00148 
00149 /** A class for driving Princeton PT6311 VFD controller
00150  *
00151  * @brief Supports 8 Grids of 20 Segments upto 16 Grids of 12 Segments. Also supports a scanned keyboard of upto 48 keys, 4 switches and 5 LEDs.
00152  *        SPI bus interface device. 
00153  */
00154 class PT6311 {
00155  public:
00156 
00157   /** Enums for display mode */
00158   enum Mode {
00159     Grid8_Seg20  = PT6311_GR8_SEG20,
00160     Grid9_Seg19  = PT6311_GR9_SEG19,
00161     Grid10_Seg18 = PT6311_GR10_SEG18,
00162     Grid11_Seg17 = PT6311_GR11_SEG17,
00163     Grid12_Seg16 = PT6311_GR12_SEG16,
00164     Grid13_Seg15 = PT6311_GR13_SEG15,
00165     Grid14_Seg14 = PT6311_GR14_SEG14,
00166     Grid15_Seg13 = PT6311_GR15_SEG13,   
00167     Grid16_Seg12 = PT6311_GR16_SEG12   
00168   };
00169  
00170   /** Datatypes for display and keymatrix data */
00171   typedef char DisplayData_t[PT6311_DISPLAY_MEM];
00172   typedef char KeyData_t[PT6311_KEY_MEM];
00173     
00174  /** Constructor for class for driving Princeton PT6311 VFD controller
00175   *
00176   *  @brief Supports 8 Grids of 20 Segments upto 16 Grids of 12 Segments. Also supports a scanned keyboard of upto 48 keys, 4 switches and 5 LEDs.
00177   *         SPI bus interface device. 
00178   *  @param  PinName mosi, miso, sclk, cs SPI bus pins
00179   *  @param  Mode selects either number of Grids and Segments (default 16 Grids, 12 Segments)
00180   */
00181   PT6311(PinName mosi, PinName miso, PinName sclk, PinName cs, Mode mode=Grid16_Seg12);
00182       
00183   /** Clear the screen and locate to 0
00184    */ 
00185   void cls();  
00186 
00187   /** Write databyte to PT6311
00188    *  @param  int address display memory location to write byte
00189    *  @param  char data byte written at given address
00190    *  @return none
00191    */ 
00192   void writeData(int address, char data); 
00193  
00194   /** Write Display datablock to PT6311
00195    *  @param  DisplayData_t data Array of PT6311_DISPLAY_MEM (=48) bytes for displaydata (starting at address 0)
00196    *  @param  length number bytes to write (valid range 0..PT6311_DISPLAY_MEM (=48), starting at address 0)   
00197    *  @return none
00198    */   
00199   void writeData(DisplayData_t data, int length = PT6311_DISPLAY_MEM);
00200 
00201 
00202   /** Read keydata block from PT6311
00203    *  @param  *keydata Ptr to Array of PT6311_KEY_MEM (=6) bytes for keydata
00204    *  @return bool keypress True when at least one key was pressed
00205    *
00206    * Note: Due to the hardware configuration the PT6311 key matrix scanner will detect multiple keys pressed at same time,
00207    *       but this may result in some spurious keys also being set in keypress data array.
00208    *       It may be best to ignore all keys in those situations. That option is implemented in this method depending on #define setting.
00209    */   
00210   bool getKeys(KeyData_t *keydata);
00211 
00212 
00213   /** Read switches from PT6311
00214    *
00215    *  @param  none
00216    *  @return char for switch data (4 least significant bits)
00217    *
00218    */   
00219   char getSwitches();
00220 
00221   /** Set LEDs
00222     *
00223     * @param  char leds (5 least significant bits)  
00224     * @return none
00225     */
00226   void setLED (char leds = 0);
00227 
00228   /** Set Brightness
00229     *
00230     * @param  char brightness (3 significant bits, valid range 0..7 (1/16 .. 14/16 dutycycle)  
00231     * @return none
00232     */
00233   void setBrightness(char brightness = PT6311_BRT_DEF);
00234   
00235   /** Set the Display mode On/off
00236     *
00237     * @param bool display mode
00238     */
00239   void setDisplay(bool on);
00240   
00241  private:  
00242   SPI _spi;
00243   DigitalOut _cs;
00244   Mode _mode;
00245   char _display;
00246   char _bright; 
00247   
00248   /** Init the SPI interface and the controller
00249     * @param  none
00250     * @return none
00251     */ 
00252   void _init();
00253 
00254   /** Helper to reverse all command or databits. The PT6311 expects LSB first, whereas SPI is MSB first
00255     *  @param  char data
00256     *  @return bitreversed data
00257     */ 
00258   char _flip(char data);
00259 
00260   /** Write command and parameter to PT6311
00261     *  @param  int cmd Command byte
00262     *  &Param  int data Parameters for command
00263     *  @return none
00264     */ 
00265   void _writeCmd(int cmd, int data);  
00266 };
00267 
00268 
00269 
00270 #if (VFDEM2_TEST == 1)
00271 // Derived class for VFDEM2 front display unit
00272 //   Grids 2-11 all display 14-Segment digits and several Icons.
00273 //   Grid 1 and 12 display Icons. 
00274 
00275 //
00276 #include "Font_16Seg.h"
00277 
00278 //VFDEM2 Display data
00279 #define VFDEM2_NR_GRIDS  12
00280 #define VFDEM2_NR_DIGITS 10
00281 #define VFDEM2_NR_UDC    8
00282 
00283 //VFDEM2 Memory size in bytes for Display
00284 #define VFDEM2_DISPLAY_MEM (VFDEM2_NR_GRIDS * PT6311_BYTES_PER_GRID)
00285 
00286 
00287 /** Constructor for class for driving Princeton PT6311 VFD controller as used in VFDEM2
00288   *
00289   *  @brief Supports 12 Grids of 16 Segments and Icons (10 digits of 14 segments plus some icons and another 2 Icon grids).
00290   *         Also supports a scanned keyboard of 7 keys and 1 LED.
00291   *  
00292   *  @param  PinName mosi, miso, sclk, cs SPI bus pins
00293   */
00294 class PT6311_VFDEM2 : public PT6311, public Stream {
00295  public:
00296 
00297   /** Enums for Icons */
00298   //  Grid encoded in 8 MSBs, Icon pattern encoded in 24 LSBs
00299   enum Icon {
00300     CIR = (1<<24) | S16_CIR,
00301     PIE_R = (1<<24) | S16_PIE_R,
00302     PIE_G = (1<<24) | S16_PIE_G,
00303     PIE_B = (1<<24) | S16_PIE_B,    
00304     STP = (1<<24) | S16_STP,
00305     PSE = (1<<24) | S16_PSE,
00306     PLY = (1<<24) | S16_PLY,
00307     RR  = (1<<24) | S16_RR,
00308     LL  = (1<<24) | S16_LL,
00309     PCM = (1<<24) | S16_PCM,
00310     DTS = (1<<24) | S16_DTS,
00311     MIC = (1<<24) | S16_MIC,
00312     DLB = (1<<24) | S16_DLB,
00313 
00314     REC = (2<<24) | S16_REC,
00315 
00316     PRG = (3<<24) | S16_PRG,    
00317     RND = (3<<24) | S16_RND,
00318 
00319     DP8 =  (4<<24) | S16_DP8,        
00320     COL8 = (4<<24) | S16_COL8,
00321 
00322     ANG = (5<<24) | S16_ANG,        
00323     ZM  = (5<<24) | S16_ZM,
00324 
00325     PBC =  (6<<24) | S16_PBC,        
00326     COL6 = (6<<24) | S16_COL6,
00327 
00328     MP3 =  (12<<24) | S16_MP3,
00329     CDDA = (12<<24) | S16_CDDA,
00330     SS  = (12<<24) | S16_SS,
00331     VCD = (12<<24) | S16_VCD,
00332     DVD = (12<<24) | S16_DVD,   
00333     ARW = (12<<24) | S16_ARW,
00334     ONE = (12<<24) | S16_ONE,    
00335     ALL = (12<<24) | S16_ALL,
00336     AA  = (12<<24) | S16_AA,    
00337     BB  = (12<<24) | S16_BB,
00338     TTL = (12<<24) | S16_TTL,
00339     CHP = (12<<24) | S16_CHP       
00340   };
00341   
00342   typedef short UDCData_t[VFDEM2_NR_UDC];
00343 
00344 
00345 /** Constructor for class for driving Princeton PT6311 VFD controller as used in VFDEM2
00346   *
00347   *  @brief Supports 12 Grids of 16 Segments and Icons (10 digits of 14 Segments plus some icons and another 2 Icon grids).
00348   *         Also supports a scanned keyboard of 7 keys and 1 LED.
00349   *  
00350   *  @param  PinName mosi, miso, sclk, cs SPI bus pins
00351   */
00352   PT6311_VFDEM2(PinName mosi, PinName miso, PinName sclk, PinName cs);
00353 
00354 #if DOXYGEN_ONLY
00355     /** Write a character to the Display
00356      *
00357      * @param c The character to write to the display
00358      */
00359     int putc(int c);
00360 
00361     /** Write a formatted string to the Display
00362      *
00363      * @param format A printf-style format string, followed by the
00364      *               variables to use in formatting the string.
00365      */
00366     int printf(const char* format, ...);   
00367 #endif
00368 
00369      /** Locate cursor to a screen column
00370      *
00371      * @param column  The horizontal position from the left, indexed from 0
00372      */
00373     void locate(int column);
00374     
00375     /** Clear the screen and locate to 0
00376      * @param bool clrAll Clear Icons also (default = false)
00377      */
00378     void cls(bool clrAll = false);
00379 
00380     /** Set Icon
00381      *
00382      * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
00383      * @return none
00384      */
00385     void setIcon(Icon icon);
00386 
00387     /** Clr Icon
00388      *
00389      * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
00390      * @return none
00391      */
00392     void clrIcon(Icon icon);
00393 
00394    /** Set User Defined Characters (UDC)
00395      *
00396      * @param unsigned char udc_idx   The Index of the UDC (0..7)
00397      * @param int udc_data            The bitpattern for the UDC (16 bits)       
00398      */
00399     void setUDC(unsigned char udc_idx, int udc_data);
00400 
00401 
00402    /** Number of screen columns
00403     *
00404     * @param none
00405     * @return columns
00406     */
00407     int columns();   
00408 
00409    /** Write databyte to PT6311
00410      *  @param  int address display memory location to write byte
00411      *  @param  char data byte written at given address
00412      *  @return none
00413      */ 
00414     void writeData(int address, char data){
00415       PT6311::writeData(address, data);
00416     }        
00417  
00418    /** Write Display datablock to PT6311
00419     *  @param  DisplayData_t data Array of PT6311_DISPLAY_MEM (=48) bytes for displaydata (starting at address 0)
00420     *  @param  length number bytes to write (valid range 0..(VFDEM2_NR_GRIDS * PT6311_BYTES_PER_GRID) == 36, starting at address 0)   
00421     *  @return none
00422     */   
00423     void writeData(DisplayData_t data, int length = (VFDEM2_NR_GRIDS * PT6311_BYTES_PER_GRID)) {
00424       PT6311::writeData(data, length);
00425     }  
00426 
00427 protected:  
00428     // Stream implementation functions
00429     virtual int _putc(int value);
00430     virtual int _getc();
00431 
00432 private:
00433     int _column;                     // Current cursor location
00434     int _columns;                    // Max number of columns
00435     
00436     DisplayData_t _displaybuffer;    // Local mirror for all chars and icons
00437     UDCData_t _UDC_16S;              // User Defined Character pattterns (UDC)    
00438 };
00439 #endif
00440 
00441 #endif