| Line No. | Rev | Author | Line |
|---|---|---|---|
| 1 | 32 | kaklik | /***************************************************************************** |
| 2 | * Module for Microchip Graphics Library |
||
| 3 | * GOL Layer |
||
| 4 | * DigitalMeter |
||
| 5 | ***************************************************************************** |
||
| 6 | * FileName: DigitalMeter.c |
||
| 7 | * Dependencies: None |
||
| 8 | * Processor: PIC24F, PIC24H, dsPIC, PIC32 |
||
| 9 | * Compiler: MPLAB C30 V3.00, MPLAB C32 |
||
| 10 | * Linker: MPLAB LINK30, MPLAB LINK32 |
||
| 11 | * Company: Microchip Technology Incorporated |
||
| 12 | * |
||
| 13 | * Software License Agreement |
||
| 14 | * |
||
| 15 | * Copyright © 2008 Microchip Technology Inc. All rights reserved. |
||
| 16 | * Microchip licenses to you the right to use, modify, copy and distribute |
||
| 17 | * Software only when embedded on a Microchip microcontroller or digital |
||
| 18 | * signal controller, which is integrated into your product or third party |
||
| 19 | * product (pursuant to the sublicense terms in the accompanying license |
||
| 20 | * agreement). |
||
| 21 | * |
||
| 22 | * You should refer to the license agreement accompanying this Software |
||
| 23 | * for additional information regarding your rights and obligations. |
||
| 24 | * |
||
| 25 | * SOFTWARE AND DOCUMENTATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY |
||
| 26 | * KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY |
||
| 27 | * OF MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR |
||
| 28 | * PURPOSE. IN NO EVENT SHALL MICROCHIP OR ITS LICENSORS BE LIABLE OR |
||
| 29 | * OBLIGATED UNDER CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, |
||
| 30 | * BREACH OF WARRANTY, OR OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT |
||
| 31 | * DAMAGES OR EXPENSES INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, |
||
| 32 | * INDIRECT, PUNITIVE OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, |
||
| 33 | * COST OF PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY |
||
| 34 | * CLAIMS BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), |
||
| 35 | * OR OTHER SIMILAR COSTS. |
||
| 36 | * |
||
| 37 | * Author Date Comment |
||
| 38 | *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||
| 39 | * Arpan kumar 06/11/09 Version 1.0 release |
||
| 40 | * PAT 01/18/10 Added draw state to redraw only text. |
||
| 41 | ******************************************************************************/ |
||
| 42 | #include "Graphics\Graphics.h" |
||
| 43 | |||
| 44 | #ifdef USE_DIGITALMETER |
||
| 45 | |||
| 46 | /************************************************************************************ |
||
| 47 | * Function: NumberToString(DWORD Value,XCHAR *pText, BYTE NoOfDigits, BYTE DotPos ) |
||
| 48 | * |
||
| 49 | * Notes: convert the number to string |
||
| 50 | * |
||
| 51 | *************************************************************************************/ |
||
| 52 | static void NumberToString(DWORD Value, XCHAR *pText, BYTE NoOfDigits, BYTE DotPos) |
||
| 53 | { |
||
| 54 | BYTE i; |
||
| 55 | BYTE bIndex; |
||
| 56 | |||
| 57 | for(bIndex = 0; bIndex < NoOfDigits; bIndex++) |
||
| 58 | { |
||
| 59 | pText[NoOfDigits - bIndex - 1] = '0' + (Value % 10); |
||
| 60 | Value /= 10; |
||
| 61 | } |
||
| 62 | |||
| 63 | if(DotPos != 0 && DotPos <= NoOfDigits) |
||
| 64 | { |
||
| 65 | for(i = 0; i < DotPos; i++) |
||
| 66 | { |
||
| 67 | pText[NoOfDigits - i] = pText[NoOfDigits - 1 - i]; |
||
| 68 | } |
||
| 69 | |||
| 70 | pText[NoOfDigits - DotPos] = '.'; |
||
| 71 | pText[NoOfDigits + 1] = '\0'; |
||
| 72 | } |
||
| 73 | |||
| 74 | //If dot position is 0 or greater than number of digits, then don't put dot in the display |
||
| 75 | else |
||
| 76 | { |
||
| 77 | pText[NoOfDigits] = '\0'; |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | /********************************************************************************************************* |
||
| 82 | * Function: DIGITALMETER *DmCreate(WORD ID, SHORT left, SHORT top, SHORT right, SHORT bottom, WORD state, |
||
| 83 | * DWORD Value, BYTE NoOfDigits, BYTE DotPos, GOL_SCHEME *pScheme) |
||
| 84 | * |
||
| 85 | * Notes: Creates a DIGITALMETER object and adds it to the current active list. |
||
| 86 | * If the creation is successful, the pointer to the created Object |
||
| 87 | * is returned. If not successful, NULL is returned. |
||
| 88 | * |
||
| 89 | **********************************************************************************************************/ |
||
| 90 | DIGITALMETER *DmCreate |
||
| 91 | ( |
||
| 92 | WORD ID, |
||
| 93 | SHORT left, |
||
| 94 | SHORT top, |
||
| 95 | SHORT right, |
||
| 96 | SHORT bottom, |
||
| 97 | WORD state, |
||
| 98 | DWORD Value, |
||
| 99 | BYTE NoOfDigits, |
||
| 100 | BYTE DotPos, |
||
| 101 | GOL_SCHEME *pScheme |
||
| 102 | ) |
||
| 103 | { |
||
| 104 | DIGITALMETER *pDm = NULL; |
||
| 105 | pDm = GFX_malloc(sizeof(DIGITALMETER)); |
||
| 106 | if(pDm == NULL) |
||
| 107 | return (pDm); |
||
| 108 | |||
| 109 | pDm->hdr.ID = ID; // unique id assigned for referencing |
||
| 110 | pDm->hdr.pNxtObj = NULL; // initialize pointer to NULL |
||
| 111 | pDm->hdr.type = OBJ_DIGITALMETER; // set object type |
||
| 112 | pDm->hdr.left = left; // left,top corner |
||
| 113 | pDm->hdr.top = top; |
||
| 114 | pDm->hdr.right = right; // right buttom corner |
||
| 115 | pDm->hdr.bottom = bottom; |
||
| 116 | pDm->Cvalue = Value; // initial value to be displayed |
||
| 117 | pDm->hdr.state = state; |
||
| 118 | pDm->NoOfDigits = NoOfDigits; // number of digits to be displayed |
||
| 119 | pDm->DotPos = DotPos; // position of decimal point |
||
| 120 | |||
| 121 | // Set the color scheme to be used |
||
| 122 | if(pScheme == NULL) |
||
| 123 | pDm->hdr.pGolScheme = _pDefaultGolScheme; |
||
| 124 | else |
||
| 125 | pDm->hdr.pGolScheme = (GOL_SCHEME *)pScheme; |
||
| 126 | |||
| 127 | pDm->textHeight = 0; |
||
| 128 | if(pDm->Cvalue != 0) |
||
| 129 | { |
||
| 130 | |||
| 131 | // Set the text height |
||
| 132 | pDm->textHeight = GetTextHeight(pDm->hdr.pGolScheme->pFont); |
||
| 133 | } |
||
| 134 | |||
| 135 | GOLAddObject((OBJ_HEADER *)pDm); |
||
| 136 | |||
| 137 | return (pDm); |
||
| 138 | } |
||
| 139 | |||
| 140 | /********************************************************************* |
||
| 141 | * Function: DmSetValue(DIGITALMETER *pDm, DWORD Value) |
||
| 142 | * |
||
| 143 | * Notes: Sets the value to be displayed. |
||
| 144 | * |
||
| 145 | ********************************************************************/ |
||
| 146 | void DmSetValue(DIGITALMETER *pDm, DWORD Value) |
||
| 147 | { |
||
| 148 | |||
| 149 | // store the previous and current value to be displayed |
||
| 150 | pDm->Pvalue = pDm->Cvalue; |
||
| 151 | pDm->Cvalue = Value; |
||
| 152 | |||
| 153 | pDm->textHeight = GetTextHeight(pDm->hdr.pGolScheme->pFont); |
||
| 154 | } |
||
| 155 | |||
| 156 | /********************************************************************* |
||
| 157 | * Function: WORD DmTranslateMsg(DIGITALMETER *pDm, GOL_MSG *pMsg) |
||
| 158 | * |
||
| 159 | * Notes: Evaluates the message if the object will be affected by the |
||
| 160 | * message or not. |
||
| 161 | * |
||
| 162 | **********************************************************************/ |
||
| 163 | WORD DmTranslateMsg(DIGITALMETER *pDm, GOL_MSG *pMsg) |
||
| 164 | { |
||
| 165 | |||
| 166 | // Evaluate if the message is for the static text |
||
| 167 | // Check if disabled first |
||
| 168 | if(GetState(pDm, DM_DISABLED)) |
||
| 169 | return (OBJ_MSG_INVALID); |
||
| 170 | |||
| 171 | #ifdef USE_TOUCHSCREEN |
||
| 172 | if(pMsg->type == TYPE_TOUCHSCREEN) |
||
| 173 | { |
||
| 174 | |||
| 175 | // Check if it falls in static text control borders |
||
| 176 | if |
||
| 177 | ( |
||
| 178 | (pDm->hdr.left < pMsg->param1) && |
||
| 179 | (pDm->hdr.right > pMsg->param1) && |
||
| 180 | (pDm->hdr.top < pMsg->param2) && |
||
| 181 | (pDm->hdr.bottom > pMsg->param2) |
||
| 182 | ) |
||
| 183 | { |
||
| 184 | return (DM_MSG_SELECTED); |
||
| 185 | } |
||
| 186 | } |
||
| 187 | |||
| 188 | #endif |
||
| 189 | return (OBJ_MSG_INVALID); |
||
| 190 | } |
||
| 191 | |||
| 192 | /********************************************************************* |
||
| 193 | * Function: WORD DmDraw(DIGITALMETER *pDm) |
||
| 194 | * |
||
| 195 | * Notes: This is the state machine to display the changing numbers. |
||
| 196 | * |
||
| 197 | **********************************************************************/ |
||
| 198 | WORD DmDraw(DIGITALMETER *pDigMeter) |
||
| 199 | { |
||
| 200 | typedef enum |
||
| 201 | { |
||
| 202 | DM_STATE_IDLE, |
||
| 203 | DM_STATE_FRAME, |
||
| 204 | DM_STATE_INIT, |
||
| 205 | DM_STATE_SETALIGN, |
||
| 206 | DM_STATE_DRAWTEXT |
||
| 207 | } DM_DRAW_STATES; |
||
| 208 | |||
| 209 | static DIGITALMETER *pDm = NULL; |
||
| 210 | static DM_DRAW_STATES state = DM_STATE_IDLE; |
||
| 211 | static SHORT charCtr = 0, lineCtr = 0; |
||
| 212 | static XCHAR CurValue[DM_WIDTH], PreValue[DM_WIDTH]; |
||
| 213 | SHORT textWidth = 0; |
||
| 214 | XCHAR ch = 0, pch = 0; |
||
| 215 | |||
| 216 | pDm = pDigMeter; |
||
| 217 | |||
| 218 | if(IsDeviceBusy()) |
||
| 219 | return (0); |
||
| 220 | |||
| 221 | switch(state) |
||
| 222 | { |
||
| 223 | case DM_STATE_IDLE: |
||
| 224 | SetClip(CLIP_DISABLE); |
||
| 225 | |||
| 226 | if(GetState(pDm, DM_HIDE) || GetState(pDm, DM_DRAW)) |
||
| 227 | { |
||
| 228 | SetColor(pDm->hdr.pGolScheme->CommonBkColor); |
||
| 229 | if(Bar(pDm->hdr.left, pDm->hdr.top, pDm->hdr.right, pDm->hdr.bottom) == 0) |
||
| 230 | return (0); |
||
| 231 | } |
||
| 232 | // if the draw state was to hide then state is still IDLE STATE so no need to change state |
||
| 233 | if (GetState(pDm, DM_HIDE)) |
||
| 234 | return (1); |
||
| 235 | state = DM_STATE_FRAME; |
||
| 236 | |||
| 237 | case DM_STATE_FRAME: |
||
| 238 | if(GetState(pDm, DM_DRAW | DM_FRAME) == (DM_DRAW | DM_FRAME)) |
||
| 239 | { |
||
| 240 | // show frame if specified to be shown |
||
| 241 | SetLineType(SOLID_LINE); |
||
| 242 | SetLineThickness(NORMAL_LINE); |
||
| 243 | if(!GetState(pDm, DM_DISABLED)) |
||
| 244 | { |
||
| 245 | |||
| 246 | // show enabled color |
||
| 247 | SetColor(pDm->hdr.pGolScheme->Color1); |
||
| 248 | if(Rectangle(pDm->hdr.left, pDm->hdr.top, pDm->hdr.right, pDm->hdr.bottom) == 0) |
||
| 249 | return (0); |
||
| 250 | } |
||
| 251 | else |
||
| 252 | { |
||
| 253 | |||
| 254 | // show disabled color |
||
| 255 | SetColor(pDm->hdr.pGolScheme->ColorDisabled); |
||
| 256 | if(Rectangle(pDm->hdr.left, pDm->hdr.top, pDm->hdr.right, pDm->hdr.bottom) == 0) |
||
| 257 | return (0); |
||
| 258 | } |
||
| 259 | } |
||
| 260 | |||
| 261 | // set clipping area, text will only appear inside the static text area. |
||
| 262 | SetClip(CLIP_ENABLE); |
||
| 263 | SetClipRgn(pDm->hdr.left + DM_INDENT, pDm->hdr.top, pDm->hdr.right - DM_INDENT, pDm->hdr.bottom); |
||
| 264 | state = DM_STATE_INIT; |
||
| 265 | |||
| 266 | case DM_STATE_INIT: |
||
| 267 | if(IsDeviceBusy()) |
||
| 268 | return (0); |
||
| 269 | |||
| 270 | // set the text color |
||
| 271 | if(!GetState(pDm, DM_DISABLED)) |
||
| 272 | { |
||
| 273 | SetColor(pDm->hdr.pGolScheme->TextColor0); |
||
| 274 | } |
||
| 275 | else |
||
| 276 | { |
||
| 277 | SetColor(pDm->hdr.pGolScheme->TextColorDisabled); |
||
| 278 | } |
||
| 279 | |||
| 280 | // convert the values to be displayed in string format |
||
| 281 | NumberToString(pDm->Pvalue, PreValue, pDm->NoOfDigits, pDm->DotPos); |
||
| 282 | NumberToString(pDm->Cvalue, CurValue, pDm->NoOfDigits, pDm->DotPos); |
||
| 283 | |||
| 284 | // use the font specified in the object |
||
| 285 | SetFont(pDm->hdr.pGolScheme->pFont); |
||
| 286 | |||
| 287 | state = DM_STATE_SETALIGN; // go to drawing of text |
||
| 288 | |||
| 289 | case DM_STATE_SETALIGN: |
||
| 290 | if(!charCtr) |
||
| 291 | { |
||
| 292 | |||
| 293 | // set position of the next character (based on alignment and next character) |
||
| 294 | textWidth = GetTextWidth(CurValue, pDm->hdr.pGolScheme->pFont); |
||
| 295 | |||
| 296 | // Display text with center alignment |
||
| 297 | if(GetState(pDm, (DM_CENTER_ALIGN))) |
||
| 298 | { |
||
| 299 | MoveTo((pDm->hdr.left + pDm->hdr.right - textWidth) >> 1, pDm->hdr.top + (lineCtr * pDm->textHeight)); |
||
| 300 | } |
||
| 301 | |||
| 302 | // Display text with right alignment |
||
| 303 | else if(GetState(pDm, (DM_RIGHT_ALIGN))) |
||
| 304 | { |
||
| 305 | MoveTo((pDm->hdr.right - textWidth - DM_INDENT), pDm->hdr.top + (lineCtr * pDm->textHeight)); |
||
| 306 | } |
||
| 307 | |||
| 308 | // Display text with left alignment |
||
| 309 | else |
||
| 310 | { |
||
| 311 | MoveTo(pDm->hdr.left + DM_INDENT, pDm->hdr.top + (lineCtr * pDm->textHeight)); |
||
| 312 | } |
||
| 313 | } |
||
| 314 | |||
| 315 | state = DM_STATE_DRAWTEXT; |
||
| 316 | |||
| 317 | case DM_STATE_DRAWTEXT: |
||
| 318 | pch = *(PreValue + charCtr); |
||
| 319 | ch = *(CurValue + charCtr); |
||
| 320 | |||
| 321 | // output one character at time until a newline character or a NULL character is sampled |
||
| 322 | while((0x0000 != ch)) |
||
| 323 | { |
||
| 324 | if(IsDeviceBusy()) |
||
| 325 | { |
||
| 326 | return (0); // device is busy return |
||
| 327 | } |
||
| 328 | |||
| 329 | if(GetState(pDm, DM_DRAW)) |
||
| 330 | { |
||
| 331 | SetColor(pDm->hdr.pGolScheme->CommonBkColor); |
||
| 332 | if(Bar(GetX(), pDm->hdr.top + 1, GetX() + textWidth, pDm->hdr.bottom - 1) == 0) |
||
| 333 | return (0); |
||
| 334 | } |
||
| 335 | else if(GetState(pDm, DM_UPDATE)) |
||
| 336 | { |
||
| 337 | if(pch != ch) |
||
| 338 | { |
||
| 339 | SetColor(pDm->hdr.pGolScheme->CommonBkColor); |
||
| 340 | if(Bar(GetX(), pDm->hdr.top + 1, GetX() + textWidth, pDm->hdr.bottom - 1) == 0) |
||
| 341 | return (0); |
||
| 342 | } |
||
| 343 | } |
||
| 344 | |||
| 345 | SetColor(pDm->hdr.pGolScheme->TextColor0); |
||
| 346 | |||
| 347 | // render the character |
||
| 348 | while(!OutChar(ch)); |
||
| 349 | charCtr++; // update to next character |
||
| 350 | ch = *(CurValue + charCtr); |
||
| 351 | pch = *(PreValue + charCtr); |
||
| 352 | } |
||
| 353 | |||
| 354 | // end of text string is reached no more lines to display |
||
| 355 | lineCtr = 0; |
||
| 356 | charCtr = 0; |
||
| 357 | SetClip(CLIP_DISABLE); // remove clipping |
||
| 358 | state = DM_STATE_IDLE; // go back to IDLE state |
||
| 359 | return (1); |
||
| 360 | } |
||
| 361 | |||
| 362 | return (1); |
||
| 363 | } |
||
| 364 | |||
| 365 | #endif // USE_DIGITALMETER |
Powered by WebSVN v2.8.3