Subversion Repositories HomeAutomation

Rev

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

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