Fabio Brembilla / X_NUCLEO_6180XA1

Dependencies:   X_NUCLEO_COMMON

Fork of X_NUCLEO_6180XA1 by ST

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Display_class.h Source File

Display_class.h

00001 /**
00002  ******************************************************************************
00003  * @file    Display.h
00004  * @author  AST / EST
00005  * @version V0.0.1
00006  * @date    14-April-2015
00007  * @brief   Header file for display
00008  ******************************************************************************
00009  * @attention
00010  *
00011  * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
00012  *
00013  * Redistribution and use in source and binary forms, with or without modification,
00014  * are permitted provided that the following conditions are met:
00015  *   1. Redistributions of source code must retain the above copyright notice,
00016  *      this list of conditions and the following disclaimer.
00017  *   2. Redistributions in binary form must reproduce the above copyright notice,
00018  *      this list of conditions and the following disclaimer in the documentation
00019  *      and/or other materials provided with the distribution.
00020  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00021  *      may be used to endorse or promote products derived from this software
00022  *       without specific prior written permission.
00023  *
00024  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00025  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00026  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00028  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00029  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00030  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00032  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00033  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034  *
00035  ******************************************************************************
00036 */
00037 
00038 #ifndef __DISPLAY_H
00039 #define __DISPLAY_H
00040 
00041 /* Includes ------------------------------------------------------------------*/
00042 #include "mbed.h"
00043 #include "stmpe1600_class.h"
00044 #include "DevI2C.h"
00045 
00046 #ifdef __cplusplus
00047 extern "C" {
00048 #endif
00049 
00050 #define DP  (1<<7)
00051 #define NOT_7_NO_DP( ... ) (uint8_t) ~( __VA_ARGS__ + DP )
00052 #define S0 (1<<0)
00053 #define S1 (1<<1)
00054 #define S2 (1<<2)
00055 #define S3 (1<<3)
00056 #define S4 (1<<4)
00057 #define S5 (1<<5)
00058 #define S6 (1<<6)
00059 
00060 #ifndef __cpluplus  
00061 /* refer to http://www.twyman.org.uk/Fonts/ */
00062 static const uint8_t ascii_to_display_lut[256]={
00063     0,0,0,0,0,0,0,0,
00064     0,0,0,0,0,0,0,0,
00065     0,0,0,0,0,0,0,0,
00066     0,0,0,0,0,0,0,0,
00067     [32]=           0,
00068     0,0,0,0,0,0,0,0,
00069     0,
00070     [42]=           NOT_7_NO_DP(),
00071     0,0,
00072     [45]=           S6,
00073     0,0,
00074     [48]=           NOT_7_NO_DP(S6),
00075     [49]=           S1+S2,
00076     [50]=           S0+S1+S6+S4+S3,
00077     [51]=           NOT_7_NO_DP(S4+S5),
00078     [52]=           S5+S1+S6+S2,
00079     [53]=           NOT_7_NO_DP(S1+S4),
00080     [54]=           NOT_7_NO_DP(S1),
00081     [55]=           S0+S1+S2,
00082     [56]=           NOT_7_NO_DP(0),
00083     [57]=           NOT_7_NO_DP(S4),
00084     0,0,0,
00085     [61]=           S3+S6,
00086     0,
00087     [63]=           NOT_7_NO_DP(S5+S3+S2),
00088     [64]=           S0+S3,
00089     [65]=           NOT_7_NO_DP(S3),
00090     [66]=           NOT_7_NO_DP(S0+S1), /* as b  */
00091     [67]=           S0+S3+S4+S5, // same as [
00092     [68]=           S0+S3+S4+S5, // same as [ DUMMY
00093     [69]=           NOT_7_NO_DP(S1+S2),
00094     [70]=           S6+S5+S4+S0,
00095     [71]=           NOT_7_NO_DP(S4), /* same as 9 */
00096     [72]=           NOT_7_NO_DP(S0+S3),
00097     [73]=           S1+S2,
00098     [74]=           S1+S2+S3+S4,
00099     [75]=           NOT_7_NO_DP(S0+S3), /* same as H */
00100     [76]=           S3+S4+S5,
00101     [77]=           S0+S4+S2, /* same as  m*/
00102     [78]=           S2+S4+S6, /* same as n*/
00103     [79]=           NOT_7_NO_DP(S6),
00104     [80]=           S0+S1+S2+S5+S6, // sames as 'q'
00105     [81]=           NOT_7_NO_DP(S3+S2),
00106     [82]=           S4+S6,
00107     [83]=           NOT_7_NO_DP(S1+S4), /* sasme as 5 */
00108     [84]=           NOT_7_NO_DP(S0+S1+S2), /* sasme as t */
00109     [85]=           NOT_7_NO_DP(S6+S0),
00110     [86]=           S4+S3+S2, // is u but u use U
00111     [87]=           S1+S3+S5,
00112     [88]=           NOT_7_NO_DP(S0+S3), // similar to H
00113     [89]=           NOT_7_NO_DP(S0+S4),
00114     [90]=           S0+S1+S6+S4+S3, // same as 2
00115     [91]=           S0+S3+S4+S5,
00116     0,
00117     [93]=           S0+S3+S2+S1,
00118     [94]=           S0, /* use as top bar */
00119     [95]=           S3, 
00120     0,
00121     [97]=           S2+ S3+ S4+ S6 ,
00122     [98]=           NOT_7_NO_DP(S0+S1),
00123     [99]=           S6+S4+S3,
00124     [100]=           NOT_7_NO_DP(S0+S5),
00125     [101]=           NOT_7_NO_DP(S2),
00126     [102]=           S6+S5+S4+S0, /* same as F */
00127     [103]=           NOT_7_NO_DP(S4), /* same as 9 */
00128     [104]=           S6+S5+S4+S2,
00129     [105]=           S4,
00130     [106]=           S1+S2+S3+S4,
00131     [107]=           S6+S5+S4+S2, /* a h */
00132     [108]=           S3+S4,
00133     [109]=           S0+S4+S2, /* same as  */
00134     [110]=           S2+S4+S6,
00135     [111]=           S6+S4+S3+S2,
00136     [112]=           NOT_7_NO_DP(S3+S2), // same as P
00137     [113]=           S0+S1+S2+S5+S6,
00138     [114]=           S4+S6,
00139     [115]=           NOT_7_NO_DP(S1+S4),
00140     [116]=           NOT_7_NO_DP(S0+S1+S2),
00141     [117]=           S4+S3+S2+S5+S1, // U
00142     [118]=           S4+S3+S2, // is u but u use U
00143     [119]=           S1+S3+S5,
00144     [120]=           NOT_7_NO_DP(S0+S3), // similar to H
00145     [121]=           NOT_7_NO_DP(S0+S4),
00146     [122]=           S0+S1+S6+S4+S3, // same as 2
00147     0,0,0,
00148     [126]=           S0+S3+S6 /* 3 h bar */
00149 };
00150 #else 
00151 /* refer to http://www.twyman.org.uk/Fonts/ */
00152 static const uint8_t ascii_to_display_lut[256]={
00153       [' ']=           0,
00154       ['-']=           S6,
00155       ['_']=           S3,
00156       ['=']=           S3+S6,
00157       ['~']=           S0+S3+S6, /* 3 h bar */
00158       ['^']=           S0, /* use as top bar */
00159 
00160       ['?']=           NOT_7_NO_DP(S5+S3+S2),
00161       ['*']=           NOT_7_NO_DP(),
00162       ['[']=           S0+S3+S4+S5,
00163       [']']=           S0+S3+S2+S1,
00164       ['@']=           S0+S3,
00165 
00166       ['0']=           NOT_7_NO_DP(S6),
00167       ['1']=           S1+S2,
00168       ['2']=           S0+S1+S6+S4+S3,
00169       ['3']=           NOT_7_NO_DP(S4+S5),
00170       ['4']=           S5+S1+S6+S2,
00171       ['5']=           NOT_7_NO_DP(S1+S4),
00172       ['6']=           NOT_7_NO_DP(S1),
00173       ['7']=           S0+S1+S2,
00174       ['8']=           NOT_7_NO_DP(0),
00175       ['9']=           NOT_7_NO_DP(S4),
00176     
00177       ['a']=           S2+ S3+ S4+ S6 ,
00178       ['b']=           NOT_7_NO_DP(S0+S1),
00179       ['c']=           S6+S4+S3,
00180       ['d']=           NOT_7_NO_DP(S0+S5),
00181       ['e']=           NOT_7_NO_DP(S2),
00182       ['f']=           S6+S5+S4+S0, /* same as F */
00183       ['g']=           NOT_7_NO_DP(S4), /* same as 9 */
00184       ['h']=           S6+S5+S4+S2,
00185       ['i']=           S4,
00186       ['j']=           S1+S2+S3+S4,
00187       ['k']=           S6+S5+S4+S2, /* a h */
00188       ['l']=           S3+S4,
00189       ['m']=           S0+S4+S2, /* same as  */
00190       ['n']=           S2+S4+S6,
00191       ['o']=           S6+S4+S3+S2,
00192       ['p']=           NOT_7_NO_DP(S3+S2), // same as P
00193       ['q']=           S0+S1+S2+S5+S6,
00194       ['r']=           S4+S6,
00195       ['s']=           NOT_7_NO_DP(S1+S4),
00196       ['t']=           NOT_7_NO_DP(S0+S1+S2),
00197       ['u']=           S4+S3+S2+S5+S1, // U
00198       ['v']=           S4+S3+S2, // is u but u use U
00199       ['w']=           S1+S3+S5,
00200       ['x']=           NOT_7_NO_DP(S0+S3), // similar to H
00201       ['y']=           NOT_7_NO_DP(S0+S4),
00202       ['z']=           S0+S1+S6+S4+S3, // same as 2
00203       
00204       ['A']=           NOT_7_NO_DP(S3),
00205       ['B']=           NOT_7_NO_DP(S0+S1), /* as b  */
00206       ['C']=           S0+S3+S4+S5, // same as [
00207       ['E']=           NOT_7_NO_DP(S1+S2),
00208       ['F']=           S6+S5+S4+S0,
00209       ['G']=           NOT_7_NO_DP(S4), /* same as 9 */
00210       ['H']=           NOT_7_NO_DP(S0+S3),
00211       ['I']=           S1+S2,
00212       ['J']=           S1+S2+S3+S4,
00213       ['K']=           NOT_7_NO_DP(S0+S3), /* same as H */
00214       ['L']=           S3+S4+S5,
00215       ['M']=           S0+S4+S2, /* same as  m*/
00216       ['N']=           S2+S4+S6, /* same as n*/
00217       ['O']=           NOT_7_NO_DP(S6),
00218       ['P']=           S0+S1+S2+S5+S6, // sames as 'q'
00219       ['Q']=           NOT_7_NO_DP(S3+S2),
00220       ['R']=           S4+S6,
00221       ['S']=           NOT_7_NO_DP(S1+S4), /* sasme as 5 */
00222       ['T']=           NOT_7_NO_DP(S0+S1+S2), /* sasme as t */
00223       ['U']=           NOT_7_NO_DP(S6+S0),
00224       ['V']=           S4+S3+S2, // is u but u use U
00225       ['W']=           S1+S3+S5,
00226       ['X']=           NOT_7_NO_DP(S0+S3), // similar to H
00227       ['Y']=           NOT_7_NO_DP(S0+S4),
00228       ['Z']=           S0+S1+S6+S4+S3 // same as 2
00229 };
00230 #endif
00231 
00232 #undef S0
00233 #undef S1
00234 #undef S2
00235 #undef S3
00236 #undef S4
00237 #undef S5
00238 #undef S6
00239 #undef DP
00240 
00241 #define DISPLAY_DELAY   1  // in mSec       
00242 
00243 /* Classes -------------------------------------------------------------------*/
00244 /** Class representing Display
00245  */ 
00246 
00247 class Display
00248 {
00249  public:
00250      /** Constructor
00251      * @param[in] &stmpe_1600 device handler to be used for display control
00252      */    
00253      Display(STMPE1600 &stmpe_1600) : stmpe1600(stmpe_1600) {
00254         stmpe1600.setGPIOdir (GPIO_7,  OUTPUT);  // Digit1   
00255         stmpe1600.setGPIOdir (GPIO_8,  OUTPUT);  // Digit2   
00256         stmpe1600.setGPIOdir (GPIO_9,  OUTPUT);  // Digit3   
00257         stmpe1600.setGPIOdir (GPIO_10, OUTPUT);  // Digit4       
00258      
00259         stmpe1600.setGPIOdir (GPIO_0,  OUTPUT);  // SegmentA             
00260         stmpe1600.setGPIOdir (GPIO_1,  OUTPUT);  // SegmentB             
00261         stmpe1600.setGPIOdir (GPIO_2,  OUTPUT);  // SegmentC         
00262         stmpe1600.setGPIOdir (GPIO_3,  OUTPUT);  // SegmentD         
00263         stmpe1600.setGPIOdir (GPIO_4,  OUTPUT);  // SegmentE         
00264         stmpe1600.setGPIOdir (GPIO_5,  OUTPUT);  // SegmentF         
00265         stmpe1600.setGPIOdir (GPIO_6,  OUTPUT);  // SegmentG                 
00266      }
00267      
00268     /*** Interface Methods ***/      
00269     /**
00270      * @brief       Print the string on display
00271      * @param[in]   String to be printed
00272      * @param[in]   String lenght [min 1, max 4]
00273      * @return      void
00274      */             
00275      void DisplayString (char str[4], char strlen)
00276      {
00277        int i, dgt;
00278        const char *pc;
00279        uint8_t data[2];
00280        uint16_t *pdata = (uint16_t*)data;               
00281             
00282        for(i=0, dgt=4-strlen, pc=str; i<strlen && *pc!=0; i++, pc++, dgt++)
00283        {
00284           _V2_Set7Segment( ascii_to_display_lut[(uint8_t)*pc], dgt);
00285           if( *(pc+1)== '.')
00286           {
00287              pc++;
00288           }
00289           wait_ms(DISPLAY_DELAY);                   
00290                     stmpe1600.read16bitReg(GPSR_0_7, pdata);
00291           *pdata = *pdata | (uint16_t)0x0780;               // all digits off
00292                     stmpe1600.write16bitReg(GPSR_0_7, pdata);
00293        }                        
00294             
00295      }
00296      
00297     /**
00298      * @brief       Print the digit on display
00299      * @param[in]   Digit value to be printed
00300      * @param[in]   Digit to be used [min 0, max 3]
00301      * @return      void
00302      */              
00303      void DisplayDigit (char *val, char dgt)    
00304      {
00305        uint8_t data[2];
00306        uint16_t *pdata = (uint16_t*)data;       
00307             
00308        stmpe1600.read16bitReg(GPSR_0_7, pdata);
00309        *pdata = *pdata | (uint16_t)0x0780;              // all digits off
00310        stmpe1600.write16bitReg(GPSR_0_7, pdata);
00311        _V2_Set7Segment( ascii_to_display_lut[(uint8_t)*val], dgt);                      
00312      }      
00313         
00314 private:        
00315      void _V2_Set7Segment( int Leds, int digit )
00316      {
00317        //Digits_off();
00318        uint16_t dgt;
00319        dgt = 1<<digit;
00320        dgt = ((uint16_t)dgt)<<7;
00321        dgt = ~dgt;  
00322        uint8_t data[2];
00323        uint16_t *pdata = (uint16_t*)data;            
00324 
00325          
00326        /* set the exppinname state to lvl */
00327        stmpe1600.read16bitReg(GPSR_0_7, pdata);
00328        *pdata = *pdata  | (uint16_t)0x007F;      // 7 segments off
00329        stmpe1600.write16bitReg(GPSR_0_7, pdata);
00330          
00331        int mask=1;
00332        for (int i=0; i<7; i++) 
00333        { 
00334           if (Leds & mask) *pdata = *pdata & ~(uint16_t)mask;  
00335           mask = mask<<1;
00336        }         
00337        *pdata = *pdata & dgt;
00338        stmpe1600.write16bitReg(GPSR_0_7, pdata);
00339      }              
00340      STMPE1600 &stmpe1600;
00341 };
00342 
00343 #ifdef __cplusplus
00344 }
00345 #endif
00346 #endif // __DISPLAY_H