Subversion Repositories HomeAutomation

Rev

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

  1. #include "dotmatrix.h"
  2. #include <avr/io.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. #include <avr/pgmspace.h>
  7.  
  8. //Some parts of the code is from proycon avrlib written by Pascal Stang:
  9. //*****************************************************************************
  10. //
  11. // File Name    : 'glcd.c'
  12. // Title        : Graphic LCD API functions
  13. // Author       : Pascal Stang - Copyright (C) 2002
  14. // Date         : 5/30/2002
  15. // Revised      : 5/30/2002
  16. // Version      : 0.5
  17. // Target MCU   : Atmel AVR
  18. // Editor Tabs  : 4
  19. //
  20. // NOTE: This code is currently below version 1.0, and therefore is considered
  21. // to be lacking in some functionality or documentation, or may not be fully
  22. // tested.  Nonetheless, you can expect most functions to work.
  23. //
  24. // This code is distributed under the GNU Public License
  25. //      which can be found at http://www.gnu.org/licenses/gpl.txt
  26. //
  27. //*****************************************************************************
  28. #ifndef GRAPHICS_WIDTH
  29. #error GRAPHICS_WIDTH, GRAPHICS_HEIGHT must be defined
  30. #endif
  31.  
  32. #define OUTPUT  0
  33. #define INPUT   1
  34.  
  35. volatile GrLcdStateType GrLcdState;
  36.  
  37. void dotmatrixSetControls(uint8_t RS, uint8_t RW){
  38. //  if(RS) {gpio_set_pin(LCD_CONTROL_RS);} else {gpio_clr_pin(LCD_CONTROL_RS);}
  39. //  if(RW) {gpio_set_pin(LCD_CONTROL_RW);} else {gpio_clr_pin(LCD_CONTROL_RW);}
  40. }
  41.  
  42. void dotmatrixSetData(uint8_t Data){
  43. /*  if(Data&0x01) {gpio_set_pin(LCD_DATA_DB0);} else {gpio_clr_pin(LCD_DATA_DB0);}
  44.     if(Data&0x02) {gpio_set_pin(LCD_DATA_DB1);} else {gpio_clr_pin(LCD_DATA_DB1);}
  45.     if(Data&0x04) {gpio_set_pin(LCD_DATA_DB2);} else {gpio_clr_pin(LCD_DATA_DB2);}
  46.     if(Data&0x08) {gpio_set_pin(LCD_DATA_DB3);} else {gpio_clr_pin(LCD_DATA_DB3);}
  47.     if(Data&0x10) {gpio_set_pin(LCD_DATA_DB4);} else {gpio_clr_pin(LCD_DATA_DB4);}
  48.     if(Data&0x20) {gpio_set_pin(LCD_DATA_DB5);} else {gpio_clr_pin(LCD_DATA_DB5);}
  49.     if(Data&0x40) {gpio_set_pin(LCD_DATA_DB6);} else {gpio_clr_pin(LCD_DATA_DB6);}
  50.     if(Data&0x80) {gpio_set_pin(LCD_DATA_DB7);} else {gpio_clr_pin(LCD_DATA_DB7);}*/
  51. }
  52. uint8_t dotmatrixGetData(void){
  53.     uint8_t input = 0;
  54. /*  if (gpio_get_state(LCD_DATA_DB0)) {input += 1;}
  55.     if (gpio_get_state(LCD_DATA_DB1)) {input += 2;}
  56.     if (gpio_get_state(LCD_DATA_DB2)) {input += 4;}
  57.     if (gpio_get_state(LCD_DATA_DB3)) {input += 8;}
  58.     if (gpio_get_state(LCD_DATA_DB4)) {input += 16;}
  59.     if (gpio_get_state(LCD_DATA_DB5)) {input += 32;}
  60.     if (gpio_get_state(LCD_DATA_DB6)) {input += 64;}
  61.     if (gpio_get_state(LCD_DATA_DB7)) {input += 128;}*/
  62.     return input;
  63. }
  64.  
  65. void dotmatrixDisable(){
  66. //  gpio_clr_pin(LCD_CONTROL_E);
  67. }
  68.  
  69. void dotmatrixDelay(){
  70. //  delay_us(2);
  71. }
  72.  
  73. void dotmatrixEnable(){
  74. /*  dotmatrixDelay();
  75.     gpio_set_pin(LCD_CONTROL_E);
  76.     dotmatrixDelay();
  77.     dotmatrixDisable();
  78.     dotmatrixDelay();*/
  79. }
  80.  
  81. void dotmatrixSetDirection(uint8_t dir){
  82. /*  if (dir == OUTPUT){
  83.     gpio_set_out(LCD_DATA_DB0);
  84.     gpio_set_out(LCD_DATA_DB1);
  85.     gpio_set_out(LCD_DATA_DB2);
  86.     gpio_set_out(LCD_DATA_DB3);
  87.     gpio_set_out(LCD_DATA_DB4);
  88.     gpio_set_out(LCD_DATA_DB5);
  89.     gpio_set_out(LCD_DATA_DB6);
  90.     gpio_set_out(LCD_DATA_DB7);
  91.   }else if (dir == INPUT){
  92.     gpio_set_in(LCD_DATA_DB0);
  93.     gpio_set_in(LCD_DATA_DB1);
  94.     gpio_set_in(LCD_DATA_DB2);
  95.     gpio_set_in(LCD_DATA_DB3);
  96.     gpio_set_in(LCD_DATA_DB4);
  97.     gpio_set_in(LCD_DATA_DB5);
  98.     gpio_set_in(LCD_DATA_DB6);
  99.     gpio_set_in(LCD_DATA_DB7);
  100.   }*/
  101. }
  102. void dotmatrixSetColor(uint8_t color){
  103.   GrLcdState.color=color;
  104. }
  105. uint8_t dotmatrixGetColor(void){
  106.   return GrLcdState.color;
  107. }
  108. void dotmatrixWriteData(uint8_t data, uint8_t color){
  109. /*  dotmatrixSetControls(1,0);
  110.     if (color == GLCD_COLOR_CLEAR)
  111.       data = ~data;
  112.     if (GrLcdState.color == GLCD_COLOR_BLACK)
  113.       dotmatrixSetData(data);
  114.     else
  115.       dotmatrixSetData(~data);
  116.     dotmatrixEnable();
  117.     GrLcdState.lcdXAddr++;
  118.  
  119.     if (GrLcdState.lcdXAddr == 64 ){
  120.         dotmatrixSetXY(GrLcdState.lcdXAddr,GrLcdState.lcdYAddr);
  121.     }
  122. #if KS0108_WIDTH > 128
  123. else if (GrLcdState.lcdXAddr == 128 ){
  124.         dotmatrixSetXY(GrLcdState.lcdXAddr,GrLcdState.lcdYAddr);
  125.     }
  126. #endif
  127.     if (GrLcdState.lcdXAddr > KS0108_WIDTH ){
  128.         dotmatrixSetXY(0,GrLcdState.lcdYAddr+8);
  129.     }*/
  130. }
  131. void dotmatrixWriteDataTransparent(uint8_t inputdata, uint8_t color){
  132. /*  uint8_t data = 0;
  133.     dotmatrixSetDirection(INPUT);
  134.     dotmatrixSetControls(1,1);
  135.     dotmatrixEnable();  //dummy read
  136.     dotmatrixDelay();
  137.     gpio_set_pin(LCD_CONTROL_E);
  138.     dotmatrixDelay();
  139.     if (GrLcdState.color == GLCD_COLOR_BLACK)
  140.       data = dotmatrixGetData();
  141.     else
  142.       data = ~dotmatrixGetData();
  143.     dotmatrixDisable();
  144.     dotmatrixDelay();
  145.     dotmatrixSetControls(1,0);
  146.     dotmatrixSetDirection(OUTPUT);
  147.     dotmatrixSetXY(GrLcdState.lcdXAddr, GrLcdState.lcdYAddr);
  148.     dotmatrixSetControls(1,0);
  149.     if (color == GLCD_COLOR_CLEAR)
  150.       data = data&(~inputdata);
  151.     else
  152.       data = data|inputdata;
  153.     if (GrLcdState.color == GLCD_COLOR_BLACK)
  154.       dotmatrixSetData(data);
  155.     else
  156.       dotmatrixSetData(~data);
  157.     dotmatrixEnable();
  158.     GrLcdState.lcdXAddr++;
  159.  
  160.  
  161.     if (GrLcdState.lcdXAddr == 64 ){
  162.         dotmatrixSetXY(GrLcdState.lcdXAddr,GrLcdState.lcdYAddr);
  163.     }
  164. #if KS0108_WIDTH > 128
  165.     else if (GrLcdState.lcdXAddr == 128 ){
  166.         dotmatrixSetXY(GrLcdState.lcdXAddr,GrLcdState.lcdYAddr);
  167.     }
  168. #endif
  169.     if (GrLcdState.lcdXAddr > KS0108_WIDTH ){
  170.         dotmatrixSetXY(0,GrLcdState.lcdYAddr+8);
  171.     }*/
  172. }
  173.  
  174. uint8_t dotmatrixReadData(void){
  175.     uint8_t data = 0;
  176. /*  dotmatrixSetDirection(INPUT);
  177.     dotmatrixSetControls(1,1);
  178.     dotmatrixEnable();  //dummy read
  179.     dotmatrixDelay();
  180.     gpio_set_pin(LCD_CONTROL_E);
  181.     dotmatrixDelay();
  182.     if (GrLcdState.color == GLCD_COLOR_BLACK)
  183.       data = dotmatrixGetData();
  184.     else
  185.       data = ~dotmatrixGetData();
  186.     dotmatrixDisable();
  187.     dotmatrixDelay();
  188.     dotmatrixSetControls(1,0);
  189.     dotmatrixSetDirection(OUTPUT);
  190.     dotmatrixSetXY(GrLcdState.lcdXAddr, GrLcdState.lcdYAddr);*/
  191.     return data;
  192. }
  193.  
  194. void dotmatrixRefresh()
  195. {
  196.  
  197. }
  198.  
  199. void dotmatrixInit(){
  200. /*  gpio_set_out(LCD_CONTROL_RS);
  201.     gpio_set_out(LCD_CONTROL_RW);
  202.     gpio_set_out(LCD_CONTROL_E);
  203.     gpio_set_out(LCD_CONTROL_CS1);
  204.     gpio_set_out(LCD_CONTROL_CS2);
  205.  
  206.     dotmatrixSetDirection(OUTPUT);
  207.     GrLcdState.color = GLCD_COLOR_WHITE;
  208. #if KS0108_INVERT_CS == 1
  209.     gpio_clr_pin(LCD_CONTROL_CS1);
  210.     gpio_clr_pin(LCD_CONTROL_CS2);
  211. #else
  212.     gpio_set_pin(LCD_CONTROL_CS1);
  213.     gpio_set_pin(LCD_CONTROL_CS2);
  214. #endif
  215.     dotmatrixDisable();
  216.     dotmatrixDelay();
  217.     //set display on
  218.     dotmatrixSetControls(0,0);
  219.     dotmatrixSetData(0x3F);
  220.     dotmatrixEnable();
  221.  
  222.     //set startaddress of the first row (set to row 0)
  223.     dotmatrixSetControls(0,0);
  224.     dotmatrixSetData(0xc0);
  225.     dotmatrixEnable();
  226.     dotmatrixClear();*/
  227. }
  228.  
  229. void dotmatrixClear(){
  230. /*
  231. #if KS0108_INVERT_CS == 1
  232.     gpio_clr_pin(LCD_CONTROL_CS1);
  233.     gpio_clr_pin(LCD_CONTROL_CS2);
  234. #else
  235.     gpio_set_pin(LCD_CONTROL_CS1);
  236.     gpio_set_pin(LCD_CONTROL_CS2);
  237. #endif
  238.     dotmatrixEnable();
  239.     dotmatrixDelay();
  240.     dotmatrixDisable();
  241.  
  242.     uint8_t i;
  243.     uint8_t j;
  244.  
  245.     for(j = 0; j < 8; j++){
  246.         dotmatrixSetControls(0,0);
  247.         dotmatrixSetData(0xB8 + j);
  248.         dotmatrixEnable();
  249.  
  250.         dotmatrixSetControls(0,0);
  251.         dotmatrixSetData(0x40);
  252.         dotmatrixEnable();
  253.         for (i = 0; i < 64; i++){
  254.             dotmatrixSetControls(1,0);
  255.             if (GrLcdState.color == GLCD_COLOR_BLACK)
  256.               dotmatrixSetData(0x00);
  257.             else
  258.               dotmatrixSetData(0xff);
  259.             dotmatrixEnable();
  260.         }
  261.     }
  262. */
  263. }
  264.  
  265. void dotmatrixSetXY(uint8_t x, uint8_t y){
  266. /*  GrLcdState.lcdXAddr = x;
  267.     GrLcdState.lcdYAddr = y;
  268.     GrLcdState.lcdYpage = y/8;
  269.  
  270.     //Vi b�rjar med X. Steg 1: V�lj r�tt chip:
  271.     if (x > 63 && x <= 127){
  272. #if KS0108_INVERT_CS == 1
  273.     gpio_set_pin(LCD_CONTROL_CS1);
  274.     gpio_clr_pin(LCD_CONTROL_CS2);
  275. #else
  276.     gpio_set_pin(LCD_CONTROL_CS2);
  277.     gpio_clr_pin(LCD_CONTROL_CS1);
  278. #endif
  279.     } else if (x > 127){
  280. #if KS0108_INVERT_CS == 1
  281.     gpio_set_pin(LCD_CONTROL_CS1);
  282.     gpio_set_pin(LCD_CONTROL_CS2);
  283. #else
  284.     gpio_clr_pin(LCD_CONTROL_CS2);
  285.     gpio_clr_pin(LCD_CONTROL_CS1);
  286. #endif
  287.     } else {
  288. #if KS0108_INVERT_CS == 1
  289.     gpio_set_pin(LCD_CONTROL_CS2);
  290.     gpio_clr_pin(LCD_CONTROL_CS1);
  291. #else
  292.     gpio_set_pin(LCD_CONTROL_CS1);
  293.     gpio_clr_pin(LCD_CONTROL_CS2);
  294. #endif
  295.     }
  296.  
  297.     //Steg 2: S�tt r�tt x-adress p� det aktiva chippet
  298.     dotmatrixSetControls(0,0);
  299.     dotmatrixSetData(0x40 + x%64);
  300.     dotmatrixEnable();
  301.  
  302.     //Steg 3: S�tt r�tt y-adress p� det aktiva chippet
  303.     dotmatrixSetData(0xB8 + GrLcdState.lcdYpage);
  304.     dotmatrixEnable();*/
  305. }
  306. uint8_t dotmatrixGetX(void){
  307.     return GrLcdState.lcdXAddr;
  308. }
  309. uint8_t dotmatrixGetY(void){
  310.     return GrLcdState.lcdYAddr;
  311. }
  312.  
  313.