Files @ 281ec15d1b40
Branch filter:

Location: therm/libraries/oleddrv/UserMenu.c - annotation

Ethan Zonca
Remove dox folder
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
1c5909c633fa
/*******************************************************************************
* File Name          : UserMenu.c
* Author             : lxyppc
* Version            : V1.0
* Date               : 10-03-05
* Description        : User menu implemention 
*******************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_lib.h"
#include "menu.h"
#include "SSD1303.h"
#include "Graphics\Graphics.h"
#include "time.h"
#include "icon.h"
#include "bsp.h"
#include "ClockUI.h"
#include "string.h"
#include "StringResource.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
//#define     OnMeasureBat      DefaultMenuFunc
//#define     OnMeasureGravity  DefaultMenuFunc
//#define     OnSetContrast     DefaultMenuFunc
//#define     OnSetTime         DefaultMenuFunc
//#define     OnLanguageSet     DefaultMenuFunc
/* Private variables ---------------------------------------------------------*/
extern  const MenuItem    MainMenu[];
extern  const MenuItem    SettingMenu[];
extern  const MenuItem    StatusMenu[];
extern  const MenuItem    LanguageMenu[];
const LPCSTR* curLang = StringTable_CHS;
Device    appDevice;
extern    ADResult_t  ADCResult;

#define   LoadString(STR_ID)      curLang[(STR_ID)]
// January is 0
#define   LoadStrMonth(Month)     LoadString(STR_JAN + (Month))
// Sunday is 0
#define   LoadStrWeek(WeekDay)    LoadString(STR_SUNDAY + (WeekDay))
// Year is 0, then month,date,hour and minute
#define   LoadStrTimeDesc(index)  LoadString(STR_YEAR + (index))

//LPCSTR* MonthText = MonthText_En;
//LPCSTR* WeekText = WeekText_En;
//LPCSTR * SetTimeDesc  = SetTimeDesc_En;
//LPCSTR* UserText = UserText_En;
//#define   UserText    (curLang)
//#define   MonthText   (curLang + STR_JAN)
//#define   WeekText    (curLang + STR_SUNDAY)
//#define   SetTimeDesc (curLang + STR_YEAR)

/* Private function prototypes -----------------------------------------------*/
MenuResult OnMenuAbout(void* p, Msg* msg);
MenuResult OnMenuTest(void* p, Msg* msg);
MenuResult DefaultMenuFunc(void* p, Msg* msg);
MenuResult OnMenuRoot(void* param, Msg* msg);
MenuResult OnSetContrast(void* param, Msg* msg);
MenuResult OnSetTime(void* param, Msg* msg);
MenuResult OnMeasureGravity(void* param, Msg* msg);
MenuResult OnMeasureBat(void* param, Msg* msg);
MenuResult OnLanguageSet(void* param, Msg* msg);

/* Private functions ---------------------------------------------------------*/

/******************************************************************************/
/************************** Menu definitions begin ****************************/
/* Root main */
const MenuItem    RootMenu = {
        /*Parent    child/pfn           index      cnt        resource        type*/
            0,      (void*)OnMenuRoot,         0,        1,        STR_ROOT,    MT_NULL
};
/* Main menu */
const MenuItem    MainMenu[] = {
      /*Parent    child/pfn           index      cnt        resource        type*/
  {         0,     StatusMenu,         0,        4,         STR_STATUS,     MT_SUB},
  {         0,     (void*)OnMenuTest,   1,        4,        STR_TEST,       MT_NULL},
  {         0,     SettingMenu,         2,        4,        STR_SETTING,    MT_SUB},
  {         0,     (void*)OnMenuAbout,  3,        4,        STR_ABOUT,      MT_NULL},
};

/* Setting menu */
const MenuItem    SettingMenu[] = {
      /*Parent    child/pfn                 index      cnt        resource        type*/
  {   MainMenu+2,   (void*)OnSetContrast,     0,        3,  STR_SETTING_CONTRAST,    MT_NULL},
  {   MainMenu+2,   (void*)OnSetTime,         1,        3,  STR_SETTING_TIME,        MT_NULL},
  {   MainMenu+2,   LanguageMenu,             2,        3,  STR_SETTING_LANGUAGE,    MT_SUB},
};
/* Language menu*/
const MenuItem    LanguageMenu[] = {
      /*Parent    child/pfn                 index      cnt        resource        type*/
  {SettingMenu+2,   (void*)OnLanguageSet,     0,        2,        STR_LANG_ENG,   MT_NULL},
  {SettingMenu+2,   (void*)OnLanguageSet,     1,        2,        STR_LANG_CHS,   MT_NULL},
};

/**/
const MenuItem    StatusMenu[] = {
  /*Parent          child/pfn               index      cnt        resource        type*/
  {   MainMenu+0,   (void*)OnMeasureBat,      0,        2,        STR_STATUS_BAT,     MT_NULL},
  {   MainMenu+0,   (void*)OnMeasureGravity,  1,        2,        STR_STATUS_GRAVITY, MT_NULL},
};
/************************** Menu definitions end   ****************************/
/******************************************************************************/

/*******************************************************************************
* Function Name  : OnMenuAbout
* Description    : Callback function for about menu
* Input          : void*  pointer of the menu item when message is MSG_INIT
* Output         : Msg*   pointer of the msg structure
* Return         : None
*******************************************************************************/
MenuResult OnMenuAbout(void* p, Msg* msg)
{
  if(msg->message == MSG_INIT){
    SetColor(BLACK);
    ClearDevice();
    TextOut(&appDevice, 0,  5,LoadString(STR_ABOUT_0), 0xFF);
    TextOut(&appDevice, 0, 25,LoadString(STR_ABOUT_1), 0xFF);
    TextOut(&appDevice, 0, 45,LoadString(STR_ABOUT_2), 0xFF);
    return MR_Continue;
  }else if(msg->message == MSG_KEY_UP){
    return MR_Finish;
  }
  return MR_Continue;
}

/*******************************************************************************
* Function Name  : OnMenuTest
* Description    : Callback function for test menu
* Input          : void*  pointer of the menu item when message is MSG_INIT
* Output         : Msg*   pointer of the msg structure
* Return         : None
*******************************************************************************/
MenuResult OnMenuTest(void* p, Msg* msg)
{
  if(msg->message == MSG_INIT){
    SetColor(BLACK);
    ClearDevice();
    TextOut(&appDevice, 0, 0, "Scroll Test", 0xFF);
    return MR_Continue;
  }else if(msg->message == MSG_KEY_UP){
    return MR_Finish;
  }else if(msg->message == MSG_SCROLL){
    s32 res = (s32)msg->param;
    char buf[3] = "+";
    if(res<0){
      res = -res;
      buf[0] = '-';
    }
    static Pos_t x = 0;
    static Pos_t y = 13;
    buf[1] = res%10+'0';
    buf[2] = ',';
    x = TextOut(&appDevice, x, y, buf, 3);
    if(x > 110){
      x = 0;
      y+=13;
    }
    if( y > 64-12){
      y = 13;
    }
    
  }
  return MR_Continue;
}

/*******************************************************************************
* Function Name  : DefaultMenuFunc
* Description    : Default menu callback function
* Input          : void*  pointer of the menu item when message is MSG_INIT
* Output         : Msg*   pointer of the msg structure
* Return         : None
*******************************************************************************/
MenuResult DefaultMenuFunc(void* p, Msg* msg)
{
  if(msg->message == MSG_INIT){
    SetColor(BLACK);
    ClearDevice();
    TextOut(&appDevice, 0, 25,curLang[STR_DEFAULT], 0xFF);
    return MR_Continue;
  }else if(msg->message == MSG_KEY_UP){
    return MR_Finish;
  }
  return MR_Continue;
}

/*******************************************************************************
* Function Name  : GetBatCap
* Description    : Get battery capacity
* Input          : None
* Output         : Battary capacity percentage
* Return         : None
* Details        : Battary fully charge is 4.2V, and lower power is 3.3V
                   Vref = 3.28V, resistive partial-pressure is 10K/13.6K
                   In 12 bit ADC, 4.2*10/13.6/3.28*4096 = 3875
                                  3.3*10/13.6/3.28*4096 = 3030
*******************************************************************************/
unsigned long  GetBatCap()
{
  u32 res = ADCResult.ADBat;
  if(res > 3835){
    res = 800;
  }else if(res < 3030){
    res = 0;
  }else{
    res -= 3030;
  }
  return 100*res/800;
}

/*******************************************************************************
* Function Name  : UpdateBatIcon
* Description    : Update the battery icon
* Input          : Pos_t  x position of the icon
*                  Pos_t  y position of the icon
*                  u32    battery capacity, in percentage
* Output         : Msg*   pointer of the msg structure
* Return         : None
*******************************************************************************/
void  UpdateBatIcon(Pos_t x, Pos_t y,u32 cap)
{
  if(IsCHG()){
    DrawIcon(x,y,ICON_ID_CHARGE);
  }else if(IsPGOOD()){
    DrawIcon(x,y,ICON_ID_POWER);
  }else{
    DrawIcon(x,y,ICON_ID_BATTARY);
    SetColor(BLACK);
    SetLineThickness(NORMAL_LINE);
    u32 res = cap*16/100;
    res += 106;
    Bar(res,2,121,7);
  }
}

/*******************************************************************************
* Function Name  : OnMenuRoot
* Description    : Root menu callback function
* Input          : void*  pointer of the menu item when message is MSG_INIT
* Output         : Msg*   pointer of the msg structure
* Return         : None
*******************************************************************************/
MenuResult OnMenuRoot(void* param, Msg* msg)
{
  static  u8  deviceClose = 0;
  switch(msg->message){
  case MSG_INIT:
    SetColor(BLACK);
    ClearDevice();
    Clock_DrawFace(GetMaxY()>>1,GetMaxY()>>1,GetMaxY()>>1);
    deviceClose = 0;
  case MSG_SECOND:
    {
    Pos_t x = 70;
    Pos_t y = 35;
    time_t now = (time_t)RTC_GetCounter();
    struct tm* curTm = localtime(&now);
    Clock_UpdateTime(curTm->tm_hour,curTm->tm_min,curTm->tm_sec);
    u32 year = curTm->tm_year + 1900;
    char tBuf[8] = {year/1000+'0',year/100%10+'0',year/10%10+'0',year%10+'0'};
    x = TextOut(&appDevice,x,y,tBuf,4);
    x = TextOut(&appDevice,x,y," ",1);
    x = TextOut(&appDevice,x,y,LoadStrWeek(curTm->tm_wday),3);
    x = 70;
    y += 13;
    x = TextOut(&appDevice,x,y,LoadStrMonth(curTm->tm_mon),0xFF);
    x = TextOut(&appDevice,x,y," ",1);
    tBuf[0] = curTm->tm_mday/10+'0';
    tBuf[1] = curTm->tm_mday%10+'0';
    x = TextOut(&appDevice,x,y,tBuf,2);
    x = TextOut(&appDevice,x,y," ",1);
    x = TextOut(&appDevice,70,y-26,LoadString(curTm->tm_hour>12 ? STR_PM : STR_AM),2);

#ifdef    DEBUG_BOARD
      {
        DrawIcon(104,0,ICON_ID_BATTARY);
        static u32 res1 = 16;
        if(res1)res1--;
        else res1=16;
        u32 res = res1 + 106;
        SetColor(BLACK);
        SetLineThickness(NORMAL_LINE);
        Bar(res,2,121,7);
      }
#else
      UpdateBatIcon(104,0,GetBatCap());
#endif
    }
    break;
  case MSG_KEY_UP:
    if((u32)msg->param >  SSD1303_FPS*2){
      deviceClose = 1;
      SSD1303_TurnOff();
      MMA_SLEEP();
    }else if(deviceClose){
      deviceClose = 0;
      SSD1303_TurnOn();
      MMA_WAKEUP();
    }else{
      PopupMenu(MainMenu);
      return MR_Finish;
    }
  }
  return MR_Continue;
}

/*******************************************************************************
* Function Name  : OnSetContrast
* Description    : Set contrast menu callback function
* Input          : void*  pointer of the menu item when message is MSG_INIT
* Output         : Msg*   pointer of the msg structure
* Return         : None
*******************************************************************************/
MenuResult OnSetContrast(void* param, Msg* msg)
{
  if(msg->message == MSG_INIT){
    SetColor(BLACK);
    ClearDevice();
    TextOut(&appDevice,0,0,LoadString(STR_CHANGE_CONTRAST),0xFF);
    u32 con = SSD1303_GetContrast();
    char buf[] = {con/100+'0',(con/10)%10+'0',con%10+'0'};
    TextOut(&appDevice,45,18,buf,3);
    SetLineThickness(NORMAL_LINE);
    SetColor(WHITE);
    Rectangle(0,30,127,45);
    con = 123*con/256 + 2;
    Bar(2,32,con,43);
  }else if(msg->message == MSG_SCROLL){
    s16 enc = (s16)((s32)msg->param);
    u32 con = SSD1303_GetContrast();
    if(enc>0){
      if(con<255)con++;
    }else if(con){
      con--;
    }
    char buf[] = {con/100+'0',(con/10)%10+'0',con%10+'0'};
    TextOut(&appDevice,45,18,buf,3);
    SSD1303_SetContrast(con);
    SetColor(BLACK);
    Bar(2,32,125,43);
    SetColor(WHITE);
    con = 123*con/256 + 2;
    Bar(2,32,con,43);
  }else if(msg->message == MSG_KEY_UP){
    return MR_Finish;
  }
  return MR_Continue;
}

typedef unsigned long(*pfnTextOut)(Device*,Pos_t,Pos_t,const char*,Size_t);
#define   DrawOK()            UpdateTimeText(TextOut,0, TI_OK)
#define   DrawCancel()        UpdateTimeText(TextOut,0, TI_Cancel)
#define   HighLightOK()       UpdateTimeText(TextOut_HighLight,0, TI_OK)
#define   HighLightCancel()   UpdateTimeText(TextOut_HighLight,0, TI_Cancel)
typedef   enum{
  TI_All = 0,
  TI_Year = 1,
  TI_First = 1,
  TI_Month = 2,
  TI_Date = 3,
  TI_Hour = 4,
  TI_Minute = 5,
  TI_OK = 6,
  TI_Cancel = 7,
  TI_Last = 8,
  TI_Unknown = 0xFF,
}TimeIndex;
/*******************************************************************************
* Function Name  : UpdateTimeText
* Description    : Update texts when set time
* Input          : pfnTextOut   pointer of textOut Function
*                  struct tm*   time structure to display
*                  TimeIndex    time item index
* Output         : Msg*   pointer of the msg structure
* Return         : None
*******************************************************************************/
void  UpdateTimeText(pfnTextOut textOut,struct tm* pTM, TimeIndex timeIndex)
{
  u8 wid = GetFontTextWidth(appDevice.pfnFont,' ');
  Pos_t yPos = 13;
  switch(timeIndex){
  case TI_All:// Inital
    textOut(&appDevice,2+16*wid,yPos,":",1);
  case TI_Year:// year
    {
    u32 year = pTM->tm_year + 1900;
    char buf[] = {year/1000+'0',year/100%10+'0',year/10%10+'0',year%10+'0'};
    textOut(&appDevice,2,yPos,buf,4);
    }
    if(timeIndex!=TI_All)break;
  case TI_Month:// Month
    textOut(&appDevice,2+5*wid,yPos,LoadStrMonth(pTM->tm_mon),3);
    if(timeIndex!=TI_All)break;
  case TI_Date:// Date
    {
    char buf[] = {pTM->tm_mday/10+'0',pTM->tm_mday%10+'0'};
    textOut(&appDevice,2+10*wid,yPos,buf,2);
    }
    if(timeIndex!=TI_All)break;
  case TI_Hour:// Hour
    {
    char buf[] = {pTM->tm_hour/10+'0',pTM->tm_hour%10+'0'};
    textOut(&appDevice,2+14*wid,yPos,buf,2);
    }
    if(timeIndex!=TI_All)break;
  case TI_Minute:// Minute
    {
    char buf[] = {pTM->tm_min/10+'0',pTM->tm_min%10+'0'};
    textOut(&appDevice,2+17*wid,yPos,buf,2);
    }
    if(timeIndex!=TI_All)break;
  case TI_OK:// OK
    textOut(&appDevice,2+1*wid,52,LoadString(STR_OK),2);
    if(timeIndex!=TI_All)break;
  case TI_Cancel:// cancel
    textOut(&appDevice,126-7*wid,52,LoadString(STR_CANCEL),6);
    if(timeIndex!=TI_All)break;
  default:
    break;
  }
}

#define   Round(param,low,high)   \
  if((param)<(low)){(param)=(high);}else if((param)>(high)){(param)=(low);}
/*******************************************************************************
* Function Name  : ModifyTime
* Description    : Set contrast menu callback function
* Input          : int          Current encoder roll dirction
*                  struct tm*   time structure to be modified
*                  TimeIndex    time item index          
* Output         : Msg*   pointer of the msg structure
* Return         : None
*******************************************************************************/
void  ModifyTime(int enc, struct tm* pTM, TimeIndex timeIndex)
{
      switch(timeIndex){
      case TI_Year:
        pTM->tm_year += enc;
        Round(pTM->tm_year,-130,130);
        {
          u32 year = pTM->tm_year + 1900;
          char buf[] = {year/1000+'0',year/100%10+'0',year/10%10+'0',year%10+'0'};
          TextOut_HighLight(&appDevice,64,30,buf,4);
        }
        break;
      case TI_Month:
        pTM->tm_mon += enc;
        Round(pTM->tm_mon,0,11);
        TextOut_HighLight(&appDevice,64,30,LoadStrMonth(pTM->tm_mon),3);
        break;
      case TI_Date:
        pTM->tm_mday += enc;
        Round(pTM->tm_mday,1,31);
        {
          char buf[] = {pTM->tm_mday/10+'0',pTM->tm_mday%10+'0'};
          TextOut_HighLight(&appDevice,64,30,buf,2);
        }
        break;
      case TI_Hour:
        pTM->tm_hour += enc;
        Round(pTM->tm_hour,0,23);
        {
          char buf[] = {pTM->tm_hour/10+'0',pTM->tm_hour%10+'0'};
          TextOut_HighLight(&appDevice,64,30,buf,2);
        }
        break;
      case TI_Minute:
        pTM->tm_min += enc;
        Round(pTM->tm_min,0,59);
        {
          char buf[] = {pTM->tm_min/10+'0',pTM->tm_min%10+'0'};
          TextOut_HighLight(&appDevice,64,30,buf,2);
        }
        break;
      default:
        break;
      }
}

/*******************************************************************************
* Function Name  : OnSetTime
* Description    : Set time menu callback function
* Input          : void*  pointer of the menu item when message is MSG_INIT
* Output         : Msg*   pointer of the msg structure
* Return         : None
*******************************************************************************/
MenuResult OnSetTime(void* param, Msg* msg)
{
  static  struct tm oldTm;
  static TimeIndex position;
  static TimeIndex lastPosition;
  static u8 modifyMode;
  if(msg->message == MSG_INIT){
    time_t now = (time_t)RTC_GetCounter();
    struct tm* ptm = localtime(&now);
    memcpy(&oldTm,ptm,sizeof(oldTm));
    lastPosition = TI_Unknown;
    position = TI_All;
    SetColor(BLACK);
    ClearDevice();
    TextOut(&appDevice,0,0,LoadString(STR_SET_TIME),0xFF);
    UpdateTimeText(TextOut,&oldTm,position);
    position++;
    UpdateTimeText(TextOut_HighLight,&oldTm,position);
    lastPosition = position;
    modifyMode = 0;
  }else if(msg->message == MSG_KEY_UP){
    if(position == TI_Cancel){
      return MR_Finish;
    }else if(position == TI_OK){
      oldTm.tm_sec = 0;
      RTC_SetCounter(mktime(&oldTm));
      RTC_WaitForLastTask();
      return MR_Finish;
    }else if(position<7 && position>0){
      if(modifyMode){
        modifyMode = 0;
        SetColor(BLACK);
        Bar(10,30,100,50);
        UpdateTimeText(TextOut_HighLight,&oldTm,position);
      }else{
        modifyMode = 1;
        TextOut(&appDevice,20,30,LoadStrTimeDesc(position-1),0xFF);
        ModifyTime(0,&oldTm,position);
      }
    }
  }else if(msg->message == MSG_SCROLL){
    int enc = (int)msg->param;
    if(enc>0)enc=1;
    else enc = -1;
    if(modifyMode){
      ModifyTime(enc,&oldTm,position);
    }else{
      position+=enc;
      if(position>=TI_Last){
        position = TI_Year;
      }else if(position<TI_First){
        position = TI_Cancel;
      }
      UpdateTimeText(TextOut_HighLight,&oldTm,position);
      UpdateTimeText(TextOut,&oldTm,lastPosition);
      lastPosition = position;
    }
  }
  return MR_Continue;
}

/*******************************************************************************
* Function Name  : OnMeasureGravity
* Description    : Measure gravity menu callback function
* Input          : void*  pointer of the menu item when message is MSG_INIT
* Output         : Msg*   pointer of the msg structure
* Return         : None
*******************************************************************************/
MenuResult OnMeasureGravity(void* param, Msg* msg)
{
  if(msg->message == MSG_INIT){
    SetColor(BLACK);
    ClearDevice();
    SetColor(WHITE);
    SetLineThickness(NORMAL_LINE);
    Rectangle(64,0,127,63);
    Line(0,63,63,63);
    Line(63,63,48,40);
    Line(16,40,48,40);
    Line(16,40,0,63);
    MMA_WAKEUP();
  }else if(msg->message == MSG_KEY_UP){
    MMA_SLEEP();
    return MR_Finish;
  }else if(msg->message == MSG_GRAV_XY){
    static u16 lastX = 64+64*2;
    static u16 lastY = 64/2;
    u16 gX = 4096-GetGravX(msg);
    u16 gY = 4096-GetGravY(msg);
    gX = gX*64 / 4096+64;
    gY = gY*64 / 4096;
    SetColor(BLACK);
    FillCircle(lastX,lastY,2);
    SetColor(WHITE);
    FillCircle(gX,gY,2);
    lastX = gX;
    lastY = gY;
  }else if(msg->message == MSG_GRAV_Z){
    static u16 lastZ = 50/2;
    u16 gZ = GetGravZ(msg);
    gZ = gZ*50 / 4096;
    SetColor(BLACK);
    Circle(32,lastZ,2);
    SetColor(WHITE);
    Circle(32,gZ,2);
    lastZ = gZ;
  }
  return MR_Continue;
}

/*******************************************************************************
* Function Name  : OnMeasureBat
* Description    : Measure battery menu callback function
* Input          : void*  pointer of the menu item when message is MSG_INIT
* Output         : Msg*   pointer of the msg structure
* Return         : None
*******************************************************************************/
MenuResult OnMeasureBat(void* param, Msg* msg)
{
  switch(msg->message){
  case MSG_INIT:
    SetColor(BLACK);
    ClearDevice();
    TextOut(&appDevice,0,0,LoadString(STR_VIEW_BATTARY),0xFF);
  case MSG_SECOND:
    if(IsCHG()){
      TextOut(&appDevice,10,16,LoadString(STR_BAT_CHARGE),0xFF);
    }else if(IsPGOOD()){
      TextOut(&appDevice,10,16,LoadString(STR_BAT_FULL),0xFF);
    }else{
      TextOut(&appDevice,10,16,LoadString(STR_BAT_NORMAL),0xFF);
      //DrawIcon(104,0,ICON_ID_BATTARY);
    }
    {
      u32 res = GetBatCap();
      UpdateBatIcon(104,0,res);
      char buf[4] = {res/100 + '0',res/10%10+'0',res%10+'0','%'};
      Pos_t x = TextOut(&appDevice,10,32,LoadString(STR_BAT_CAPACITY),0xFF);
      x = TextOut(&appDevice,x,32,buf,4);
      res = ADCResult.ADBat;
      res = ((res*136*120) / ADCResult.ADRef) / 100;
      
      //res = res * 328 * 136 / (4096*100);
      char ADBuf[] = {(res/100)%10+'0', '.', 
      (res/10)%10+'0', res%10+'0','V', 0};
      x = TextOut(&appDevice,10,48,LoadString(STR_BAT_VOLTAGE),0xFF);
      TextOut(&appDevice,x,48,ADBuf,5);
    }
    break;
  case MSG_KEY_UP:
    return MR_Finish;
  default:
    break;
  }
  return MR_Continue;
}

/*******************************************************************************
* Function Name  : OnLanguageSet
* Description    : Set language menu callback function
* Input          : void*  pointer of the menu item when message is MSG_INIT
* Output         : Msg*   pointer of the msg structure
* Return         : None
*******************************************************************************/
MenuResult OnLanguageSet(void* param, Msg* msg)
{
  static const LPCSTR* preLang = NULL;
  static  u8 curSel = 0;
  switch(msg->message){
  case MSG_INIT:
  {
    SetColor(BLACK);
    ClearDevice();
    switch(((const MenuItem*)param)->res){
    case STR_LANG_ENG:
      preLang = StringTable_ENG;
      TextOut(&appDevice,30,20,preLang[STR_LANG_ENG],0xFF);
      break;
    case STR_LANG_CHS:
    default:
      preLang = StringTable_CHS;
      TextOut(&appDevice,30,20,preLang[STR_LANG_CHS],0xFF);
      break;
    }
    curSel = 1;
  }
  case MSG_SCROLL:
    {
      curSel = !curSel;
      const LPCSTR* tmp = curLang;
      curLang = preLang;
      if(curSel){
        HighLightOK();
        DrawCancel();
      }else{
        DrawOK();
        HighLightCancel();
      }
      curLang = tmp;
    }
    break;
  case MSG_KEY_UP:
    if(curSel){
      curLang = preLang;
    }
    return MR_Finish;
  }
  
  return MR_Continue;
}