32 const unsigned char CMD_HDR_SYNC = 0; |
32 const unsigned char CMD_HDR_SYNC = 0; |
33 const unsigned char CMD_HDR_COMMAND = 1; |
33 const unsigned char CMD_HDR_COMMAND = 1; |
34 const unsigned char CMD_HDR_LENGTH = 2; |
34 const unsigned char CMD_HDR_LENGTH = 2; |
35 const unsigned char CMD_DATA_START = 4; |
35 const unsigned char CMD_DATA_START = 4; |
36 |
36 |
37 const unsigned char CMD_SYNC_SEND = 0xAA; |
37 const unsigned char CMD_SYNC_SEND = 0xAA; |
38 const unsigned char CMD_SYNC_RECV = 0x55; |
38 const unsigned char CMD_SYNC_RECV = 0x55; |
|
39 const unsigned char CMD_ESCAPE_BYTE = 0x42; |
39 |
40 |
40 |
41 |
41 const unsigned char CMD_SYS_SYNC = 0x00; |
42 const unsigned char CMD_SYS_SYNC = 0x00; |
42 const unsigned char CMD_SYS_ACK = 0x01; |
43 const unsigned char CMD_SYS_ACK = 0x01; |
43 const unsigned char CMD_SYS_NACK = 0x02; |
44 const unsigned char CMD_SYS_NACK = 0x02; |
55 |
56 |
56 const unsigned char CMD_SET_MODE_MANAGED = 0x70; |
57 const unsigned char CMD_SET_MODE_MANAGED = 0x70; |
57 const unsigned char CMD_SET_MODE_UNMANAGED = 0x71; |
58 const unsigned char CMD_SET_MODE_UNMANAGED = 0x71; |
58 |
59 |
59 const unsigned char CMD_BOOT = 0x80; |
60 const unsigned char CMD_BOOT = 0x80; |
|
61 |
|
62 |
60 // singleton |
63 // singleton |
61 cDriverPICCtl* cDriverPICCtl::instance = 0; |
64 cDriverPICCtl* cDriverPICCtl::instance = 0; |
62 |
65 |
63 cDriverPICCtl* cDriverPICCtl::getInstance(cDriverConfig * config) |
66 cDriverPICCtl* cDriverPICCtl::getInstance(cDriverConfig * config) |
64 { |
67 { |
89 delete oldConfig; |
92 delete oldConfig; |
90 } |
93 } |
91 |
94 |
92 void cDriverPICCtl::SignalHandler(int signal) |
95 void cDriverPICCtl::SignalHandler(int signal) |
93 { |
96 { |
94 unsigned char buf[255]; |
97 // read all available data |
95 int numbytes,i; |
98 while (instance->port->ReadData(&buf[buf_pos])) |
96 |
99 { |
97 numbytes = instance->port->ReadData(buf,255); |
100 // serarch for SYNC byte |
98 |
101 if ((buf[buf_pos] == CMD_SYNC_RECV) && (!buf_flag_escape)) |
99 |
|
100 if ((buf[CMD_HDR_SYNC] == CMD_SYNC_RECV) && (buf[CMD_HDR_LENGTH+1] == (numbytes - CMD_DATA_START))) |
|
101 { |
|
102 switch (buf[CMD_HDR_COMMAND]) |
|
103 { |
102 { |
104 case CMD_SYS_ACK: instance->ack_flag = true; |
103 if (buf_cmd_start != 255) |
|
104 { |
|
105 // bytes = buf_cmd_start - buf_pos |
|
106 syslog(LOG_INFO, "PICCtl received a malformed packet. Scrapped xxx bytes\n"); |
|
107 buf_cmd_start = 0; |
|
108 buf_pos = 0; |
|
109 buf[buf_pos] = CMD_SYNC_RECV; |
|
110 } |
|
111 buf_cmd_start = buf_pos; |
|
112 } |
|
113 |
|
114 |
|
115 if ((buf[buf_pos] == CMD_ESCAPE_BYTE) && (!buf_flag_escape)) |
|
116 { |
|
117 buf_flag_escape = true; |
|
118 buf_pos--; // should do!!! better would be something like next(); |
|
119 // set flag... |
|
120 // next; |
|
121 } |
|
122 else |
|
123 { |
|
124 buf_flag_escape = false; |
|
125 } |
|
126 |
|
127 // we need to check for a valid packet... |
|
128 if (buf_pos >=CMD_DATA_START) |
|
129 { |
|
130 // if we recieved a valid packet: start decoding |
|
131 if (buf[CMD_HDR_LENGTH+1] == (buf_pos - CMD_DATA_START)) |
|
132 { |
|
133 instance->DecodeCmd(&buf[buf_cmd_start],(buf_pos-1)); |
|
134 buf_pos = 0; |
|
135 buf_cmd_start = 0; |
|
136 } |
|
137 } |
|
138 |
|
139 buf_pos++; |
|
140 |
|
141 } |
|
142 } |
|
143 |
|
144 |
|
145 void cDriverPICCtl::DecodeCmd(uint8_t * cmd, uint8_t len) |
|
146 { |
|
147 switch (cmd[CMD_HDR_COMMAND]) |
|
148 { |
|
149 case CMD_SYS_ACK: instance->ack_flag = true; |
|
150 break; |
|
151 |
|
152 case CMD_SYS_IR: printf(" Incoming Key-Event 0x%02x (Source 0x%02x)\n",cmd[CMD_DATA_START+1],cmd[CMD_DATA_START]); |
105 break; |
153 break; |
106 |
154 |
107 case CMD_SYS_IR: printf(" Incoming Key-Event 0x%02x (Source 0x%02x)\n",buf[CMD_DATA_START+1],buf[CMD_DATA_START]); |
155 default: printf(" recieved Message 0x%x \n",cmd[CMD_HDR_COMMAND]); |
108 break; |
|
109 |
|
110 default: printf(" recieved Message 0x%x \n",buf[CMD_HDR_COMMAND]); |
|
111 } |
|
112 } |
|
113 else |
|
114 { |
|
115 syslog(LOG_INFO, "PICCtl received a malformed packet.\n"); |
|
116 for (i=0; i<numbytes; i++) |
|
117 { |
|
118 printf(" 0x%x",buf[i]); |
|
119 } |
|
120 printf("; recieved %d bytes2\n",numbytes); |
|
121 } |
156 } |
122 |
157 |
123 } |
158 } |
124 |
159 |
125 int cDriverPICCtl::Init() |
160 int cDriverPICCtl::Init() |
164 } |
199 } |
165 if (port->Open(config->device.c_str(), SignalHandler) != 0) |
200 if (port->Open(config->device.c_str(), SignalHandler) != 0) |
166 return -1; |
201 return -1; |
167 |
202 |
168 *oldConfig = *config; |
203 *oldConfig = *config; |
|
204 |
|
205 buf_pos = 1; |
|
206 buf_cmd_start = 255; |
169 |
207 |
170 // clear display |
208 // clear display |
171 Clear(); |
209 Clear(); |
172 CmdDispClearScreen(); |
210 CmdDispClearScreen(); |
173 CmdDispSetManaged(); |
211 CmdDispSetManaged(); |
174 CmdDispSetBrightness(100); |
|
175 |
212 |
176 syslog(LOG_INFO, "%s: PICCtl initialized.\n", config->name.c_str()); |
213 syslog(LOG_INFO, "%s: PICCtl initialized.\n", config->name.c_str()); |
177 return 0; |
214 return 0; |
178 } |
215 } |
179 |
216 |