Subversion Repositories HomeAutomation

Rev

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