Subversion Repositories HomeAutomation

Rev

Rev 2214 | 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 dotmatrixSIZEX
  29. #error dotmatrixSIZEX, dotmatrixSIZEY must be defined
  30. #endif
  31.  
  32. #define OUTPUT  0
  33. #define INPUT   1
  34.  
  35. volatile GrLcdStateType GrLcdState;
  36. uint8_t dotmatrixFramebuf[dotmatrixSIZEY/8][dotmatrixSIZEX];
  37. uint8_t dotmatrixRowCounter=0;
  38.  
  39. /* Write a byte on SPI */
  40. void dotmatrixSPIWrite(uint8_t data) {
  41.     uint8_t dummy = 0;
  42.     /* Wait for empty transmit buffer */
  43.     while ( !( UCSR0A & (1<<UDRE0)) );
  44.     // write data
  45.     dummy = UDR0;
  46.     UDR0 = data;
  47.     waitspi();
  48. }
  49.  
  50. void dotmatrixSetColor(uint8_t color){
  51.   GrLcdState.color=color;
  52. }
  53.  
  54. uint8_t dotmatrixGetColor(void){
  55.   return GrLcdState.color;
  56. }
  57.  
  58. /* Sets data into framebuffer, data arrives in a byte as a column */
  59. void dotmatrixSetData(uint8_t Data)
  60. {
  61.     /* Which ledmodule */
  62.     uint8_t module = GrLcdState.lcdXAddr;
  63.     /* Which column in ledmodule */
  64.     uint8_t column = GrLcdState.lcdYAddr/8;
  65.     //uint8_t column = GrLcdState.lcdXAddr%8;
  66.     /* Which row */
  67. //  uint8_t row = GrLcdState.lcdYAddr%8;
  68.  
  69.     /* Check panel size */
  70.     if ((column < dotmatrixSIZEY/8) && (module < dotmatrixSIZEX))
  71.     {
  72.         dotmatrixFramebuf[column][module] = Data;
  73.     }
  74. }
  75.  
  76. uint8_t dotmatrixGetData(void){
  77.     /* Which ledmodule */
  78.     uint8_t module = GrLcdState.lcdXAddr;
  79.     /* Which column in ledmodule */
  80.     uint8_t column = GrLcdState.lcdYAddr/8;
  81.     /* Which row */
  82. //  uint8_t row = GrLcdState.lcdYAddr%8;
  83.    
  84.     /* Check panel size */
  85.     if ((column < dotmatrixSIZEY/8) && (module < dotmatrixSIZEX))
  86.     {
  87.         return (dotmatrixFramebuf[column][module]);
  88.     }
  89.     else
  90.     {
  91.         return 0;
  92.     }
  93. }
  94.  
  95. void dotmatrixWriteData(uint8_t data, uint8_t color){
  96.     if (color == GLCD_COLOR_CLEAR)
  97.     {
  98.       data = ~data;
  99.     }
  100.     if (GrLcdState.color == GLCD_COLOR_BLACK)
  101.     {
  102.       dotmatrixSetData(data);
  103.     }
  104.     else
  105.     {
  106.       dotmatrixSetData(~data);
  107.     }
  108.    
  109.     GrLcdState.lcdXAddr++;
  110.  
  111.     if (GrLcdState.lcdXAddr > dotmatrixSIZEX ){
  112.         dotmatrixSetXY(0,GrLcdState.lcdYAddr+8);
  113.     }
  114. }
  115.  
  116. void dotmatrixWriteDataTransparent(uint8_t inputdata, uint8_t color){
  117.     uint8_t data = dotmatrixGetData();
  118.     if (GrLcdState.color != GLCD_COLOR_BLACK)
  119.     {
  120.       data = ~data;
  121.     }
  122.  
  123.     if (color == GLCD_COLOR_CLEAR)
  124.     {
  125.       data = data&(~inputdata);
  126.     }
  127.     else
  128.     {
  129.       data = data|inputdata;
  130.     }
  131.    
  132.     if (GrLcdState.color == GLCD_COLOR_BLACK)
  133.     {
  134.       dotmatrixSetData(data);
  135.     }
  136.     else
  137.     {
  138.       dotmatrixSetData(~data);
  139.     }
  140.  
  141.     GrLcdState.lcdXAddr++;
  142.  
  143.     if (GrLcdState.lcdXAddr > dotmatrixSIZEX ){
  144.         dotmatrixSetXY(0,GrLcdState.lcdYAddr+8);
  145.     }
  146. }
  147.  
  148. uint8_t dotmatrixReadData(void){
  149.     uint8_t data = 0;
  150.     if (GrLcdState.color == GLCD_COLOR_BLACK)
  151.     {
  152.       data = dotmatrixGetData();
  153.     }
  154.     else
  155.     {
  156.       data = ~dotmatrixGetData();
  157.     }
  158.  
  159.     return data;
  160. }
  161.  
  162. /* Callback run periodically to manage rows.
  163.    For each time callback is run, the next row is enabled and the last disabled
  164.  */
  165. void dotmatrixRefresh()
  166. {
  167.     uint8_t col = 0;
  168.     /* Increase row counter */
  169.     if (dotmatrixRowCounter++ == 8) {dotmatrixRowCounter = 0;}
  170.  
  171.  
  172.     /* Write one column of frame buffer to shift registers */
  173.     for (col = 0; col < (dotmatrixSIZEX/8*dotmatrixSIZEY/8); col++) {
  174.         dotmatrixSPIWrite(dotmatrixFramebuf[(col*8+dotmatrixRowCounter)/dotmatrixSIZEX][(col*8+dotmatrixRowCounter)%dotmatrixSIZEX]);
  175.     }
  176.  
  177.     /* Disable all rows */
  178.     gpio_clr_pin(dotmatrixROW_IO1);
  179.     gpio_clr_pin(dotmatrixROW_IO2);
  180.     gpio_clr_pin(dotmatrixROW_IO3);
  181.     gpio_clr_pin(dotmatrixROW_IO4);
  182.     gpio_clr_pin(dotmatrixROW_IO5);
  183.     gpio_clr_pin(dotmatrixROW_IO6);
  184.     gpio_clr_pin(dotmatrixROW_IO7);
  185.     gpio_clr_pin(dotmatrixROW_IO8);
  186.  
  187.    
  188.     /* Toggle shift register latch */
  189.     gpio_clr_pin(dotmatrixLATCHCLOCK_IO);
  190.     gpio_set_pin(dotmatrixLATCHCLOCK_IO);
  191.  
  192.     /* Enable one row */
  193.     switch (dotmatrixRowCounter)
  194.     {
  195.         case 0:
  196.             gpio_set_pin(dotmatrixROW_IO1);
  197.             break;
  198.         case 1:
  199.             gpio_set_pin(dotmatrixROW_IO2);
  200.             break;
  201.         case 2:
  202.             gpio_set_pin(dotmatrixROW_IO3);
  203.             break;
  204.         case 3:
  205.             gpio_set_pin(dotmatrixROW_IO4);
  206.             break;
  207.         case 4:
  208.             gpio_set_pin(dotmatrixROW_IO5);
  209.             break;
  210.         case 5:
  211.             gpio_set_pin(dotmatrixROW_IO6);
  212.             break;
  213.         case 6:
  214.             gpio_set_pin(dotmatrixROW_IO7);
  215.             break;
  216.         case 7:
  217.             gpio_set_pin(dotmatrixROW_IO8);
  218.             break;
  219.            
  220.         default:
  221.             break;
  222.     }
  223. }
  224.  
  225. void dotmatrixInit(){
  226.     /* Set up row driver IO as low output */
  227.     gpio_clr_pin(dotmatrixROW_IO1);
  228.     gpio_set_out(dotmatrixROW_IO1);
  229.     gpio_clr_pin(dotmatrixROW_IO2);
  230.     gpio_set_out(dotmatrixROW_IO2);
  231.     gpio_clr_pin(dotmatrixROW_IO3);
  232.     gpio_set_out(dotmatrixROW_IO3);
  233.     gpio_clr_pin(dotmatrixROW_IO4);
  234.     gpio_set_out(dotmatrixROW_IO4);
  235.     gpio_clr_pin(dotmatrixROW_IO5);
  236.     gpio_set_out(dotmatrixROW_IO5);
  237.     gpio_clr_pin(dotmatrixROW_IO6);
  238.     gpio_set_out(dotmatrixROW_IO6);
  239.     gpio_clr_pin(dotmatrixROW_IO7);
  240.     gpio_set_out(dotmatrixROW_IO7);
  241.     gpio_clr_pin(dotmatrixROW_IO8);
  242.     gpio_set_out(dotmatrixROW_IO8);
  243.  
  244.     /* Set up PWM controlling brightness */
  245. //  TCCR0A |= (1<<COM0B1)|(1<<WGM01)|(1<<WGM00);
  246. //  TCCR0B |= (1<<CS00);
  247. //  OCR0B = 0xff-act_DotMatrix_INITIAL_BRIGHTNESS;
  248. //  DDRD |= (1<<PD5);
  249.  
  250.     /* Set up output latch clock */
  251.     gpio_clr_pin(dotmatrixLATCHCLOCK_IO);
  252.     gpio_set_out(dotmatrixLATCHCLOCK_IO);
  253.  
  254.     /* Initialize USART in SPI-mode */
  255.     UBRR0 = 0;
  256.     USART_SPI_XCK_DDR |= (1<<USART_SPI_XCK); // xck (sck) output
  257.     UCSR0C = (1<<UMSEL01)|(1<<UMSEL00)|(0<<UCPHA0)|(0<<UCPOL0);
  258.     UCSR0B = (1<<RXEN0)|(1<<TXEN0);
  259.     UBRR0 = 1;
  260.  
  261.     dotmatrixClear();
  262.  
  263.     GrLcdState.color = GLCD_COLOR_BLACK;
  264. }
  265.  
  266. void dotmatrixClear(){
  267.     /* Clear buffer memory */
  268.     for (uint8_t i=0; i<dotmatrixSIZEY/8; i++)
  269.     {
  270.         for (uint8_t j=0; j<(dotmatrixSIZEX); j++)
  271.         {
  272.             dotmatrixFramebuf[i][j] = dotmatrixINITIAL_ROW;
  273.         }
  274.     }
  275. }
  276.  
  277. void dotmatrixSetXY(uint8_t x, uint8_t y){
  278.     GrLcdState.lcdXAddr = x;
  279.     GrLcdState.lcdYAddr = y;
  280. }
  281.  
  282. uint8_t dotmatrixGetX(void){
  283.     return GrLcdState.lcdXAddr;
  284. }
  285.  
  286. uint8_t dotmatrixGetY(void){
  287.     return GrLcdState.lcdYAddr;
  288. }
  289.  
  290.