reworked input-code
authorcpresser@rika
Wed, 06 Feb 2008 18:41:02 +0000
changeset 85fa05abec614
parent 7 aac14789cd1d
child 9 63442d0b7eca
reworked input-code
graphlcd-base/glcddrivers/picctl.c
graphlcd-base/glcddrivers/picctl.h
graphlcd-base/glcddrivers/port.h
     1.1 --- a/graphlcd-base/glcddrivers/picctl.c	Wed Feb 06 17:55:56 2008 +0000
     1.2 +++ b/graphlcd-base/glcddrivers/picctl.c	Wed Feb 06 18:41:02 2008 +0000
     1.3 @@ -34,8 +34,9 @@
     1.4  const unsigned char CMD_HDR_LENGTH  = 2;
     1.5  const unsigned char CMD_DATA_START  = 4;
     1.6  
     1.7 -const unsigned char CMD_SYNC_SEND = 0xAA;
     1.8 -const unsigned char CMD_SYNC_RECV = 0x55;
     1.9 +const unsigned char CMD_SYNC_SEND   = 0xAA;
    1.10 +const unsigned char CMD_SYNC_RECV   = 0x55;
    1.11 +const unsigned char CMD_ESCAPE_BYTE = 0x42;
    1.12  
    1.13  
    1.14  const unsigned char CMD_SYS_SYNC =		0x00;
    1.15 @@ -57,6 +58,8 @@
    1.16  const unsigned char CMD_SET_MODE_UNMANAGED =	0x71;
    1.17  
    1.18  const unsigned char CMD_BOOT =			0x80;
    1.19 +
    1.20 +
    1.21  // singleton
    1.22  cDriverPICCtl* cDriverPICCtl::instance = 0;
    1.23  
    1.24 @@ -91,33 +94,65 @@
    1.25  
    1.26  void cDriverPICCtl::SignalHandler(int signal)
    1.27  {
    1.28 -    unsigned char buf[255];
    1.29 -    int numbytes,i;
    1.30 -
    1.31 -    numbytes = instance->port->ReadData(buf,255);
    1.32 -
    1.33 -
    1.34 -    if ((buf[CMD_HDR_SYNC] == CMD_SYNC_RECV) && (buf[CMD_HDR_LENGTH+1] == (numbytes - CMD_DATA_START)))
    1.35 -    {
    1.36 -    	switch (buf[CMD_HDR_COMMAND])
    1.37 +    // read all available data
    1.38 +    while (instance->port->ReadData(&buf[buf_pos]))
    1.39 +    {
    1.40 +        // serarch for SYNC byte
    1.41 +        if ((buf[buf_pos] == CMD_SYNC_RECV) && (!buf_flag_escape))
    1.42  	{
    1.43 -	  case CMD_SYS_ACK:	instance->ack_flag = true;
    1.44 +	    if (buf_cmd_start != 255)
    1.45 +	    {
    1.46 +	        // bytes = buf_cmd_start - buf_pos
    1.47 +                syslog(LOG_INFO, "PICCtl received a malformed packet. Scrapped xxx bytes\n");
    1.48 +		buf_cmd_start = 0;
    1.49 +		buf_pos = 0;
    1.50 +		buf[buf_pos] = CMD_SYNC_RECV;
    1.51 +	    }
    1.52 + 	    buf_cmd_start = buf_pos;
    1.53 +	}
    1.54 +
    1.55 +
    1.56 +        if ((buf[buf_pos] == CMD_ESCAPE_BYTE) && (!buf_flag_escape))
    1.57 +	{
    1.58 +	    buf_flag_escape = true;
    1.59 +	    buf_pos--; // should do!!! better would be something like next();
    1.60 +	    // set flag...
    1.61 +	    // next;
    1.62 +	} 
    1.63 +	else
    1.64 +	{
    1.65 +	    buf_flag_escape = false;
    1.66 +	}
    1.67 +
    1.68 +        // we need to check for a valid packet...
    1.69 +	if (buf_pos >=CMD_DATA_START)
    1.70 +	{
    1.71 +            // if we recieved a valid packet: start decoding
    1.72 +	    if (buf[CMD_HDR_LENGTH+1] == (buf_pos - CMD_DATA_START))
    1.73 +	    {
    1.74 +	        instance->DecodeCmd(&buf[buf_cmd_start],(buf_pos-1));
    1.75 +		buf_pos = 0;
    1.76 +		buf_cmd_start = 0;
    1.77 +	    }
    1.78 +	}
    1.79 +
    1.80 +        buf_pos++;
    1.81 +
    1.82 +    }
    1.83 +}
    1.84 +
    1.85 +
    1.86 +void cDriverPICCtl::DecodeCmd(uint8_t * cmd, uint8_t len)
    1.87 +{
    1.88 +    switch (cmd[CMD_HDR_COMMAND])
    1.89 +    {
    1.90 +        case CMD_SYS_ACK:	instance->ack_flag = true;
    1.91 +	 			break;
    1.92 +
    1.93 +	case CMD_SYS_IR:	printf(" Incoming Key-Event 0x%02x (Source 0x%02x)\n",cmd[CMD_DATA_START+1],cmd[CMD_DATA_START]);
    1.94  	  			break;
    1.95  
    1.96 -	  case CMD_SYS_IR:	printf(" Incoming Key-Event 0x%02x (Source 0x%02x)\n",buf[CMD_DATA_START+1],buf[CMD_DATA_START]);
    1.97 -	  			break;
    1.98 -
    1.99 -	  default:		printf(" recieved Message 0x%x \n",buf[CMD_HDR_COMMAND]);
   1.100 -	}
   1.101 -    }
   1.102 -    else
   1.103 -    {
   1.104 -        syslog(LOG_INFO, "PICCtl received a malformed packet.\n");
   1.105 -        for (i=0; i<numbytes; i++)
   1.106 -        {
   1.107 -            printf(" 0x%x",buf[i]);
   1.108 -        }
   1.109 -        printf("; recieved %d bytes2\n",numbytes);
   1.110 +	default:		printf(" recieved Message 0x%x \n",cmd[CMD_HDR_COMMAND]);
   1.111      }
   1.112  
   1.113  }
   1.114 @@ -167,11 +202,13 @@
   1.115  
   1.116      *oldConfig = *config;
   1.117  
   1.118 +    buf_pos = 1;
   1.119 +    buf_cmd_start = 255;
   1.120 +
   1.121      // clear display
   1.122      Clear();
   1.123      CmdDispClearScreen();
   1.124      CmdDispSetManaged();
   1.125 -    CmdDispSetBrightness(100);
   1.126  
   1.127      syslog(LOG_INFO, "%s: PICCtl initialized.\n", config->name.c_str());
   1.128      return 0;
     2.1 --- a/graphlcd-base/glcddrivers/picctl.h	Wed Feb 06 17:55:56 2008 +0000
     2.2 +++ b/graphlcd-base/glcddrivers/picctl.h	Wed Feb 06 18:41:02 2008 +0000
     2.3 @@ -39,10 +39,15 @@
     2.4      void CmdDispSetUnManaged(void);
     2.5      void CmdDispSetBrightness(uint8_t percent);
     2.6      void CmdDispSetColData(uint8_t yoffset, uint8_t xoffset, uint8_t length, uint8_t * data);
     2.7 +    void DecodeCmd(uint8_t * cmd, uint8_t len);
     2.8  
     2.9      int CheckSetup();
    2.10  
    2.11      static void SignalHandler(int signal);
    2.12 +    static uint8_t buf[255];
    2.13 +    static uint8_t buf_pos;
    2.14 +    static uint8_t buf_cmd_start;
    2.15 +    static bool buf_flag_escape;
    2.16  
    2.17  // singleton
    2.18      static cDriverPICCtl* instance;		// die EINE instant
     3.1 --- a/graphlcd-base/glcddrivers/port.h	Wed Feb 06 17:55:56 2008 +0000
     3.2 +++ b/graphlcd-base/glcddrivers/port.h	Wed Feb 06 18:41:02 2008 +0000
     3.3 @@ -68,7 +68,7 @@
     3.4      int Open(const char * device, void (*FP)(int));
     3.5      int Close();
     3.6  
     3.7 -    int ReadData(unsigned char * data)
     3.8 +    int ReadData(unsigned char * data);
     3.9      void WriteData(unsigned char data);
    3.10      void WriteData(unsigned char * data, unsigned short length);
    3.11  };