Subversion Repositories HomeAutomation

Rev

Rev 1801 | Rev 1803 | 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. /* FIXME */
  96.     return input;
  97. }
  98.  
  99. void dotmatrixWriteData(uint8_t data, uint8_t color){
  100.     if (color == GLCD_COLOR_CLEAR)
  101.     {
  102.       data = ~data;
  103.     }
  104.     if (GrLcdState.color == GLCD_COLOR_BLACK)
  105.     {
  106.       dotmatrixSetData(data);
  107.     }
  108.     else
  109.     {
  110.       dotmatrixSetData(~data);
  111.     }
  112.    
  113.     GrLcdState.lcdXAddr++;
  114.  
  115.     if (GrLcdState.lcdXAddr > GRAPHICS_WIDTH ){
  116.         dotmatrixSetXY(0,GrLcdState.lcdYAddr+8);
  117.     }
  118. }
  119.  
  120. void dotmatrixWriteDataTransparent(uint8_t inputdata, uint8_t color){
  121.     uint8_t data = dotmatrixGetData();
  122.     if (GrLcdState.color != GLCD_COLOR_BLACK)
  123.     {
  124.       data = ~data;
  125.     }
  126.  
  127.     if (color == GLCD_COLOR_CLEAR)
  128.     {
  129.       data = data&(~inputdata);
  130.     }
  131.     else
  132.     {
  133.       data = data|inputdata;
  134.     }
  135.    
  136.     if (GrLcdState.color == GLCD_COLOR_BLACK)
  137.     {
  138.       dotmatrixSetData(data);
  139.     }
  140.     else
  141.     {
  142.       dotmatrixSetData(~data);
  143.     }
  144.  
  145.     GrLcdState.lcdXAddr++;
  146.  
  147.     if (GrLcdState.lcdXAddr > GRAPHICS_WIDTH ){
  148.         dotmatrixSetXY(0,GrLcdState.lcdYAddr+8);
  149.     }
  150. }
  151.  
  152. uint8_t dotmatrixReadData(void){
  153.     uint8_t data = 0;
  154.     if (GrLcdState.color == GLCD_COLOR_BLACK)
  155.     {
  156.       data = dotmatrixGetData();
  157.     }
  158.     else
  159.     {
  160.       data = ~dotmatrixGetData();
  161.     }
  162.  
  163.     return data;
  164. }
  165.  
  166. /* Callback run periodically to manage rows.
  167.    For each time callback is run, the next row is enabled and the last disabled
  168.  */
  169. void dotmatrixRefresh()
  170. {
  171.     /* Increase row counter */
  172.     if (dotmatrixRowCounter++ == 8) {dotmatrixRowCounter = 0;}
  173.  
  174.     /* Disable all rows */
  175.     gpio_clr_pin(dotmatrixROW_IO1);
  176.     gpio_clr_pin(dotmatrixROW_IO2);
  177.     gpio_clr_pin(dotmatrixROW_IO3);
  178.     gpio_clr_pin(dotmatrixROW_IO4);
  179.     gpio_clr_pin(dotmatrixROW_IO5);
  180.     gpio_clr_pin(dotmatrixROW_IO6);
  181.     gpio_clr_pin(dotmatrixROW_IO7);
  182.     gpio_clr_pin(dotmatrixROW_IO8);
  183.  
  184.     /* Write one column of frame buffer to shift registers */
  185.     dotmatrixSPIWrite(dotmatrixFramebuf[7-dotmatrixRowCounter][3]);
  186.     dotmatrixSPIWrite(dotmatrixFramebuf[7-dotmatrixRowCounter][2]);
  187.     dotmatrixSPIWrite(dotmatrixFramebuf[7-dotmatrixRowCounter][1]);
  188.     dotmatrixSPIWrite(dotmatrixFramebuf[7-dotmatrixRowCounter][0]);
  189.     /* Add more here to support 8-module panels */
  190.  
  191.     /* Toggle shift register latch */
  192.     gpio_clr_pin(dotmatrixLATCHCLOCK_IO);
  193.     gpio_set_pin(dotmatrixLATCHCLOCK_IO);
  194.  
  195.     /* Enable one row */
  196.     switch (dotmatrixRowCounter)
  197.     {
  198.         case 0:
  199.             gpio_set_pin(dotmatrixROW_IO1);
  200.             break;
  201.         case 1:
  202.             gpio_set_pin(dotmatrixROW_IO2);
  203.             break;
  204.         case 2:
  205.             gpio_set_pin(dotmatrixROW_IO3);
  206.             break;
  207.         case 3:
  208.             gpio_set_pin(dotmatrixROW_IO4);
  209.             break;
  210.         case 4:
  211.             gpio_set_pin(dotmatrixROW_IO5);
  212.             break;
  213.         case 5:
  214.             gpio_set_pin(dotmatrixROW_IO6);
  215.             break;
  216.         case 6:
  217.             gpio_set_pin(dotmatrixROW_IO7);
  218.             break;
  219.         case 7:
  220.             gpio_set_pin(dotmatrixROW_IO8);
  221.             break;
  222.            
  223.         default:
  224.             break;
  225.     }
  226. }
  227.  
  228. void dotmatrixInit(){
  229.     /* Set up row driver IO as low output */
  230.     gpio_clr_pin(dotmatrixROW_IO1);
  231.     gpio_set_out(dotmatrixROW_IO1);
  232.     gpio_clr_pin(dotmatrixROW_IO2);
  233.     gpio_set_out(dotmatrixROW_IO2);
  234.     gpio_clr_pin(dotmatrixROW_IO3);
  235.     gpio_set_out(dotmatrixROW_IO3);
  236.     gpio_clr_pin(dotmatrixROW_IO4);
  237.     gpio_set_out(dotmatrixROW_IO4);
  238.     gpio_clr_pin(dotmatrixROW_IO5);
  239.     gpio_set_out(dotmatrixROW_IO5);
  240.     gpio_clr_pin(dotmatrixROW_IO6);
  241.     gpio_set_out(dotmatrixROW_IO6);
  242.     gpio_clr_pin(dotmatrixROW_IO7);
  243.     gpio_set_out(dotmatrixROW_IO7);
  244.     gpio_clr_pin(dotmatrixROW_IO8);
  245.     gpio_set_out(dotmatrixROW_IO8);
  246.  
  247.     /* Set up PWM controlling brightness */
  248. //  TCCR0A |= (1<<COM0B1)|(1<<WGM01)|(1<<WGM00);
  249. //  TCCR0B |= (1<<CS00);
  250. //  OCR0B = 0xff-act_DotMatrix_INITIAL_BRIGHTNESS;
  251. //  DDRD |= (1<<PD5);
  252.  
  253.     /* Set up output latch clock */
  254.     gpio_clr_pin(dotmatrixLATCHCLOCK_IO);
  255.     gpio_set_out(dotmatrixLATCHCLOCK_IO);
  256.  
  257.     /* Initialize USART in SPI-mode */
  258.     UBRR0 = 0;
  259.     USART_SPI_XCK_DDR |= (1<<USART_SPI_XCK); // xck (sck) output
  260.     UCSR0C = (1<<UMSEL01)|(1<<UMSEL00)|(0<<UCPHA0)|(0<<UCPOL0);
  261.     UCSR0B = (1<<RXEN0)|(1<<TXEN0);
  262.     UBRR0 = 0;
  263.  
  264.     dotmatrixClear();
  265.  
  266.     GrLcdState.color = GLCD_COLOR_WHITE;
  267. }
  268.  
  269. void dotmatrixClear(){
  270.     /* Clear buffer memory */
  271.     for (uint8_t i=0; i<8; i++)
  272.     {
  273.         for (uint8_t j=0; j<4; j++)
  274.         {
  275.             dotmatrixFramebuf[i][j] = dotmatrixINITIAL_ROW;
  276.         }
  277.     }
  278. }
  279.  
  280. void dotmatrixSetXY(uint8_t x, uint8_t y){
  281.     GrLcdState.lcdXAddr = x;
  282.     GrLcdState.lcdYAddr = y;
  283.     GrLcdState.lcdYpage = y/8;
  284. }
  285.  
  286. uint8_t dotmatrixGetX(void){
  287.     return GrLcdState.lcdXAddr;
  288. }
  289.  
  290. uint8_t dotmatrixGetY(void){
  291.     return GrLcdState.lcdYAddr;
  292. }
  293.  
  294.