Advertisement
Guest User

ST7735.h

a guest
Mar 27th, 2017
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 19.05 KB | None | 0 0
  1. #include "Config.h"
  2. #include "Peripheral.h"
  3. #include "font.h"
  4. //#include "MDD File System/FSIO.h"
  5. //#include "MDD File System/SD-SPI.h"
  6. //#include "uart.h"
  7. //#include "colors.h"
  8.  
  9. #define swap(a, b) { int t = a; a = b; b = t; }
  10.  
  11. #define SCREEN_WIDTH 128
  12. #define SCREEN_HEIGHT 160
  13.  
  14. #define ST7735_NOP 0x0
  15. #define ST7735_SWRESET 0x01
  16. #define ST7735_RDDID 0x04
  17. #define ST7735_RDDST 0x09
  18.  
  19. #define ST7735_SLPIN  0x10
  20. #define ST7735_SLPOUT  0x11
  21. #define ST7735_PTLON  0x12
  22. #define ST7735_NORON  0x13
  23.  
  24. #define ST7735_INVOFF 0x20
  25. #define ST7735_INVON 0x21
  26. #define ST7735_DISPOFF 0x28
  27. #define ST7735_DISPON 0x29
  28. #define ST7735_CASET 0x2A
  29. #define ST7735_RASET 0x2B
  30. #define ST7735_RAMWR 0x2C
  31. #define ST7735_RAMRD 0x2E
  32.  
  33. #define ST7735_COLMOD 0x3A
  34. #define ST7735_MADCTL 0x36
  35.  
  36.  
  37. #define ST7735_FRMCTR1 0xB1
  38. #define ST7735_FRMCTR2 0xB2
  39. #define ST7735_FRMCTR3 0xB3
  40. #define ST7735_INVCTR 0xB4
  41. #define ST7735_DISSET5 0xB6
  42.  
  43. #define ST7735_PWCTR1 0xC0
  44. #define ST7735_PWCTR2 0xC1
  45. #define ST7735_PWCTR3 0xC2
  46. #define ST7735_PWCTR4 0xC3
  47. #define ST7735_PWCTR5 0xC4
  48. #define ST7735_VMCTR1 0xC5
  49.  
  50. #define ST7735_RDID1 0xDA
  51. #define ST7735_RDID2 0xDB
  52. #define ST7735_RDID3 0xDC
  53. #define ST7735_RDID4 0xDD
  54.  
  55. #define ST7735_PWCTR6 0xFC
  56.  
  57. #define ST7735_GMCTRP1 0xE0
  58. #define ST7735_GMCTRN1 0xE1
  59. //#define RGB565(r,g,b) ((((r>>3)<<11) | ((g>>2)<<5) | (b>>3)))
  60.  
  61. // Color defenitions //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  62.  
  63. #define BLACK   0x0000
  64. #define BLUE    0x001F
  65. #define RED     0xF800
  66. #define GREEN   0x0700
  67. #define CYAN    0x07FF
  68. #define MAGENTA 0xF81F
  69. #define YELLOW  0xFFE0
  70. #define WHITE   0xFFFF
  71.  
  72.  
  73.  
  74.  
  75. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  76. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  77. void setAddrWindow(unsigned char x0, unsigned char y0, unsigned char x1, unsigned char y1);
  78. void ST7735_pushColor(unsigned int color); // for what?
  79. void ST7735_drawPixel(unsigned char x, unsigned char y, unsigned int color);
  80. void ST7735_fillScreen(unsigned int color);
  81. void ST7735_initB(void); // not used
  82. void ST7735_initR(void); // initialize a ST7735 chip
  83. void ST7735_drawString(unsigned char x, unsigned char y, char *c, unsigned int color, unsigned char size);
  84. void ST7735_drawChar(unsigned char x, unsigned char y, char c, unsigned int color, unsigned char size);
  85. void ST7735_fillCircle(unsigned char x0, unsigned char y0, unsigned char r, unsigned int color);
  86. void ST7735_drawCircle(unsigned char x0, unsigned char y0, unsigned char r, unsigned int color);
  87. void ST7735_setRotation(unsigned char m);
  88. void ST7735_drawRect(unsigned char x, unsigned char y, unsigned char w, unsigned char h, unsigned int color);
  89. void ST7735_fillRect(unsigned char x, unsigned char y, unsigned char w, unsigned char h, unsigned int color);
  90. void ST7735_drawVerticalLine(unsigned char x, unsigned char y, unsigned char length, unsigned int color);
  91. void ST7735_drawHorizontalLine(unsigned char x, unsigned char y, unsigned char length, unsigned int color);
  92. void ST7735_drawFastLine(unsigned char x, unsigned char y, unsigned char length, unsigned int color, unsigned char rotflag);
  93. void ST7735_drawLine(int x0, int y0, int x1, int y1, unsigned int color);
  94. unsigned char ST7735_getRotation();
  95.  
  96. void loadBitmapToLCD(char* filename);
  97.  
  98. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  99. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  100.  
  101.  
  102.  
  103. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  104. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  105. #define LCD_SCK LATDbits.LATD7
  106. #define LCD_SDO LATDbits.LATD6
  107.  
  108. char madctl;
  109.  
  110. void write_spi_byte(unsigned char c){
  111.     // using software SPI
  112. //   char x;
  113. //   for(x=0;x<8;x++){       //clock the byte out
  114. //      LCD_SCK = 0;
  115. //      LCD_SDO = 0;
  116. //      if(c & 0x80)
  117. //      LCD_SDO = 1;
  118. //      LCD_SCK = 1;
  119. //      c <<= 1;
  120. //   }
  121.  
  122.     // using hardware SPI
  123.      SPI_write(c); //
  124. }
  125.  
  126.  
  127. void writecommand(unsigned char c)
  128. {
  129.     LCD1_RS = 0;
  130.     LCD1_CS = 0;
  131.     write_spi_byte(c);
  132.     LCD1_CS = 1;
  133. }
  134.  
  135. void writedata(unsigned char c)
  136. {
  137.     LCD1_RS = 1;
  138.     LCD1_CS = 0;
  139.     write_spi_byte(c);
  140.     LCD1_CS = 1;
  141. }
  142.  
  143. void setAddrWindow(unsigned char x0, unsigned char y0, unsigned char x1, unsigned char y1)
  144. {
  145.     writecommand(ST7735_CASET);  // column addr set
  146.     writedata(0x00);
  147.     writedata(x0+0);   // XSTART
  148.     writedata(0x00);
  149.     writedata(x1+0);   // XEND
  150.  
  151.     writecommand(ST7735_RASET);  // row addr set
  152.     writedata(0x00);
  153.     writedata(y0+0);    // YSTART
  154.     writedata(0x00);
  155.     writedata(y1+0);    // YEND
  156.  
  157.     writecommand(ST7735_RAMWR);  // write to RAM
  158. }
  159.  
  160. void ST7735_pushColor(unsigned int color)
  161. {
  162.     LCD1_RS = 1;
  163.     LCD1_CS = 0;
  164.     write_spi_byte(color >> 8);
  165.     write_spi_byte(color);
  166.     LCD1_CS = 1;
  167. }
  168.  
  169. void ST7735_drawPixel(unsigned char x, unsigned char y, unsigned int color)
  170. {
  171.     setAddrWindow(x, y, x+1, y+1);
  172.  
  173.     LCD1_RS = 1;
  174.     LCD1_CS = 0;
  175.  
  176.     write_spi_byte(color >> 8);    
  177.     write_spi_byte(color);  
  178.  
  179.     LCD1_CS = 1;
  180. }
  181.  
  182. void ST7735_fillScreen(unsigned int color)
  183. {
  184.     unsigned char x;
  185.     unsigned char y;
  186.  
  187.     setAddrWindow(0, 0, SCREEN_WIDTH-1, SCREEN_HEIGHT-1);
  188.  
  189.     // setup for data
  190.     LCD1_RS = 1;
  191.     LCD1_CS = 0;
  192.    
  193.     unsigned char colorB = color >> 8;
  194.  
  195.     for (x=0; x < SCREEN_WIDTH; x++) {
  196.         for (y=0; y < SCREEN_HEIGHT; y++) {
  197.             write_spi_byte(colorB);    
  198.             write_spi_byte(color);  
  199.         }
  200.     }
  201.  
  202.     LCD1_CS = 1;
  203. }
  204.  
  205. void ST7735_initB(void) {
  206.     LCD1_RESET = 1;
  207.     __delay_us(500);
  208.     LCD1_RESET = 0;
  209.     __delay_us(500);
  210.     LCD1_RESET = 1;
  211.     __delay_us(500);
  212.  
  213.     LCD1_CS = 0;
  214.  
  215.     writecommand(ST7735_SWRESET); // software reset
  216.     __delay_us(50);
  217.     writecommand(ST7735_SLPOUT);  // out of sleep mode
  218.     __delay_us(500);
  219.  
  220.     writecommand(ST7735_COLMOD);  // set color mode
  221.     writedata(0x05);        // 16-bit color
  222.     __delay_us(10);
  223.  
  224.     writecommand(ST7735_FRMCTR1);  // frame rate control
  225.     writedata(0x00);  // fastest refresh
  226.     writedata(0x06);  // 6 lines front porch
  227.     writedata(0x03);  // 3 lines backporch
  228.     __delay_us(10);
  229.  
  230.     writecommand(ST7735_MADCTL);  // memory access control (directions)
  231.     writedata(0x08);  // row address/col address, bottom to top refresh
  232.     madctl = 0x08;
  233.  
  234.     writecommand(ST7735_DISSET5);  // display settings #5
  235.     writedata(0x15);  // 1 clock cycle nonoverlap, 2 cycle gate rise, 3 cycle oscil. equalize
  236.     writedata(0x02);  // fix on VTL
  237.  
  238.     writecommand(ST7735_INVCTR);  // display inversion control
  239.     writedata(0x0);  // line inversion
  240.  
  241.     writecommand(ST7735_PWCTR1);  // power control
  242.     writedata(0x02);      // GVDD = 4.7V
  243.     writedata(0x70);      // 1.0uA
  244.     __delay_us(10);
  245.     writecommand(ST7735_PWCTR2);  // power control
  246.     writedata(0x05);      // VGH = 14.7V, VGL = -7.35V
  247.     writecommand(ST7735_PWCTR3);  // power control
  248.     writedata(0x01);      // Opamp current small
  249.     writedata(0x02);      // Boost frequency
  250.  
  251.  
  252.     writecommand(ST7735_VMCTR1);  // power control
  253.     writedata(0x3C);      // VCOMH = 4V
  254.     writedata(0x38);      // VCOML = -1.1V
  255.     __delay_us(10);
  256.  
  257.     writecommand(ST7735_PWCTR6);  // power control
  258.     writedata(0x11);
  259.     writedata(0x15);
  260.  
  261.     writecommand(ST7735_GMCTRP1);
  262.     writedata(0x0f);    //writedata(0x09);  
  263.     writedata(0x1a);  //writedata(0x16);
  264.     writedata(0x0f);  //writedata(0x09);
  265.     writedata(0x18);  //writedata(0x20);
  266.     writedata(0x2f);  //writedata(0x21);
  267.     writedata(0x28);  //writedata(0x1B);
  268.     writedata(0x20);  //writedata(0x13);
  269.     writedata(0x22);  //writedata(0x19);
  270.     writedata(0x1f);  //writedata(0x17);
  271.     writedata(0x1b);  //writedata(0x15);
  272.     writedata(0x23);  //writedata(0x1E);
  273.     writedata(0x37);  //writedata(0x2B);
  274.     writedata(0x00);  //writedata(0x04);
  275.     writedata(0x07);  //writedata(0x05);
  276.     writedata(0x02);  //writedata(0x02);
  277.     writedata(0x10);  //writedata(0x0E);
  278.     writecommand(ST7735_GMCTRN1);
  279.     writedata(0x0f);   //writedata(0x0B);
  280.     writedata(0x1b);   //writedata(0x14);
  281.     writedata(0x0f);   //writedata(0x08);
  282.     writedata(0x17);   //writedata(0x1E);
  283.     writedata(0x33);   //writedata(0x22);
  284.     writedata(0x2c);   //writedata(0x1D);
  285.     writedata(0x29);   //writedata(0x18);
  286.     writedata(0x2e);   //writedata(0x1E);
  287.     writedata(0x30);   //writedata(0x1B);
  288.     writedata(0x30);   //writedata(0x1A);
  289.     writedata(0x39);   //writedata(0x24);
  290.     writedata(0x3f);   //writedata(0x2B);
  291.     writedata(0x00);   //writedata(0x06);
  292.     writedata(0x07);   //writedata(0x06);
  293.     writedata(0x03);   //writedata(0x02);
  294.     writedata(0x10);   //writedata(0x0F);
  295.     __delay_us(10);
  296.  
  297.     writecommand(ST7735_CASET);  // column addr set
  298.     writedata(0x00);
  299.     writedata(0x02);   // XSTART = 2
  300.     writedata(0x00);
  301.     writedata(0x81);   // XEND = 129
  302.  
  303.     writecommand(ST7735_RASET);  // row addr set
  304.     writedata(0x00);
  305.     writedata(0x02);    // XSTART = 1
  306.     writedata(0x00);
  307.     writedata(0x81);    // XEND = 160
  308.  
  309.     writecommand(ST7735_NORON);  // normal display on
  310.     __delay_us(10);
  311.  
  312.     writecommand(ST7735_RAMWR);
  313.     __delay_us(500);
  314.  
  315.     writecommand(ST7735_DISPON);
  316.     __delay_us(500);
  317.  
  318.     LCD1_CS = 1;
  319. }
  320.  
  321. void ST7735_initR(void) {
  322.     LCD1_RESET = 1;
  323.     __delay_us(500);
  324.     LCD1_RESET = 0;
  325.     __delay_us(500);
  326.     LCD1_RESET = 1;
  327.     __delay_us(500);
  328.  
  329.     LCD1_CS = 0;
  330.  
  331.     writecommand(ST7735_SWRESET); // software reset
  332.     __delay_us(150);
  333.  
  334.     writecommand(ST7735_SLPOUT);  // out of sleep mode
  335.     __delay_us(500);
  336.  
  337.     writecommand(ST7735_COLMOD);  // set color mode
  338.     writedata(0x05);        // 16-bit color
  339.     __delay_us(10);
  340.  
  341.     writecommand(ST7735_FRMCTR1);  // frame rate control - normal mode
  342.     writedata(0x01);  // frame rate = fosc / (1 x 2 + 40) * (LINE + 2C + 2D)
  343.     writedata(0x2C);
  344.     writedata(0x2D);
  345.  
  346.     writecommand(ST7735_FRMCTR2);  // frame rate control - idle mode
  347.     writedata(0x01);  // frame rate = fosc / (1 x 2 + 40) * (LINE + 2C + 2D)
  348.     writedata(0x2C);
  349.     writedata(0x2D);
  350.  
  351.     writecommand(ST7735_FRMCTR3);  // frame rate control - partial mode
  352.     writedata(0x01); // dot inversion mode
  353.     writedata(0x2C);
  354.     writedata(0x2D);
  355.     writedata(0x01); // line inversion mode
  356.     writedata(0x2C);
  357.     writedata(0x2D);
  358.  
  359.     writecommand(ST7735_INVCTR);  // display inversion control
  360.     writedata(0x07);  // no inversion
  361.  
  362.     writecommand(ST7735_PWCTR1);  // power control
  363.     writedata(0xA2);      
  364.     writedata(0x02);      // -4.6V
  365.     writedata(0x84);      // AUTO mode
  366.  
  367.     writecommand(ST7735_PWCTR2);  // power control
  368.     writedata(0xC5);      // VGH25 = 2.4C VGSEL = -10 VGH = 3 * AVDD
  369.  
  370.     writecommand(ST7735_PWCTR3);  // power control
  371.     writedata(0x0A);      // Opamp current small
  372.     writedata(0x00);      // Boost frequency
  373.  
  374.     writecommand(ST7735_PWCTR4);  // power control
  375.     writedata(0x8A);      // BCLK/2, Opamp current small & Medium low
  376.     writedata(0x2A);    
  377.  
  378.     writecommand(ST7735_PWCTR5);  // power control
  379.     writedata(0x8A);    
  380.     writedata(0xEE);    
  381.  
  382.     writecommand(ST7735_VMCTR1);  // power control
  383.     writedata(0x0E);  
  384.  
  385.     writecommand(ST7735_INVOFF);    // don't invert display
  386.  
  387.     writecommand(ST7735_MADCTL);  // memory access control (directions)
  388.  
  389.     // http://www.adafruit.com/forums/viewtopic.php?f=47&p=180341
  390.  
  391.     // R and B byte are swapped
  392.     // madctl = 0xC8;
  393.  
  394.     // normal R G B order
  395.     madctl = 0xC0;
  396.     writedata(madctl);  // row address/col address, bottom to top refresh
  397.  
  398.     writecommand(ST7735_COLMOD);  // set color mode
  399.     writedata(0x05);        // 16-bit color
  400.  
  401.     writecommand(ST7735_CASET);  // column addr set
  402.     writedata(0x00);
  403.     writedata(0x00);   // XSTART = 0
  404.     writedata(0x00);
  405.     writedata(0x7F);   // XEND = 127
  406.  
  407.     writecommand(ST7735_RASET);  // row addr set
  408.     writedata(0x00);
  409.     writedata(0x00);    // XSTART = 0
  410.     writedata(0x00);
  411.     writedata(0x9F);    // XEND = 159
  412.  
  413.     writecommand(ST7735_GMCTRP1);
  414.     writedata(0x0f);
  415.     writedata(0x1a);
  416.     writedata(0x0f);
  417.     writedata(0x18);
  418.     writedata(0x2f);
  419.     writedata(0x28);
  420.     writedata(0x20);
  421.     writedata(0x22);
  422.     writedata(0x1f);
  423.     writedata(0x1b);
  424.     writedata(0x23);
  425.     writedata(0x37);
  426.     writedata(0x00);
  427.     writedata(0x07);
  428.     writedata(0x02);
  429.     writedata(0x10);
  430.     writecommand(ST7735_GMCTRN1);
  431.     writedata(0x0f);
  432.     writedata(0x1b);
  433.     writedata(0x0f);
  434.     writedata(0x17);
  435.     writedata(0x33);
  436.     writedata(0x2c);
  437.     writedata(0x29);
  438.     writedata(0x2e);
  439.     writedata(0x30);
  440.     writedata(0x30);
  441.     writedata(0x39);
  442.     writedata(0x3f);
  443.     writedata(0x00);
  444.     writedata(0x07);
  445.     writedata(0x03);
  446.     writedata(0x10);
  447.  
  448.     writecommand(ST7735_DISPON);
  449.     __delay_us(100);
  450.  
  451.     writecommand(ST7735_NORON);  // normal display on
  452.     __delay_us(10);
  453.  
  454.     LCD1_CS = 1;
  455. }
  456.  
  457. void ST7735_drawString(unsigned char x, unsigned char y, char *c, unsigned int color, unsigned char size)
  458. {
  459.     while (c[0] != 0) {
  460.         ST7735_drawChar(x, y, c[0], color, size);
  461.         x += size*6;
  462.         c++;
  463.         if (x + 5 >= SCREEN_WIDTH) {
  464.             y += 10;
  465.             x = 0;
  466.         }
  467.     }
  468. }
  469.  
  470. void ST7735_drawChar(unsigned char x, unsigned char y, char c, unsigned int color, unsigned char size)
  471. {
  472.     unsigned char i, j;
  473.  
  474.     unsigned char letter = c < 0x52 ? c - 0x20 : c - 0x52;
  475.     for (i =0; i<5; i++ ) {
  476.         unsigned char line = c < 0x52 ? Alpha1[letter*5+i] : Alpha2[letter*5+i];
  477.  
  478.         for (j = 0; j<8; j++) {
  479.             if (line & 0x1) {
  480.                 if (size == 1) // default size
  481.                 ST7735_drawPixel(x+i, y+j, color);
  482.                 else {  // big size
  483.                     ST7735_fillRect(x+i*size, y+j*size, size, size, color);
  484.                 }
  485.             }
  486.             line >>= 1;
  487.         }
  488.     }
  489. }
  490.  
  491. // fill a circle
  492. void ST7735_fillCircle(unsigned char x0, unsigned char y0, unsigned char r, unsigned int color) {
  493.     int f = 1 - r;
  494.     int ddF_x = 1;
  495.     int ddF_y = -2 * r;
  496.     int x = 0;
  497.     int y = r;
  498.  
  499.     ST7735_drawVerticalLine(x0, y0-r, 2*r+1, color);
  500.  
  501.     while (x<y) {
  502.         if (f >= 0) {
  503.             y--;
  504.             ddF_y += 2;
  505.             f += ddF_y;
  506.         }
  507.         x++;
  508.         ddF_x += 2;
  509.         f += ddF_x;
  510.  
  511.         ST7735_drawVerticalLine(x0+x, y0-y, 2*y+1, color);
  512.         ST7735_drawVerticalLine(x0-x, y0-y, 2*y+1, color);
  513.         ST7735_drawVerticalLine(x0+y, y0-x, 2*x+1, color);
  514.         ST7735_drawVerticalLine(x0-y, y0-x, 2*x+1, color);
  515.     }
  516. }
  517.  
  518. // draw a circle outline
  519. void ST7735_drawCircle(unsigned char x0, unsigned char y0, unsigned char r, unsigned int color) {
  520.     int f = 1 - r;
  521.     int ddF_x = 1;
  522.     int ddF_y = -2 * r;
  523.     int x = 0;
  524.     int y = r;
  525.  
  526.     ST7735_drawPixel(x0, y0+r, color);
  527.     ST7735_drawPixel(x0, y0-r, color);
  528.     ST7735_drawPixel(x0+r, y0, color);
  529.     ST7735_drawPixel(x0-r, y0, color);
  530.  
  531.     while (x<y) {
  532.         if (f >= 0) {
  533.             y--;
  534.             ddF_y += 2;
  535.             f += ddF_y;
  536.         }
  537.         x++;
  538.         ddF_x += 2;
  539.         f += ddF_x;
  540.  
  541.         ST7735_drawPixel(x0 + x, y0 + y, color);
  542.         ST7735_drawPixel(x0 - x, y0 + y, color);
  543.         ST7735_drawPixel(x0 + x, y0 - y, color);
  544.         ST7735_drawPixel(x0 - x, y0 - y, color);
  545.        
  546.         ST7735_drawPixel(x0 + y, y0 + x, color);
  547.         ST7735_drawPixel(x0 - y, y0 + x, color);
  548.         ST7735_drawPixel(x0 + y, y0 - x, color);
  549.         ST7735_drawPixel(x0 - y, y0 - x, color);
  550.        
  551.     }
  552. }
  553.  
  554. unsigned char ST7735_getRotation() {
  555.     return madctl;
  556. }
  557.  
  558. void ST7735_setRotation(unsigned char m) {
  559.     madctl = m;
  560.     writecommand(ST7735_MADCTL);  // memory access control (directions)
  561.     writedata(madctl);  // row address/col address, bottom to top refresh
  562. }
  563.  
  564. // draw a rectangle
  565. void ST7735_drawRect(unsigned char x, unsigned char y, unsigned char w, unsigned char h,
  566. unsigned int color) {
  567.     // smarter version
  568.     ST7735_drawHorizontalLine(x, y, w, color);
  569.     ST7735_drawHorizontalLine(x, y+h-1, w, color);
  570.     ST7735_drawVerticalLine(x, y, h, color);
  571.     ST7735_drawVerticalLine(x+w-1, y, h, color);
  572. }
  573.  
  574. void ST7735_fillRect(unsigned char x, unsigned char y, unsigned char w, unsigned char h,
  575. unsigned int color) {
  576.     // smarter version
  577.  
  578.     setAddrWindow(x, y, x+w-1, y+h-1);
  579.  
  580.     // setup for data
  581.     LCD1_RS = 1;
  582.     LCD1_CS = 0;
  583.  
  584.     unsigned char colorB = color >> 8;
  585.     for (x=0; x < w; x++) {
  586.         for (y=0; y < h; y++) {
  587.             write_spi_byte(colorB);    
  588.             write_spi_byte(color);    
  589.         }
  590.     }
  591.  
  592.     LCD1_CS = 1;
  593. }
  594.  
  595. void ST7735_drawVerticalLine(unsigned char x, unsigned char y, unsigned char length, unsigned int color)
  596. {
  597.     if (x >= SCREEN_WIDTH) return;
  598.     if (y+length >= SCREEN_HEIGHT) length = SCREEN_HEIGHT-y-1;
  599.  
  600.     ST7735_drawFastLine(x,y,length,color,1);
  601. }
  602.  
  603. void ST7735_drawHorizontalLine(unsigned char x, unsigned char y, unsigned char length, unsigned int color)
  604. {
  605.     if (y >= SCREEN_HEIGHT) return;
  606.     if (x+length >= SCREEN_WIDTH) length = SCREEN_WIDTH-x-1;
  607.  
  608.     ST7735_drawFastLine(x,y,length,color,0);
  609. }
  610.  
  611. void ST7735_drawFastLine(unsigned char x, unsigned char y, unsigned char length,
  612. unsigned int color, unsigned char rotflag)
  613. {
  614.     if (rotflag) {
  615.         setAddrWindow(x, y, x, y+length);
  616.     } else {
  617.         setAddrWindow(x, y, x+length, y+1);
  618.     }
  619.     // setup for data
  620.     LCD1_RS = 1;
  621.     LCD1_CS = 0;
  622.  
  623.     unsigned char colorB = color >> 8;
  624.     while (length--) {
  625.         write_spi_byte(colorB);    
  626.         write_spi_byte(color);    
  627.     }
  628.     LCD1_CS = 1;
  629. }
  630.  
  631. int abs(int val)
  632. {
  633.     return (val > 0) ? val : -val;
  634. }
  635.  
  636. // bresenham's algorithm - thx wikpedia
  637. void ST7735_drawLine(int x0, int y0, int x1, int y1,
  638. unsigned int color) {
  639.     unsigned int steep = abs(y1 - y0) > abs(x1 - x0);
  640.     if (steep) {
  641.         swap(x0, y0);
  642.         swap(x1, y1);
  643.     }
  644.  
  645.     if (x0 > x1) {
  646.         swap(x0, x1);
  647.         swap(y0, y1);
  648.     }
  649.  
  650.     unsigned int dx, dy;
  651.     dx = x1 - x0;
  652.     dy = abs(y1 - y0);
  653.  
  654.     int err = dx / 2;
  655.     int ystep;
  656.  
  657.     if (y0 < y1) {
  658.         ystep = 1;
  659.     } else {
  660.         ystep = -1;}
  661.  
  662.     for (; x0<=x1; x0++) {
  663.         if (steep) {
  664.             ST7735_drawPixel(y0, x0, color);
  665.         } else {
  666.             ST7735_drawPixel(x0, y0, color);
  667.         }
  668.         err -= dy;
  669.         if (err < 0) {
  670.             y0 += ystep;
  671.             err += dx;
  672.         }
  673.     }
  674. }
  675.  
  676.  
  677. //void loadBitmapToLCD(char* filename)
  678. //{
  679. //  char buf[30];
  680. //  int i;
  681. //  FSFILE * pointer;
  682. //
  683. //#define BUFFER_LENGTH 1024
  684. //#define BMP_HEADER_SIZE 54
  685. //
  686. //  unsigned char buffer[BUFFER_LENGTH];
  687. //
  688. //  sprintf(buf, "Loading file %s", filename); 
  689. //  SendUARTStr(buf);
  690. //
  691. //  pointer = FSfopen (filename, FS_READ);
  692. //  if (pointer == NULL)
  693. //  {
  694. //      SendUARTStr("Error opening file.");
  695. //      return;
  696. //  }
  697. //
  698. //  SendUARTStr("Showing on LCD. Assuming 24-bit bitmap");
  699. //
  700. //  unsigned int bytesRead = 0;
  701. //  unsigned char lastRed;
  702. //  unsigned char lastGreen;
  703. //  unsigned char lastBlue;
  704. //  unsigned char curX = 0;
  705. //  unsigned char curY = 0;
  706. //  unsigned int batchCount = 0;
  707. //  long totalBytes = 0;
  708. //  unsigned char bitmapWidth = 128;
  709. //  unsigned char bitmapHeight = 160;
  710. //
  711. //  do
  712. //  {
  713. //      bytesRead = FSfread(buffer, 1, BUFFER_LENGTH, pointer);        
  714. //
  715. //      for (i=(batchCount == 0 ? BMP_HEADER_SIZE : 0); i<bytesRead;i++)
  716. //      {  
  717. //          switch (totalBytes %3)
  718. //          {
  719. //          case 0:
  720. //              lastBlue = buffer[i];
  721. //              break;
  722. //          case 1:
  723. //              lastGreen = buffer[i];
  724. //              break;
  725. //          default:
  726. //              lastRed = buffer[i];
  727. //
  728. //              unsigned int color = RGB565(lastRed, lastGreen, lastBlue);
  729. //              ST7735_drawPixel(curX, bitmapHeight-curY-1, color);
  730. //
  731. //              curX++;
  732. //             
  733. //              if (curX == bitmapWidth)
  734. //              {
  735. //                  curY++;
  736. //                  curX = 0;
  737. //              }
  738. //
  739. //              break;
  740. //          }
  741. //
  742. //          totalBytes++;
  743. //      }
  744. //
  745. //      batchCount++;
  746. //  }
  747. //  while (bytesRead > 0);
  748. //
  749. //  curX++;
  750. //  sprintf(buf, "Read file done (%ld bytes)", totalBytes);
  751. //  SendUARTStr(buf);
  752. //
  753. //  FSfclose (pointer);
  754. //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement