graphlcd-base/glcddrivers/picctl.c
changeset 30 7fd00015f62f
parent 29 2723893dc865
     1.1 --- a/graphlcd-base/glcddrivers/picctl.c	Mon Mar 30 18:40:54 2009 +0200
     1.2 +++ b/graphlcd-base/glcddrivers/picctl.c	Thu Apr 23 19:10:12 2009 +0200
     1.3 @@ -13,6 +13,7 @@
     1.4  #include <stdint.h>
     1.5  #include <syslog.h>
     1.6  #include <math.h>
     1.7 +#include <signal.h>
     1.8  
     1.9  #include "common.h"
    1.10  #include "config.h"
    1.11 @@ -60,25 +61,6 @@
    1.12  const unsigned char CMD_BOOT =			0x80;
    1.13  
    1.14  
    1.15 -// globals...
    1.16 -uint8_t cDriverPICCtl::buf[255];
    1.17 -uint8_t cDriverPICCtl::buf_pos;
    1.18 -uint8_t cDriverPICCtl::buf_cmd_start;
    1.19 -bool cDriverPICCtl::buf_flag_escape;
    1.20 -
    1.21 -// singleton
    1.22 -cDriverPICCtl* cDriverPICCtl::instance = NULL;
    1.23 -
    1.24 -cDriverPICCtl* cDriverPICCtl::getInstance(cDriverConfig * config)
    1.25 -{
    1.26 -    if (!instance)
    1.27 -        instance = new cDriverPICCtl(config);
    1.28 -    return instance;
    1.29 -}
    1.30 -// end singleton-pattern
    1.31 -
    1.32 -
    1.33 -
    1.34  cDriverPICCtl::cDriverPICCtl(cDriverConfig * config)
    1.35  :   config(config)
    1.36  {
    1.37 @@ -95,36 +77,59 @@
    1.38      buf_pos = 0;
    1.39      buf_cmd_start = 255;
    1.40      buf_flag_escape = false;
    1.41 +
    1.42 +    keys_pos  = 0;
    1.43 +    keys_pos_ = 0;
    1.44  }
    1.45  
    1.46  cDriverPICCtl::~cDriverPICCtl()
    1.47  {
    1.48 -    instance = NULL;
    1.49 -
    1.50      delete port;
    1.51      port = NULL;
    1.52      delete oldConfig;
    1.53      oldConfig = NULL;
    1.54  }
    1.55  
    1.56 -void cDriverPICCtl::SignalHandler(int signal)
    1.57 -{
    1.58 -    //uint8_t i;
    1.59 -
    1.60 +bool cDriverPICCtl::HasKeys()
    1.61 +{
    1.62 +    return true;
    1.63 +}
    1.64 +
    1.65 +int cDriverPICCtl::GetKey()
    1.66 +{
    1.67 +    InputHandler();
    1.68 +
    1.69 +    if (keys_pos != keys_pos_)
    1.70 +    {
    1.71 +        keys_pos_ = keys_pos;
    1.72 +        return(keys[keys_pos-1]);
    1.73 +    }
    1.74 +    else
    1.75 +    {
    1.76 +        return(-1);
    1.77 +    }
    1.78 +
    1.79 +}
    1.80 +
    1.81 +void cDriverPICCtl::InputHandler()
    1.82 +{
    1.83      // read all available data (always get single bytes...)
    1.84 -    while (instance->port->ReadData(&buf[buf_pos]) > 0)
    1.85 +    while (port->ReadData(&buf[buf_pos]) > 0)
    1.86      {
    1.87          // search for SYNC byte
    1.88          if ((buf[buf_pos] == CMD_SYNC_RECV) && (!buf_flag_escape))
    1.89  	{
    1.90  	    if (buf_cmd_start != 255)
    1.91  	    {
    1.92 -		/*printf("Damaged Packet: ");
    1.93 +	    	#ifdef DEBUG
    1.94 +    		uint8_t i;
    1.95 +		printf("Damaged Packet: ");
    1.96  	        for (i=buf_cmd_start; i<buf_pos; i++)
    1.97  		{
    1.98  		  printf("0x%02x ",buf[i]);
    1.99  		}
   1.100 -		printf("; buf_pos = %d, buf_cmd_start = %d\n",buf_pos,buf_cmd_start);*/
   1.101 +		printf("; buf_pos = %d, buf_cmd_start = %d\n",buf_pos,buf_cmd_start);
   1.102 +		#endif
   1.103  
   1.104                  syslog(LOG_INFO, "PICCtl received a malformed packet. Scrapped %d bytes\n",(buf_pos - buf_cmd_start));
   1.105  		buf_pos = 0;
   1.106 @@ -134,18 +139,15 @@
   1.107  	}
   1.108  
   1.109  
   1.110 -        if ((buf[buf_pos] == CMD_ESCAPE_BYTE) && (!buf_flag_escape))
   1.111 +	// escaping is not used so far...
   1.112 +/*        if ((buf[buf_pos] == CMD_ESCAPE_BYTE) && (!buf_flag_escape))
   1.113  	{
   1.114  	    // this byte was for masking only.. ignore it! 
   1.115  	    buf_flag_escape = true;
   1.116  	    continue;
   1.117  	} 
   1.118 -        buf_flag_escape = false;
   1.119 -
   1.120 -/*        printf("buf: ");
   1.121 -        for (i=1; i<=buf_pos; i++) { printf("0x%02x ",buf[i]);}
   1.122 -	printf("\n");*/
   1.123 -        
   1.124 +        buf_flag_escape = false;*/
   1.125 +
   1.126  	buf_pos++;
   1.127  
   1.128          // we need to check for a valid packet...
   1.129 @@ -154,7 +156,7 @@
   1.130              // if we recieved a valid packet: start decoding
   1.131  	    if (buf[buf_cmd_start + CMD_HDR_LENGTH+1] == (buf_pos - buf_cmd_start - CMD_DATA_START))
   1.132  	    {
   1.133 -	        instance->DecodeCmd(&buf[buf_cmd_start],buf_pos);
   1.134 +	        DecodeCmd(&buf[buf_cmd_start],buf_pos);
   1.135  		buf_pos = 0;
   1.136  		buf_cmd_start = 255;
   1.137  	    }
   1.138 @@ -163,21 +165,25 @@
   1.139      }
   1.140  }
   1.141  
   1.142 -
   1.143  void cDriverPICCtl::DecodeCmd(uint8_t * cmd, uint8_t len)
   1.144  {
   1.145 -/*    uint8_t i;
   1.146 +    #ifdef DEBUG
   1.147 +    uint8_t i;
   1.148      printf("cmd(%2d): ",len);
   1.149      for (i=0; i<len; i++) { printf("0x%02x ",cmd[i]);}
   1.150 -    printf("\n"); */
   1.151 +    printf("\n");
   1.152 +    #endif
   1.153  
   1.154      switch (cmd[CMD_HDR_COMMAND])
   1.155      {
   1.156          case CMD_SYS_ACK:	ack_flag = true;
   1.157  	 			break;
   1.158  
   1.159 -	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]);
   1.160 -	                         //printf(" %s: incoming Key-Event 0x%02x (Source 0x%02x)\n",config->name.c_str(), cmd[CMD_DATA_START+1], cmd[CMD_DATA_START]);
   1.161 +	case CMD_SYS_IR:	keys[keys_pos] =   256 * cmd[CMD_DATA_START] + cmd[CMD_DATA_START+1];
   1.162 +				keys_pos++;
   1.163 +				#ifdef DEBUG
   1.164 +				printf(" %s: recieved IR Command 0x%x \n",config->name.c_str(), cmd[CMD_DATA_START+1]);
   1.165 +				#endif
   1.166  	  			break;
   1.167  
   1.168  	default:		syslog(LOG_INFO," %s: recieved Message 0x%x \n",config->name.c_str(), cmd[CMD_HDR_COMMAND]);
   1.169 @@ -226,7 +232,7 @@
   1.170      {
   1.171          return -1;
   1.172      }
   1.173 -    if (port->Open(config->device.c_str(), SignalHandler) != 0)
   1.174 +    if (port->Open(config->device.c_str()) != 0)
   1.175          return -1;
   1.176  
   1.177      *oldConfig = *config;
   1.178 @@ -235,7 +241,6 @@
   1.179      Clear();
   1.180      CmdDispClearScreen();
   1.181      CmdDispSetManaged();
   1.182 -//    CmdDispSetBrightness(50);
   1.183  
   1.184      syslog(LOG_INFO, "%s: PICCtl initialized.\n", config->name.c_str());
   1.185      return 0;
   1.186 @@ -244,6 +249,10 @@
   1.187  int cDriverPICCtl::DeInit()
   1.188  {
   1.189      int y;
   1.190 +    
   1.191 +    CmdDispSetUnManaged();
   1.192 +    CmdDispClearScreen();
   1.193 +
   1.194      // free lcd array (wanted state)
   1.195      if (newLCD)
   1.196      {
   1.197 @@ -263,7 +272,6 @@
   1.198          delete[] oldLCD;
   1.199      }
   1.200  
   1.201 -    CmdDispSetUnManaged();
   1.202      
   1.203      if (port->Close() != 0)
   1.204          return -1;
   1.205 @@ -362,21 +370,29 @@
   1.206      CmdDispSetBrightness(percent);
   1.207  }
   1.208  
   1.209 -void cDriverPICCtl::SetClock(unsigned int percent)
   1.210 -{
   1.211 -    CmdDispSetBrightness(percent);
   1.212 -}
   1.213 -
   1.214  int cDriverPICCtl::WaitForAck(void)
   1.215  {
   1.216      int timeout;
   1.217  
   1.218      for (timeout=0; timeout<1000; timeout++)
   1.219      {
   1.220 +        InputHandler();
   1.221      	usleep(100);
   1.222  	if (ack_flag) return 0;
   1.223      }
   1.224      syslog(LOG_INFO, "%s: PICCtl missed a ACK packet!\n", config->name.c_str());
   1.225 +
   1.226 +
   1.227 +#ifdef DEBUG
   1.228 +    int i;
   1.229 +    printf("%s: PICCtl missed a ACK packet!\n", config->name.c_str());
   1.230 +    for (i=0; i<buf_pos; i++)
   1.231 +    {
   1.232 +      printf("0x%02x ",buf[i]);
   1.233 +    }
   1.234 +    printf("; buf_pos = %d, buf_cmd_start = %d\n",buf_pos,buf_cmd_start);
   1.235 +#endif
   1.236 +
   1.237      return 1;
   1.238  }
   1.239