Subversion Repositories HomeAutomation

Rev

Rev 1796 | Rev 1799 | 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. uint8_t dotmatrixFramebuf[8][4];
  37. uint8_t dotmatrixRowCounter=0;
  38. uint16_t dotmatrixBrightness=0;
  39.  
  40. /* Write a byte on SPI */
  41. void dotmatrixSPIWrite(uint8_t data) {
  42.     uint8_t dummy = 0;
  43.     /* Wait for empty transmit buffer */
  44.     while ( !( UCSR0A & (1<<UDRE0)) );
  45.     // write data
  46.     dummy = UDR0;
  47.     UDR0 = data;
  48.     waitspi();
  49. }
  50.  
  51. void dotmatrixSetColor(uint8_t color){
  52.   GrLcdState.color=color;
  53. }
  54.  
  55. uint8_t dotmatrixGetColor(void){
  56.   return GrLcdState.color;
  57. }
  58.  
  59. void dotmatrixSetData(uint8_t Data)
  60. {
  61. //  dotmatrixFramebuf[8][4]
  62. }
  63.  
  64. uint8_t dotmatrixGetData(void){
  65.     uint8_t input = 0;
  66.  
  67.     return input;
  68. }
  69.  
  70. void dotmatrixWriteData(uint8_t data, uint8_t color){
  71. /*  dotmatrixSetControls(1,0);
  72.     if (color == GLCD_COLOR_CLEAR)
  73.       data = ~data;
  74.     if (GrLcdState.color == GLCD_COLOR_BLACK)
  75.       dotmatrixSetData(data);
  76.     else
  77.       dotmatrixSetData(~data);
  78.     dotmatrixEnable();
  79.     GrLcdState.lcdXAddr++;
  80.  
  81.     if (GrLcdState.lcdXAddr == 64 ){
  82.         dotmatrixSetXY(GrLcdState.lcdXAddr,GrLcdState.lcdYAddr);
  83.     }
  84. #if KS0108_WIDTH > 128
  85. else if (GrLcdState.lcdXAddr == 128 ){
  86.         dotmatrixSetXY(GrLcdState.lcdXAddr,GrLcdState.lcdYAddr);
  87.     }
  88. #endif
  89.     if (GrLcdState.lcdXAddr > KS0108_WIDTH ){
  90.         dotmatrixSetXY(0,GrLcdState.lcdYAddr+8);
  91.     }*/
  92. }
  93. void dotmatrixWriteDataTransparent(uint8_t inputdata, uint8_t color){
  94. /*  uint8_t data = 0;
  95.     dotmatrixSetDirection(INPUT);
  96.     dotmatrixSetControls(1,1);
  97.     dotmatrixEnable();  //dummy read
  98.     dotmatrixDelay();
  99.     gpio_set_pin(LCD_CONTROL_E);
  100.     dotmatrixDelay();
  101.     if (GrLcdState.color == GLCD_COLOR_BLACK)
  102.       data = dotmatrixGetData();
  103.     else
  104.       data = ~dotmatrixGetData();
  105.     dotmatrixDisable();
  106.     dotmatrixDelay();
  107.     dotmatrixSetControls(1,0);
  108.     dotmatrixSetDirection(OUTPUT);
  109.     dotmatrixSetXY(GrLcdState.lcdXAddr, GrLcdState.lcdYAddr);
  110.     dotmatrixSetControls(1,0);
  111.     if (color == GLCD_COLOR_CLEAR)
  112.       data = data&(~inputdata);
  113.     else
  114.       data = data|inputdata;
  115.     if (GrLcdState.color == GLCD_COLOR_BLACK)
  116.       dotmatrixSetData(data);
  117.     else
  118.       dotmatrixSetData(~data);
  119.     dotmatrixEnable();
  120.     GrLcdState.lcdXAddr++;
  121.  
  122.  
  123.     if (GrLcdState.lcdXAddr == 64 ){
  124.         dotmatrixSetXY(GrLcdState.lcdXAddr,GrLcdState.lcdYAddr);
  125.     }
  126. #if KS0108_WIDTH > 128
  127.     else if (GrLcdState.lcdXAddr == 128 ){
  128.         dotmatrixSetXY(GrLcdState.lcdXAddr,GrLcdState.lcdYAddr);
  129.     }
  130. #endif
  131.     if (GrLcdState.lcdXAddr > KS0108_WIDTH ){
  132.         dotmatrixSetXY(0,GrLcdState.lcdYAddr+8);
  133.     }*/
  134. }
  135.  
  136. uint8_t dotmatrixReadData(void){
  137.     uint8_t data = 0;
  138. /*  dotmatrixSetDirection(INPUT);
  139.     dotmatrixSetControls(1,1);
  140.     dotmatrixEnable();  //dummy read
  141.     dotmatrixDelay();
  142.     gpio_set_pin(LCD_CONTROL_E);
  143.     dotmatrixDelay();
  144.     if (GrLcdState.color == GLCD_COLOR_BLACK)
  145.       data = dotmatrixGetData();
  146.     else
  147.       data = ~dotmatrixGetData();
  148.     dotmatrixDisable();
  149.     dotmatrixDelay();
  150.     dotmatrixSetControls(1,0);
  151.     dotmatrixSetDirection(OUTPUT);
  152.     dotmatrixSetXY(GrLcdState.lcdXAddr, GrLcdState.lcdYAddr);*/
  153.     return data;
  154. }
  155.  
  156. /* Callback run periodically to manage rows.
  157.    For each time callback is run, the next row is enabled and the last disabled
  158.  */
  159. void dotmatrixRefresh()
  160. {
  161.     /* Increase row counter */
  162.     if (dotmatrixRowCounter++ == 8) {dotmatrixRowCounter = 0;}
  163.  
  164.     /* Disable all rows */
  165.     gpio_clr_pin(dotmatrixROW_IO1);
  166.     gpio_clr_pin(dotmatrixROW_IO2);
  167.     gpio_clr_pin(dotmatrixROW_IO3);
  168.     gpio_clr_pin(dotmatrixROW_IO4);
  169.     gpio_clr_pin(dotmatrixROW_IO5);
  170.     gpio_clr_pin(dotmatrixROW_IO6);
  171.     gpio_clr_pin(dotmatrixROW_IO7);
  172.     gpio_clr_pin(dotmatrixROW_IO8);
  173.  
  174.     /* Write one column of frame buffer to shift registers */
  175.     dotmatrixSPIWrite(dotmatrixFramebuf[7-dotmatrixRowCounter][3]);
  176.     dotmatrixSPIWrite(dotmatrixFramebuf[7-dotmatrixRowCounter][2]);
  177.     dotmatrixSPIWrite(dotmatrixFramebuf[7-dotmatrixRowCounter][1]);
  178.     dotmatrixSPIWrite(dotmatrixFramebuf[7-dotmatrixRowCounter][0]);
  179.     /* Add more here to support 8-module panels */
  180.  
  181.     /* Toggle shift register latch */
  182.     gpio_clr_pin(dotmatrixLATCHCLOCK_IO);
  183.     gpio_set_pin(dotmatrixLATCHCLOCK_IO);
  184.  
  185.     /* Enable one row */
  186.     switch (dotmatrixRowCounter)
  187.     {
  188.         case 0:
  189.             gpio_set_pin(dotmatrixROW_IO1);
  190.             break;
  191.         case 1:
  192.             gpio_set_pin(dotmatrixROW_IO2);
  193.             break;
  194.         case 2:
  195.             gpio_set_pin(dotmatrixROW_IO3);
  196.             break;
  197.         case 3:
  198.             gpio_set_pin(dotmatrixROW_IO4);
  199.             break;
  200.         case 4:
  201.             gpio_set_pin(dotmatrixROW_IO5);
  202.             break;
  203.         case 5:
  204.             gpio_set_pin(dotmatrixROW_IO6);
  205.             break;
  206.         case 6:
  207.             gpio_set_pin(dotmatrixROW_IO7);
  208.             break;
  209.         case 7:
  210.             gpio_set_pin(dotmatrixROW_IO8);
  211.             break;
  212.            
  213.         default:
  214.             break;
  215.     }
  216. }
  217.  
  218. void dotmatrixInit(){
  219.     /* Set up row driver IO as low output */
  220.     gpio_clr_pin(dotmatrixROW_IO1);
  221.     gpio_set_out(dotmatrixROW_IO1);
  222.     gpio_clr_pin(dotmatrixROW_IO2);
  223.     gpio_set_out(dotmatrixROW_IO2);
  224.     gpio_clr_pin(dotmatrixROW_IO3);
  225.     gpio_set_out(dotmatrixROW_IO3);
  226.     gpio_clr_pin(dotmatrixROW_IO4);
  227.     gpio_set_out(dotmatrixROW_IO4);
  228.     gpio_clr_pin(dotmatrixROW_IO5);
  229.     gpio_set_out(dotmatrixROW_IO5);
  230.     gpio_clr_pin(dotmatrixROW_IO6);
  231.     gpio_set_out(dotmatrixROW_IO6);
  232.     gpio_clr_pin(dotmatrixROW_IO7);
  233.     gpio_set_out(dotmatrixROW_IO7);
  234.     gpio_clr_pin(dotmatrixROW_IO8);
  235.     gpio_set_out(dotmatrixROW_IO8);
  236.  
  237.     /* Set up PWM controlling brightness */
  238. //  TCCR0A |= (1<<COM0B1)|(1<<WGM01)|(1<<WGM00);
  239. //  TCCR0B |= (1<<CS00);
  240. //  OCR0B = 0xff-act_DotMatrix_INITIAL_BRIGHTNESS;
  241. //  DDRD |= (1<<PD5);
  242.  
  243.     /* Set up output latch clock */
  244.     gpio_clr_pin(dotmatrixLATCHCLOCK_IO);
  245.     gpio_set_out(dotmatrixLATCHCLOCK_IO);
  246.  
  247.     /* Initialize USART in SPI-mode */
  248.     UBRR0 = 0;
  249.     USART_SPI_XCK_DDR |= (1<<USART_SPI_XCK); // xck (sck) output
  250.     UCSR0C = (1<<UMSEL01)|(1<<UMSEL00)|(0<<UCPHA0)|(0<<UCPOL0);
  251.     UCSR0B = (1<<RXEN0)|(1<<TXEN0);
  252.     UBRR0 = 0;
  253.  
  254.     dotmatrixClear();
  255.  
  256.     GrLcdState.color = GLCD_COLOR_WHITE;
  257.    
  258. /*  gpio_set_out(LCD_CONTROL_RS);
  259.     gpio_set_out(LCD_CONTROL_RW);
  260.     gpio_set_out(LCD_CONTROL_E);
  261.     gpio_set_out(LCD_CONTROL_CS1);
  262.     gpio_set_out(LCD_CONTROL_CS2);
  263.  
  264.     dotmatrixSetDirection(OUTPUT);
  265.     GrLcdState.color = GLCD_COLOR_WHITE;
  266. #if KS0108_INVERT_CS == 1
  267.     gpio_clr_pin(LCD_CONTROL_CS1);
  268.     gpio_clr_pin(LCD_CONTROL_CS2);
  269. #else
  270.     gpio_set_pin(LCD_CONTROL_CS1);
  271.     gpio_set_pin(LCD_CONTROL_CS2);
  272. #endif
  273.     dotmatrixDisable();
  274.     dotmatrixDelay();
  275.     //set display on
  276.     dotmatrixSetControls(0,0);
  277.     dotmatrixSetData(0x3F);
  278.     dotmatrixEnable();
  279.  
  280.     //set startaddress of the first row (set to row 0)
  281.     dotmatrixSetControls(0,0);
  282.     dotmatrixSetData(0xc0);
  283.     dotmatrixEnable();
  284.     dotmatrixClear();*/
  285. }
  286.  
  287. void dotmatrixClear(){
  288.     /* Clear buffer memory */
  289.     for (uint8_t i=0; i<8; i++)
  290.     {
  291.         for (uint8_t j=0; j<4; j++)
  292.         {
  293.             dotmatrixFramebuf[i][j] = dotmatrixINITIAL_ROW;
  294.         }
  295.     }
  296. }
  297.  
  298. void dotmatrixSetXY(uint8_t x, uint8_t y){
  299.     GrLcdState.lcdXAddr = x;
  300.     GrLcdState.lcdYAddr = y;
  301.     GrLcdState.lcdYpage = y/8;
  302.  
  303. /*
  304.     //Vi b�rjar med X. Steg 1: V�lj r�tt chip:
  305.     if (x > 63 && x <= 127){
  306. #if KS0108_INVERT_CS == 1
  307.     gpio_set_pin(LCD_CONTROL_CS1);
  308.     gpio_clr_pin(LCD_CONTROL_CS2);
  309. #else
  310.     gpio_set_pin(LCD_CONTROL_CS2);
  311.     gpio_clr_pin(LCD_CONTROL_CS1);
  312. #endif
  313.     } else if (x > 127){
  314. #if KS0108_INVERT_CS == 1
  315.     gpio_set_pin(LCD_CONTROL_CS1);
  316.     gpio_set_pin(LCD_CONTROL_CS2);
  317. #else
  318.     gpio_clr_pin(LCD_CONTROL_CS2);
  319.     gpio_clr_pin(LCD_CONTROL_CS1);
  320. #endif
  321.     } else {
  322. #if KS0108_INVERT_CS == 1
  323.     gpio_set_pin(LCD_CONTROL_CS2);
  324.     gpio_clr_pin(LCD_CONTROL_CS1);
  325. #else
  326.     gpio_set_pin(LCD_CONTROL_CS1);
  327.     gpio_clr_pin(LCD_CONTROL_CS2);
  328. #endif
  329.     }
  330.  
  331.     //Steg 2: S�tt r�tt x-adress p� det aktiva chippet
  332.     dotmatrixSetControls(0,0);
  333.     dotmatrixSetData(0x40 + x%64);
  334.     dotmatrixEnable();
  335.  
  336.     //Steg 3: S�tt r�tt y-adress p� det aktiva chippet
  337.     dotmatrixSetData(0xB8 + GrLcdState.lcdYpage);
  338.     dotmatrixEnable();*/
  339. }
  340. uint8_t dotmatrixGetX(void){
  341.     return GrLcdState.lcdXAddr;
  342. }
  343. uint8_t dotmatrixGetY(void){
  344.     return GrLcdState.lcdYAddr;
  345. }
  346.  
  347.