Spin.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /********************************************************************************
  2. * File Name: Spin.c
  3. * Function Describe:
  4. * Relate Module: lcd.c,lcd.h,Key.c,Key.h
  5. * Explain:
  6. * Writer: LiuYanZhi
  7. * Date: 2003-11-7
  8. * Rewriter:
  9. * Date:
  10. *******************************************************************************/
  11. #include <string.h>
  12. #include <stdio.h>
  13. #include "lcd.h"
  14. #include "KeyLed.h"
  15. #include "Spin.h"
  16. /********************************************************************************
  17. * Function:SpinInit
  18. * initialize spin struct
  19. *******************************************************************************/
  20. void SpinInit
  21. (
  22. struct SUT_SPIN *p,
  23. unsigned short x,
  24. unsigned char y,
  25. char **item
  26. )
  27. {
  28. unsigned char i;
  29. p->x = x;
  30. p->y = y;
  31. p->item = item;
  32. p->itemnum = 0;
  33. i = 0;
  34. p->width = 0;
  35. for(;;)
  36. {
  37. if(strlen(p->item[i]) == 0)break;
  38. if(strlen(p->item[i]) > p->width)
  39. p->width = strlen(p->item[i]);
  40. i++;
  41. p->itemnum ++;
  42. }
  43. p->top = 0;
  44. p->bottom = p->itemnum - 1;
  45. if(p->handle < p->top)p->handle = p->top;
  46. if(p->handle > p->bottom)p->handle = p->bottom;
  47. p->focus = 0;
  48. p->disable = 0;
  49. p->keyflag = 0;
  50. p->type = 0;
  51. p->step = 1;
  52. }
  53. /********************************************************************************
  54. * Function:SpinInitData
  55. * initialize spin struct to data mode
  56. *******************************************************************************/
  57. void SpinInitData
  58. (
  59. struct SUT_SPIN *p,
  60. unsigned short x,
  61. unsigned char y,
  62. unsigned char width,
  63. short min,
  64. short max,
  65. short step,
  66. char *unit
  67. )
  68. {
  69. p->x = x;
  70. p->y = y;
  71. p->width = width;
  72. p->top = min;
  73. p->bottom = max;
  74. p->unit = unit;
  75. p->step = step;
  76. if(p->handle < p->top || p->handle > p->bottom)
  77. p->handle = p->top;
  78. p->focus = 0;
  79. p->disable = 0;
  80. p->keyflag = 0;
  81. p->type = 1;
  82. }
  83. /********************************************************************************
  84. * Function:SpinSetPage
  85. *******************************************************************************/
  86. void SpinSetRange(struct SUT_SPIN *p,short top,short bottom)
  87. {
  88. p->top = top;
  89. p->bottom = bottom;
  90. if(p->type == 0)
  91. {
  92. if(p->top >= p->itemnum)p->top = p->itemnum - 1;
  93. if(p->bottom >= p->itemnum)p->bottom = p->itemnum - 1;
  94. if(p->bottom < p->top)p->bottom = p->top;
  95. }
  96. if(p->handle < p->top)p->handle = p->top;
  97. if(p->handle > p->bottom)p->handle = p->bottom;
  98. }
  99. /********************************************************************************
  100. * Function:SpinSetFocus
  101. *******************************************************************************/
  102. void SpinSetFocus(struct SUT_SPIN *p)
  103. {
  104. if(p->disable)return;
  105. if(p->focus)return;
  106. p->focus = 1;
  107. LcdReverseBar(p->x,p->y-1,p->x+(p->width-1)*8,p->y+16);
  108. }
  109. /********************************************************************************
  110. * Function:SpinLoseFocus
  111. *******************************************************************************/
  112. void SpinLoseFocus(struct SUT_SPIN *p)
  113. {
  114. if(p->disable)return;
  115. if(!p->focus)return;
  116. p->focus = 0;
  117. LcdReverseBar(p->x,p->y-1,p->x+(p->width-1)*8,p->y+16);
  118. }
  119. /********************************************************************************
  120. * Function:SpinShow
  121. * show spin
  122. *******************************************************************************/
  123. void SpinShow(struct SUT_SPIN *p)
  124. {
  125. unsigned char x;
  126. char buffer[20];
  127. LcdClearArea(p->x,p->y,p->x+(p->width-1)*8,p->y+16);
  128. if(p->type == 0)
  129. {
  130. if(p->disable)LcdShowStr(p->x,p->y,p->item[p->handle],0x03);
  131. else LcdShowStr(p->x,p->y,p->item[p->handle],0x01);
  132. if(p->disable)return;
  133. }
  134. else
  135. {
  136. sprintf(buffer,"%d",p->handle);
  137. strcat(buffer,p->unit);
  138. x = strlen(buffer);
  139. if(x > p->width)
  140. {
  141. x = p->width;
  142. buffer[x] = 0;
  143. }
  144. x = (p->width - x) / 2;
  145. LcdShowStr(p->x+x*8,p->y,buffer,0x00);
  146. }
  147. if(p->focus)LcdReverseBar(p->x,p->y-1,p->x+(p->width-1)*8,p->y+16);
  148. }
  149. /********************************************************************************
  150. * Function:SpinShowBorder
  151. *******************************************************************************/
  152. void SpinShowBorder(struct SUT_SPIN *p)
  153. {
  154. unsigned short x;
  155. unsigned char width;
  156. x = (p->x >> 3) * 8;
  157. width = p->width * 8;
  158. LcdShowRect(x-1,p->y-2,x+width,p->y+17,0x01);
  159. }
  160. /********************************************************************************
  161. * Function:SpinGetHandle
  162. * get spin's handle
  163. *******************************************************************************/
  164. short SpinGetHandle(struct SUT_SPIN *p)
  165. {
  166. return p->handle;
  167. }
  168. /********************************************************************************
  169. * Function:SpinResponse
  170. * spin response
  171. *******************************************************************************/
  172. unsigned char SpinResponse(struct SUT_SPIN *p)
  173. {
  174. unsigned char task;
  175. if(p->disable)return 0;
  176. if(!p->focus)return 0;
  177. task = 0xFF;
  178. switch(g_ucKeyValue)
  179. {
  180. case KEY_UP:
  181. if(p->keyflag)task = 2;
  182. else task = 0;
  183. break;
  184. case KEY_DOWN:
  185. if(p->keyflag)task = 3;
  186. else task = 1;
  187. break;
  188. case KEY_LEFT:
  189. if(p->keyflag)task = 0;
  190. else task = 2;
  191. break;
  192. case KEY_RIGHT:
  193. if(p->keyflag)task = 1;
  194. else task = 3;
  195. break;
  196. case KEY_BACK:
  197. return 1;
  198. case KEY_ENTER:
  199. return 2;
  200. default:
  201. break;
  202. }
  203. switch(task)
  204. {
  205. case 0:
  206. LcdReverseBar(p->x,p->y-1,p->x+(p->width-1)*8,p->y+16);
  207. if((p->handle - p->step) < p->top)p->handle = p->bottom;
  208. else p->handle -= p->step;
  209. SpinShow(p);
  210. break;
  211. case 1:
  212. LcdReverseBar(p->x,p->y-1,p->x+(p->width-1)*8,p->y+16);
  213. if((p->handle + p->step) > p->bottom)p->handle = p->top;
  214. else p->handle += p->step;
  215. SpinShow(p);
  216. break;
  217. case 2:
  218. return 3;
  219. // break;
  220. case 3:
  221. return 4;
  222. //break;
  223. }
  224. return 0;
  225. }
  226. void SpinSetNext(struct SUT_SPIN *p)
  227. {
  228. LcdReverseBar(p->x,p->y-1,p->x+(p->width-1)*8,p->y+16);
  229. if(p->handle >= p->bottom)p->handle = p->top;
  230. else p->handle += p->step;
  231. SpinShow(p);
  232. }
  233. /********************************************************************************
  234. * End of focus
  235. *******************************************************************************/