Subversion Repositories HomeAutomation

Rev

Rev 2043 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1.  
  2. #include "act_ks0108.h"
  3.  
  4. static unsigned char Splash_left[] PROGMEM = {
  5. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,//Line 1
  6. 0xff,0xff,0xff,0x3,0x7f,0x7f,0xbf,0xbf,0xdf,0x7,0xff,0xff,0xff,0x3f,0xbf,0xdf,0xdf,0xdf,0x3f,0xff,0xff,0xff,0x3f,0x3f,0xdf,0x9f,0x7f,0x7f,0xbf,0x9f,0x3f,0xff,0xff,0x7f,0xbf,0xdf,0xdf,0x9f,0x3f,0xff,0xff,0xff,0xff,0xff,0xff,0x3f,0xc7,0xf3,0xf,0xff,0xff,0xff,0x1f,0xff,0xff,0xff,0xff,0x3f,0xdf,0xff,0xef,0xef,0xef,0x40,0x5,0xf7,0xff,0xff,0x3f,0xbf,0xdf,0xdf,0xdf,0x3f,0xff,0xff,0xff,0x1f,0x7f,0xdf,0x9f,0x7f,0x7f,0x9f,0xdf,0x3f,0xff,0xff,0xbf,0xbf,0xdf,0xdf,0xdf,0x3f,0xff,0xff,0xef,0xef,0xef,0x40,0x5,0xf7,0xff,0x1d,0xff,0xff,0xff,0xff,0x3f,0xbf,0xdf,0xdf,0xdf,0x3f,0xff,0xff,0xff,0x1f,0xff,0x7f,0x9f,0xdf,0x3f,0xff,0xff,0xff,0xff,0xff,//Line 2
  7. 0xff,0xff,0xff,0xd0,0xff,0xff,0xff,0xff,0xff,0xf0,0xff,0xff,0xe0,0xef,0xdf,0xdf,0xcf,0xe3,0xf8,0xff,0xff,0xff,0xc0,0xfe,0xff,0xff,0xe0,0xfa,0xff,0xff,0xe0,0xff,0xff,0xf8,0xe6,0xee,0xee,0xf6,0xf7,0xfb,0xff,0xff,0xef,0xf3,0xfc,0xfe,0xfe,0xfe,0xff,0xf0,0xff,0xff,0xf4,0xcf,0xdf,0xe7,0xfb,0xf0,0xe7,0xff,0xff,0xff,0xff,0xff,0xe0,0xff,0xff,0xe0,0xef,0xdf,0xdf,0xcf,0xe3,0xf8,0xff,0xff,0xff,0xc0,0xfe,0xff,0xff,0xe0,0xfa,0xff,0xff,0xe0,0xff,0xff,0xf3,0xeb,0xed,0xed,0xf5,0xf0,0xff,0xff,0xff,0xff,0xff,0xff,0xe0,0xff,0xff,0xe0,0xff,0xff,0xff,0xf0,0xcf,0xdf,0xdf,0xcf,0xe3,0xf8,0xff,0xff,0xff,0xf4,0xf9,0xfe,0xff,0xff,0xe0,0xff,0xff,0xff,0xff,0xff,//Line 3
  8. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x3f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x7f,0xff,0xff,0xff,0xff,0xff,//Line 4
  9. 0xff,0xff,0xff,0xff,0xff,0xdf,0xef,0xef,0xef,0x1f,0xff,0xff,0xf,0xff,0xff,0xff,0xff,0x3f,0xcf,0xff,0xff,0x1f,0x9f,0xef,0xcf,0x3f,0x3f,0xdf,0xcf,0x1f,0xff,0xff,0x40,0x2f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x3f,0x5f,0xef,0xef,0xef,0xff,0xff,0x3f,0x1f,0x6f,0x6f,0xcf,0x9f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x3f,0xcf,0xf3,0xfc,0xff,0xff,0xff,0x47,0xb7,0xf7,0xf7,0xf7,0x77,0x8f,0xff,0xff,0xf,0x7f,0x3f,0xdf,0xdf,0xff,0xff,0x7f,0x9f,0xdf,0xef,0xef,0xef,0x1f,0xff,0xff,0xff,0xff,0xdd,0x3f,0xff,0xff,0xff,0x3f,0x1f,0x6f,0x6f,0xcf,0x9f,0xff,0xff,0xfd,0x3,0x7f,0xbf,0xdf,0xef,0xff,0xff,0xf7,0xf7,0xf7,0xa0,0x2,0xfb,0xff,0xff,0xff,//Line 5
  10. 0xff,0xff,0xff,0xff,0xf9,0xf6,0xf6,0xfa,0xfa,0xf8,0xff,0xff,0xf8,0xe7,0xef,0xf3,0xfd,0xfc,0xf1,0xff,0xff,0xe0,0xff,0xff,0xff,0xf0,0xfd,0xff,0xff,0xf0,0xff,0xff,0xff,0xf8,0xff,0xff,0xff,0xcf,0xc3,0xd3,0xe3,0xff,0xff,0xff,0xf7,0xe7,0xee,0xf6,0xf1,0xff,0xff,0xfc,0xf3,0xf7,0xf7,0xfb,0xfb,0xfd,0xff,0xff,0xff,0xcf,0xf3,0xfc,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x80,0x5b,0xf9,0xfd,0xfe,0xff,0xff,0xff,0xf4,0xf8,0xff,0xff,0xff,0xff,0xff,0xf8,0xe7,0xef,0xef,0xe7,0xf1,0xfc,0xff,0xff,0xcf,0xbf,0xbf,0xc0,0xff,0xff,0xff,0xfc,0xfb,0xf7,0xf7,0xfb,0xfb,0xfd,0xff,0xff,0xf8,0xfd,0xfd,0xfb,0xfb,0xff,0xff,0xff,0xff,0xff,0xff,0xf0,0xff,0xff,0xff,0xff,//Line 6
  11. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,//Line 7
  12. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0//Line 7
  13. };
  14.  
  15.  
  16.  
  17.  
  18. #define SOLID   0
  19. #define TRANSPARENT 1
  20. uint8_t color=GLCD_COLOR_BLACK;
  21. uint8_t Transparent=SOLID;
  22.  
  23. #if act_ks0108_USE_EXTERNAL_EEPROM==1
  24.  
  25.     void storeImage(uint8_t id, uint8_t size_x , uint8_t size_y);
  26.  
  27.     void saveImageFromScreen(uint8_t id, uint8_t size_x , uint8_t size_y);
  28.  
  29.     #define MEMORY_I2C_ADDR  0x57   // 64K EEPROM - allowed addresses are 0x50 to 0x57
  30.     #define MEMORY_ADDR_MSB  0      // start at base of memory. Next one is 0x01.
  31.     #define MEMORY_ADDR_LSB  0      // start on a page boundary. Next one is 0x80.
  32.     unsigned char messageBuf[MESSAGEBUF_SIZE];
  33. #endif
  34.  
  35.  
  36. void numtoascii( int16_t num, char **str );
  37. void signedtoascii(int16_t num, uint8_t decimalplace, char *string, uint8_t numberofdecimals);
  38. void unsignedtoascii(uint16_t num, uint8_t decimalplace, char *string, uint8_t numberofdecimals);
  39.  
  40. #if act_ks0108_USE_EXTERNAL_EEPROM==1
  41. //valid id is 1 to 255;
  42. void printImage(uint8_t id, uint8_t x_start, uint8_t y_start) {
  43.     if (id == 0 )
  44.         return;     //id=0 not allowed
  45.     uint16_t address = 0;
  46.     uint16_t buf_index = 0;
  47.     uint8_t index;
  48.     uint8_t size_x = 0;
  49.     uint8_t size_y = 0;
  50.     uint16_t max_picture_size;
  51.     index = (id>>5)&0x07;
  52.     //find starting adress of the requested data
  53.     switch (index) {
  54.         case 0:
  55.             address = (index)*1024;
  56.             max_picture_size = 1024;
  57.             break;
  58.         case 1:
  59.             address = 32768+(index)*512;
  60.             max_picture_size = 512;
  61.             break;
  62.         case 2:
  63.             address = 32768+32*512+(index)*256;
  64.             max_picture_size = 256;
  65.             break;
  66.         case 3:
  67.             address = 32768+32*512+32*256+(index)*128;
  68.             max_picture_size = 128;
  69.             break;
  70.         case 4:
  71.             address = 32768+32*512+32*256+32*128+(index)*64;
  72.             max_picture_size = 64;
  73.             break;
  74.         case 5:
  75.             address = 32768+32*512+32*256+32*128+32*64+(index)*32;
  76.             max_picture_size = 32;
  77.             break;
  78.         case 6:
  79.             address = 32768+32*512+32*256+32*128+32*64+32*32+(index)*16;
  80.             max_picture_size = 16;
  81.             break;
  82.         case 7:
  83.             address = 32768+32*512+32*256+32*128+32*64+32*32+32*16+(index)*16;
  84.             max_picture_size = 16;
  85.             break;
  86.     }
  87.     //Get size of picture (every picture has an 4 byte info-tag)
  88.     messageBuf[0] = (MEMORY_I2C_ADDR<<TWI_ADR_BITS); // Read/write bit doesn't matter here
  89.     messageBuf[1] = (uint8_t)(0);       // Starting address in memory
  90.     messageBuf[2] = (uint8_t)(((id&0xe0)>>5) & 0xff);           // Starting address in memory
  91.     TWI_Start_Random_Read( messageBuf, 129, 3 );    // Desired data length plus one (command byte).
  92.     // Could do other actions in here, then get data.
  93.     TWI_Read_Data_From_Buffer( messageBuf, 129 );
  94.     size_x = messageBuf[((id&0x1f)<<2)];
  95.     size_y = messageBuf[((id&0x1f)<<2)+1];
  96.     //more_data = messageBuf[((id&0x1f)<<2)+2];
  97.     //last_data = messageBuf[((id&0x1f)<<2)+3];
  98.  
  99.     // get data from eeprom
  100.     messageBuf[0] = (MEMORY_I2C_ADDR<<TWI_ADR_BITS); // Read/write bit doesn't matter here
  101.     messageBuf[1] = (uint8_t)((address >> 8) & 0xff);       // Starting address in memory
  102.     messageBuf[2] = (uint8_t)(address & 0xff);       // Starting address in memory
  103.     TWI_Start_Random_Read( messageBuf, 129, 3 );    // Desired data length plus one (command byte).
  104.     // Could do other actions in here, then get data.
  105.     TWI_Read_Data_From_Buffer( messageBuf, 129 );
  106.     uint8_t jy = 0;
  107.     uint8_t jx = 0;
  108.     buf_index = 0;
  109.     for (jy = 0; jy< size_y; jy+=8){
  110.         glcdSetXY(x_start,y_start+jy);
  111.         for (jx = 0; jx< size_x ; jx++){
  112.             if (buf_index>=128) {   //get new data if buffer empty
  113.                 address +=128;
  114.                 messageBuf[0] = (MEMORY_I2C_ADDR<<TWI_ADR_BITS); // Read/write bit doesn't matter here
  115.                 messageBuf[1] = (uint8_t)((address >> 8) & 0xff);       // Starting address in memory
  116.                 messageBuf[2] = (uint8_t)(address & 0xff);       // Starting address in memory
  117.                 TWI_Start_Random_Read( messageBuf, 129, 3 );    // Desired data length plus one (command byte).
  118.                 // Could do other actions in here, then get data.
  119.                 TWI_Read_Data_From_Buffer( messageBuf, 129 );
  120.                 buf_index=0;
  121.             }
  122.             glcdWriteData(messageBuf[buf_index+1], GLCD_COLOR_CLEAR);
  123.             buf_index++;
  124.         }
  125.     }
  126. }
  127. volatile uint8_t state;
  128. volatile uint8_t lastId = 255;
  129. #define IDLE    0
  130. #define RECEIVING_DATA 1
  131. void newImageData(StdCan_Msg_t *rxMsg) {
  132.     //State machine for storing new data
  133.     switch (state) {
  134.         case IDLE:
  135.             if (rxMsg->Header.Command == CAN_MODULE_CMD_KS0108_LCD_NEW_IMAGE)
  136.             {
  137.                 storeImage(rxMsg->Data[0],rxMsg->Data[1],rxMsg->Data[2]);
  138.                 state = RECEIVING_DATA;
  139.                 lastId = 255;
  140.                 //response message
  141.                 rxMsg->Length = 1;
  142.                 rxMsg->Header.Command = CAN_MODULE_CMD_KS0108_LCD_ACK;
  143.                 while (StdCan_Put(rxMsg) != StdCan_Ret_OK);  
  144.             }else {
  145.                 //error message
  146.                 rxMsg->Length = 0;
  147.                 rxMsg->Header.Command = CAN_MODULE_CMD_KS0108_LCD_ERROR;
  148.                 while (StdCan_Put(rxMsg) != StdCan_Ret_OK);  
  149.             }
  150.             break;
  151.         case RECEIVING_DATA:
  152.             if (rxMsg->Header.Command == CAN_MODULE_CMD_KS0108_LCD_IMAGE_DATA)
  153.             {
  154.                 if (rxMsg->Data[0] == (lastId+1)) {
  155.                     storeImage(rxMsg->Data[0],rxMsg->Data[1],rxMsg->Data[2]);
  156.                     state = RECEIVING_DATA;
  157.                     lastId++;
  158.  
  159.                     //FIXME: call function that handles the data
  160.  
  161.                     //response message
  162.                     rxMsg->Length = 1;
  163.                     rxMsg->Header.Command = CAN_MODULE_CMD_KS0108_LCD_ACK;
  164.                     while (StdCan_Put(rxMsg) != StdCan_Ret_OK);  
  165.                 } else {
  166.                     //wrong id
  167.                     rxMsg->Length = 1;
  168.                     rxMsg->Header.Command = CAN_MODULE_CMD_KS0108_LCD_DATA_ID_ERROR;
  169.                     rxMsg->Data[0] = (lastId+1);
  170.                     while (StdCan_Put(rxMsg) != StdCan_Ret_OK);  
  171.                 }
  172.             } else if (rxMsg->Header.Command == CAN_MODULE_CMD_KS0108_LCD_DATA_DONE) {
  173.                 //Call function to flush eeprom cache
  174.                 //FIXME
  175.                 state=IDLE;
  176.             } else {
  177.                 //error message
  178.                 rxMsg->Length = 0;
  179.                 rxMsg->Header.Command = CAN_MODULE_CMD_KS0108_LCD_ERROR;
  180.                 while (StdCan_Put(rxMsg) != StdCan_Ret_OK);  
  181.             }
  182.             break;
  183.     }
  184.  
  185. }
  186.  
  187.  
  188. //valid id is 1 to 255;
  189. void storeImage(uint8_t id, uint8_t size_x , uint8_t size_y) {
  190.     if (id == 0 )
  191.         return;     //id=0 not allowed
  192.     //uint16_t address = 0;
  193.     //uint16_t buf_index = 0;
  194.     uint8_t index;
  195.     //uint16_t max_picture_size;
  196.     index = (id>>5)&0x07;
  197.     //find starting adress of the requested data
  198.     //Get size of picture (every picture has an 4 byte info-tag)
  199.     messageBuf[0] = (MEMORY_I2C_ADDR<<TWI_ADR_BITS); // Read/write bit doesn't matter here
  200.     messageBuf[1] = (uint8_t)(0);       // Starting address in memory
  201.     messageBuf[2] = (uint8_t)(((id&0xe0)>>5) & 0xff);           // Starting address in memory
  202.     TWI_Start_Random_Read( messageBuf, 129, 3 );    // Desired data length plus one (command byte).
  203.     // Could do other actions in here, then get data.
  204.     TWI_Read_Data_From_Buffer( messageBuf, 129 );
  205.     messageBuf[((id&0x1f)<<2)+1] = size_x;
  206.     messageBuf[((id&0x1f)<<2)+2] = size_y;
  207.     //more_data = messageBuf[((id&0x1f)<<2)+2];
  208.     //last_data = messageBuf[((id&0x1f)<<2)+3];
  209.  
  210.     //move data in the buffer
  211.     for (index = 128; index > 0; index--) {
  212.         messageBuf[index+2] = messageBuf[index];
  213.     }
  214.     messageBuf[0] = (MEMORY_I2C_ADDR<<TWI_ADR_BITS) | (FALSE<<TWI_READ_BIT);
  215.     messageBuf[1] = (uint8_t)(0);       // Starting address in memory
  216.     messageBuf[2] = (uint8_t)(((id&0xe0)>>5) & 0xff);           // Starting address in memory
  217.     TWI_Start_Read_Write( messageBuf, 131 );
  218.     // Should wait for completion of EEPROM write cycle. Blinking the LED will do this.
  219.     while ( TWI_Transceiver_Busy() );    // Check for an error.
  220.  
  221.     //Now the info-tag is written, only to add data left
  222.  
  223.     //FIXME........ FIX!!!
  224.  
  225. }
  226.  
  227.  
  228. //valid id is 1 to 255;
  229. void saveImageFromScreen(uint8_t id, uint8_t size_x , uint8_t size_y) {
  230.     if (id == 0 )
  231.         return;     //id=0 not allowed
  232.     uint16_t max_picture_size;
  233.     uint8_t index;
  234.     uint8_t bufferIndex;
  235.     uint16_t address = 0;
  236.     index = (id>>5)&0x07;
  237.     //find starting adress of the requested data
  238.     switch (index) {
  239.         case 0:
  240.             address = (index)*1024;
  241.             max_picture_size = 1024;
  242.             break;
  243.         case 1:
  244.             address = 32768+(index)*512;
  245.             max_picture_size = 512;
  246.             break;
  247.         case 2:
  248.             address = 32768+32*512+(index)*256;
  249.             max_picture_size = 256;
  250.             break;
  251.         case 3:
  252.             address = 32768+32*512+32*256+(index)*128;
  253.             max_picture_size = 128;
  254.             break;
  255.         case 4:
  256.             address = 32768+32*512+32*256+32*128+(index)*64;
  257.             max_picture_size = 64;
  258.             break;
  259.         case 5:
  260.             address = 32768+32*512+32*256+32*128+32*64+(index)*32;
  261.             max_picture_size = 32;
  262.             break;
  263.         case 6:
  264.             address = 32768+32*512+32*256+32*128+32*64+32*32+(index)*16;
  265.             max_picture_size = 16;
  266.             break;
  267.         case 7:
  268.             address = 32768+32*512+32*256+32*128+32*64+32*32+32*16+(index)*16;
  269.             max_picture_size = 16;
  270.             break;
  271.     }
  272.     //Get size of picture (every picture has an 4 byte info-tag)
  273.     messageBuf[0] = (MEMORY_I2C_ADDR<<TWI_ADR_BITS); // Read/write bit doesn't matter here
  274.     messageBuf[1] = (uint8_t)(0);       // Starting address in memory
  275.     messageBuf[2] = (uint8_t)(((id&0xe0)>>5) & 0xff);           // Starting address in memory
  276.     TWI_Start_Random_Read( messageBuf, 129, 3 );    // Desired data length plus one (command byte).
  277.     // Could do other actions in here, then get data.
  278.     TWI_Read_Data_From_Buffer( messageBuf, 129 );
  279.     messageBuf[((id&0x1f)<<2)+1] = size_x;
  280.     messageBuf[((id&0x1f)<<2)+2] = size_y;
  281.  
  282.     //move data in the buffer
  283.     for (index = 128; index > 0; index--) {
  284.         messageBuf[index+2] = messageBuf[index];
  285.     }
  286.     messageBuf[0] = (MEMORY_I2C_ADDR<<TWI_ADR_BITS) | (FALSE<<TWI_READ_BIT);
  287.     messageBuf[1] = (uint8_t)(0);       // Starting address in memory
  288.     messageBuf[2] = (uint8_t)(((id&0xe0)>>5) & 0xff);           // Starting address in memory
  289.     TWI_Start_Read_Write( messageBuf, 131 );
  290.     // Should wait for completion of EEPROM write cycle. Blinking the LED will do this.
  291.     while ( TWI_Transceiver_Busy() );    // Check for an error.
  292.  
  293.     //Now the info-tag is written, only to add data left
  294.     uint8_t  h, i ;
  295.        
  296.  
  297.     messageBuf[0] = (MEMORY_I2C_ADDR<<TWI_ADR_BITS) | (FALSE<<TWI_READ_BIT);
  298.     messageBuf[1] = (uint8_t)((address >> 8) & 0xff);       // Starting address in memory
  299.     messageBuf[2] = (uint8_t)(address & 0xff);       // Starting address in memory
  300.     bufferIndex = 3;
  301.  
  302.     h = 0;
  303.     while(h+8 <= size_y) {
  304.         h += 8;
  305.         glcdSetXY(0, h);
  306.        
  307.         for(i=0; i<=size_x; i++) {
  308.             messageBuf[bufferIndex] = glcdReadData();
  309.             bufferIndex++;
  310.             if (bufferIndex >= 131) {
  311.                 TWI_Start_Read_Write( messageBuf, 131 );
  312.                 // Should wait for completion of EEPROM write cycle. Blinking the LED will do this.
  313.                 while ( TWI_Transceiver_Busy() );    // Check for an error.
  314.                 bufferIndex = 3;
  315.                 address+=128;
  316.                 messageBuf[0] = (MEMORY_I2C_ADDR<<TWI_ADR_BITS) | (FALSE<<TWI_READ_BIT);
  317.                 messageBuf[1] = (uint8_t)((address >> 8) & 0xff);       // Starting address in memory
  318.                 messageBuf[2] = (uint8_t)(address & 0xff);       // Starting address in memory
  319.             }
  320.         }
  321.     }
  322. }
  323. #endif
  324.  
  325. #if GRAPHICS_DRIVER==DOTMATRIX
  326. /* Refresh display */
  327. void act_ks0108_Refresh(uint8_t timer)
  328. {
  329.     glcdRefresh();
  330. }
  331. #endif
  332.  
  333. void act_ks0108_Init(void)
  334. {
  335.     /* Set up PWM controlling brightness */
  336. #if GRAPHICS_DRIVER==KS0108
  337.     TCCR0A |= (1<<COM0A1)|(1<<WGM01)|(1<<WGM00);
  338.     TCCR0B |= (1<<CS00);
  339.     OCR0A = act_ks0108_INITIAL_BACKLIGHT;
  340.     DDRD |= (1<<PD6);
  341. #endif
  342. #if GRAPHICS_DRIVER==DOTMATRIX
  343.     TCCR0A |= (1<<COM0B1)|(1<<WGM01)|(1<<WGM00);
  344.     TCCR0B |= (1<<CS00);
  345.     OCR0B = 0xff-act_ks0108_INITIAL_BACKLIGHT;
  346.     DDRD |= (1<<PD5);
  347.    
  348.     /* Start timer for row management */
  349.     // TODO polled from process() or callback??
  350.     Timer_SetTimeout(act_ks0108_DOTMATRIX_TIMER, 1, TimerTypeFreeRunning, &act_ks0108_Refresh);
  351. #endif
  352.  
  353.  
  354. #if act_ks0108_USE_EXTERNAL_EEPROM==1
  355.     TWI_Master_Initialise();
  356.     uint8_t temp = 0;
  357.     /// Do initial write to EEPROM. Write cycle takes 5 msecs, so flashing the led will delay enough.
  358.     //
  359.    
  360.     messageBuf[0] = (MEMORY_I2C_ADDR<<TWI_ADR_BITS) | (FALSE<<TWI_READ_BIT);
  361.     messageBuf[1] = (((1024+128)>>8)&0xff);             // Starting address MSB
  362.     messageBuf[2] = ((1024+128)&0xff);              // Starting address LSB
  363.    
  364.     for (temp=0; temp<128; temp++)
  365.     {
  366.         messageBuf[temp+3] = 128-temp; 
  367.     }
  368.     TWI_Start_Read_Write( messageBuf, 131 );
  369.     // Should wait for completion of EEPROM write cycle. Blinking the LED will do this.
  370.     while ( TWI_Transceiver_Busy() );    // Check for an error.
  371.    
  372.     //delay 10ms
  373.     uint32_t temp2;
  374.     temp2 =Timer_GetTicks();
  375.     while (Timer_GetTicks() < temp2 + 10);
  376.  
  377.     #define TEMPID 0
  378.     uint16_t address = (TEMPID+1)*1024; //only addresses on page border is allowed
  379.     uint16_t ixa = 0;
  380.     uint8_t jxa = 0;
  381.     for (jxa = 0; jxa< 8; jxa++){
  382.         for (ixa = 0; ixa < 128; ixa++){
  383.             messageBuf[ixa+3] = (((uint8_t)pgm_read_byte((uint16_t)&Splash_left+ixa+jxa*128)) ^ 0xff);
  384.         }
  385.         messageBuf[0] = (MEMORY_I2C_ADDR<<TWI_ADR_BITS) | (FALSE<<TWI_READ_BIT);
  386.         messageBuf[1] = (uint8_t)((address >> 8) & 0xff);       // Starting address in memory
  387.         messageBuf[2] = (uint8_t)(address & 0xff);       // Starting address in memory
  388.         TWI_Start_Read_Write( messageBuf, 131 );
  389.         // Should wait for completion of EEPROM write cycle. Blinking the LED will do this.
  390.         while ( TWI_Transceiver_Busy() );    // Check for an error.
  391.         //delay 10ms
  392.         temp2 =Timer_GetTicks();
  393.         while (Timer_GetTicks() < temp2 + 10);
  394.         address += 128;
  395.     }
  396.  
  397.     address = (TEMPID+2)*1024;  //only addresses on page border is allowed
  398.     ixa = 0;
  399.     jxa = 0;
  400.     for (jxa = 0; jxa< 8; jxa++){
  401.         for (ixa = 0; ixa < 128; ixa++){
  402.             messageBuf[ixa+3] = (uint8_t)pgm_read_byte((uint16_t)&Splash_left+ixa+jxa*128);
  403.         }
  404.         messageBuf[0] = (MEMORY_I2C_ADDR<<TWI_ADR_BITS) | (FALSE<<TWI_READ_BIT);
  405.         messageBuf[1] = (uint8_t)((address >> 8) & 0xff);       // Starting address in memory
  406.         messageBuf[2] = (uint8_t)(address & 0xff);       // Starting address in memory
  407.         TWI_Start_Read_Write( messageBuf, 131 );
  408.         // Should wait for completion of EEPROM write cycle. Blinking the LED will do this.
  409.         while ( TWI_Transceiver_Busy() );    // Check for an error.
  410.         //delay 10ms
  411.         temp2 =Timer_GetTicks();
  412.         while (Timer_GetTicks() < temp2 + 10);
  413.         address += 128;
  414.     }
  415.     for (temp=0; temp<131; temp++)
  416.     {
  417.         messageBuf[temp] = 0;  
  418.     }
  419.  
  420. #endif
  421.  
  422.     glcdInit();
  423.  
  424. #if act_ks0108_USE_EXTERNAL_EEPROM==0
  425.     uint16_t ixa = 0;
  426.     uint8_t jxa = 0;
  427.     for (jxa = 0; jxa< 8; jxa++){
  428.         glcdSetXY(0,jxa*8);
  429.         for (ixa = 0; ixa < 128; ixa++){
  430.             glcdWriteData((uint8_t)pgm_read_byte((uint16_t)&Splash_left+ixa+jxa*128), GLCD_COLOR_CLEAR);
  431.         }
  432.     }
  433. #endif
  434.  
  435. #if act_ks0108_USE_EXTERNAL_EEPROM==1
  436.     printImage(1,0,0);
  437.     //delay 10ms
  438.     temp2 =Timer_GetTicks();
  439.     while (Timer_GetTicks() < temp2 + 5000);
  440.     printImage(0,0,0);
  441. #endif
  442.     //glcdSetXY(0,0);
  443.     //glcdPutStrTransparent("=_-[]_-=",GLCD_COLOR_SET);
  444.     //glcdSetXY(0,8);
  445.     //glcdPutStrTransparent("--------",GLCD_COLOR_SET);
  446.     //glcdSetXY(0,16);
  447.     //glcdPutStr("-_=[]=-_",GLCD_COLOR_SET);
  448.     //glcdDrawRect(32, 32, 40, 16, GLCD_COLOR_SET);
  449.     //glcdDrawLine(10, 10, 100, 60, GLCD_COLOR_SET);
  450.     //glcdFillRect(90, 10, 20, 20, GLCD_COLOR_SET);
  451.     //glcdInvertRect(28, 10, 20, 20);
  452.     //glcdInvert();
  453.     //glcdDrawRoundRect(85, 5, 30, 30, 5, GLCD_COLOR_SET);
  454.     //glcdDrawCircle(64, 32, 10, 1);
  455.     glcdSetXY(87,8);
  456.     glcdPutStrTransparent("Home-",GLCD_COLOR_SET);
  457.     glcdSetXY(80,16);
  458.     glcdPutStrTransparent("Automa-",GLCD_COLOR_SET);
  459.     glcdSetXY(91,24);
  460.     glcdPutStrTransparent("ion",GLCD_COLOR_SET);
  461.     //glcdDrawLine(10, 62, 50, 5, GLCD_COLOR_CLEAR);
  462.    
  463. }
  464.  
  465. void act_ks0108_Process(void)
  466. {
  467.     ///TODO: Stuff that needs doing is done here
  468. }
  469.  
  470. void act_ks0108_HandleMessage(StdCan_Msg_t *rxMsg)
  471. {
  472.     //StdCan_Msg_t txMsg;
  473.     uint8_t n = 0;
  474.  
  475.     if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_ACT &&
  476.         StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_TO_OWNER &&
  477.         rxMsg->Header.ModuleType == CAN_MODULE_TYPE_ACT_KS0108 &&
  478.         rxMsg->Header.ModuleId == act_ks0108_ID)
  479.     {
  480.     switch (rxMsg->Header.Command)
  481.     {
  482.         case CAN_MODULE_CMD_KS0108_LCD_CLEAR:
  483.           if (rxMsg->Length == 1) {
  484.             glcdSetColor((0x80&rxMsg->Data[0])>>7);
  485.           }
  486.           glcdClear();
  487.           rxMsg->Data[0] = glcdGetColor()<<7;
  488.           StdCan_Set_direction(rxMsg->Header, DIRECTIONFLAG_FROM_OWNER);
  489.           rxMsg->Length = 1;
  490.           #if act_ks0108_USE_EXTERNAL_EEPROM==1
  491.               uint8_t temp = 0;
  492.               //    Read the data from Memory. Value of read/write bit doesn't matter.
  493.                 messageBuf[0] = (MEMORY_I2C_ADDR<<TWI_ADR_BITS); // Read/write bit doesn't matter here
  494.                 messageBuf[1] = MEMORY_ADDR_MSB;       // Starting address in memory
  495.                 messageBuf[2] = MEMORY_ADDR_LSB;       // Starting address in memory
  496.                 TWI_Start_Random_Read( messageBuf, 129, 3 );    // Desired data length plus one (command byte).
  497.                 // Could do other actions in here, then get data.
  498.                 temp = TWI_Read_Data_From_Buffer( messageBuf, 129 );
  499.               rxMsg->Length = 6;
  500.               rxMsg->Data[1] = temp;
  501.               rxMsg->Data[2] = messageBuf[0+3];
  502.               rxMsg->Data[3] = messageBuf[0+4];
  503.               rxMsg->Data[4] = messageBuf[0+5];
  504.               rxMsg->Data[5] = messageBuf[0+80];
  505.           #endif
  506.           while (StdCan_Put(rxMsg) != StdCan_Ret_OK);
  507.           break;
  508.         case CAN_MODULE_CMD_KS0108_LCD_INVERT:
  509.           if (rxMsg->Length == 1) {
  510.             if ((0x80&rxMsg->Data[0])>>7 != glcdGetColor())
  511.                 glcdInvert();
  512.           } else
  513.             glcdInvert();
  514.           rxMsg->Data[0] = glcdGetColor()<<7;
  515.           StdCan_Set_direction(rxMsg->Header, DIRECTIONFLAG_FROM_OWNER);
  516.           rxMsg->Length = 1;
  517.           while (StdCan_Put(rxMsg) != StdCan_Ret_OK);
  518.           break;
  519.         case CAN_MODULE_CMD_KS0108_LCD_CURSOR:
  520.           if (rxMsg->Length == 2) {
  521.             glcdSetXY((0x3f&rxMsg->Data[0])*6, rxMsg->Data[1]*8);
  522.             color = ((0x80&rxMsg->Data[2])>>7);
  523.             Transparent = ((0x40&rxMsg->Data[2])>>6);
  524.           }
  525.           rxMsg->Data[0] = glcdGetColor()<<7;
  526.           rxMsg->Data[0] = Transparent<<6;
  527.           rxMsg->Data[0] |= (0x3f & (glcdGetX()/6));
  528.           rxMsg->Data[1] |= ((glcdGetY()/8));
  529.           StdCan_Set_direction(rxMsg->Header, DIRECTIONFLAG_FROM_OWNER);
  530.           rxMsg->Length = 2;
  531.           while (StdCan_Put(rxMsg) != StdCan_Ret_OK);
  532.  
  533.           break;
  534.  
  535.         case CAN_MODULE_CMD_KS0108_LCD_TEXTAT:
  536.           glcdSetXY((0x3f&rxMsg->Data[0])*6, rxMsg->Data[1]*8);
  537.           for (n = 2; n < rxMsg->Length; n++)
  538.           {
  539.               if (((0x40&rxMsg->Data[0])>>6)== TRANSPARENT)
  540.                 glcdWriteCharTransparent((char)rxMsg->Data[n], (0x80&rxMsg->Data[0])>>7);
  541.               else
  542.                 glcdWriteChar((char)rxMsg->Data[n], (0x80&rxMsg->Data[0])>>7);
  543.           }
  544.         break;
  545.  
  546.         case CAN_MODULE_CMD_KS0108_LCD_TEXT:
  547.         for (n = 0; n < rxMsg->Length; n++)
  548.         {
  549.             if (Transparent == TRANSPARENT)
  550.                 glcdWriteCharTransparent((char)rxMsg->Data[n], color);
  551.             else
  552.                 glcdWriteChar((char)rxMsg->Data[n], color);
  553.         }
  554.         break;
  555.  
  556.         case CAN_MODULE_CMD_KS0108_LCD_BACKLIGHT:
  557.             if (rxMsg->Length > 0) {
  558.                 if ( rxMsg->Data[0] == 0) {
  559.                     #if GRAPHICS_DRIVER==KS0108
  560.                     TCCR0A &= ~(1<<COM0A0);
  561.                     TCCR0A &= ~(1<<COM0A1);
  562.                     PORTD &= ~(1<<PD6);
  563.                     OCR0A = rxMsg->Data[0];
  564.                     #endif
  565.                         #if GRAPHICS_DRIVER==DOTMATRIX
  566.                     TCCR0A &= ~(1<<COM0B0);
  567.                     TCCR0A &= ~(1<<COM0B1);
  568.                     PORTD |= (1<<PD5);
  569.                     OCR0B = 0xff-rxMsg->Data[0];
  570.                     #endif
  571.                 } else {
  572.                     #if GRAPHICS_DRIVER==KS0108
  573.                     TCCR0A |= (1<<COM0A1)|(1<<WGM01)|(1<<WGM00);
  574.                     OCR0A = rxMsg->Data[0];
  575.                     #endif
  576.                         #if GRAPHICS_DRIVER==DOTMATRIX
  577.                     TCCR0A |= (1<<COM0B1)|(1<<WGM01)|(1<<WGM00);
  578.                     OCR0B = 0xff-rxMsg->Data[0];
  579.                     #endif
  580.                 }
  581.  
  582.             }
  583.             StdCan_Msg_t txMsg;
  584.             StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_ACT);
  585.             StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  586.             txMsg.Header.ModuleType = CAN_MODULE_TYPE_ACT_KS0108;
  587.             txMsg.Header.ModuleId = act_ks0108_ID;
  588.             txMsg.Header.Command = CAN_MODULE_CMD_KS0108_LCD_BACKLIGHT;
  589.             txMsg.Length = 1;
  590.             txMsg.Data[0] = OCR0A;
  591.             while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  592.         break;
  593.        
  594.         case CAN_MODULE_CMD_KS0108_LCD_DRAWRECT:
  595.           if ((0x40&rxMsg->Data[0])>>6)
  596.             glcdFillRect(rxMsg->Data[1], rxMsg->Data[2], rxMsg->Data[3], rxMsg->Data[4], (0x80&rxMsg->Data[0])>>7);
  597.           else
  598.             if (rxMsg->Data[5] == 0)
  599.                 glcdDrawRect(rxMsg->Data[1], rxMsg->Data[2], rxMsg->Data[3], rxMsg->Data[4], (0x80&rxMsg->Data[0])>>7);
  600.             else
  601.                 glcdDrawRoundRect(rxMsg->Data[1], rxMsg->Data[2], rxMsg->Data[3], rxMsg->Data[4], rxMsg->Data[5],(0x80&rxMsg->Data[0])>>7);
  602.           break;
  603.        
  604.         case CAN_MODULE_CMD_KS0108_LCD_DRAWLINE:
  605.           glcdDrawLine(rxMsg->Data[1], rxMsg->Data[2], rxMsg->Data[3], rxMsg->Data[4], (0x80&rxMsg->Data[0])>>7);
  606.           break;
  607.  
  608.         case CAN_MODULE_CMD_KS0108_LCD_DRAWCIRCLE:
  609.           glcdDrawCircle(rxMsg->Data[1], rxMsg->Data[2], rxMsg->Data[3], (0x80&rxMsg->Data[0])>>7);
  610.           break;
  611.    
  612.         case CAN_MODULE_CMD_KS0108_LCD_INVERTRECT:
  613.           glcdInvertRect(rxMsg->Data[0], rxMsg->Data[1], rxMsg->Data[2], rxMsg->Data[3]);
  614.           break;
  615.  
  616.         }
  617.     }
  618. /*
  619.     static uint8_t saer=0;
  620.     static uint8_t saer2=0;
  621.     saer++;
  622.     if (saer>10) {
  623.         saer2++;
  624.         uint8_t ixa = 0;
  625.         uint8_t jxa = 0;
  626.         for (jxa = 0; jxa< 8; jxa++){
  627.             glcdSetXY(0,jxa);
  628.             for (ixa = 0; ixa < 128; ixa++){
  629.                 glcdWriteData((uint8_t)pgm_read_byte((uint16_t)&Splash_left+ixa+jxa*128));
  630.             }
  631.         }
  632.         saer=0;
  633.         glcdSetXY(83,1);
  634.         glcdPutStr("Tycker ");
  635.         glcdSetXY(83,2);
  636.         glcdPutStr(" detta ");
  637.         glcdSetXY(83,3);
  638.         glcdPutStr("funkar!");
  639.         glcdSetXY(83,4);
  640.         char buffer[20];   
  641.         //numtoascii((int16_t)saer2 ,*buffer);
  642.         unsignedtoascii((int16_t)saer2,0,buffer,1);
  643.         glcdPutStr(buffer);
  644.     }
  645. */
  646. }
  647.  
  648. void act_ks0108_List(uint8_t ModuleSequenceNumber)
  649. {
  650.     StdCan_Msg_t txMsg;
  651.  
  652.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_ACT);
  653.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  654.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_ACT_KS0108;
  655.     txMsg.Header.ModuleId = act_ks0108_ID;
  656.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  657.     txMsg.Length = 6;
  658.  
  659.     uint32_t HwId=BIOS_GetHwId();
  660.     txMsg.Data[0] = HwId&0xff;
  661.     txMsg.Data[1] = (HwId>>8)&0xff;
  662.     txMsg.Data[2] = (HwId>>16)&0xff;
  663.     txMsg.Data[3] = (HwId>>24)&0xff;
  664.  
  665.     txMsg.Data[4] = NUMBER_OF_MODULES;
  666.     txMsg.Data[5] = ModuleSequenceNumber;
  667.  
  668.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  669. }
  670.  
  671. //� la pengi. Skall libbifieras.
  672.     void numtoascii( int16_t num, char **str ) {
  673.         if( num==0 ) return;
  674.         numtoascii( num/10, str );
  675.         **str = '0' + (num%10);
  676.         (*str)++;
  677.     }
  678.  
  679.     void signedtoascii(int16_t num, uint8_t decimalplace, char *string, uint8_t numberofdecimals){ //decimalplace is number of decimals
  680.         uint8_t i;
  681.  
  682.         if( num<0 ) {
  683.             *(string++) = '-';
  684.             num = -num;
  685.         }
  686.         numtoascii(num>>decimalplace, &string );
  687.         if(numberofdecimals!=0) *(string++) = '.';
  688.         for(i=0;i<numberofdecimals;i++) {
  689.             num %= 1<<decimalplace;
  690.             num *= 10;
  691.             *(string++) = '0' + (num>>decimalplace);
  692.         }
  693.         *(string++) = 0;
  694.     }
  695.  
  696.     void unsignedtoascii(uint16_t num, uint8_t decimalplace, char *string, uint8_t numberofdecimals){ //decimalplace is number of decimals
  697.         uint8_t i;
  698.         numtoascii(num>>decimalplace, &string );
  699.         if(numberofdecimals!=0) *(string++) = '.';
  700.         for(i=0;i<numberofdecimals;i++) {
  701.             num %= 1<<decimalplace;
  702.             num *= 10;
  703.             *(string++) = '0' + (num>>decimalplace);
  704.         }
  705.         *(string++) = 0;
  706.     }
  707.