Subversion Repositories HomeAutomation

Rev

Rev 1789 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. #include "graphics.h"
  2. #include "font.h"
  3. #include <avr/io.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. #include <avr/pgmspace.h>
  8.  
  9. #ifndef GRAPHICSDRIVER
  10. #define GRAPHICSDRIVER KS0108
  11. #warning Please define GRAPHICSDRIVER in your config.inc
  12. #endif
  13.  
  14. #if GRAPHICSDRIVER==KS0108
  15. #include "ks0108.h"
  16. #elif GRAPHICSDRIVER==DOTMATRIX
  17. #include "dotmatrix.h"
  18. #else
  19. #error Non supported graphics driver
  20. #endif
  21.  
  22.  
  23. //Some parts of the code is from proycon avrlib written by Pascal Stang:
  24. //*****************************************************************************
  25. //
  26. // File Name    : 'glcd.c'
  27. // Title        : Graphic LCD API functions
  28. // Author       : Pascal Stang - Copyright (C) 2002
  29. // Date         : 5/30/2002
  30. // Revised      : 5/30/2002
  31. // Version      : 0.5
  32. // Target MCU   : Atmel AVR
  33. // Editor Tabs  : 4
  34. //
  35. // NOTE: This code is currently below version 1.0, and therefore is considered
  36. // to be lacking in some functionality or documentation, or may not be fully
  37. // tested.  Nonetheless, you can expect most functions to work.
  38. //
  39. // This code is distributed under the GNU Public License
  40. //      which can be found at http://www.gnu.org/licenses/gpl.txt
  41. //
  42. //*****************************************************************************
  43.  
  44. void glcdSetColor(uint8_t color){
  45. #if GRAPHICSDRIVER==KS0108
  46.   ks0108SetColor(color);
  47. #elif GRAPHICSDRIVER==DOTMATRIX
  48.   dotmatrixSetColor(color);
  49. #endif
  50. }
  51.  
  52. uint8_t glcdGetColor(void){
  53. #if GRAPHICSDRIVER==KS0108
  54.   return ks0108GetColor();
  55. #elif GRAPHICSDRIVER==DOTMATRIX
  56.   return dotmatrixGetColor();
  57. #endif
  58. }
  59.  
  60. void glcdWriteData(uint8_t data, uint8_t color){
  61. #if GRAPHICSDRIVER==KS0108
  62.     ks0108WriteData(data, color);
  63. #elif GRAPHICSDRIVER==DOTMATRIX
  64.     dotmatrixWriteData(data, color);
  65. #endif
  66. }
  67.  
  68. void glcdWriteDataTransparent(uint8_t inputdata, uint8_t color){
  69. #if GRAPHICSDRIVER==KS0108
  70.     ks0108WriteDataTransparent(inputdata, color);
  71. #elif GRAPHICSDRIVER==DOTMATRIX
  72.     dotmatrixWriteDataTransparent(inputdata, color);
  73. #endif
  74. }
  75.  
  76. uint8_t glcdReadData(void){
  77. #if GRAPHICSDRIVER==KS0108
  78.     return ks0108ReadData();
  79. #elif GRAPHICSDRIVER==DOTMATRIX
  80.     return dotmatrixReadData();
  81. #endif
  82. }
  83.  
  84. void glcdClear(){
  85. #if GRAPHICSDRIVER==KS0108
  86.     ks0108Clear();
  87. #elif GRAPHICSDRIVER==DOTMATRIX
  88.     dotmatrixClear();
  89. #endif
  90. }
  91.  
  92. void glcdSetXY(uint8_t x, uint8_t y){
  93. #if GRAPHICSDRIVER==KS0108
  94.     ks0108SetXY(x, y);
  95. #elif GRAPHICSDRIVER==DOTMATRIX
  96.     dotmatrixSetXY(x, y);
  97. #endif
  98. }
  99.  
  100. uint8_t glcdGetX(void){
  101. #if GRAPHICSDRIVER==KS0108
  102.     return ks0108GetX();
  103. #elif GRAPHICSDRIVER==DOTMATRIX
  104.     return dotmatrixGetX();
  105. #endif
  106. }
  107.  
  108. uint8_t glcdGetY(void){
  109. #if GRAPHICSDRIVER==KS0108
  110.     return ks0108GetY();
  111. #elif GRAPHICSDRIVER==DOTMATRIX
  112.     return dotmatrixGetY();
  113. #endif
  114. }
  115.  
  116. void glcdInit(void)
  117. {
  118. #if GRAPHICSDRIVER==KS0108
  119.     ks0108Init();
  120. #elif GRAPHICSDRIVER==DOTMATRIX
  121.     dotmatrixInit();
  122. #endif
  123. }
  124.  
  125. void glcdWriteChar(char c, uint8_t color)
  126. {
  127.     uint8_t i = 0;
  128.     if (glcdGetX() > (KS0108_WIDTH-5))
  129.     {
  130.         glcdSetXY(0, glcdGetY() + 8);
  131.     }
  132.  
  133.     for(i=0; i<5; i++)
  134.     {
  135.         glcdWriteData(pgm_read_byte(&Font5x7[((c - 0x20) * 5) + i]), color); //Borde snyggas till med pekare
  136.     }
  137.     glcdWriteData(0x00, color); //space between this and the next char
  138. }
  139.  
  140. void glcdPutStr(char *data, uint8_t color){
  141.     while (*data){
  142.         glcdWriteChar(*data, color);
  143.         data++;
  144.     }
  145. }
  146. void glcdWriteCharTransparent(char c, uint8_t color)
  147. {
  148.     uint8_t i = 0;
  149.     if (glcdGetX() > (KS0108_WIDTH-5))
  150.     {
  151.         glcdSetXY(0, glcdGetY() + 8);
  152.     }
  153.  
  154.     for(i=0; i<5; i++)
  155.     {
  156.         glcdWriteDataTransparent(pgm_read_byte(&Font5x7[((c - 0x20) * 5) + i]), color); //Borde snyggas till med pekare
  157.     }
  158.     glcdWriteDataTransparent(0x00, color); //space between this and the next char
  159. }
  160.  
  161. void glcdPutStrTransparent(char *data, uint8_t color){
  162.     while (*data){
  163.         glcdWriteCharTransparent(*data, color);
  164.         data++;
  165.     }
  166. }
  167.  
  168. void glcdDrawLine(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, uint8_t color) {
  169.     uint8_t length, i, y, yAlt, xTmp, yTmp;
  170.     int16_t m;
  171.    
  172.     //
  173.     // vertical line
  174.     //
  175.     if(x1 == x2) {
  176.         // x1|y1 must be the upper point
  177.         if(y1 > y2) {
  178.             yTmp = y1;
  179.             y1 = y2;
  180.             y2 = yTmp;
  181.         }
  182.         glcdDrawVertLine(x1, y1, y2-y1, color);
  183.    
  184.     //
  185.     // horizontal line
  186.     //
  187.     } else if(y1 == y2) {  
  188.         // x1|y1 must be the left point
  189.         if(x1 > x2) {
  190.             xTmp = x1;
  191.             x1 = x2;
  192.             x2 = xTmp;
  193.         }
  194.         glcdDrawHoriLine(x1, y1, x2-x1, color);
  195.    
  196.     //
  197.     // schiefe line :)
  198.     //
  199.     } else {
  200.         // angle >= 45�
  201.         if((y2-y1) >= (x2-x1) || (y1-y2) >= (x2-x1)) {
  202.             // x1 must be smaller than x2
  203.             if(x1 > x2) {
  204.                 xTmp = x1;
  205.                 yTmp = y1;
  206.                 x1 = x2;
  207.                 y1 = y2;
  208.                 x2 = xTmp;
  209.                 y2 = yTmp;
  210.             }
  211.        
  212.             length = x2-x1;     // not really the length :)
  213.             m = ((y2-y1)*200)/length;
  214.             yAlt = y1;
  215.            
  216.             for(i=0; i<=length; i++) {
  217.                 y = ((m*i)/200)+y1;
  218.                
  219.                 if((m*i)%200 >= 100)
  220.                     y++;
  221.                 else if((m*i)%200 <= -100)
  222.                     y--;
  223.                
  224.                 glcdDrawLine(x1+i, yAlt, x1+i, y, color);
  225.                
  226.                 if(length <= (y2-y1) && y1 < y2)
  227.                     yAlt = y+1;
  228.                 else if(length <= (y1-y2) && y1 > y2)
  229.                     yAlt = y-1;
  230.                 else
  231.                     yAlt = y;
  232.             }
  233.        
  234.         // angle < 45�
  235.         } else {
  236.             // y1 must be smaller than y2
  237.             if(y1 > y2) {
  238.                 xTmp = x1;
  239.                 yTmp = y1;
  240.                 x1 = x2;
  241.                 y1 = y2;
  242.                 x2 = xTmp;
  243.                 y2 = yTmp;
  244.             }
  245.            
  246.             length = y2-y1;
  247.             m = ((x2-x1)*200)/length;
  248.             yAlt = x1;
  249.            
  250.             for(i=0; i<=length; i++) {
  251.                 y = ((m*i)/200)+x1;
  252.                
  253.                 if((m*i)%200 >= 100)
  254.                     y++;
  255.                 else if((m*i)%200 <= -100)
  256.                     y--;
  257.                
  258.                 glcdDrawLine(yAlt, y1+i, y, y1+i, color);
  259.                 if(length <= (x2-x1) && x1 < x2)
  260.                     yAlt = y+1;
  261.                 else if(length <= (x1-x2) && x1 > x2)
  262.                     yAlt = y-1;
  263.                 else
  264.                     yAlt = y;
  265.             }
  266.         }
  267.     }
  268. }
  269.  
  270. void glcdDrawRect(uint8_t x, uint8_t y, uint8_t width, uint8_t height, uint8_t color) {
  271.     glcdDrawHoriLine(x, y, width, color);               // top
  272.     glcdDrawHoriLine(x, y+height, width, color);        // bottom
  273.     glcdDrawVertLine(x, y, height, color);          // left
  274.     glcdDrawVertLine(x+width, y, height, color);        // right
  275. }
  276.  
  277.  
  278. void glcdFillRect(uint8_t x, uint8_t y, uint8_t width, uint8_t height, uint8_t color) {
  279.     uint8_t mask, pageOffset, h, i; //, data;
  280.     height++;
  281.    
  282.     pageOffset = y%8;
  283.     y -= pageOffset;
  284.     mask = 0xFF;
  285.     if(height < 8-pageOffset) {
  286.         mask >>= (8-height);
  287.         h = height;
  288.     } else {
  289.         h = 8-pageOffset;
  290.     }
  291.     mask <<= pageOffset;
  292.    
  293.     glcdSetXY(x, y);
  294.     for(i=0; i<=width; i++) {
  295.         glcdWriteDataTransparent(mask, color);
  296.     }
  297.    
  298.     while(h+8 <= height) {
  299.         h += 8;
  300.         y += 8;
  301.         glcdSetXY(x, y);
  302.        
  303.         for(i=0; i<=width; i++) {
  304.             glcdWriteData(0xff, color);
  305.         }
  306.     }
  307.    
  308.     if(h < height) {
  309.         mask = ~(0xFF << (height-h));
  310.         glcdSetXY(x, y+8);
  311.        
  312.         for(i=0; i<=width; i++) {
  313.             glcdWriteDataTransparent(mask, color);
  314.         }
  315.     }
  316. }
  317.  
  318. void glcdInvertRect(uint8_t x, uint8_t y, uint8_t width, uint8_t height) {
  319.     uint8_t mask, pageOffset, h, i, data, tmpData;
  320.     height++;
  321.    
  322.     pageOffset = y%8;
  323.     y -= pageOffset;
  324.     mask = 0xFF;
  325.     if(height < 8-pageOffset) {
  326.         mask >>= (8-height);
  327.         h = height;
  328.     } else {
  329.         h = 8-pageOffset;
  330.     }
  331.     mask <<= pageOffset;
  332.    
  333.     glcdSetXY(x, y);
  334.     for(i=0; i<=width; i++) {
  335.         data = glcdReadData(); //glcdReadData();
  336.         tmpData = ~data;
  337.         data = (tmpData & mask) | (data & ~mask);
  338.         glcdWriteData(data, GLCD_COLOR_SET);
  339.     }
  340.    
  341.     while(h+8 <= height) {
  342.         h += 8;
  343.         y += 8;
  344.         glcdSetXY(x, y);
  345.        
  346.         for(i=0; i<=width; i++) {
  347.             data = glcdReadData();
  348.             glcdWriteData(~data, GLCD_COLOR_SET);
  349.         }
  350.     }
  351.    
  352.     if(h < height) {
  353.         mask = ~(0xFF << (height-h));
  354.         glcdSetXY(x, y+8);
  355.        
  356.         for(i=0; i<=width; i++) {
  357.             data = glcdReadData(); //glcdReadData();
  358.             tmpData = ~data;
  359.             data = (tmpData & mask) | (data & ~mask);
  360.             glcdWriteData(data, GLCD_COLOR_SET);
  361.         }
  362.     }
  363. }
  364.  
  365. void glcdInvert(void) {
  366.     if(glcdGetColor() == GLCD_COLOR_BLACK)
  367.         glcdSetColor(GLCD_COLOR_WHITE);
  368.     else
  369.         glcdSetColor(GLCD_COLOR_BLACK);
  370.     glcdInvertRect(0,0,KS0108_WIDTH-1,KS0108_HIGHT-1);
  371. }
  372.  
  373. void glcdSetDot(uint8_t x, uint8_t y, uint8_t color) {
  374.     glcdSetXY(x, y-y%8);                    // read data from display memory
  375.     glcdWriteDataTransparent(0x01 << (y%8), GLCD_COLOR_SET);
  376. }
  377.  
  378. void glcdDrawRoundRect(uint8_t x, uint8_t y, uint8_t width, uint8_t height, uint8_t radius, uint8_t color) {
  379.     int16_t tSwitch, x1 = 0, y1 = radius;
  380.     tSwitch = 3 - 2 * radius;
  381.    
  382.     while (x1 <= y1) {
  383.         glcdSetDot(x+radius - x1, y+radius - y1, color);
  384.         glcdSetDot(x+radius - y1, y+radius - x1, color);
  385.  
  386.         glcdSetDot(x+width-radius + x1, y+radius - y1, color);
  387.         glcdSetDot(x+width-radius + y1, y+radius - x1, color);
  388.        
  389.         glcdSetDot(x+width-radius + x1, y+height-radius + y1, color);
  390.         glcdSetDot(x+width-radius + y1, y+height-radius + x1, color);
  391.  
  392.         glcdSetDot(x+radius - x1, y+height-radius + y1, color);
  393.         glcdSetDot(x+radius - y1, y+height-radius + x1, color);
  394.  
  395.         if (tSwitch < 0) {
  396.             tSwitch += (4 * x1 + 6);
  397.         } else {
  398.             tSwitch += (4 * (x1 - y1) + 10);
  399.             y1--;
  400.         }
  401.         x1++;
  402.     }
  403.        
  404.     glcdDrawHoriLine(x+radius, y, width-(2*radius), color);         // top
  405.     glcdDrawHoriLine(x+radius, y+height, width-(2*radius), color);  // bottom
  406.     glcdDrawVertLine(x, y+radius, height-(2*radius), color);            // left
  407.     glcdDrawVertLine(x+width, y+radius, height-(2*radius), color);  // right
  408. }
  409.