Subversion Repositories HomeAutomation

Rev

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