blob: 5b108334f1e33f625882dbe01c987993abcef9dd [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{
zongqiang.zhang578c6eb2020-08-04 16:16:22 +0800258 if(!valve_sta_get())
259 {
260 valve_sta_set(valve_state_on);
261 valve_ctrl();
262 }
zongqiang.zhang0c6a0882019-08-07 14:48:21 +0800263}
264void sp_valve_off(void)
265{
zongqiang.zhang578c6eb2020-08-04 16:16:22 +0800266 if(valve_sta_get())
267 {
268 valve_sta_set(valve_state_off);
269 valve_ctrl();
270 }
zongqiang.zhang0c6a0882019-08-07 14:48:21 +0800271}
272uint8 sp_valve_state(void)
273{
274 return valve_sta_get();
275}
276
277int16 get_2byte_int(uint8 value_str[2])
278{
279 int32 r = 0, t = 0;
280 t = value_str[0];
281 r = t << 8;
282 r |= value_str[1];
283 return (int16)r;
284}
285int32 get_3byte_int(uint8 value_str[3])
286{
287 int32 r = 0, t = 0;
288 t = value_str[0];
289 r = t << 16;
290 t = value_str[1];
291 r |= t << 8;
292 r |= value_str[2];
293 return r;
294}
295int32 get_4byte_int(uint8 value_str[4])
296{
297 int32 r = 0, t = 0;
298 t = value_str[0];
299 r = t << 24;
300 t = value_str[1];
301 r |= t << 16;
302 t = value_str[2];
303 r |= t << 8;
304 r |= value_str[3];
305 return r;
306}
307void set_2byte_int(uint8 value_str[2], int num)
308{
309 value_str[0] = (num >> 8) & 0xFF;
310 value_str[1] = num & 0xFF;
311}
312void set_3byte_int(uint8 value_str[3], int num)
313{
314 value_str[0] = (num >> 16) & 0xFF;
315 value_str[1] = (num >> 8) & 0xFF;
316 value_str[2] = num & 0xFF;
317}
318void set_4byte_int(uint8 value_str[4], int num)
319{
320 value_str[0] = (num >> 24) & 0xFF;
321 value_str[1] = (num >> 16) & 0xFF;
322 value_str[2] = (num >> 8) & 0xFF;
323 value_str[3] = num & 0xFF;
324}
325
326//»ñµÃµ¥×Ö½ÚÖÐijһλµÄÖµ
327int32 Get1Bit(uint8 buf, int n)
328{
329 return (buf >> n) & 0x01;
330}
331
332int16 get_2byte_int_le(uint8 value_str[2])
333{
334 int32 r = 0, t = 0;
335 t = value_str[1];
336 r = t << 8;
337 r |= value_str[0];
338 return (int16)r;
339}
340//С×Ö½ÚÐò
341int32 get_3byte_int_le(uint8 value_str[3])
342{
343 int32 r = 0, t = 0;
344 t = value_str[2];
345 r = t << 16;
346 t = value_str[1];
347 r |= t << 8;
348 r |= value_str[0];
349 return r;
350}
351//С×Ö½ÚÐò
352int32 get_4byte_int_le(uint8 value_str[4])
353{
354 int32 r = 0, t = 0;
355 t = value_str[3];
356 r = t << 24;
357 t = value_str[2];
358 r |= t << 16;
359 t = value_str[1];
360 r |= t << 8;
361 r |= value_str[0];
362 return r;
363}
364
365void set_2byte_int_le(uint8 value_str[2], int num)
366{
367 value_str[1] = (num >> 8) & 0xFF;
368 value_str[0] = num & 0xFF;
369}
370
371void set_3byte_int_le(uint8 value_str[3], int num)
372{
373 value_str[2] = (num >> 16) & 0xFF;
374 value_str[1] = (num >> 8) & 0xFF;
375 value_str[0] = num & 0xFF;
376}
377
378/*¼ÆËã´Óbcd starttime µ½ÏÖÔÚµÄÃëÊý*/
379uint32 diff_time(uint8 starttime[6])
380{
381 uint8 ctime[6];
382 uint32 start_second;
383 uint32 end_second;
384
385 sp_get_bcdtime(ctime);
386 start_second = format_time_covert_secs(starttime);
387 end_second = format_time_covert_secs(ctime);
388 if(end_second < start_second)
389 {
390 return 0;
391 }
392 return (end_second - start_second);
393}
394
395void mycpy(void* dest, const void* src, uint32 len)
396{
397 char* tmp_dest = (char*)dest;
398 char* tmp_src = (char*)src;
399 while(len--)
400 {
401 *tmp_dest = *tmp_src++;
402 tmp_dest++;
403 }
404}
405
406void sp_bcd2asc(const uint8 bcdbuf[], uint8 bcdlen, uint8* ascstr)
407{
408 uint8 i;
409 uint8 lch, hch;
410 for(i = 0; i < bcdlen; i++)
411 {
412 hch = (bcdbuf[i] & 0x0F);
413 lch = (bcdbuf[i] & 0xF0);
414 lch = lch >> 4;
415 ascstr[2 * i] = lch + '0';
416 ascstr[2 * i + 1] = hch + '0';
417 }
418 ascstr[2*i] = 0;
419}
420int32 sp_atoi(const char* src)
421{
422 int i = 0;
423 while(*src != 0)
424 {
425 i = i * 10 + *src - '0';
426 src++;
427 }
428 return i;
429}
430/**
431**2000.0.0:0.0.0ʱ¼äΪ»ùÊý
432**/
433static int MINUTE_SECS = 60;
434static int HOUR_SECS = 3600;
435static int DAY_SECS = 24*3600;
436static int YEAR_SECS = 365*24*3600;
437static int FOURYEAR_SECS = (365*3+366)*24*3600;
438static int norMoth[] = {0,31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
439static int leapMoth[] = {0,31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
440/*time: string(yyyyMMddHHmmss) to int */
441int format_time_covert_secs(uint8 ctime[6])
442{
443 int secs;
444 int i;
445 int remain;
446 int year = BCD2Dec(ctime[0]);
447 int month = BCD2Dec(ctime[1]);
448 int day = BCD2Dec(ctime[2]);
449 int hour = BCD2Dec(ctime[3]);
450 int minute = BCD2Dec(ctime[4]);
451 int second = BCD2Dec(ctime[5]);
452
453 secs = year/4 *FOURYEAR_SECS;
454 remain = year%4;
455 secs += remain*YEAR_SECS;
456
457 if(remain == 0)
458 {
459 for(i = 0; i < month; ++i)
460 {
461 secs += leapMoth[i] *DAY_SECS;
462 }
463 }
464 else
465 {
466 for(i = 0; i < month; ++i)
467 {
468 secs += norMoth[i] *DAY_SECS;
469 }
470 }
471 secs += day*DAY_SECS;
472 secs += hour *HOUR_SECS;
473 secs += minute*MINUTE_SECS;
474 secs += second;
475 return secs;
476}
477
478uint8 sp_check_passwd(sp_pos_t* pos,const char* hint,uint8 passwd[6])
479{
480 uint8 temp[6];
481 uint8 offset = 0;
482 uint8 kcode;
483 uint32 ticker = sp_get_ticker();
484
485 show_manage_passwd(pos,hint,temp,offset);
486 while(sp_get_ticker() -ticker < DELAY_TIME60s)
487 {
488 sp_feed_dog();
489 kcode = sp_get_key();
490 if(kcode != SP_KEY_NONE)
491 {
492 switch(kcode)
493 {
494 case SP_KEY_0:
495 case SP_KEY_1:
496 case SP_KEY_2:
497 case SP_KEY_3:
498 case SP_KEY_4:
499 case SP_KEY_5:
500 case SP_KEY_6:
501 case SP_KEY_7:
502 case SP_KEY_8:
503 case SP_KEY_9:
504 if(offset < 6)
505 {
506 temp[offset++] = (uint8)(kcode-SP_KEY_0);
507 show_manage_passwd(pos,hint,temp,offset);
508 }
509 if(offset >= 6)
510 {
511 if(0 == MEMCMP(temp,passwd,6))
512 {
513 return 0;
514 }
515 else
516 {
517 disp_hint_info_two(pos,hint,"ÃÜÂë´íÎó",DELAY_TIME2s);
518 MEMCLEAR(temp, sizeof temp);
519 offset = 0;
520 }
521 }
522 break;
523 case SP_KEY_CLEAR:
524 if(offset == 0)
525 {
526 return 1;
527 }
528 --offset;
529 temp[offset] = 0;
530 show_manage_passwd(pos,hint,temp,offset);
531 break;
532 }
533 }
534 }
535 return 1;
536}