AtomSoft

TFT STM32F4

Dec 3rd, 2012
2,187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 21.29 KB | None | 0 0
  1. //By AtomSoft : AKA Jason Lopez
  2. // Enjoy
  3.  
  4. /* Includes */
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include "stm32f4xx_conf.h"
  8. #include "stm32f4xx.h"
  9. #include "font.h"
  10.  
  11. void SysTick_Handler(void);
  12. void CONFIG_LCD_PINS(void);
  13.  
  14. #define LCD_DB 8
  15. #define LCD_IOP GPIOE
  16. #define LCD_CTP GPIOB
  17. #define LCD_PINS (GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15)
  18. #define LCD_CONT (GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14)
  19.  
  20. #define CS GPIO_Pin_11
  21. #define RS GPIO_Pin_12
  22. #define WR GPIO_Pin_13
  23. #define RD GPIO_Pin_14
  24.  
  25. #define CS_LOW GPIO_ResetBits(LCD_CTP, CS);
  26. #define RS_LOW GPIO_ResetBits(LCD_CTP, RS);
  27. #define RD_LOW GPIO_ResetBits(LCD_CTP, RD);
  28. #define WR_LOW GPIO_ResetBits(LCD_CTP, WR);
  29.  
  30. #define CS_HIGH GPIO_SetBits(LCD_CTP, CS);
  31. #define RS_HIGH GPIO_SetBits(LCD_CTP, RS);
  32. #define RD_HIGH GPIO_SetBits(LCD_CTP, RD);
  33. #define WR_HIGH GPIO_SetBits(LCD_CTP, WR);
  34.  
  35. unsigned int IC_CODE; //Stores Driver IC ID (either SPFD5408A  or ST7781R)
  36.  
  37. //Basic Colors
  38. #define RED     0xf800
  39. #define GREEN       0x7e00
  40. #define BLUE        0x001f
  41. #define BLACK       0x0000
  42. #define YELLOW      0xffe0
  43. #define WHITE       0xffff
  44.  
  45. //Other Colors
  46. #define CYAN        0x07ff
  47. #define BRIGHT_RED  0xf810
  48. #define GRAY1       0x8410
  49. #define GRAY2       0x4208
  50.  
  51. //TFT resolution 240*320
  52. #define MIN_X   0
  53. #define MIN_Y   0
  54. #define MAX_X   239
  55. #define MAX_Y   319
  56.  
  57. void init (void);
  58. void sendCommand(unsigned int index);
  59. void sendData(unsigned int data);
  60. void pushData(unsigned char data);
  61. unsigned char getData(void);
  62. unsigned int readRegister(unsigned int index);
  63.  
  64. void setXY(unsigned int poX, unsigned int poY);
  65. void setPixel(unsigned int poX, unsigned int poY,unsigned int color);
  66. void drawLine(unsigned int x0,unsigned int y0,unsigned int x1,unsigned int y1,unsigned int color);
  67. void drawVerticalLine(unsigned int poX, unsigned int poY,unsigned int length,unsigned int color);
  68. void drawHorizontalLine(unsigned int poX, unsigned int poY,unsigned int length,unsigned int color);
  69. void drawRectangle(unsigned int poX, unsigned int poY, unsigned int length,unsigned int width,unsigned int color);
  70. void fillRectangle(unsigned int poX, unsigned int poY, unsigned int length, unsigned int width, unsigned int color);
  71. void drawCircle(int poX, int poY, int r,unsigned int color);
  72. void fillCircle(int poX, int poY, int r,unsigned int color);
  73. void drawChar(unsigned char ascii,unsigned int poX, unsigned int poY,unsigned int size, unsigned int fgcolor);
  74. void drawString(char *string,unsigned int poX, unsigned int poY,unsigned int size,unsigned int fgcolor);
  75. unsigned char drawNumber(long long_num,unsigned int poX, unsigned int poY,unsigned int size,unsigned int fgcolor);
  76.  
  77. unsigned char drawFloat(float floatNumber,unsigned char decimal,unsigned int poX, unsigned int poY,unsigned int size,unsigned int fgcolor);
  78. void delay(unsigned char time, int unit);
  79. void all_pin_input(void);
  80. void all_pin_output(void);
  81. void all_pin_low(void);
  82.  
  83. void setOrientation(unsigned int HV);
  84. void LCD_WriteReg(unsigned int INDEX, unsigned int DATA);
  85.  
  86. GPIO_InitTypeDef        LCDCTR_Setup;
  87. GPIO_InitTypeDef        LCDIO_Setup;
  88.  
  89. int color = WHITE;
  90.  
  91. #define us 1
  92. #define ms 1000
  93. #define sec 100000
  94.  
  95. int main(void)
  96. {
  97.     unsigned int cur = 0;
  98.     unsigned int nColor = 1;
  99.  
  100.     /* Configure SysTick for 1ms interrupts: */
  101.     SysTick_Config(168000000/1000000);
  102.     CONFIG_LCD_PINS();
  103.  
  104.     init();  //init TFT library
  105.  
  106.     fillRectangle(0,0,239,319,BLACK);
  107.  
  108.     drawString("AtomSoftTech",10,10,2,WHITE);
  109.  
  110.     drawString("Board: STM32F4-Discovery",10,30,1,WHITE);
  111.  
  112.     drawString("Speed: 168MHz",10,40,1,WHITE);
  113.  
  114.     while (1)
  115.     {        
  116.             switch(cur)
  117.         {
  118.           case 0:
  119.             nColor = BLACK;
  120.             break;
  121.           case 1:
  122.             nColor = GREEN;
  123.             break;
  124.           case 2:
  125.             nColor = RED;
  126.             break;
  127.           case 3:
  128.             nColor = BLUE;
  129.             break;
  130.           case 4:
  131.             nColor = YELLOW;
  132.             break;
  133.           case 5:
  134.             nColor = WHITE;
  135.             break;
  136.         }
  137.  
  138.  
  139.         fillRectangle(10,60,40,40,nColor);
  140.  
  141.         if(cur == 5)
  142.             cur = 0;
  143.         else
  144.             cur++;
  145.         delay(150,ms);
  146.     }
  147.  
  148.  
  149.     return(0);
  150. }
  151.  
  152. unsigned int TimeCount = 0;
  153. unsigned char IsTimeUp = 0;
  154.  
  155. void SysTick_Handler(void)
  156. {
  157.  
  158.     static unsigned int n16Count = 0;
  159.        
  160.         if(!IsTimeUp)
  161.         {
  162.             if (TimeCount == n16Count)
  163.             {
  164.                 IsTimeUp = 1;
  165.                 n16Count = -1;
  166.             }        
  167.             else
  168.             {
  169.               n16Count = n16Count + 1;
  170.             }
  171.            
  172.         }
  173. }
  174.  
  175. void CONFIG_LCD_PINS(void)
  176. {
  177.  
  178.  
  179.     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
  180.     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
  181.  
  182.     /* Configure GPIOs to drive LCD: */
  183.     LCDIO_Setup.GPIO_Pin    = LCD_PINS;
  184.     LCDIO_Setup.GPIO_Mode   = GPIO_Mode_OUT;
  185.     LCDIO_Setup.GPIO_OType  = GPIO_OType_PP;
  186.     LCDIO_Setup.GPIO_Speed  = GPIO_Speed_50MHz;
  187.     LCDIO_Setup.GPIO_PuPd   = GPIO_PuPd_NOPULL;
  188.     GPIO_Init(LCD_IOP, &LCDIO_Setup);
  189.  
  190.     LCDCTR_Setup.GPIO_Pin   = LCD_CONT;
  191.     LCDCTR_Setup.GPIO_Mode  = GPIO_Mode_OUT;
  192.     LCDCTR_Setup.GPIO_OType = GPIO_OType_PP;
  193.     LCDCTR_Setup.GPIO_Speed = GPIO_Speed_50MHz;
  194.     LCDCTR_Setup.GPIO_PuPd  = GPIO_PuPd_NOPULL;
  195.     GPIO_Init(LCD_CTP, &LCDCTR_Setup);
  196.  
  197.     GPIO_ResetBits(LCD_CTP, LCD_CONT);
  198.     GPIO_ResetBits(LCD_IOP, LCD_PINS);
  199. }
  200.  
  201. void pushData(unsigned char data)
  202. {
  203.     //all_pin_low();
  204.     LCD_IOP->ODR = (int)((int)data << LCD_DB);
  205. }
  206.  
  207. void delay(unsigned char time, int unit)
  208. {
  209.     TimeCount=time * unit;      
  210.         IsTimeUp = 0;
  211.         while(!IsTimeUp);
  212. }
  213.  
  214. unsigned char getData(void)
  215. {
  216.     unsigned char data=0;
  217.     //delay(1,ms);
  218.  
  219.     data = (LCD_IOP->IDR >> 8);
  220.  
  221.     return data;
  222. }
  223.  
  224. void sendCommand(unsigned int index)
  225. {
  226.     CS_LOW;
  227.     RS_LOW;
  228.     RD_HIGH;
  229.     WR_HIGH;
  230.  
  231.     WR_LOW;
  232.     pushData(index>>8);
  233.     //delay(10,us);
  234.     WR_HIGH;
  235.  
  236.     WR_LOW;
  237.     pushData(index);
  238.     //delay(10,us);
  239.     WR_HIGH;
  240.  
  241.     CS_HIGH;
  242. }
  243.  
  244. void sendData(unsigned int data)
  245. {
  246.     CS_LOW;
  247.     RS_HIGH;
  248.     RD_HIGH;
  249.  
  250.     WR_LOW;
  251.     pushData(data>>8);
  252.     //delay(10,us);
  253.     WR_HIGH;
  254.  
  255.     WR_LOW;
  256.     pushData(data);
  257.     //delay(10,us);
  258.     WR_HIGH;
  259.  
  260.     CS_HIGH;
  261. }
  262.  
  263. unsigned int readRegister(unsigned int index)
  264. {
  265.     unsigned int data=0;
  266.  
  267.     CS_LOW;
  268.     RS_LOW;
  269.     RD_HIGH;
  270.     all_pin_output();
  271.  
  272.     //delay(50,us);
  273.     WR_LOW;
  274.     //delay(50,us);
  275.     pushData(index>>8);
  276.     WR_HIGH;
  277.  
  278.     //delay(50,us);
  279.     WR_LOW;
  280.     //delay(50,us);
  281.     pushData(index);
  282.     WR_HIGH;
  283.  
  284.     all_pin_input();
  285.     all_pin_low();
  286.     RS_HIGH;
  287.  
  288.     //delay(50,us);
  289.     RD_LOW;
  290.     //delay(50,us);
  291.     data = (int)getData()<<8;
  292.     RD_HIGH;
  293.  
  294.     //delay(50,us);
  295.     RD_LOW;
  296.     //delay(50,us);
  297.     data |= getData();
  298.     RD_HIGH;
  299.  
  300.     CS_HIGH;
  301.     all_pin_output();
  302.     return data;
  303. }
  304.  
  305. void init (void)
  306. {
  307.     unsigned char i;
  308.     unsigned int f;
  309.     CS_HIGH;
  310.  
  311.     //all_pin_output();
  312.     //all_pin_low();
  313.  
  314.     delay(100,us);
  315.     IC_CODE = readRegister(0x0);
  316.  
  317.     if(!IC_CODE){
  318.       while(!IC_CODE)
  319.       {
  320.          IC_CODE = readRegister(0x0);  //WAIT UNTIL KNOWN LCD
  321.       }
  322.     }
  323.  
  324.     if(IC_CODE == 0x5408) //SPFD5408B  
  325.     {
  326.         sendCommand(0x0000);
  327.         sendData(0x0001);
  328.         delay(100,ms);
  329.  
  330.         sendCommand(0x0001);
  331.         sendData(0x0000);
  332.         sendCommand(0x0002);
  333.         sendData(0x0700);
  334.         sendCommand(0x0003);
  335.         sendData(0x1030);
  336.         sendCommand(0x0004);
  337.         sendData(0x0000);
  338.         sendCommand(0x0008);
  339.         sendData(0x0207);
  340.         sendCommand(0x0009);
  341.         sendData(0x0000);
  342.         sendCommand(0x000A);
  343.         sendData(0x0000);
  344.         sendCommand(0x000C);
  345.         sendData(0x0000);
  346.         sendCommand(0x000D);
  347.         sendData(0x0000);
  348.         sendCommand(0x000F);
  349.         sendData(0x0000);
  350.         //power on sequence VGHVGL
  351.         sendCommand(0x0010);
  352.         sendData(0x0000);
  353.         sendCommand(0x0011);
  354.         sendData(0x0007);
  355.         sendCommand(0x0012);
  356.         sendData(0x0000);
  357.         sendCommand(0x0013);
  358.         sendData(0x0000);
  359.         //vgh
  360.         sendCommand(0x0010);
  361.         sendData(0x1290);
  362.         sendCommand(0x0011);
  363.         sendData(0x0227);
  364.         delay(100,ms);
  365.         //vregiout
  366.         sendCommand(0x0012);
  367.         sendData(0x001d); //0x001b
  368.         delay(100,ms);
  369.         //vom amplitude
  370.         sendCommand(0x0013);
  371.         sendData(0x1500);
  372.         delay(100,ms);
  373.         //vom H
  374.         sendCommand(0x0029);
  375.         sendData(0x0018);
  376.         sendCommand(0x002B);
  377.         sendData(0x000D);
  378.  
  379.         //gamma
  380.         sendCommand(0x0030);
  381.         sendData(0x0004);
  382.         sendCommand(0x0031);
  383.         sendData(0x0307);
  384.         sendCommand(0x0032);
  385.         sendData(0x0002);// 0006
  386.         sendCommand(0x0035);
  387.         sendData(0x0206);
  388.         sendCommand(0x0036);
  389.         sendData(0x0408);
  390.         sendCommand(0x0037);
  391.         sendData(0x0507);
  392.         sendCommand(0x0038);
  393.         sendData(0x0204);//0200
  394.         sendCommand(0x0039);
  395.         sendData(0x0707);
  396.         sendCommand(0x003C);
  397.         sendData(0x0405);// 0504
  398.         sendCommand(0x003D);
  399.         sendData(0x0F02);
  400.         //ram
  401.         sendCommand(0x0050);
  402.         sendData(0x0000);
  403.         sendCommand(0x0051);
  404.         sendData(0x00EF);
  405.         sendCommand(0x0052);
  406.         sendData(0x0000);
  407.         sendCommand(0x0053);
  408.         sendData(0x013F);
  409.         sendCommand(0x0060);
  410.         sendData(0xA700);
  411.         sendCommand(0x0061);
  412.         sendData(0x0001);
  413.         sendCommand(0x006A);
  414.         sendData(0x0000);
  415.         //
  416.         sendCommand(0x0080);
  417.         sendData(0x0000);
  418.         sendCommand(0x0081);
  419.         sendData(0x0000);
  420.         sendCommand(0x0082);
  421.         sendData(0x0000);
  422.         sendCommand(0x0083);
  423.         sendData(0x0000);
  424.         sendCommand(0x0084);
  425.         sendData(0x0000);
  426.         sendCommand(0x0085);
  427.         sendData(0x0000);
  428.         //
  429.         sendCommand(0x0090);
  430.         sendData(0x0010);
  431.         sendCommand(0x0092);
  432.         sendData(0x0600);
  433.         sendCommand(0x0093);
  434.         sendData(0x0003);
  435.         sendCommand(0x0095);
  436.         sendData(0x0110);
  437.         sendCommand(0x0097);
  438.         sendData(0x0000);
  439.         sendCommand(0x0098);
  440.         sendData(0x0000);
  441.         sendCommand(0x0007);
  442.         sendData(0x0133);
  443.  
  444.         sendCommand(0x0022);//Start to write to display RAM
  445.  
  446.     }
  447.     else
  448.     {
  449.         sendCommand(0x0001);
  450.         sendData(0x0100);
  451.         sendCommand(0x0002);
  452.         sendData(0x0700);
  453.         sendCommand(0x0003);
  454.         sendData(0x1030);
  455.         sendCommand(0x0004);
  456.         sendData(0x0000);
  457.         sendCommand(0x0008);
  458.         sendData(0x0302);
  459.  
  460.         sendCommand(0x000A);
  461.         sendData(0x0000);
  462.         sendCommand(0x000C);
  463.         sendData(0x0000);
  464.         sendCommand(0x000D);
  465.         sendData(0x0000);
  466.         sendCommand(0x000F);
  467.         sendData(0x0000);
  468.  
  469.         delay(100,ms);
  470.  
  471.         sendCommand(0x0030);
  472.         sendData(0x0000);
  473.         sendCommand(0x0031);
  474.         sendData(0x0405);
  475.         sendCommand(0x0032);
  476.         sendData(0x0203);
  477.         sendCommand(0x0035);
  478.         sendData(0x0004);
  479.         sendCommand(0x0036);
  480.         sendData(0x0B07);
  481.         sendCommand(0x0037);
  482.         sendData(0x0000);
  483.         sendCommand(0x0038);
  484.         sendData(0x0405);
  485.         sendCommand(0x0039);
  486.         sendData(0x0203);
  487.         sendCommand(0x003c);
  488.         sendData(0x0004);
  489.         sendCommand(0x003d);
  490.         sendData(0x0B07);
  491.         sendCommand(0x0020);
  492.         sendData(0x0000);
  493.         sendCommand(0x0021);
  494.         sendData(0x0000);
  495.         sendCommand(0x0050);
  496.         sendData(0x0000);
  497.         sendCommand(0x0051);
  498.         sendData(0x00ef);
  499.         sendCommand(0x0052);
  500.         sendData(0x0000);
  501.         sendCommand(0x0053);
  502.         sendData(0x013f);
  503.  
  504.         delay(100,ms);
  505.  
  506.         sendCommand(0x0060);
  507.         sendData(0xa700);
  508.         sendCommand(0x0061);
  509.         sendData(0x0001);
  510.         sendCommand(0x0090);
  511.         sendData(0x003A);
  512.         sendCommand(0x0095);
  513.         sendData(0x021E);
  514.         sendCommand(0x0080);
  515.         sendData(0x0000);
  516.         sendCommand(0x0081);
  517.         sendData(0x0000);
  518.         sendCommand(0x0082);
  519.         sendData(0x0000);
  520.         sendCommand(0x0083);
  521.         sendData(0x0000);
  522.         sendCommand(0x0084);
  523.         sendData(0x0000);
  524.         sendCommand(0x0085);
  525.         sendData(0x0000);
  526.         sendCommand(0x00FF);
  527.         sendData(0x0001);
  528.         sendCommand(0x00B0);
  529.         sendData(0x140D);
  530.         sendCommand(0x00FF);
  531.         sendData(0x0000);
  532.         delay(100,ms);
  533.         sendCommand(0x0007);
  534.         sendData(0x0133);
  535.         delay(50,ms);
  536.         //exit standby
  537.         sendCommand(0x0010);
  538.         sendData(0x14E0);
  539.         delay(100,ms);
  540.         sendCommand(0x0007);
  541.         sendData(0x0133);
  542.         sendCommand(0x0022);
  543.     }
  544.  
  545.     //paint screen black
  546.     for(i=0;i<2;i++)
  547.     {
  548.         for(f=0;f<38400;f++)
  549.         {
  550.             sendData(BLACK);
  551.         }
  552.     }
  553. }
  554. void LCD_WriteReg(unsigned int INDEX, unsigned int DATA)
  555. {
  556.       sendCommand(INDEX);
  557.       sendData(DATA);
  558. }
  559. void setOrientation(unsigned int HV)//horizontal or vertical
  560. {
  561.     sendCommand(0x03);
  562.     if(HV==1)//vertical
  563.     {
  564.         if(IC_CODE == 0x5408) { sendData(0x1038); }
  565.         else { sendData(0x5038); }
  566.     }
  567.     else//horizontal
  568.     {
  569.         if(IC_CODE == 0x5408) { sendData(0x1030); }
  570.         else { sendData(0x5030); }
  571.     }
  572.     sendCommand(0x0022); //Start to write to display RAM
  573. }
  574.  
  575. void   setXY(unsigned int poX, unsigned int poY)
  576. {
  577.     if(poX <0)
  578.         poX = 0;
  579.     if(poX>239)
  580.         poX = 239;
  581.  
  582.     if(poY <0)
  583.         poY = 0;
  584.     if(poY>319)
  585.         poY = 319;
  586.    //poX = constrain(poX,MIN_X,MAX_X); //Limits the pixel range to 0 - 239
  587.    //poY = constrain(poY,MIN_Y,MAX_Y); //Limits the pixel range to 0 - 319
  588.    /* Writing to GRAM beyond the range gives unpredictable results. The above code
  589.       is to limit this. This might also slow down the writing to TFT. You can
  590.       remove the above two lines of code, if you think your application code
  591.       does not write beyond this range. This would speed up TFT writing. */
  592.  
  593.     sendCommand(0x0020);//X
  594.     sendData(poX);
  595.     sendCommand(0x0021);//Y
  596.     sendData(poY);
  597.     sendCommand(0x0022);//Start to write to display RAM
  598. }
  599.  
  600. void setPixel(unsigned int poX, unsigned int poY,unsigned int color)
  601. {
  602.     setXY(poX,poY);
  603.     sendData(color);
  604. }
  605.  
  606. void drawCircle(int poX, int poY, int r,unsigned int color)
  607. {
  608.     int x = -r, y = 0, err = 2-2*r, e2;
  609.     do {
  610.         setPixel(poX-x, poY+y,color);
  611.         setPixel(poX+x, poY+y,color);
  612.         setPixel(poX+x, poY-y,color);
  613.         setPixel(poX-x, poY-y,color);
  614.         e2 = err;
  615.         if (e2 <= y) {
  616.             err += ++y*2+1;
  617.             if (-x == y && e2 <= x) e2 = 0;
  618.         }
  619.         if (e2 > x) err += ++x*2+1;
  620.     }
  621.     while (x <= 0);
  622. }
  623.  
  624. void fillCircle(int poX, int poY, int r,unsigned int color)
  625. {
  626.     int x = -r, y = 0, err = 2-2*r, e2;
  627.     do {
  628.  
  629.         drawVerticalLine(poX-x,poY-y,2*y,color);
  630.         drawVerticalLine(poX+x,poY-y,2*y,color);
  631.  
  632.         e2 = err;
  633.         if (e2 <= y) {
  634.             err += ++y*2+1;
  635.             if (-x == y && e2 <= x) e2 = 0;
  636.         }
  637.         if (e2 > x) err += ++x*2+1;
  638.     }
  639.     while (x <= 0);
  640.  
  641. }
  642.  
  643.  
  644. void drawLine(unsigned int x0,unsigned int y0,unsigned int x1,unsigned int y1,unsigned int color)
  645. {
  646.     int dx = (x1-x0), sx = x0<x1 ? 1 : -1;
  647.     int dy = -(y1-y0), sy = y0<y1 ? 1 : -1;
  648.     int err = dx+dy, e2; /* error value e_xy */
  649.     for (;;){ /* loop */
  650.         setPixel(x0,y0,color);
  651.         e2 = 2*err;
  652.         if (e2 >= dy) { /* e_xy+e_x > 0 */
  653.             if (x0 == x1) break;
  654.             err += dy;
  655.             x0 += sx;
  656.         }
  657.         if (e2 <= dx) { /* e_xy+e_y < 0 */
  658.             if (y0 == y1) break;
  659.             err += dx;
  660.             y0 += sy;
  661.         }
  662.     }
  663. }
  664.  
  665.  
  666. void drawVerticalLine(unsigned int poX, unsigned int poY,unsigned int length,unsigned int color)
  667. {
  668.     unsigned int i;
  669.     if(poX <0)
  670.         poX = 0;
  671.     if(poX>239)
  672.         poX = 239;
  673.  
  674.     if(poY <0)
  675.         poY = 0;
  676.     if(poY>319)
  677.         poY = 319;
  678.     //poX = constrain(poX,MIN_X,MAX_X); //Limits the pixel range to 0 - 239
  679.     //poY = constrain(poY,MIN_Y,MAX_Y); //Limits the pixel range to 0 - 319
  680.  
  681.     setXY(poX,poY);
  682.     setOrientation(1);
  683.     if(length+poY>MAX_Y)
  684.     {
  685.         length=MAX_Y-poY;
  686.     }
  687.  
  688.     for(i=0;i<length;i++)
  689.     {
  690.         sendData(color);
  691.     }
  692. }
  693.  
  694. void  drawHorizontalLine(unsigned int poX, unsigned int poY,unsigned int length,unsigned int color)
  695. {
  696.     unsigned int i;
  697.     if(poX <0)
  698.         poX = 0;
  699.     if(poX>239)
  700.         poX = 239;
  701.  
  702.     if(poY <0)
  703.         poY = 0;
  704.     if(poY>319)
  705.         poY = 319;
  706.     //poX = constrain(poX,MIN_X,MAX_X); //Limits the pixel range to 0 - 239
  707.     //poY = constrain(poY,MIN_Y,MAX_Y); //Limits the pixel range to 0 - 319
  708.  
  709.     setXY(poX,poY);
  710.     setOrientation(0);
  711.     if(length+poX>MAX_X)
  712.     {
  713.         length=MAX_X-poX;
  714.     }
  715.     for(i=0;i<length;i++)
  716.     {
  717.         sendData(color);
  718.     }
  719. }
  720.  
  721.  
  722. void drawRectangle(unsigned int poX, unsigned int poY, unsigned int length,unsigned int width,unsigned int color)
  723. {
  724.     drawHorizontalLine(poX, poY, length, color);
  725.     drawHorizontalLine(poX, poY+width, length, color);
  726.  
  727.     drawVerticalLine(poX, poY, width,color);
  728.     drawVerticalLine(poX + length, poY, width,color);
  729. }
  730.  
  731. void fillRectangle(unsigned int poX, unsigned int poY, unsigned int length, unsigned int width, unsigned int color)
  732. {
  733.     unsigned int i;
  734.     for(i=0;i<width;i++)
  735.     {
  736.         drawHorizontalLine(poX, poY+i, length, color);
  737.     }
  738. }
  739.  
  740. void drawChar(unsigned char ascii,unsigned int poX, unsigned int poY,unsigned int size, unsigned int fgcolor)
  741. {
  742.     unsigned char i;
  743.     unsigned char temp;
  744.     unsigned char f;
  745.     setXY(poX,poY);
  746.     setOrientation(1);
  747.     if((ascii < 0x20)||(ascii > 0x7e))//Unsupported char.
  748.     {
  749.         ascii = '?';
  750.     }
  751.     for(i=0;i<8;i++)
  752.     {
  753.         temp = simpleFont[ascii-0x20][i];
  754.         for(f=0;f<8;f++)
  755.         {
  756.             if((temp>>f)&0x01)
  757.             {
  758.                 fillRectangle(poX+i*size, poY+f*size, size, size, fgcolor);
  759.             }
  760.  
  761.         }
  762.     }
  763. }
  764.  
  765. void drawString(char *string,unsigned int poX, unsigned int poY,unsigned int size,unsigned int fgcolor)
  766. {
  767.     while(*string)
  768.     {
  769.          drawChar(*string, poX, poY, size, fgcolor);
  770.          string++;
  771.  
  772.         if(poX < MAX_X)
  773.         {
  774.             poX+=8*size; // Move cursor right
  775.         }
  776.     }
  777. }
  778.  
  779. unsigned char drawNumber(long long_num,unsigned int poX, unsigned int poY,unsigned int size,unsigned int fgcolor)
  780. {
  781.   unsigned char char_buffer[10]="";
  782.   unsigned char i = 0;
  783.   unsigned char f = 0;
  784.  
  785.   if (long_num < 0)
  786.   {
  787.     f=1;
  788.     drawChar('-',poX, poY, size, fgcolor);
  789.     long_num = -long_num;
  790.     if(poX < MAX_X)
  791.     {
  792.         poX+=8*size; // Move cursor right
  793.     }
  794.   }
  795.   else if (long_num == 0)
  796.   {
  797.     f=1;
  798.     drawChar('0',poX, poY, size, fgcolor);
  799.     return f;
  800.     if(poX < MAX_X)
  801.     {
  802.         poX+=8*size; // Move cursor right
  803.     }
  804.   }
  805.  
  806.  
  807.   while (long_num > 0)
  808.   {
  809.     char_buffer[i++] = long_num % 10;
  810.     long_num /= 10;
  811.   }
  812.  
  813.   f=f+i;
  814.   for(; i > 0; i--)
  815.   {
  816.     drawChar('0'+ char_buffer[i - 1],poX, poY, size, fgcolor);
  817.     if(poX < MAX_X)
  818.     {
  819.         poX+=8*size; // Move cursor right
  820.     }
  821.   }
  822.   return f;
  823. }
  824.  
  825. unsigned char drawFloat(float floatNumber,unsigned char decimal,unsigned int poX, unsigned int poY,unsigned int size,unsigned int fgcolor)
  826. {
  827.   unsigned int temp=0;
  828.   float decy=0.0;
  829.   float rounding = 0.5;
  830.   unsigned char f=0;
  831.   unsigned char i;
  832.   unsigned char howlong;
  833.  
  834.   if(floatNumber<0.0)
  835.   {
  836.     drawChar('-',poX, poY, size, fgcolor);
  837.     floatNumber = -floatNumber;
  838.     if(poX < MAX_X)
  839.     {
  840.         poX+=8*size; // Move cursor right
  841.     }
  842.     f =1;
  843.   }
  844.   for (i=0; i<decimal; ++i)
  845.   {
  846.     rounding /= 10.0;
  847.   }
  848.   floatNumber += rounding;
  849.  
  850.   temp = (unsigned int)floatNumber;
  851.   howlong=drawNumber(temp,poX, poY, size, fgcolor);
  852.   f += howlong;
  853.   if((poX+8*size*howlong) < MAX_X)
  854.     {
  855.         poX+=8*size*howlong; // Move cursor right
  856.     }
  857.  
  858.   if(decimal>0)
  859.   {
  860.     drawChar('.',poX, poY, size, fgcolor);
  861.     if(poX < MAX_X)
  862.     {
  863.         poX+=8*size; // Move cursor right
  864.     }
  865.     f +=1;
  866.  }
  867.   decy = floatNumber-temp;//decimal part,
  868.   for(i=0;i<decimal;i++)//4
  869.   {
  870.     decy *=10;// for the next decimal
  871.     temp = decy;//get the decimal
  872.     drawNumber(temp,poX, poY, size, fgcolor);
  873.     floatNumber = -floatNumber;
  874.     if(poX < MAX_X)
  875.     {
  876.         poX+=8*size; // Move cursor right
  877.     }
  878.     decy -= temp;
  879.   }
  880.   f +=decimal;
  881.   return (unsigned char)f;
  882. }
  883.  
  884. void all_pin_input(void)
  885. {
  886.     LCDIO_Setup.GPIO_Mode = GPIO_Mode_IN;   //SET PORT AS INPUT
  887.     GPIO_Init(LCD_IOP, &LCDIO_Setup);   //Re-Init Port
  888.  
  889.     //LCDCTR_Setup.GPIO_Mode = GPIO_Mode_IN;    //SET PORT AS INPUT
  890.     //GPIO_Init(LCD_CTP, &LCDCTR_Setup);    //Re-Init Port
  891. }
  892.  
  893. void all_pin_output(void)
  894. {
  895.     LCDIO_Setup.GPIO_Mode = GPIO_Mode_OUT;  //SET PORT AS INPUT
  896.     GPIO_Init(LCD_IOP, &LCDIO_Setup);   //Re-Init Port
  897.  
  898.     //LCDCTR_Setup.GPIO_Mode = GPIO_Mode_OUT;   //SET PORT AS INPUT
  899.     //GPIO_Init(LCD_CTP, &LCDCTR_Setup);    //Re-Init Port
  900. }
  901.  
  902. void all_pin_low(void)
  903. {
  904.     LCD_IOP->BSRRH = LCD_PINS;
  905.        // LCD_CTP->BSRRH = LCD_CONT;
  906. }
Advertisement
Add Comment
Please, Sign In to add comment