#include "mywin_user_home.h" #include "date.h" #include "ff.h" #include "flash_manager.h" #include "math.h" #include "mp3play.h" #include "mywin_inc.h" #include "mywin_user_lock.h" #include "mywin_user_menu.h" #include "mywin_user_set.h" #include "mywin_user_tool.h" #include "system_file.h" #include "system_updata.h" #define WIN_HOME_TYPE "WIN_HomeStruct" static WIN_TouchWinStruct *creat_menu(WIN_TouchWinStruct *base); static WIN_TouchWinStruct *creat_tool(WIN_TouchWinStruct *base); static const char *g_week[7] = {"星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期天"}; WIN_HomeStruct *WIN_CreatHome(WIN_WindowStruct *base, void (*msgLoop)(struct _WIN_WindowStruct *win, WIN_MsgStruct *msg), int x, int y, int x_size, int y_size) { // 重设消息循环 if (msgLoop == 0) { msgLoop = (void (*)(struct _WIN_WindowStruct *win, WIN_MsgStruct *msg))HOME_DefaultMsgLoop; } WIN_HomeStruct *ret = mymalloc(sizeof(WIN_HomeStruct)); // 调用父类的构造函数 if (ret) { mymemset(ret, 0, sizeof(WIN_HomeStruct)); // if (0==WIN_CreatWindowExt((WIN_WindowStruct //*)ret,base,msgLoop,x,y,x_size,y_size)) if (0 == WIN_CreatTouchEx((WIN_TouchWinStruct *)ret, base, msgLoop, x, y, x_size, y_size)) { // 创建失败 myfree(ret); ret = 0; } else { // 构造一个 ((WIN_WindowStruct *)ret)->winType = WIN_HOME_TYPE; ((WIN_WindowStruct *)ret)->bkcolor = 0; ((WIN_WindowStruct *)ret)->color = 0xd7c1ac; ret->color1 = 0x7d7064; ret->timerId = WIN_CreatTimer((WIN_WindowStruct *)ret, 100); WIN_TouchWinStruct *t = (WIN_TouchWinStruct *)ret; WIN_WindowStruct *w = (WIN_WindowStruct *)ret; // TOUCHWIN_AddChild(t,TOUCH_CON_DOWN_PAR(w->x_size,w->y_size,creat_menu)); // TOUCHWIN_AddChild(t,TOUCH_CON_LEFT_PAR(w->x_size,w->y_size,creat_tool)); } } return ret; } int HOME_Updata(WIN_HomeStruct *home, HOME_DataStruct *data) { if (data->structType == STRUCT_TYPE_HOME) { mymemcpy(home->date, data->date, strlen(data->date) + 1); mymemcpy(home->time, data->time, strlen(data->time) + 1); return 1; } return 0; } static WIN_TouchWinStruct *creat_menu(WIN_TouchWinStruct *base) { WIN_WindowStruct *temp = 0; temp = (WIN_WindowStruct *)MENU_Setting((WIN_WindowStruct *)base, &((WIN_WindowStruct *)base)->pic); WIN_SetBkColor(temp, ((WIN_WindowStruct *)base)->bkcolor); WIN_SetColor(temp, ((WIN_WindowStruct *)base)->color); WIN_TouchWinStruct *t = (WIN_TouchWinStruct *)temp; // WIN_TouchWinStruct *t=(WIN_TouchWinStruct //*)CreatPicSet((WIN_WindowStruct *)base); return t; } static WIN_TouchWinStruct *creat_tool(WIN_TouchWinStruct *base) { WIN_WindowStruct *temp = 0; temp = (WIN_WindowStruct *)MENU_Tool((WIN_WindowStruct *)base, &((WIN_WindowStruct *)base)->pic); WIN_SetBkColor(temp, ((WIN_WindowStruct *)base)->bkcolor); WIN_SetColor(temp, ((WIN_WindowStruct *)base)->color); WIN_TouchWinStruct *t = (WIN_TouchWinStruct *)temp; return t; } // 按键消息处理函数 /* 主页按键逻辑: 短按确定键,进入伞训 短按上键,应用检测 短按下键,快捷工具 长按确定键,设置 */ int HOME_KeyBord(WIN_HomeStruct *home, WIN_KeyStruct *k) { if (k->shortPress & KEY_VALUE_ENTER) { creat_menu((WIN_TouchWinStruct *)home); } else if (k->shortPress & KEY_VALUE_UP) { } else if (k->shortPress & KEY_VALUE_HOME) { creat_tool((WIN_TouchWinStruct *)home); } else if (k->longPress & KEY_VALUE_ENTER) { } return 0; } // 处理触屏消息 int HOME_TouchBord(WIN_HomeStruct *home, WIN_MsgStruct *msg) { switch (msg->data.v) { case CHID_DELETE: break; case CHID_USER: { if (msg->data2.v == BUTTON_PRESSED) { if (msg->srcWin == home->key_menu) { creat_menu((WIN_TouchWinStruct *)home); } else if (msg->srcWin == home->key_tool) { creat_tool((WIN_TouchWinStruct *)home); } } } break; default: break; } return 0; } // 触屏移动消息 int HOME_TouchMove(WIN_HomeStruct *home, WIN_MoveStruct *m) { WIN_WindowStruct *win = (WIN_WindowStruct *)home; switch (m->moveType) { case MOVE_DATA_MOVED: break; case MOVE_DATA_TOUCHIN: break; case MOVE_DATA_TOUCHOUT: break; } return 0; } // 默认绘制函数 void HOME_DefaultPaint(WIN_HomeStruct *home) { int x = 0; int y = 0; int x_size = ((WIN_WindowStruct *)home)->x_size; int y_size = ((WIN_WindowStruct *)home)->y_size; WIN_PaintBackGround((WIN_WindowStruct *)home); uint32_t font_type = WIN_SetFontSize(24); // 显示日期 WIN_SetLcdBkColor(((WIN_WindowStruct *)home)->bkcolor); WIN_SetLcdColor(((WIN_WindowStruct *)home)->color); WIN_SetFontMode(WIN_DRAWMODE_ALONE); char txt_dat[20] = {0}; int dat_x = 156; int dat_y = 190 + 20; mymemcpy(txt_dat, home->date, 10); WIN_DrawTxtHCenterAt(home->date, x_size / 2, dat_y); // 显示时间 char txt_min[10] = {0}; int time_x = 0; int time_y = 60 + 20; mymemcpy(txt_min, home->time, 5); WIN_SetFontSize(100); int min_xsize = strlen(txt_min) * WIN_GetFontWidth() / 2; WIN_SetFontSize(35); int sec_xsize = strlen(home->time + 5) * WIN_GetFontWidth() / 2; time_x = x_size / 2 - (min_xsize + sec_xsize) / 2; WIN_SetFontSize(100); WIN_DrawTxtAt(txt_min, time_x, time_y); time_x += min_xsize; time_y += WIN_GetFontHight(); WIN_SetFontSize(35); time_y -= WIN_GetFontHight(); WIN_DrawTxtAt(home->time + 5, time_x, time_y); WIN_SetFontSize(font_type); // 绘图测试 // static int angle=0; // angle++;if(angle>=360) angle=0; // int p_x=sinf(angle*3.14159f/180.0f)*50; // int p_y=cosf(angle*3.14159f/180.0f)*50; // WIN_DrawLine(350+50,136,350+50+p_x,136+p_y); // WIN_DrawLineAA(350+50,136+50,350+50+p_x,136+50+p_y,4); } static const char *g_fileSaveItem[] = { "保存到系统空间", "保存到SD卡", }; // 文件复制到SD卡的工作函数 static int savefile_to_sd(void *ptr) { SysFile_RecvedStruct *t = ptr; int ret_int = 0; FIL *file = mymalloc(sizeof(FIL)); char *file_name = mymalloc(256); FRESULT ret = FR_OK; int file_name_len = strlen(t->name); // 只支持3个字符的尾缀 if (t->name[file_name_len - 4] != '.') { myfree(file); myfree(file_name); return 0; } // 根据文件类型选择文件夹 sprintf(file_name, "0:/%s/%s", &t->name[file_name_len - 3], t->name); open: ret = f_open(file, file_name, FA_CREATE_NEW | FA_WRITE); if (ret == FR_NO_PATH) { char path[20] = {0}; sprintf(path, "0:/%s", &t->name[file_name_len - 3]); ret = f_mkdir(path); if (ret == FR_OK) goto open; } if (ret == FR_OK) { UINT real = 0; ret = f_write(file, t->data, t->dataLen, &real); if (ret != FR_OK) while (1) ; else ret_int = 1; // 返回1成功 } else ret_int = 0; f_close(file); myfree(file); myfree(file_name); return ret_int; } // 保存文件到系统工作函数 static int savefile_to_sys(void *ptr) { SysFile_RecvedStruct *r = ptr; return FLASH_SaveFile(r->name, r->data, r->dataLen); } typedef struct { uint8_t *data; uint32_t dataSize; } DataInfo_Struct; static int g_alarmRingWork(void *ptr) { DataInfo_Struct *dat = ptr; return mp3_play_song(dat->data, dat->dataSize); } // 闹钟响起 void HOME_AlarmRun(WIN_HomeStruct *home) { WIN_WindowStruct *win = (WIN_WindowStruct *)home; char *txt_buff = mymalloc(256); RTC_TimeTypeDef rtc_time = {0}; RTC_DateTypeDef rtc_date = {0}; RTC_GetTime(RTC_Format_BIN, &rtc_time); RTC_GetDate(RTC_Format_BIN, &rtc_date); sprintf(txt_buff, "%04d年%02d月%02d日 %s\n%02d:%02d", rtc_date.RTC_Year + 2000, rtc_date.RTC_Month, rtc_date.RTC_Date, g_week[rtc_date.RTC_WeekDay - 1], rtc_time.RTC_Hours, rtc_time.RTC_Minutes); // 打开闹铃 uint32_t size = 0; uint8_t *data = SysFile_GetFileByName(SysFile_GetSysFile()->alarmRing, &size); WIN_WorkFunStruct *work; int work_ret = 0; DataInfo_Struct mp3Data = {0}; if (data) { // 创建工作线程 mp3Data.data = data; mp3Data.dataSize = size; work = WIN_CreatThreadPro((int (*)(void *))g_alarmRingWork, &mp3Data, &work_ret, 17); } MSGBOX_TipsTime((WIN_WindowStruct *)win, "闹钟时间到", txt_buff, "确定", 5 * 60 * 1000); // 关闭闹铃 if (data) { if (WIN_GetExWorkFunStat(work)) { WIN_ExWorkFunClear(work); } else { mp3_stop(); while (1) { if (WIN_GetExWorkFunStat(work)) break; WIN_Delay_ms(100); } WIN_ExWorkFunClear(work); } myfree(data); } myfree(txt_buff); } // 接受到一个文件 void HOME_RecvFile(WIN_HomeStruct *home, SysFile_RecvedStruct *r) { WIN_WindowStruct *win = (WIN_WindowStruct *)home; if (r->recved) { char *txt = mymalloc(256); sprintf(txt, "文件名:%s\n文件大小:%ld Byte", r->name, r->dataLen); if (MSGBOX_Inquiry((WIN_WindowStruct *)win, "是否保存?", txt, "是", "否") == 0) { int index = MSGBOX_Select((WIN_WindowStruct *)win, "文件保存位置", (char **)g_fileSaveItem, 2, "确定", "取消"); if (index == 0) { if (WORKING_DoWork((WIN_WindowStruct *)win, "正在保存到系统。。。", savefile_to_sys, r) == 0) { MSGBOX_TipsTime((WIN_WindowStruct *)win, "提示", "文件已保存", "确定", 5000); } else MSGBOX_TipsTime((WIN_WindowStruct *)win, "提示", "文件保存失败", "确定", 5000); } else if (index == 1) { if (WORKING_DoWork((WIN_WindowStruct *)win, "正在保存到SD卡。。。", savefile_to_sd, r) == 1) { MSGBOX_TipsTime((WIN_WindowStruct *)win, "提示", "文件已保存", "确定", 5000); } else MSGBOX_TipsTime((WIN_WindowStruct *)win, "提示", "文件保存失败", "确定", 5000); } } myfree(txt); myfree(r->data); } else { MSGBOX_Inquiry((WIN_WindowStruct *)win, "接收失败!", r->name, "确定", "返回"); } } // 默认消息处理函数 void HOME_DefaultMsgLoop(WIN_HomeStruct *home, WIN_MsgStruct *msg) { HOME_DataStruct *d = 0; WIN_KeyStruct *k = 0; WIN_MoveStruct *m; switch (msg->msg) { case WIN_MSG_PAINT: HOME_DefaultPaint(home); break; case WIN_MSG_INIT: { int y_size = ((WIN_WindowStruct *)home)->y_size; int x_size = ((WIN_WindowStruct *)home)->x_size; home->key_menu = WIN_CreatButton(((WIN_WindowStruct *)home), 0, 0, y_size - 30, 65, 30); BUTTON_SetKeyString(home->key_menu, "菜单"); home->key_tool = WIN_CreatButton(((WIN_WindowStruct *)home), 0, x_size - 65, y_size - 30, 65, 30); BUTTON_SetKeyString(home->key_tool, "工具"); LOCK_EnterLock("0:/PIC/3.pic"); } break; case WIN_MSG_RUN: WIN_RunMsgFunction(msg); break; case WIN_MSG_TIMER: if (msg->data.v == home->timerId) { RTC_TimeTypeDef rtc_time = {0}; RTC_DateTypeDef rtc_date = {0}; RTC_GetTime(RTC_Format_BIN, &rtc_time); RTC_GetDate(RTC_Format_BIN, &rtc_date); if (home->sec != rtc_time.RTC_Seconds) { home->sec = rtc_time.RTC_Seconds; if (home->hour != rtc_time.RTC_Hours) { home->hour = rtc_time.RTC_Hours; } sprintf(home->date, "%04d-%02d-%02d %s", rtc_date.RTC_Year + 2000, rtc_date.RTC_Month, rtc_date.RTC_Date, g_week[rtc_date.RTC_WeekDay - 1]); sprintf(home->time, "%02d:%02d:%02d", rtc_time.RTC_Hours, rtc_time.RTC_Minutes, rtc_time.RTC_Seconds); WIN_SetInvalidWhenTop((WIN_WindowStruct *)home); } // if (WIN_FindTopWin(WIN_GetBaseWindow())==(WIN_WindowStruct *)home) { // 查找是否有外部数据 uint32_t data_size = 0; void *data = WIN_GetExtData(&data_size); if (data) { SysFile_RecvedStruct *r = data; if (r->structType == SYSFILE_TYPE_RECVFILE) { // 接收到一个文件 HOME_RecvFile(home, r); } else if (r->structType == SYSFILE_TYPE_ALARM) { // 闹钟 HOME_AlarmRun(home); } myfree(data); } } } break; case WIN_MSG_EXTMSG: d = msg->data.p; if (HOME_Updata(home, d) == 1) { // 有子窗口的时候不需要刷新 if (((WIN_WindowStruct *)home)->chidWinNum == 0) { WIN_SetInvalid((WIN_WindowStruct *)home); } } break; case WIN_MSG_CHID: HOME_TouchBord(home, msg); break; case WIN_MSG_KEY: k = msg->data.p; HOME_KeyBord(home, k); break; // case WIN_MSG_KEY: case WIN_MSG_MOVE: m = msg->data.p; HOME_TouchMove(home, m); break; default: WIN_DefaultMsgLoop((WIN_WindowStruct *)home, msg); break; } }