Subversion Repositories HomeAutomation

Rev

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