Add TODO
[SmartMotor.git] / SmartMotor-2.0-MiniGui / src / motormain.c
blobab087e4163e33e9fd236784791118a8d1a7209cd
1 /*
2 * src/motormain.c
4 * The entry window implementation
6 * Copyright 2007-2008 @ xianweizeng@gmail.com
8 * Author:
9 * Zeng Xianwei
11 * File Coding system: gb2312
12 * set-buffer-file-coding-system
14 * ChangeLog:
15 * 2008-09-24 Change code indent
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
22 #include <minigui/common.h>
23 #include <minigui/minigui.h>
24 #include <minigui/gdi.h>
25 #include <minigui/window.h>
26 #include <minigui/control.h>
27 #include <minigui/mgext.h>
29 #include "motormain.h"
30 #include "motorregister.h"
31 #include "motorfile.h"
32 #include "motor.h"
33 #include "bkgndEdit.h"
34 #include "help.h"
36 #include "userprogram.h"
37 #include "keyboard.h" /* Keyboard Check */
38 #include "rtc.h"
41 /* define globle variables */
42 HWND myIMEWindow;
43 int isIMEOpenInMotor; /* 0: disappear 1: display */
45 /* startup paramters */
46 struct startup_params_t startupParams;
48 /* motor status */
49 struct motorStatus_t motorStatus;
51 /* system font */
52 PLOGFONT systemDefaultFont,userFont24,userFont16;
54 /* system language */
55 int *curSysLang;
57 static int animationFlag = 0; /* Is flash on flag */
59 static BITMAP welcomeBkgnd;
61 struct welcome_help_t{
62 unsigned char * msg_welcome;
65 static struct welcome_help_t welcome_help[SYSLANG_NO] =
67 {msg_welcome_cn},
68 {msg_welcome_en}
72 /* Load config file
74 int loadInitConfigFile(struct startup_params_t *pStartup_params)
76 FILE *fp;
77 int isFlashOn = 0;
78 int isBkgnd = 0;
79 int lastuser = 0;
80 fp = fopen("./user/init.cfg","r");
81 if(fp == NULL){
82 printf("Open init file error!\n");
83 return -1;
86 while(!feof(fp)){
87 fscanf(fp,"%d %d %d\n",&isFlashOn, &isBkgnd, &lastuser);
90 pStartup_params->isFlashOn = isFlashOn;
91 pStartup_params->whichBkgnd = isBkgnd;
92 pStartup_params->lastuser = lastuser;
94 fclose(fp);
96 return(0);
100 int saveInitConfigFile (struct startup_params_t *pStartup_params)
102 FILE *fp;
104 fp=fopen("./user/init.cfg","w");
105 if(fp == NULL) {
106 printf("Open init file error!\n");
107 return(-1);
110 fprintf(fp,"%d %d %d\n",pStartup_params->isFlashOn,
111 pStartup_params->whichBkgnd,
112 pStartup_params->lastuser);
113 fclose(fp);
115 return(0);
119 static struct comCmdData_t cmdData;
121 static int sendUserLimits(int flag)
123 int data = 0, tmpdata;
124 int unit = currentuser->unit;
125 char cmdSendBuf[13];
127 memset(cmdSendBuf,0,12);
128 cmdData.flag = 'L';
129 switch(flag){
130 case 0:
131 data = currentuser->frontlimit;
132 break;
133 case 1:
134 data = currentuser->middlelimit;
135 break;
136 case 2:
137 data = currentuser->backlimit;
138 break;
140 tmpdata = data;
141 if(unit){
142 data = inch_to_mm(tmpdata);
145 cmdData.data = data;
146 cmdData.status = 0x30 + flag;
148 setCmdFrame(&cmdData,cmdSendBuf);
149 sendCommand(cmdSendBuf);
150 printf("Send Limit %d %d\n",flag,data);
151 return(data);
154 static void sendUserAllLimits(void)
156 int delay = 1000;
157 int flag = 0;
158 for(flag=0;flag<3;flag++){
159 delay = 1000;
160 sendUserLimits(flag);
161 while(delay--);
165 static void reinitAnimationBkgnd(HWND hWnd)
167 HDC hdc;
168 if(!animationFlag){
169 hdc = BeginPaint (hWnd);
170 FillBoxWithBitmap (hdc, 0, 0, 0, 0, &welcomeBkgnd);
171 EndPaint (hWnd, hdc);
172 } else {
173 SendMessage (GetDlgItem (hWnd, 800),
174 ANM_STARTPLAY, 0, 0);
178 static int AnimationWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
180 char cmdbuf[12];
181 unsigned char *pWelcomeText = welcome_help[*curSysLang].msg_welcome;
182 HDC hdc;
183 switch (message) {
184 case MSG_CREATE:
187 if(!animationFlag){
188 hdc = BeginPaint (hWnd);
189 if (LoadBitmap (HDC_SCREEN, &welcomeBkgnd, "./res/welcome.jpg"))
190 return 1;
191 FillBoxWithBitmap (hdc, 0, 0, 0, 0, &welcomeBkgnd);
192 EndPaint (hWnd, hdc);
194 break;
197 hdc = BeginPaint (hWnd);
198 TextOut (hdc, 210, 220, pWelcomeText);
199 EndPaint (hWnd, hdc);
200 ANIMATION* anim = CreateAnimationFromGIF89aFile (HDC_SCREEN, "./res/welcome.gif");
201 if (anim == NULL)
202 return 1;
204 SetWindowAdditionalData (hWnd, (DWORD) anim);
205 CreateWindow (CTRL_ANIMATION,
207 WS_VISIBLE | ANS_AUTOLOOP,
208 800, // ID
209 0, 0, 640, 480, hWnd, (DWORD)anim);
210 SendMessage (GetDlgItem (hWnd, 800), ANM_STARTPLAY, 0, 0);
212 break;
215 case MSG_KEYDOWN: // anykey press,pause
216 switch(LOWORD(wParam)){
217 case SCANCODE_HELP:
218 EnterKeyboardCheck(hWnd);
219 /* Re-init background */
220 reinitAnimationBkgnd(hWnd);
221 break;
223 case SCANCODE_RUN:
224 memset(cmdbuf,0,12);
225 cmdData.flag = 'R';
226 cmdData.status = 0x30;
227 cmdData.data = 80000;
228 setCmdFrame(&cmdData,cmdbuf); // En-frame
229 sendCommand(cmdbuf); // Sent it out
231 #if defined (CONFIG_DEBUG_ON_PC) || defined (CONFIG_DEBUG_ON_TARGET)
232 if(animationFlag){
233 SendMessage (GetDlgItem (hWnd, 800), ANM_STOPPLAY, 0, 0);
235 #endif
236 break;
238 break;
240 #if defined (CONFIG_DEBUG_ON_PC) || defined (CONFIG_DEBUG_ON_TARGET)
241 case MSG_KEYUP: // anykey press,go out
242 switch(LOWORD(wParam)){
243 case SCANCODE_RUN:
244 SendMessage(hWnd,MSG_CLOSE,0,0);
245 break;
247 break;
248 #endif
250 case MSG_RECV: // Reveive frame from LowerMachine
251 recvCommand(cmdbuf);
252 //int ret = isCmdValid(cmdbuf); // Now, there is no Check
253 //if(ret){
254 memset(&cmdData,0,sizeof(cmdData));
255 getCmdFrame(&cmdData,cmdbuf); // Decode, get data
256 if(cmdData.flag == 'R'){
257 motorStatus.currentCutPos = cmdData.data; // Refresh Data
258 motorStatus.devStatus = cmdData.status; // Refresh Status
259 if(animationFlag){
260 SendMessage (GetDlgItem (hWnd, 800), ANM_STOPPLAY, 0, 0);
262 SendMessage(hWnd,MSG_CLOSE,0,0);
265 break;
267 case MSG_DESTROY:
268 if(animationFlag){
269 DestroyAnimation ((ANIMATION*)GetWindowAdditionalData (hWnd), TRUE);
270 DestroyAllControls (hWnd);
272 return 0;
274 case MSG_CLOSE:
276 UnloadBitmap (&welcomeBkgnd);
278 DestroyMainWindow (hWnd);
279 PostQuitMessage (hWnd);
280 return 0;
283 return DefaultMainWinProc(hWnd, message, wParam, lParam);
287 int MiniGUIMain (int argc, const char* argv[])
289 MSG Msg;
290 HWND hMainWnd;
291 MAINWINCREATE CreateInfo;
293 isIMEOpenInMotor = 0;
294 motorStatus.currentCutPos = 0;
295 motorStatus.newCutPos = 0;
296 motorStatus.devStatus = 0;
297 motorStatus.flags = 0;
298 motorStatus.cutCounter = 0;
300 #ifdef _IME_GB2312
301 myIMEWindow=GBIMEWindow (HWND_DESKTOP); // IME window handler
302 if(myIMEWindow){
303 MoveWindow(myIMEWindow,170,110,365,40,0);
304 ShowWindow(myIMEWindow,SW_HIDE); // Hide IME window
305 isIMEOpenInMotor = 0;
307 #endif
309 // Create font
310 userFont24 = CreateLogFont (NULL, "song", "GB2312",
311 FONT_WEIGHT_BOOK, FONT_SLANT_ROMAN, FONT_SETWIDTH_NORMAL,
312 FONT_SPACING_CHARCELL, FONT_UNDERLINE_NONE,
313 FONT_STRUCKOUT_NONE, 24, 0);
314 userFont16 = CreateLogFont (NULL, "song", "GB2312",
315 FONT_WEIGHT_BOOK, FONT_SLANT_ROMAN, FONT_SETWIDTH_NORMAL,
316 FONT_SPACING_CHARCELL, FONT_UNDERLINE_NONE,
317 FONT_STRUCKOUT_NONE, 16, 0);
318 //systemDefaultFont = GetSystemFont(SYSLOGFONT_DEFAULT);
320 #ifndef CONFIG_DEBUG_ON_PC
321 initSerialCommunication(); /* Init Serial Communication Port */
322 #endif
325 /* update system time by RTC */
326 update_systime();
328 loadInitConfigFile(&startupParams);
329 animationFlag = startupParams.isFlashOn;
331 loadUserConfigFile();
332 /* printf("Unit Flag = %d \n",userinformation[0].unit); */
333 curSysLang = &userinformation[0].language;
335 #ifdef _LITE_VERSION
336 SetDesktopRect(0, 0, 1024, 768);
337 #endif
339 if (!InitMiniGUIExt()) {
340 return 1;
343 CreateInfo.dwStyle = WS_VISIBLE;
344 CreateInfo.dwExStyle = WS_EX_NONE;
345 CreateInfo.spCaption = "" ;
346 CreateInfo.hMenu = 0;
347 CreateInfo.hCursor = 0;
348 CreateInfo.hIcon = 0;
349 CreateInfo.MainWindowProc = AnimationWinProc;
350 CreateInfo.lx = 0;
351 CreateInfo.ty = 0;
352 CreateInfo.rx = 640;
353 CreateInfo.by = 480;
354 CreateInfo.iBkColor = PIXEL_lightwhite;
355 CreateInfo.dwAddData = 0;
356 CreateInfo.dwReserved = 0;
357 CreateInfo.hHosting = HWND_DESKTOP;
359 #if 1
360 hMainWnd = CreateMainWindow (&CreateInfo);
361 if (hMainWnd == HWND_INVALID)
362 return -1;
364 ShowWindow (hMainWnd, SW_SHOWNORMAL);
366 while (GetMessage(&Msg, hMainWnd)) {
367 TranslateMessage(&Msg);
368 DispatchMessage(&Msg);
371 MainWindowThreadCleanup (hMainWnd);
372 MiniGUIExtCleanUp();
373 #endif
375 /* Enter user window directly */
376 /* before entering user window, at least these data structure should
377 * be loaded:
378 * - userinformation[] and the currentuser pointer is set correctly
379 * - the user limits should be send to machine
380 * - programCreate should be filled and parsed to user window
382 loadUserConfigFile(); /* fill userinformation[] */
383 currentid = startupParams.lastuser; /* get lastuser id */
384 currentuser = &userinformation[currentid];
386 sendUserAllLimits(); /* send user limits to machine */
388 PRINTF("current user %d current program %d \n",currentid,
389 currentuser->recent_programe);
391 /* load program file */
392 loadProgramFile(currentuser->recent_programe, &programCreate);
394 EnterUserProgram();
396 return 0;
399 #ifndef _LITE_VERSION
400 #include <minigui/dti.c>
401 #endif