blob: df70b139942e03ff7c5ec4c38ad7142e7888d7a2 [file] [log] [blame]
zongqiang.zhang0c6a0882019-08-07 14:48:21 +08001#include "../sys_hw/keypad.h"
2#include "../sys_hw/drv_adc.h"
3#include "sp_display.h"
4
5#include "sp_util.h"
6
7uint16 crc_table[] =
8{
9 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
10 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
11 0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
12 0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
13 0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
14 0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
15 0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
16 0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
17 0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
18 0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
19 0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
20 0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
21 0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
22 0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
23 0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
24 0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
25 0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,
26 0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
27 0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
28 0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
29 0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
30 0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
31 0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
32 0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
33 0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
34 0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
35 0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
36 0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
37 0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
38 0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
39 0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
40 0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0
41};
42
43void sp_protocol_crc(const uint8* buf, uint16 len, uint8 crc[2])
44{
45 //uint16 accnum = 0xc78c;
46 uint8 temp_crc[2];
47 temp_crc[0] = 0xc7;
48 temp_crc[1] = 0x8c;
49 sp_protocol_crc_init(buf,len,temp_crc,crc);
50}
51
52void sp_protocol_crc_init(const uint8* buf, uint16 len,uint8 init[2], uint8 crc[2])
53{
54 uint16 accnum = (uint16)init[0]<<8 |(uint16)init[1];
55 uint16 i;
56 for(i = 0; i < len; ++i)
57 {
58 accnum = (accnum << 8) ^ crc_table[((accnum >> 8)
59 ^ buf[i]) & 0xFF];
60 }
61 crc[0] = (uint8)((accnum >> 8) & 0xFF);
62 crc[1] = (uint8)(accnum & 0xFF);
63}
64
65void sp_key_calibrate(void)
66{
67 calibrate_key(0);
68}
69
70uint8 sp_key_init(void)
71{
72 adc_init();
73 return keypad_init();
74}
75uint8 sp_get_key(void)
76{
77 uint8 key = keypad_get_key();
78 switch(key)
79 {
80 case KEY_NONE:
81 return SP_KEY_NONE;
82 case KEY_DIG0:
83 return SP_KEY_0;
84 case KEY_DIG1:
85 return SP_KEY_1;
86 case KEY_DIG2:
87 return SP_KEY_2;
88 case KEY_DIG3:
89 return SP_KEY_3;
90 case KEY_DIG4:
91 return SP_KEY_4;
92 case KEY_DIG5:
93 return SP_KEY_5;
94 case KEY_DIG6:
95 return SP_KEY_6;
96 case KEY_DIG7:
97 return SP_KEY_7;
98 case KEY_DIG8:
99 return SP_KEY_8;
100 case KEY_DIG9:
101 return SP_KEY_9;
102 case KEY_ENTER:
103 return SP_KEY_ENTER;
104 case KEY_CANCEL:
105 return SP_KEY_CLEAR;
106 default:
107 return SP_KEY_NONE;
108 }
109}
110void sp_reset(void)
111{
112 while(1);
113}
114void sp_bcd_to_str(const uint8* bcd, uint8 bcd_len, char* str)
115{
116 uint8 i;
117 uint16 j;
118 for(i = 0; i < bcd_len; ++i)
119 {
120 j = i << 1;
121 str[j] = ((bcd[i] >> 4) & 0x0F) + 0x30;
122 str[j + 1] = (bcd[i] & 0x0F) + 0x30;
123 }
124 str[i << 1] = 0;
125}
126void sp_str_to_bcd(const char* str, uint8 str_len, uint8* bcd)
127{
128 uint8 i,j;
129 for(i = 0; i < str_len; i += 2)
130 {
131 j = i/2;
132 bcd[j] = (((str[i]-0x30) & 0x0F) <<4) | ((str[i+1]-0x30) & 0x0F);
133 }
134}
135static uint8 charTohex(char ch)
136{
137 if(ch >= '0' && ch <= '9')
138 {
139 return (ch -'0');
140 }
141 if(ch >= 'a' && ch <= 'f')
142 {
143 return (ch -'a' +10);
144 }
145 if(ch >= 'A' && ch <= 'F')
146 {
147 return (ch -'A' +10);
148 }
149 return 0;
150}
151void sp_str_to_hex(const char* str,const uint16 len,uint8* hex)
152{
153 uint8 t,temp;
154 uint16 i;
155 for(i = 0; i < len; i += 2)
156 {
157 temp = charTohex(str[i]);
158 t = temp << 4;
159 temp = charTohex(str[i+1]);
160 t += temp;
161 hex[i/2] = t;
162 }
163}
164
165static char hexToCharTable[]= {"0123456789ABCDEF"};
166void sp_hex_to_str(const uint8* hex,const uint8 len,char* str)
167{
168 uint8 i,j;
169 for(i = 0; i < len; i++)
170 {
171 j = i << 1;
172 str[j] = hexToCharTable[(hex[i] >> 4) &0x0F];
173 str[j +1] = hexToCharTable[hex[i] &0x0F];
174 }
175 str[i << 1] = 0;
176}
177
178void sp_get_bcdtime(uint8 ctime[6])
179{
180 _SystemTime time;
181 memset(&time,0,sizeof time);
182 rtc_get_time(&time); //bcd
183 ctime[0] = time.year;
184 ctime[1] = time.month;
185 ctime[2] = time.day;
186 ctime[3] = time.hour;
187 ctime[4] = time.minute;
188 ctime[5] = time.second;
189}
190void sp_set_bcdtime(uint8 ctime[6])
191{
192 _SystemTime time;
193 memset(&time,0,sizeof time);
194 time.year = ctime[0];
195 time.month = ctime[1];
196 time.day = ctime[2];
197 time.hour = ctime[3];
198 time.minute = ctime[4];
199 time.second = ctime[5];
200
201 rtc_set_time(&time);
202}
203uint8 sp_check_time_valid(uint8 ctime[6])
204{
205 if(BCD2Dec(ctime[0]) < 18)
206 return 1;
207 if(BCD2Dec(ctime[1]) > 12)
208 return 1;
209 if(BCD2Dec(ctime[2]) > 31)
210 return 1;
211 if(BCD2Dec(ctime[3]) > 23)
212 return 1;
213 if(BCD2Dec(ctime[4]) > 59)
214 return 1;
215 if(BCD2Dec(ctime[5]) > 59)
216 return 1;
217
218 return 0;
219}
220uint8 sp_crc_sum(uint8 buf[],uint8 len)
221{
222 uint8 i;
223 uint8 sum = 0;
224 //¼ÆËãУÑéºÍ
225 for(i = 0; i < len; i++)
226 {
227 sum = sum + buf[i];
228 }
229 return sum;
230}
231
232int8 isFF(uint8 buf[],uint16 len)
233{
234 uint16 i;
235 for(i = 0; i < len; i++)
236 {
237 if(0xFF != buf[i])
238 {
239 return -1;
240 }
241 }
242 return 0;
243}
244
245//ms
246uint32 sp_get_ticker(void)
247{
248 return timer_get_ticker();
249}
250
251void Delay_ms(uint32 ms)
252{
253 uint32 t = sp_get_ticker();
254 while(sp_get_ticker() - t < ms);
255}
256void sp_valve_on(void)
257{
258 valve_sta_set(valve_state_on);
259 valve_ctrl();
260}
261void sp_valve_off(void)
262{
263 valve_sta_set(valve_state_off);
264 valve_ctrl();
265}
266uint8 sp_valve_state(void)
267{
268 return valve_sta_get();
269}
270
271int16 get_2byte_int(uint8 value_str[2])
272{
273 int32 r = 0, t = 0;
274 t = value_str[0];
275 r = t << 8;
276 r |= value_str[1];
277 return (int16)r;
278}
279int32 get_3byte_int(uint8 value_str[3])
280{
281 int32 r = 0, t = 0;
282 t = value_str[0];
283 r = t << 16;
284 t = value_str[1];
285 r |= t << 8;
286 r |= value_str[2];
287 return r;
288}
289int32 get_4byte_int(uint8 value_str[4])
290{
291 int32 r = 0, t = 0;
292 t = value_str[0];
293 r = t << 24;
294 t = value_str[1];
295 r |= t << 16;
296 t = value_str[2];
297 r |= t << 8;
298 r |= value_str[3];
299 return r;
300}
301void set_2byte_int(uint8 value_str[2], int num)
302{
303 value_str[0] = (num >> 8) & 0xFF;
304 value_str[1] = num & 0xFF;
305}
306void set_3byte_int(uint8 value_str[3], int num)
307{
308 value_str[0] = (num >> 16) & 0xFF;
309 value_str[1] = (num >> 8) & 0xFF;
310 value_str[2] = num & 0xFF;
311}
312void set_4byte_int(uint8 value_str[4], int num)
313{
314 value_str[0] = (num >> 24) & 0xFF;
315 value_str[1] = (num >> 16) & 0xFF;
316 value_str[2] = (num >> 8) & 0xFF;
317 value_str[3] = num & 0xFF;
318}
319
320//»ñµÃµ¥×Ö½ÚÖÐijһλµÄÖµ
321int32 Get1Bit(uint8 buf, int n)
322{
323 return (buf >> n) & 0x01;
324}
325
326int16 get_2byte_int_le(uint8 value_str[2])
327{
328 int32 r = 0, t = 0;
329 t = value_str[1];
330 r = t << 8;
331 r |= value_str[0];
332 return (int16)r;
333}
334//С×Ö½ÚÐò
335int32 get_3byte_int_le(uint8 value_str[3])
336{
337 int32 r = 0, t = 0;
338 t = value_str[2];
339 r = t << 16;
340 t = value_str[1];
341 r |= t << 8;
342 r |= value_str[0];
343 return r;
344}
345//С×Ö½ÚÐò
346int32 get_4byte_int_le(uint8 value_str[4])
347{
348 int32 r = 0, t = 0;
349 t = value_str[3];
350 r = t << 24;
351 t = value_str[2];
352 r |= t << 16;
353 t = value_str[1];
354 r |= t << 8;
355 r |= value_str[0];
356 return r;
357}
358
359void set_2byte_int_le(uint8 value_str[2], int num)
360{
361 value_str[1] = (num >> 8) & 0xFF;
362 value_str[0] = num & 0xFF;
363}
364
365void set_3byte_int_le(uint8 value_str[3], int num)
366{
367 value_str[2] = (num >> 16) & 0xFF;
368 value_str[1] = (num >> 8) & 0xFF;
369 value_str[0] = num & 0xFF;
370}
371
372/*¼ÆËã´Óbcd starttime µ½ÏÖÔÚµÄÃëÊý*/
373uint32 diff_time(uint8 starttime[6])
374{
375 uint8 ctime[6];
376 uint32 start_second;
377 uint32 end_second;
378
379 sp_get_bcdtime(ctime);
380 start_second = format_time_covert_secs(starttime);
381 end_second = format_time_covert_secs(ctime);
382 if(end_second < start_second)
383 {
384 return 0;
385 }
386 return (end_second - start_second);
387}
388
389void mycpy(void* dest, const void* src, uint32 len)
390{
391 char* tmp_dest = (char*)dest;
392 char* tmp_src = (char*)src;
393 while(len--)
394 {
395 *tmp_dest = *tmp_src++;
396 tmp_dest++;
397 }
398}
399
400void sp_bcd2asc(const uint8 bcdbuf[], uint8 bcdlen, uint8* ascstr)
401{
402 uint8 i;
403 uint8 lch, hch;
404 for(i = 0; i < bcdlen; i++)
405 {
406 hch = (bcdbuf[i] & 0x0F);
407 lch = (bcdbuf[i] & 0xF0);
408 lch = lch >> 4;
409 ascstr[2 * i] = lch + '0';
410 ascstr[2 * i + 1] = hch + '0';
411 }
412 ascstr[2*i] = 0;
413}
414int32 sp_atoi(const char* src)
415{
416 int i = 0;
417 while(*src != 0)
418 {
419 i = i * 10 + *src - '0';
420 src++;
421 }
422 return i;
423}
424/**
425**2000.0.0:0.0.0ʱ¼äΪ»ùÊý
426**/
427static int MINUTE_SECS = 60;
428static int HOUR_SECS = 3600;
429static int DAY_SECS = 24*3600;
430static int YEAR_SECS = 365*24*3600;
431static int FOURYEAR_SECS = (365*3+366)*24*3600;
432static int norMoth[] = {0,31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
433static int leapMoth[] = {0,31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
434/*time: string(yyyyMMddHHmmss) to int */
435int format_time_covert_secs(uint8 ctime[6])
436{
437 int secs;
438 int i;
439 int remain;
440 int year = BCD2Dec(ctime[0]);
441 int month = BCD2Dec(ctime[1]);
442 int day = BCD2Dec(ctime[2]);
443 int hour = BCD2Dec(ctime[3]);
444 int minute = BCD2Dec(ctime[4]);
445 int second = BCD2Dec(ctime[5]);
446
447 secs = year/4 *FOURYEAR_SECS;
448 remain = year%4;
449 secs += remain*YEAR_SECS;
450
451 if(remain == 0)
452 {
453 for(i = 0; i < month; ++i)
454 {
455 secs += leapMoth[i] *DAY_SECS;
456 }
457 }
458 else
459 {
460 for(i = 0; i < month; ++i)
461 {
462 secs += norMoth[i] *DAY_SECS;
463 }
464 }
465 secs += day*DAY_SECS;
466 secs += hour *HOUR_SECS;
467 secs += minute*MINUTE_SECS;
468 secs += second;
469 return secs;
470}
471
472uint8 sp_check_passwd(sp_pos_t* pos,const char* hint,uint8 passwd[6])
473{
474 uint8 temp[6];
475 uint8 offset = 0;
476 uint8 kcode;
477 uint32 ticker = sp_get_ticker();
478
479 show_manage_passwd(pos,hint,temp,offset);
480 while(sp_get_ticker() -ticker < DELAY_TIME60s)
481 {
482 sp_feed_dog();
483 kcode = sp_get_key();
484 if(kcode != SP_KEY_NONE)
485 {
486 switch(kcode)
487 {
488 case SP_KEY_0:
489 case SP_KEY_1:
490 case SP_KEY_2:
491 case SP_KEY_3:
492 case SP_KEY_4:
493 case SP_KEY_5:
494 case SP_KEY_6:
495 case SP_KEY_7:
496 case SP_KEY_8:
497 case SP_KEY_9:
498 if(offset < 6)
499 {
500 temp[offset++] = (uint8)(kcode-SP_KEY_0);
501 show_manage_passwd(pos,hint,temp,offset);
502 }
503 if(offset >= 6)
504 {
505 if(0 == MEMCMP(temp,passwd,6))
506 {
507 return 0;
508 }
509 else
510 {
511 disp_hint_info_two(pos,hint,"ÃÜÂë´íÎó",DELAY_TIME2s);
512 MEMCLEAR(temp, sizeof temp);
513 offset = 0;
514 }
515 }
516 break;
517 case SP_KEY_CLEAR:
518 if(offset == 0)
519 {
520 return 1;
521 }
522 --offset;
523 temp[offset] = 0;
524 show_manage_passwd(pos,hint,temp,offset);
525 break;
526 }
527 }
528 }
529 return 1;
530}