Subversion Repositories HomeAutomation

Rev

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