Wim Huiskamp / PT6318
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PT6318.h Source File

PT6318.h

00001 /* mbed PT6318 Library, for Princeton PT6318 VFD controller
00002  * Copyright (c) 2016, v01: WH, Initial version for KUH8300
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 PT6318_H
00024 #define PT6318_H
00025 
00026 // Select one of the testboards for Princeton PT6318 VFD controller
00027 #include "PT6318_Config.h"
00028 
00029 /** An interface for driving Princeton PT6318 VFD controller
00030  *
00031  * @code
00032  *
00033  * #if (PT6318_TEST == 1)  
00034  * // Direct driving of PT6318 Test
00035  *
00036  * #include "mbed.h"
00037  * #include "PT6318.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  * PT6318::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  * PT6318::KeyData_t keydata; 
00046  *
00047  * // PT6318 declaration, Default setting 16 Grids @ 12 Segments
00048  * PT6318 PT6318(p5,p6,p7, p8);
00049  *
00050  * int main() {
00051  *   PT6318.cls(); 
00052  *   PT6318.writeData(all_str);
00053  *   wait(4);
00054  *   PT6318.setBrightness(PT6318_BRT0);
00055  *   wait(1);
00056  *   PT6318.setBrightness(PT6318_BRT3);
00057  *
00058  *   while (1) {
00059  *    // Check and read keydata
00060  *    if (PT6318.getKeys(&keydata)) {
00061  *      pc.printf("Keydata 0..5 = 0x%02x 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] == 0x01) { //sw1   
00064  *        PT6318.cls(); 
00065  *        wait(1);
00066  *        PT6318.writeData(all_str);
00067  *      }  
00068  *    } 
00069  *   }   
00070  * }
00071  * #endif
00072  *
00073  * @endcode
00074  */
00075 
00076 //PT6318 Display and Keymatrix data
00077 #define PT6318_MAX_NR_GRIDS   16
00078 #define PT6318_BYTES_PER_GRID  3
00079 //Significant bits Keymatrix data
00080 #define PT6318_KEY_MSK      0xFF 
00081 
00082 //Memory size in bytes for Display and Keymatrix
00083 #define PT6318_DISPLAY_MEM  (PT6318_MAX_NR_GRIDS * PT6318_BYTES_PER_GRID)
00084 #define PT6318_KEY_MEM         6
00085 
00086 //Reserved bits for commands
00087 #define PT6318_CMD_MSK      0xC0
00088 
00089 //Mode setting command
00090 #define PT6318_MODE_SET_CMD 0x00
00091 #define PT6318_GR8_SEG20    0x00
00092 #define PT6318_GR9_SEG19    0x08
00093 #define PT6318_GR10_SEG18   0x09
00094 #define PT6318_GR11_SEG17   0x0A
00095 #define PT6318_GR12_SEG16   0x0B
00096 #define PT6318_GR13_SEG15   0x0C
00097 #define PT6318_GR14_SEG14   0x0D
00098 #define PT6318_GR15_SEG13   0x0E
00099 #define PT6318_GR16_SEG12   0x0F //default
00100 
00101 //Data setting commands
00102 #define PT6318_DATA_SET_CMD 0x40
00103 #define PT6318_DATA_WR      0x00
00104 #define PT6318_LED_WR       0x01
00105 #define PT6318_KEY_RD       0x02
00106 #define PT6318_SW_RD        0x03
00107 #define PT6318_ADDR_INC     0x00
00108 #define PT6318_ADDR_FIXED   0x04
00109 #define PT6318_MODE_NORM    0x00
00110 #define PT6318_MODE_TEST    0x08
00111 
00112 //LED settings data
00113 #define PT6318_LED_MSK      0x1F
00114 #define PT6318_LED1         0x01
00115 #define PT6318_LED2         0x02
00116 #define PT6318_LED3         0x04
00117 #define PT6318_LED4         0x08
00118 #define PT6318_LED5         0x10
00119 
00120 //Switch settings data
00121 #define PT6318_SW_MSK       0x0F
00122 #define PT6318_SW1          0x01
00123 #define PT6318_SW2          0x02
00124 #define PT6318_SW3          0x04
00125 #define PT6318_SW4          0x08
00126 
00127 //Address setting commands
00128 #define PT6318_ADDR_SET_CMD 0xC0
00129 #define PT6318_ADDR_MSK     0x3F
00130 
00131 //Display control commands
00132 #define PT6318_DSP_CTRL_CMD 0x80
00133 #define PT6318_BRT_MSK      0x07
00134 #define PT6318_BRT0         0x00 //Pulsewidth 1/16, Default
00135 #define PT6318_BRT1         0x01
00136 #define PT6318_BRT2         0x02
00137 #define PT6318_BRT3         0x03
00138 #define PT6318_BRT4         0x04
00139 #define PT6318_BRT5         0x05
00140 #define PT6318_BRT6         0x06
00141 #define PT6318_BRT7         0x07 //Pulsewidth 14/16
00142 
00143 #define PT6318_BRT_DEF      PT6318_BRT3
00144 
00145 #define PT6318_DSP_OFF      0x00 //Default
00146 #define PT6318_DSP_ON       0x08
00147 
00148 
00149 /** A class for driving Princeton PT6318 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 PT6318 {
00155  public:
00156 
00157   /** Enums for display mode */
00158   enum Mode {  
00159     Grid8_Seg20  = PT6318_GR8_SEG20,  /**<   8 Grids, 20 Segments */
00160     Grid9_Seg19  = PT6318_GR9_SEG19,  /**<   9 Grids, 19 Segments */
00161     Grid10_Seg18 = PT6318_GR10_SEG18, /**<  10 Grids, 18 Segments */
00162     Grid11_Seg17 = PT6318_GR11_SEG17, /**<  11 Grids, 17 Segments */
00163     Grid12_Seg16 = PT6318_GR12_SEG16, /**<  12 Grids, 16 Segments */
00164     Grid13_Seg15 = PT6318_GR13_SEG15, /**<  13 Grids, 15 Segments */
00165     Grid14_Seg14 = PT6318_GR14_SEG14, /**<  14 Grids, 14 Segments */
00166     Grid15_Seg13 = PT6318_GR15_SEG13, /**<  15 Grids, 13 Segments */
00167     Grid16_Seg12 = PT6318_GR16_SEG12  /**<  16 Grids, 12 Segments */
00168   };
00169  
00170   /** Datatypes for display and keymatrix data */
00171   typedef char DisplayData_t[PT6318_DISPLAY_MEM];
00172   typedef char KeyData_t[PT6318_KEY_MEM];
00173     
00174  /** Constructor for class for driving Princeton PT6318 VFD controller
00175   *
00176   *  @brief Supports 8 Grids of 20 Segments upto 16 Grids of 12 Segments. Also supports a scanned keyboard of upto 32 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   PT6318(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 PT6318
00188    *  @param  char data byte written at given address
00189    *  @param  int address display memory location to write byte   
00190    *  @return none
00191    */ 
00192   void writeData(char data, int address); 
00193  
00194   /** Write Display datablock to PT6318
00195    *  @param  DisplayData_t data Array of PT6318_DISPLAY_MEM (=48) bytes for displaydata (starting at address)
00196    *  @param  length number bytes to write (valid range 0..PT6318_DISPLAY_MEM (=48), starting at address)   
00197    *  @param  int address display memory location to write byte (default = 0)
00198    *  @return none
00199    */   
00200   void writeData(DisplayData_t data, int length = PT6318_DISPLAY_MEM, int address = 0);
00201 
00202 
00203   /** Read keydata block from PT6318
00204    *  @param  *keydata Ptr to Array of PT6318_KEY_MEM (=6) bytes for keydata
00205    *  @return bool keypress True when at least one key was pressed
00206    *
00207    * Note: Due to the hardware configuration the PT6318 key matrix scanner will detect multiple keys pressed at same time,
00208    *       but this may result in some spurious keys also being set in keypress data array.
00209    *       It may be best to ignore all keys in those situations. That option is implemented in this method depending on #define setting.
00210    */   
00211   bool getKeys(KeyData_t *keydata);
00212 
00213 
00214   /** Read switches from PT6318
00215    *
00216    *  @param  none
00217    *  @return char for switch data (4 least significant bits)
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 = PT6318_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 PT6318 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 PT6318
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 (KUH8300_TEST == 1)
00271 // Derived class for KUH8300 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_7Seg.h"
00277 
00278 //KUH8300 Display data
00279 #define KUH8300_NR_GRIDS   6
00280 #define KUH8300_NR_DIGITS  8
00281 #define KUH8300_NR_UDC     8
00282 
00283 //KUH8300 Memory size in bytes for Display
00284 #define KUH8300_DISPLAY_MEM (KUH8300_NR_GRIDS * PT6318_BYTES_PER_GRID)
00285 
00286 
00287 /** Constructor for class for driving Princeton PT6318 VFD controller as used in KUH8300
00288   *
00289   *  @brief Supports 8 Grids of 20 Segments and Icons (8 1/2 digits of 14 segments plus some icons).
00290   *         Also supports a scanned keyboard of 12 keys and 1 LED.
00291   *  
00292   *  @param  PinName mosi, miso, sclk, cs SPI bus pins
00293   */
00294 class PT6318_KUH8300 : public PT6318, 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     CAM  = (1<<24) | S7_CAM,
00301     DIG  = (1<<24) | S7_DIG,
00302     MONO = (1<<24) | S7_MONO,
00303     PRG  = (1<<24) | S7_PRG,    
00304     DTS  = (1<<24) | S7_DTS,
00305     MEM  = (1<<24) | S7_MEM,
00306     KHZ  = (1<<24) | S7_KHZ,
00307     STR  = (1<<24) | S7_STR,
00308     MHZ  = (1<<24) | S7_MHZ,
00309 
00310     KEY  = (2<<24) | S7_KEY,
00311     ALL  = (2<<24) | S7_ALL,
00312     DP6  = (2<<24) | S7_DP6,
00313     COL6 = (2<<24) | S7_COL6,    
00314 
00315     ARW  = (3<<24) | S7_ARW,
00316     A_   = (3<<24) | S7_A_,
00317     B_   = (3<<24) | S7_B_,
00318     CD4A = (3<<24) | S7_CD4A,    
00319     
00320     CHP  = (4<<24) | S7_CHP,
00321     N3   = (4<<24) | S7_N3,
00322     L3   = (4<<24) | S7_L3,
00323     HK3  = (4<<24) | S7_HK3,    
00324 
00325     TTL  = (5<<24) | S7_TTL,
00326     JM1  = (5<<24) | S7_JM1,    
00327     HK2  = (5<<24) | S7_HK2,    
00328 
00329     PSE  = (6<<24) | S7_PSE,
00330     PLY  = (6<<24) | S7_PLY,    
00331     PBC  = (6<<24) | S7_PBC,    
00332     DVD  = (6<<24) | S7_DVD,
00333     CD   = (6<<24) | S7_CD,    
00334     V    = (6<<24) | S7_V,    
00335     S    = (6<<24) | S7_S
00336   };
00337   
00338   typedef char UDCData_t[KUH8300_NR_UDC];
00339 
00340 
00341 /** Constructor for class for driving Princeton PT6318 VFD controller as used in KUH8300
00342   *
00343   *  @brief Supports 8 Grids of 20 Segments and Icons (8 1/2 digits of 14 Segments plus some icons).
00344   *         Also supports a scanned keyboard of 12 keys and 1 LED.
00345   *  
00346   *  @param  PinName mosi, miso, sclk, cs SPI bus pins
00347   */
00348   PT6318_KUH8300(PinName mosi, PinName miso, PinName sclk, PinName cs);
00349 
00350 #if DOXYGEN_ONLY
00351     /** Write a character to the Display
00352      *
00353      * @param c The character to write to the display
00354      */
00355     int putc(int c);
00356 
00357     /** Write a formatted string to the Display
00358      *
00359      * @param format A printf-style format string, followed by the
00360      *               variables to use in formatting the string.
00361      */
00362     int printf(const char* format, ...);   
00363 #endif
00364 
00365      /** Locate cursor to a screen column
00366      *
00367      * @param column  The horizontal position from the left, indexed from 0
00368      */
00369     void locate(int column);
00370     
00371     /** Clear the screen and locate to 0
00372      * @param bool clrAll Clear Icons also (default = false)
00373      */
00374     void cls(bool clrAll = false);
00375 
00376     /** Set Icon
00377      *
00378      * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 24 LSBs
00379      * @return none
00380      */
00381     void setIcon(Icon icon);
00382 
00383     /** Clr Icon
00384      *
00385      * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 24 LSBs
00386      * @return none
00387      */
00388     void clrIcon(Icon icon);
00389 
00390    /** Set User Defined Characters (UDC)
00391      *
00392      * @param unsigned char udc_idx   The Index of the UDC (0..7)
00393      * @param int udc_data            The bitpattern for the UDC (16 bits)       
00394      */
00395     void setUDC(unsigned char udc_idx, int udc_data);
00396 
00397 
00398    /** Number of screen columns
00399     *
00400     * @param none
00401     * @return columns
00402     */
00403     int columns();   
00404 
00405    /** Write databyte to PT6318
00406      *  @param  char data byte written at given address
00407      *  @param  int address display memory location to write byte     
00408      *  @return none
00409      */ 
00410     void writeData(int address, char data){
00411       PT6318::writeData(address, data);
00412     }        
00413  
00414    /** Write Display datablock to PT6318
00415     *  @param  DisplayData_t data Array of PT6318_DISPLAY_MEM (=36) bytes for displaydata (starting at address)
00416     *  @param  length number bytes to write (valid range 0..(KUH8300_NR_GRIDS * PT6318_BYTES_PER_GRID) == 24, starting at address)   
00417     *  @param  int address display memory location to write byte (default = 0)
00418     *  @return none
00419     */   
00420     void writeData(DisplayData_t data, int length = (KUH8300_NR_GRIDS * PT6318_BYTES_PER_GRID), int address = 0) {
00421       PT6318::writeData(data, length, address);
00422     }  
00423 
00424 protected:  
00425     // Stream implementation functions
00426     virtual int _putc(int value);
00427     virtual int _getc();
00428 
00429 private:
00430     int _column;                     // Current cursor location
00431     int _columns;                    // Max number of columns
00432     
00433     DisplayData_t _displaybuffer;    // Local mirror for all chars and icons
00434     UDCData_t _UDC_7S;               // User Defined Character pattterns (UDC)    
00435 }; 
00436 #endif
00437 
00438 #endif