graphlcd-base/glcddrivers/picctl.c
changeset 30 7fd00015f62f
parent 29 2723893dc865
equal deleted inserted replaced
29:2723893dc865 30:7fd00015f62f
    11  */
    11  */
    12 
    12 
    13 #include <stdint.h>
    13 #include <stdint.h>
    14 #include <syslog.h>
    14 #include <syslog.h>
    15 #include <math.h>
    15 #include <math.h>
       
    16 #include <signal.h>
    16 
    17 
    17 #include "common.h"
    18 #include "common.h"
    18 #include "config.h"
    19 #include "config.h"
    19 #include "port.h"
    20 #include "port.h"
    20 #include "picctl.h"
    21 #include "picctl.h"
    58 const unsigned char CMD_SET_MODE_UNMANAGED =	0x71;
    59 const unsigned char CMD_SET_MODE_UNMANAGED =	0x71;
    59 
    60 
    60 const unsigned char CMD_BOOT =			0x80;
    61 const unsigned char CMD_BOOT =			0x80;
    61 
    62 
    62 
    63 
    63 // globals...
       
    64 uint8_t cDriverPICCtl::buf[255];
       
    65 uint8_t cDriverPICCtl::buf_pos;
       
    66 uint8_t cDriverPICCtl::buf_cmd_start;
       
    67 bool cDriverPICCtl::buf_flag_escape;
       
    68 
       
    69 // singleton
       
    70 cDriverPICCtl* cDriverPICCtl::instance = NULL;
       
    71 
       
    72 cDriverPICCtl* cDriverPICCtl::getInstance(cDriverConfig * config)
       
    73 {
       
    74     if (!instance)
       
    75         instance = new cDriverPICCtl(config);
       
    76     return instance;
       
    77 }
       
    78 // end singleton-pattern
       
    79 
       
    80 
       
    81 
       
    82 cDriverPICCtl::cDriverPICCtl(cDriverConfig * config)
    64 cDriverPICCtl::cDriverPICCtl(cDriverConfig * config)
    83 :   config(config)
    65 :   config(config)
    84 {
    66 {
    85     oldConfig = new cDriverConfig(*config);
    67     oldConfig = new cDriverConfig(*config);
    86 
    68 
    93     ack_flag = false;
    75     ack_flag = false;
    94     
    76     
    95     buf_pos = 0;
    77     buf_pos = 0;
    96     buf_cmd_start = 255;
    78     buf_cmd_start = 255;
    97     buf_flag_escape = false;
    79     buf_flag_escape = false;
       
    80 
       
    81     keys_pos  = 0;
       
    82     keys_pos_ = 0;
    98 }
    83 }
    99 
    84 
   100 cDriverPICCtl::~cDriverPICCtl()
    85 cDriverPICCtl::~cDriverPICCtl()
   101 {
    86 {
   102     instance = NULL;
       
   103 
       
   104     delete port;
    87     delete port;
   105     port = NULL;
    88     port = NULL;
   106     delete oldConfig;
    89     delete oldConfig;
   107     oldConfig = NULL;
    90     oldConfig = NULL;
   108 }
    91 }
   109 
    92 
   110 void cDriverPICCtl::SignalHandler(int signal)
    93 bool cDriverPICCtl::HasKeys()
   111 {
    94 {
   112     //uint8_t i;
    95     return true;
   113 
    96 }
       
    97 
       
    98 int cDriverPICCtl::GetKey()
       
    99 {
       
   100     InputHandler();
       
   101 
       
   102     if (keys_pos != keys_pos_)
       
   103     {
       
   104         keys_pos_ = keys_pos;
       
   105         return(keys[keys_pos-1]);
       
   106     }
       
   107     else
       
   108     {
       
   109         return(-1);
       
   110     }
       
   111 
       
   112 }
       
   113 
       
   114 void cDriverPICCtl::InputHandler()
       
   115 {
   114     // read all available data (always get single bytes...)
   116     // read all available data (always get single bytes...)
   115     while (instance->port->ReadData(&buf[buf_pos]) > 0)
   117     while (port->ReadData(&buf[buf_pos]) > 0)
   116     {
   118     {
   117         // search for SYNC byte
   119         // search for SYNC byte
   118         if ((buf[buf_pos] == CMD_SYNC_RECV) && (!buf_flag_escape))
   120         if ((buf[buf_pos] == CMD_SYNC_RECV) && (!buf_flag_escape))
   119 	{
   121 	{
   120 	    if (buf_cmd_start != 255)
   122 	    if (buf_cmd_start != 255)
   121 	    {
   123 	    {
   122 		/*printf("Damaged Packet: ");
   124 	    	#ifdef DEBUG
       
   125     		uint8_t i;
       
   126 		printf("Damaged Packet: ");
   123 	        for (i=buf_cmd_start; i<buf_pos; i++)
   127 	        for (i=buf_cmd_start; i<buf_pos; i++)
   124 		{
   128 		{
   125 		  printf("0x%02x ",buf[i]);
   129 		  printf("0x%02x ",buf[i]);
   126 		}
   130 		}
   127 		printf("; buf_pos = %d, buf_cmd_start = %d\n",buf_pos,buf_cmd_start);*/
   131 		printf("; buf_pos = %d, buf_cmd_start = %d\n",buf_pos,buf_cmd_start);
       
   132 		#endif
   128 
   133 
   129                 syslog(LOG_INFO, "PICCtl received a malformed packet. Scrapped %d bytes\n",(buf_pos - buf_cmd_start));
   134                 syslog(LOG_INFO, "PICCtl received a malformed packet. Scrapped %d bytes\n",(buf_pos - buf_cmd_start));
   130 		buf_pos = 0;
   135 		buf_pos = 0;
   131 		buf[buf_pos] = CMD_SYNC_RECV;
   136 		buf[buf_pos] = CMD_SYNC_RECV;
   132 	    }
   137 	    }
   133  	    buf_cmd_start = buf_pos;
   138  	    buf_cmd_start = buf_pos;
   134 	}
   139 	}
   135 
   140 
   136 
   141 
   137         if ((buf[buf_pos] == CMD_ESCAPE_BYTE) && (!buf_flag_escape))
   142 	// escaping is not used so far...
       
   143 /*        if ((buf[buf_pos] == CMD_ESCAPE_BYTE) && (!buf_flag_escape))
   138 	{
   144 	{
   139 	    // this byte was for masking only.. ignore it! 
   145 	    // this byte was for masking only.. ignore it! 
   140 	    buf_flag_escape = true;
   146 	    buf_flag_escape = true;
   141 	    continue;
   147 	    continue;
   142 	} 
   148 	} 
   143         buf_flag_escape = false;
   149         buf_flag_escape = false;*/
   144 
   150 
   145 /*        printf("buf: ");
       
   146         for (i=1; i<=buf_pos; i++) { printf("0x%02x ",buf[i]);}
       
   147 	printf("\n");*/
       
   148         
       
   149 	buf_pos++;
   151 	buf_pos++;
   150 
   152 
   151         // we need to check for a valid packet...
   153         // we need to check for a valid packet...
   152 	if ((buf_pos - buf_cmd_start) >= CMD_DATA_START)
   154 	if ((buf_pos - buf_cmd_start) >= CMD_DATA_START)
   153 	{
   155 	{
   154             // if we recieved a valid packet: start decoding
   156             // if we recieved a valid packet: start decoding
   155 	    if (buf[buf_cmd_start + CMD_HDR_LENGTH+1] == (buf_pos - buf_cmd_start - CMD_DATA_START))
   157 	    if (buf[buf_cmd_start + CMD_HDR_LENGTH+1] == (buf_pos - buf_cmd_start - CMD_DATA_START))
   156 	    {
   158 	    {
   157 	        instance->DecodeCmd(&buf[buf_cmd_start],buf_pos);
   159 	        DecodeCmd(&buf[buf_cmd_start],buf_pos);
   158 		buf_pos = 0;
   160 		buf_pos = 0;
   159 		buf_cmd_start = 255;
   161 		buf_cmd_start = 255;
   160 	    }
   162 	    }
   161 	}
   163 	}
   162 
   164 
   163     }
   165     }
   164 }
   166 }
   165 
   167 
   166 
       
   167 void cDriverPICCtl::DecodeCmd(uint8_t * cmd, uint8_t len)
   168 void cDriverPICCtl::DecodeCmd(uint8_t * cmd, uint8_t len)
   168 {
   169 {
   169 /*    uint8_t i;
   170     #ifdef DEBUG
       
   171     uint8_t i;
   170     printf("cmd(%2d): ",len);
   172     printf("cmd(%2d): ",len);
   171     for (i=0; i<len; i++) { printf("0x%02x ",cmd[i]);}
   173     for (i=0; i<len; i++) { printf("0x%02x ",cmd[i]);}
   172     printf("\n"); */
   174     printf("\n");
       
   175     #endif
   173 
   176 
   174     switch (cmd[CMD_HDR_COMMAND])
   177     switch (cmd[CMD_HDR_COMMAND])
   175     {
   178     {
   176         case CMD_SYS_ACK:	ack_flag = true;
   179         case CMD_SYS_ACK:	ack_flag = true;
   177 	 			break;
   180 	 			break;
   178 
   181 
   179 	case CMD_SYS_IR:	syslog(LOG_INFO," %s: incoming Key-Event 0x%02x (Source 0x%02x)\n",config->name.c_str(), cmd[CMD_DATA_START+1], cmd[CMD_DATA_START]);
   182 	case CMD_SYS_IR:	keys[keys_pos] =   256 * cmd[CMD_DATA_START] + cmd[CMD_DATA_START+1];
   180 	                         //printf(" %s: incoming Key-Event 0x%02x (Source 0x%02x)\n",config->name.c_str(), cmd[CMD_DATA_START+1], cmd[CMD_DATA_START]);
   183 				keys_pos++;
       
   184 				#ifdef DEBUG
       
   185 				printf(" %s: recieved IR Command 0x%x \n",config->name.c_str(), cmd[CMD_DATA_START+1]);
       
   186 				#endif
   181 	  			break;
   187 	  			break;
   182 
   188 
   183 	default:		syslog(LOG_INFO," %s: recieved Message 0x%x \n",config->name.c_str(), cmd[CMD_HDR_COMMAND]);
   189 	default:		syslog(LOG_INFO," %s: recieved Message 0x%x \n",config->name.c_str(), cmd[CMD_HDR_COMMAND]);
   184     }
   190     }
   185 
   191 
   224 
   230 
   225     if (config->device == "")
   231     if (config->device == "")
   226     {
   232     {
   227         return -1;
   233         return -1;
   228     }
   234     }
   229     if (port->Open(config->device.c_str(), SignalHandler) != 0)
   235     if (port->Open(config->device.c_str()) != 0)
   230         return -1;
   236         return -1;
   231 
   237 
   232     *oldConfig = *config;
   238     *oldConfig = *config;
   233 
   239 
   234     // clear display
   240     // clear display
   235     Clear();
   241     Clear();
   236     CmdDispClearScreen();
   242     CmdDispClearScreen();
   237     CmdDispSetManaged();
   243     CmdDispSetManaged();
   238 //    CmdDispSetBrightness(50);
       
   239 
   244 
   240     syslog(LOG_INFO, "%s: PICCtl initialized.\n", config->name.c_str());
   245     syslog(LOG_INFO, "%s: PICCtl initialized.\n", config->name.c_str());
   241     return 0;
   246     return 0;
   242 }
   247 }
   243 
   248 
   244 int cDriverPICCtl::DeInit()
   249 int cDriverPICCtl::DeInit()
   245 {
   250 {
   246     int y;
   251     int y;
       
   252     
       
   253     CmdDispSetUnManaged();
       
   254     CmdDispClearScreen();
       
   255 
   247     // free lcd array (wanted state)
   256     // free lcd array (wanted state)
   248     if (newLCD)
   257     if (newLCD)
   249     {
   258     {
   250         for (y = 0; y < height; y++)
   259         for (y = 0; y < height; y++)
   251         {
   260         {
   261             delete[] oldLCD[y];
   270             delete[] oldLCD[y];
   262         }
   271         }
   263         delete[] oldLCD;
   272         delete[] oldLCD;
   264     }
   273     }
   265 
   274 
   266     CmdDispSetUnManaged();
       
   267     
   275     
   268     if (port->Close() != 0)
   276     if (port->Close() != 0)
   269         return -1;
   277         return -1;
   270 
   278 
   271     return 0;
   279     return 0;
   360 void cDriverPICCtl::SetBrightness(unsigned int percent)
   368 void cDriverPICCtl::SetBrightness(unsigned int percent)
   361 {
   369 {
   362     CmdDispSetBrightness(percent);
   370     CmdDispSetBrightness(percent);
   363 }
   371 }
   364 
   372 
   365 void cDriverPICCtl::SetClock(unsigned int percent)
       
   366 {
       
   367     CmdDispSetBrightness(percent);
       
   368 }
       
   369 
       
   370 int cDriverPICCtl::WaitForAck(void)
   373 int cDriverPICCtl::WaitForAck(void)
   371 {
   374 {
   372     int timeout;
   375     int timeout;
   373 
   376 
   374     for (timeout=0; timeout<1000; timeout++)
   377     for (timeout=0; timeout<1000; timeout++)
   375     {
   378     {
       
   379         InputHandler();
   376     	usleep(100);
   380     	usleep(100);
   377 	if (ack_flag) return 0;
   381 	if (ack_flag) return 0;
   378     }
   382     }
   379     syslog(LOG_INFO, "%s: PICCtl missed a ACK packet!\n", config->name.c_str());
   383     syslog(LOG_INFO, "%s: PICCtl missed a ACK packet!\n", config->name.c_str());
       
   384 
       
   385 
       
   386 #ifdef DEBUG
       
   387     int i;
       
   388     printf("%s: PICCtl missed a ACK packet!\n", config->name.c_str());
       
   389     for (i=0; i<buf_pos; i++)
       
   390     {
       
   391       printf("0x%02x ",buf[i]);
       
   392     }
       
   393     printf("; buf_pos = %d, buf_cmd_start = %d\n",buf_pos,buf_cmd_start);
       
   394 #endif
       
   395 
   380     return 1;
   396     return 1;
   381 }
   397 }
   382 
   398 
   383 void cDriverPICCtl::CmdDispClearScreen(void)
   399 void cDriverPICCtl::CmdDispClearScreen(void)
   384 {
   400 {