]> begriffs open source - cmsis-freertos/blob - Demo/CORTEX_STM32F103_Primer_GCC/ST_Code/draw.c
Update README.md - branch main is now the base branch
[cmsis-freertos] / Demo / CORTEX_STM32F103_Primer_GCC / ST_Code / draw.c
1 /********************* (C) COPYRIGHT 2007 RAISONANCE S.A.S. *******************/
2 /**
3 *
4 * @file     draw.c
5 * @brief    Various utilities for drawings (characters, ..)
6 * @author   FL
7 * @author   IB
8 * @date     07/2007
9 *
10 **/
11 /******************************************************************************/
12
13 /* Includes ------------------------------------------------------------------*/
14 #include "circle.h"
15
16 /// @cond Internal
17
18 /* Private define ------------------------------------------------------------*/
19 #define V9_MADCTRVAL    0x90
20 #define V12_MADCTRVAL   0x30
21 #define V3_MADCTRVAL    0x50
22 #define V6_MADCTRVAL    0xF0
23
24 #define ROTATE_DIVISER  500
25
26 /* Private variables ---------------------------------------------------------*/
27
28 static u16 CharMagniCoeff = 1;   /*!< Current character magnify coefficient.  */
29 static u16 BGndColor;            /*!< Current background color.               */
30 static u16 TextColor;            /*!< Current text color.                     */
31
32 int fDisplayTime = 0;
33 u16 BatState;
34 u16 OldBatState;
35 u32 OldTHH;
36 u32 OldTMM;
37 u32 OldTSS;
38 u32 OldTemp;   //FL071107
39 u16 xBat;
40 u16 yBat;
41 u16 widthBat;
42 u16 heightBat;
43 u8 UsbState,OldUsbState;
44 static int divider_coord = 0;
45
46 // Screen orientation management
47 int                        rotate_counter             = 0;
48 Rotate_H12_V_Match_TypeDef previous_H12               = V9;
49 Rotate_H12_V_Match_TypeDef previous_previous_H12      = V9;
50 Rotate_H12_V_Match_TypeDef H12;
51 Rotate_H12_V_Match_TypeDef CurrentScreenOrientation;
52 int                        CurrentRotateScreen        = 1;
53
54 extern s16 XInit;
55 extern s16 YInit;
56
57 /* Private functions ---------------------------------------------------------*/
58
59 /*******************************************************************************
60 *
61 *                                vbattoa
62 *
63 *******************************************************************************/
64 /**
65 *
66 *  This function convert an u16 in ascii radix 10
67 *
68 *  @param[out] ptr   A pointer to a string where the converted value will be put.
69 *  @param[in]  X     The value to convert.
70 *
71 *  @see  DRAW_DisplayVbat
72 *
73 **/
74 /******************************************************************************/
75 static void vbattoa( char* ptr, u16 X )
76    {
77    u8 c;
78    u16 r = 0;
79
80    // 1 000 digit
81    c = ((X-r)/1000);
82    r = r + (c*1000);
83    *ptr++ = c + 0x30;
84
85    // dot
86    *ptr++ = '.';
87
88    // 100 digit
89    c = ((X-r)/100);
90    r = r + (c*100);
91    *ptr++ = c + 0x30;
92
93    // 10 digit
94    c = ((X-r)/10);
95    r = r + (c*10);
96    *ptr++ = c + 0x30;
97
98    // Volt
99    *ptr++ = 'V';
100    *ptr++ = 0;
101    }
102
103 /*******************************************************************************
104 *
105 *                                DRAW_DisplayStringWithMode
106 *
107 *******************************************************************************/
108 /**
109 *
110 *  This function is used to display a 17char max string of
111 *  characters on the LCD display on the selected line.
112 *  Note:
113 *  this function is the user interface to use the LCD driver.
114 *
115 *  @param[in]  x     The horizontal screen coordinate where to draw the string.
116 *  @param[in]  y     The vertical screen coordinate where to draw the string.
117 *  @param[in]  ptr   Pointer to string to display.
118 *  @param[in]  len   String size.
119 *  @param[in]  mode  Display mode: 0 normal, 1 inverted colors.
120 *
121 *  @warning       The (0x0) point in on the low left corner.
122 *
123 *  @see           DRAW_DisplayString
124 *  @see           DRAW_DisplayStringInverted
125 *
126 **/
127 /******************************************************************************/
128 static void DRAW_DisplayStringWithMode( u8 x, u8 y, const u8* ptr, u8 len, int mode )
129    {
130    u8 ref_x = x, i = 0;
131
132    /* Send the string character by character on LCD */
133    while ((*ptr!=0)&&(i<18))
134       {
135       /* Display one character on LCD */
136       LCD_DisplayChar( ref_x, y, *ptr, mode ? BGndColor : TextColor,  mode ? TextColor : BGndColor, CharMagniCoeff );
137
138       /* Increment the column position by 7 */
139       ref_x+= (7*CharMagniCoeff);
140
141       /* Point on the next character */
142       ptr++;
143
144       /* Increment the character counter */
145       i++;
146       /* If we reach the maximum Line character */
147       }
148
149    while ( i < len )
150       {
151       /* Display one character on LCD */
152       LCD_DisplayChar( ref_x, y, ' ', mode ? BGndColor : TextColor, mode ? TextColor : BGndColor, CharMagniCoeff );
153
154       /* Increment the column position by 7 */
155       ref_x += ( 7 * CharMagniCoeff );
156
157       /* Increment the character counter */
158       i++;
159       }
160    }
161
162 /* Public functions for CircleOS ---------------------------------------------*/
163
164 /*******************************************************************************
165 *
166 *                                DRAW_Init
167 *
168 *******************************************************************************/
169 /**
170 *
171 *  Initialize GUI drawing. Called at CircleOS startup.
172 *
173 *  @attention  This function must <b>NOT</b> be called by the user.
174 *
175 **/
176 /******************************************************************************/
177 void DRAW_Init( void )
178    {
179    LCD_Init();
180 #ifdef _MEMS
181    MEMS_GetRotation( &CurrentScreenOrientation );
182 #endif
183    LCD_SetScreenOrientation( CurrentScreenOrientation );
184
185    xBat        = 98;
186    yBat        = 3;
187    OldBatState = 10;
188    OldTSS      = 100;
189    OldTMM      = 100;
190    OldTHH      = 100;
191    OldTemp     = -1;
192
193    // Clear LCD and draw black and white logo
194    DRAW_SetDefaultColor();
195         LCD_FillRect( 0, 0, CHIP_SCREEN_WIDTH, CHIP_SCREEN_HEIGHT, BGndColor );
196 //   POINTER_Init();
197    }
198
199
200
201
202 /* Public functions ----------------------------------------------------------*/
203
204 /*******************************************************************************
205 *
206 *                                DRAW_SetCharMagniCoeff
207 *
208 *******************************************************************************/
209 /**
210 *
211 *  Set the magnifying value for the characters (should be 1 or 2)
212 *
213 *  @param[in]  Coeff The new magnifying coefficent.
214 *
215 **/
216 /******************************************************************************/
217 void DRAW_SetCharMagniCoeff( u16 Coeff )
218    {
219    CharMagniCoeff = Coeff;
220    }
221
222  /******************************************************************************
223 *
224 *                                DRAW_GetCharMagniCoeff
225 *
226 *******************************************************************************/
227 /**
228 *
229 *  Return the current magnifying value for the characters
230 *
231 *  @return  Current magnifying value.
232 *
233 **/
234 /******************************************************************************/
235 u16 DRAW_GetCharMagniCoeff( void )
236    {
237    return CharMagniCoeff;
238    }
239
240  /******************************************************************************
241 *
242 *                                DRAW_GetTextColor
243 *
244 *******************************************************************************/
245 /**
246 *
247 *  Return current text color.
248 *
249 *  @return  The current RGB color used to draw text.
250 *
251 **/
252 /******************************************************************************/
253 u16 DRAW_GetTextColor( void )
254    {
255    return TextColor;
256    }
257
258 /*******************************************************************************
259 *
260 *                                DRAW_SetTextColor
261 *
262 *******************************************************************************/
263 /**
264 *
265 *  Set current text color.
266 *
267 *  @param[in]  Color The new RGB color used when drawing text.
268 *
269 **/
270 /******************************************************************************/
271 void DRAW_SetTextColor( u16 Color )
272    {
273    TextColor = Color ;
274    }
275
276 /*******************************************************************************
277 *
278 *                                DRAW_GetBGndColor
279 *
280 *******************************************************************************/
281 /**
282 *
283 *  Return current background color.
284 *
285 *  @return  The current RGB color used for the background.
286 *
287 **/
288 /******************************************************************************/
289 u16 DRAW_GetBGndColor( void )
290    {
291    return BGndColor;
292    }
293
294 /*******************************************************************************
295 *
296 *                                DRAW_SetBGndColor
297 *
298 *******************************************************************************/
299 /**
300 *
301 *  Set current background color
302 *
303 *  @param[in]  Color The new RGB color for background.
304 *
305 **/
306 /******************************************************************************/
307 void DRAW_SetBGndColor(u16 Color)
308    {
309    BGndColor = Color;
310    }
311
312
313 /*******************************************************************************
314 *
315 *                                DRAW_SetImage
316 *
317 *******************************************************************************/
318 /**
319 *
320 *  The provided bitmap is made width * height 2 byte words. Each 2 byte word contains
321 *  the RGB color of a pixel.
322 *
323 *  @brief      Draw a color bitmap at the provided coordinates.
324 *  @param[in]  imageptr    A pointer to an array of width * height 2 byte words.
325 *  @param[in]  x           The horizontal coordinate of the low left corner of the bitmap.
326 *  @param[in]  y           The vertical coordinate of the low left corner of the bitmap.
327 *  @param[in]  width       The bitmap width.
328 *  @param[in]  height      The bitmap height.
329 *
330 *  @warning       The (0x0) point in on the low left corner.
331 *
332 **/
333 /******************************************************************************/
334 void DRAW_SetImage( const u16* imageptr, u8 x, u8 y, u8 width, u8 height )
335    {
336    int i;
337
338    // Select screen area to access.
339    LCD_SetRect_For_Cmd( x, y, width, height );
340
341    // Send command to write data on the LCD screen.
342    LCD_SendLCDCmd(ST7637_RAMWR);
343
344    for( i = 0; i < ( width * height ); i++ )
345       {
346       LCD_SendLCDData( imageptr[ i ] & 0xff );
347       LCD_SendLCDData( ( imageptr[ i ] >> 8 ) & 0xff );
348       }
349    }
350
351
352
353 /*******************************************************************************
354 *
355 *                                DRAW_DisplayString
356 *
357 *******************************************************************************/
358 /**
359 *
360 *  This function is used to display a 17 character max string of
361 *  characters on the LCD display at the provided coordinates.
362 *
363 *  @param[in] x      The horizontal coordinate of the displayed string.
364 *  @param[in] y      The vertical coordinate of the display string.
365 *  @param[in] *ptr   Pointer to the string to display on LCD.
366 *  @param[in] len    String length.
367 *
368 *  @warning       The (0x0) point in on the low left corner.
369 *
370 **/
371 /******************************************************************************/
372 void DRAW_DisplayString( u8 x, u8 y, const u8* ptr, u8 len )
373    {
374    DRAW_DisplayStringWithMode( x, y, ptr, len, 0 );
375    }
376
377 /*******************************************************************************
378 *
379 *                                DRAW_DisplayStringInverted
380 *
381 *******************************************************************************/
382 /**
383 *
384 *  This function is used to display a 17 character max string of
385 *  characters on the LCD display at the provided coordinates with inverted colors.
386 *
387 *  @param[in] x      The horizontal coordinate of the displayed string.
388 *  @param[in] y      The vertical coordinate of the display string.
389 *  @param[in] *ptr   Pointer to the string to display on LCD.
390 *  @param[in] len    String length.
391 *
392 *  @warning       The (0x0) point in on the low left corner.
393 *
394 **/
395 /******************************************************************************/
396 void DRAW_DisplayStringInverted( u8 x, u8 y, const u8* ptr, u8 len )
397    {
398    //BackGround and Text Colors are inverted
399    DRAW_DisplayStringWithMode( x, y, ptr, len, 1 );
400    }
401
402 /*******************************************************************************
403 *
404 *                                DRAW_SetDefaultColor
405 *
406 *******************************************************************************/
407 /**
408 *
409 *  Reset text and background colors to their default values.
410 *
411 **/
412 /******************************************************************************/
413 void DRAW_SetDefaultColor (void)
414    {
415    BGndColor = RGB_WHITE;
416    TextColor = RGB_BLACK;
417    }
418
419 /*******************************************************************************
420 *
421 *                                DRAW_DisplayTemp
422 *
423 *******************************************************************************/
424 /**
425 *
426 *  This function is used to display the current temperature in ascii.
427 *  The choice between Celcius and Fahrenheit is fixed by UTIL_SetModeTemp()
428 *
429 *  @param[in]  x  The horizontal coordinate of the displayed string.
430 *  @param[in]  y  The vertical coordinate of the display string.
431 *
432 *  @warning       The (0x0) point in on the low left corner.
433 *
434 **/
435 /******************************************************************************/
436 void DRAW_DisplayTemp( u8 x, u8 y )
437    {
438    u32 Temp = 0;
439    u8  TextBuffer[5] = { 0,0,0,0,0};
440    
441    // Get Time
442    Temp = UTIL_GetTemp() ;
443
444    if( Temp != OldTemp ) 
445       {
446       // Display C (if modified).
447       UTIL_uint2str( TextBuffer, Temp/10, 2, 1 );
448       TextBuffer[ 2 ] = '.';
449       DRAW_DisplayString( x + ( 0 * CharMagniCoeff * 7 ), y, TextBuffer, 3 );
450
451       // Display C/10 (if modified).
452       UTIL_uint2str( TextBuffer, Temp%10, 1, 1 );
453       TextBuffer[ 1 ] = fTemperatureInFahrenheit ? 'F' : 'C'; TextBuffer[ 2 ] = 0;
454       DRAW_DisplayString( x + ( 3 * CharMagniCoeff * 7 ), y, TextBuffer, 2 );
455       }
456    OldTemp = Temp;
457    }
458
459 /*******************************************************************************
460 *
461 *                                DRAW_Line
462 *
463 *******************************************************************************/
464 /**
465 *  Draw a line on the LCD screen. Optimized for horizontal/vertical lines, 
466 *  and use the Bresenham algorithm for other cases.
467 *
468 *  @param[in]  x1          The x-coordinate of the first line endpoint.
469 *  @param[in]  x2          The x-coordinate of the second line endpoint.
470 *  @param[in]  y1          The y-coordinate of the first line endpoint.
471 *  @param[in]  y2          The y-coordinate of the second line endpoint.
472 *  @param[in]  color       The line color.
473 *
474 **/
475 void DRAW_Line (s16 x1, s16 y1, s16 x2, s16 y2, u16 color )
476    {
477    int i,dx,dy,sdx,sdy,dxabs,dyabs,x,y,px,py;
478
479    #define abs(X) ( ( (X) < 0 ) ? -(X) : (X) )
480    #define sgn(X) ( ( (X) < 0 ) ? -1 : 1 )
481
482    if ( x1==x2 )  //Vertical Line
483       {
484       if ( y1 > y2 ) //We assume y2>y1 and invert if not
485          {
486          i = y2;
487          y2 = y1;
488          y1 = i;
489          }
490       LCD_FillRect( x1, y1, 1, y2-y1+1, color );
491       return;
492       }
493    else if ( y1==y2 )  //Horizontal Line
494       {
495       if ( x1 > x2 ) //We assume x2>x1 and we swap them if not
496          {
497          i = x2;
498          x2 = x1;
499          x1 = i;
500          }
501       LCD_FillRect( x1, y1, x2-x1+1, 1, color );
502       return;
503       }
504    
505    dx=x2-x1;      /* the horizontal distance of the line */
506    dy=y2-y1;      /* the vertical distance of the line */
507    dxabs=abs(dx);
508    dyabs=abs(dy);
509    sdx=sgn(dx);
510    sdy=sgn(dy);
511    x=dyabs>>1;
512    y=dxabs>>1;
513    px=x1;
514    py=y1;
515
516    if (dxabs>=dyabs) /* the line is more horizontal than vertical */
517       {
518       for(i=0;i<dxabs;i++)
519          {
520          y+=dyabs;
521          if (y>=dxabs)
522             {
523             y-=dxabs;
524             py+=sdy;
525             }
526          px+=sdx;
527          LCD_DrawPixel(px,py,color);
528          }
529       }
530    else /* the line is more vertical than horizontal */
531       {
532       for(i=0;i<dyabs;i++)
533          {
534          x+=dxabs;
535          if (x>=dyabs)
536             {
537             x-=dyabs;
538             px+=sdx;
539             }
540          py+=sdy;
541          LCD_DrawPixel(px,py,color);
542          }
543       }
544    }
545