blob: df70b139942e03ff7c5ec4c38ad7142e7888d7a2 [file] [log] [blame]
#include "../sys_hw/keypad.h"
#include "../sys_hw/drv_adc.h"
#include "sp_display.h"
#include "sp_util.h"
uint16 crc_table[] =
{
0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,
0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0
};
void sp_protocol_crc(const uint8* buf, uint16 len, uint8 crc[2])
{
//uint16 accnum = 0xc78c;
uint8 temp_crc[2];
temp_crc[0] = 0xc7;
temp_crc[1] = 0x8c;
sp_protocol_crc_init(buf,len,temp_crc,crc);
}
void sp_protocol_crc_init(const uint8* buf, uint16 len,uint8 init[2], uint8 crc[2])
{
uint16 accnum = (uint16)init[0]<<8 |(uint16)init[1];
uint16 i;
for(i = 0; i < len; ++i)
{
accnum = (accnum << 8) ^ crc_table[((accnum >> 8)
^ buf[i]) & 0xFF];
}
crc[0] = (uint8)((accnum >> 8) & 0xFF);
crc[1] = (uint8)(accnum & 0xFF);
}
void sp_key_calibrate(void)
{
calibrate_key(0);
}
uint8 sp_key_init(void)
{
adc_init();
return keypad_init();
}
uint8 sp_get_key(void)
{
uint8 key = keypad_get_key();
switch(key)
{
case KEY_NONE:
return SP_KEY_NONE;
case KEY_DIG0:
return SP_KEY_0;
case KEY_DIG1:
return SP_KEY_1;
case KEY_DIG2:
return SP_KEY_2;
case KEY_DIG3:
return SP_KEY_3;
case KEY_DIG4:
return SP_KEY_4;
case KEY_DIG5:
return SP_KEY_5;
case KEY_DIG6:
return SP_KEY_6;
case KEY_DIG7:
return SP_KEY_7;
case KEY_DIG8:
return SP_KEY_8;
case KEY_DIG9:
return SP_KEY_9;
case KEY_ENTER:
return SP_KEY_ENTER;
case KEY_CANCEL:
return SP_KEY_CLEAR;
default:
return SP_KEY_NONE;
}
}
void sp_reset(void)
{
while(1);
}
void sp_bcd_to_str(const uint8* bcd, uint8 bcd_len, char* str)
{
uint8 i;
uint16 j;
for(i = 0; i < bcd_len; ++i)
{
j = i << 1;
str[j] = ((bcd[i] >> 4) & 0x0F) + 0x30;
str[j + 1] = (bcd[i] & 0x0F) + 0x30;
}
str[i << 1] = 0;
}
void sp_str_to_bcd(const char* str, uint8 str_len, uint8* bcd)
{
uint8 i,j;
for(i = 0; i < str_len; i += 2)
{
j = i/2;
bcd[j] = (((str[i]-0x30) & 0x0F) <<4) | ((str[i+1]-0x30) & 0x0F);
}
}
static uint8 charTohex(char ch)
{
if(ch >= '0' && ch <= '9')
{
return (ch -'0');
}
if(ch >= 'a' && ch <= 'f')
{
return (ch -'a' +10);
}
if(ch >= 'A' && ch <= 'F')
{
return (ch -'A' +10);
}
return 0;
}
void sp_str_to_hex(const char* str,const uint16 len,uint8* hex)
{
uint8 t,temp;
uint16 i;
for(i = 0; i < len; i += 2)
{
temp = charTohex(str[i]);
t = temp << 4;
temp = charTohex(str[i+1]);
t += temp;
hex[i/2] = t;
}
}
static char hexToCharTable[]= {"0123456789ABCDEF"};
void sp_hex_to_str(const uint8* hex,const uint8 len,char* str)
{
uint8 i,j;
for(i = 0; i < len; i++)
{
j = i << 1;
str[j] = hexToCharTable[(hex[i] >> 4) &0x0F];
str[j +1] = hexToCharTable[hex[i] &0x0F];
}
str[i << 1] = 0;
}
void sp_get_bcdtime(uint8 ctime[6])
{
_SystemTime time;
memset(&time,0,sizeof time);
rtc_get_time(&time); //bcd
ctime[0] = time.year;
ctime[1] = time.month;
ctime[2] = time.day;
ctime[3] = time.hour;
ctime[4] = time.minute;
ctime[5] = time.second;
}
void sp_set_bcdtime(uint8 ctime[6])
{
_SystemTime time;
memset(&time,0,sizeof time);
time.year = ctime[0];
time.month = ctime[1];
time.day = ctime[2];
time.hour = ctime[3];
time.minute = ctime[4];
time.second = ctime[5];
rtc_set_time(&time);
}
uint8 sp_check_time_valid(uint8 ctime[6])
{
if(BCD2Dec(ctime[0]) < 18)
return 1;
if(BCD2Dec(ctime[1]) > 12)
return 1;
if(BCD2Dec(ctime[2]) > 31)
return 1;
if(BCD2Dec(ctime[3]) > 23)
return 1;
if(BCD2Dec(ctime[4]) > 59)
return 1;
if(BCD2Dec(ctime[5]) > 59)
return 1;
return 0;
}
uint8 sp_crc_sum(uint8 buf[],uint8 len)
{
uint8 i;
uint8 sum = 0;
//¼ÆËãУÑéºÍ
for(i = 0; i < len; i++)
{
sum = sum + buf[i];
}
return sum;
}
int8 isFF(uint8 buf[],uint16 len)
{
uint16 i;
for(i = 0; i < len; i++)
{
if(0xFF != buf[i])
{
return -1;
}
}
return 0;
}
//ms
uint32 sp_get_ticker(void)
{
return timer_get_ticker();
}
void Delay_ms(uint32 ms)
{
uint32 t = sp_get_ticker();
while(sp_get_ticker() - t < ms);
}
void sp_valve_on(void)
{
valve_sta_set(valve_state_on);
valve_ctrl();
}
void sp_valve_off(void)
{
valve_sta_set(valve_state_off);
valve_ctrl();
}
uint8 sp_valve_state(void)
{
return valve_sta_get();
}
int16 get_2byte_int(uint8 value_str[2])
{
int32 r = 0, t = 0;
t = value_str[0];
r = t << 8;
r |= value_str[1];
return (int16)r;
}
int32 get_3byte_int(uint8 value_str[3])
{
int32 r = 0, t = 0;
t = value_str[0];
r = t << 16;
t = value_str[1];
r |= t << 8;
r |= value_str[2];
return r;
}
int32 get_4byte_int(uint8 value_str[4])
{
int32 r = 0, t = 0;
t = value_str[0];
r = t << 24;
t = value_str[1];
r |= t << 16;
t = value_str[2];
r |= t << 8;
r |= value_str[3];
return r;
}
void set_2byte_int(uint8 value_str[2], int num)
{
value_str[0] = (num >> 8) & 0xFF;
value_str[1] = num & 0xFF;
}
void set_3byte_int(uint8 value_str[3], int num)
{
value_str[0] = (num >> 16) & 0xFF;
value_str[1] = (num >> 8) & 0xFF;
value_str[2] = num & 0xFF;
}
void set_4byte_int(uint8 value_str[4], int num)
{
value_str[0] = (num >> 24) & 0xFF;
value_str[1] = (num >> 16) & 0xFF;
value_str[2] = (num >> 8) & 0xFF;
value_str[3] = num & 0xFF;
}
//»ñµÃµ¥×Ö½ÚÖÐijһλµÄÖµ
int32 Get1Bit(uint8 buf, int n)
{
return (buf >> n) & 0x01;
}
int16 get_2byte_int_le(uint8 value_str[2])
{
int32 r = 0, t = 0;
t = value_str[1];
r = t << 8;
r |= value_str[0];
return (int16)r;
}
//С×Ö½ÚÐò
int32 get_3byte_int_le(uint8 value_str[3])
{
int32 r = 0, t = 0;
t = value_str[2];
r = t << 16;
t = value_str[1];
r |= t << 8;
r |= value_str[0];
return r;
}
//С×Ö½ÚÐò
int32 get_4byte_int_le(uint8 value_str[4])
{
int32 r = 0, t = 0;
t = value_str[3];
r = t << 24;
t = value_str[2];
r |= t << 16;
t = value_str[1];
r |= t << 8;
r |= value_str[0];
return r;
}
void set_2byte_int_le(uint8 value_str[2], int num)
{
value_str[1] = (num >> 8) & 0xFF;
value_str[0] = num & 0xFF;
}
void set_3byte_int_le(uint8 value_str[3], int num)
{
value_str[2] = (num >> 16) & 0xFF;
value_str[1] = (num >> 8) & 0xFF;
value_str[0] = num & 0xFF;
}
/*¼ÆËã´Óbcd starttime µ½ÏÖÔÚµÄÃëÊý*/
uint32 diff_time(uint8 starttime[6])
{
uint8 ctime[6];
uint32 start_second;
uint32 end_second;
sp_get_bcdtime(ctime);
start_second = format_time_covert_secs(starttime);
end_second = format_time_covert_secs(ctime);
if(end_second < start_second)
{
return 0;
}
return (end_second - start_second);
}
void mycpy(void* dest, const void* src, uint32 len)
{
char* tmp_dest = (char*)dest;
char* tmp_src = (char*)src;
while(len--)
{
*tmp_dest = *tmp_src++;
tmp_dest++;
}
}
void sp_bcd2asc(const uint8 bcdbuf[], uint8 bcdlen, uint8* ascstr)
{
uint8 i;
uint8 lch, hch;
for(i = 0; i < bcdlen; i++)
{
hch = (bcdbuf[i] & 0x0F);
lch = (bcdbuf[i] & 0xF0);
lch = lch >> 4;
ascstr[2 * i] = lch + '0';
ascstr[2 * i + 1] = hch + '0';
}
ascstr[2*i] = 0;
}
int32 sp_atoi(const char* src)
{
int i = 0;
while(*src != 0)
{
i = i * 10 + *src - '0';
src++;
}
return i;
}
/**
**2000.0.0:0.0.0ʱ¼äΪ»ùÊý
**/
static int MINUTE_SECS = 60;
static int HOUR_SECS = 3600;
static int DAY_SECS = 24*3600;
static int YEAR_SECS = 365*24*3600;
static int FOURYEAR_SECS = (365*3+366)*24*3600;
static int norMoth[] = {0,31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
static int leapMoth[] = {0,31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
/*time: string(yyyyMMddHHmmss) to int */
int format_time_covert_secs(uint8 ctime[6])
{
int secs;
int i;
int remain;
int year = BCD2Dec(ctime[0]);
int month = BCD2Dec(ctime[1]);
int day = BCD2Dec(ctime[2]);
int hour = BCD2Dec(ctime[3]);
int minute = BCD2Dec(ctime[4]);
int second = BCD2Dec(ctime[5]);
secs = year/4 *FOURYEAR_SECS;
remain = year%4;
secs += remain*YEAR_SECS;
if(remain == 0)
{
for(i = 0; i < month; ++i)
{
secs += leapMoth[i] *DAY_SECS;
}
}
else
{
for(i = 0; i < month; ++i)
{
secs += norMoth[i] *DAY_SECS;
}
}
secs += day*DAY_SECS;
secs += hour *HOUR_SECS;
secs += minute*MINUTE_SECS;
secs += second;
return secs;
}
uint8 sp_check_passwd(sp_pos_t* pos,const char* hint,uint8 passwd[6])
{
uint8 temp[6];
uint8 offset = 0;
uint8 kcode;
uint32 ticker = sp_get_ticker();
show_manage_passwd(pos,hint,temp,offset);
while(sp_get_ticker() -ticker < DELAY_TIME60s)
{
sp_feed_dog();
kcode = sp_get_key();
if(kcode != SP_KEY_NONE)
{
switch(kcode)
{
case SP_KEY_0:
case SP_KEY_1:
case SP_KEY_2:
case SP_KEY_3:
case SP_KEY_4:
case SP_KEY_5:
case SP_KEY_6:
case SP_KEY_7:
case SP_KEY_8:
case SP_KEY_9:
if(offset < 6)
{
temp[offset++] = (uint8)(kcode-SP_KEY_0);
show_manage_passwd(pos,hint,temp,offset);
}
if(offset >= 6)
{
if(0 == MEMCMP(temp,passwd,6))
{
return 0;
}
else
{
disp_hint_info_two(pos,hint,"ÃÜÂë´íÎó",DELAY_TIME2s);
MEMCLEAR(temp, sizeof temp);
offset = 0;
}
}
break;
case SP_KEY_CLEAR:
if(offset == 0)
{
return 1;
}
--offset;
temp[offset] = 0;
show_manage_passwd(pos,hint,temp,offset);
break;
}
}
}
return 1;
}