example_complex.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. #include <rtthread.h>
  2. #include <board.h>
  3. #include "arm_2d.h"
  4. #include "arm_2d_helper.h"
  5. #include "lcd_printf.h"
  6. /* GLCD RGB color definitions */
  7. #define GLCD_COLOR_BLACK 0x0000 /* 0, 0, 0 */
  8. #define GLCD_COLOR_NAVY 0x000F /* 0, 0, 128 */
  9. #define GLCD_COLOR_DARK_GREEN 0x03E0 /* 0, 128, 0 */
  10. #define GLCD_COLOR_DARK_CYAN 0x03EF /* 0, 128, 128 */
  11. #define GLCD_COLOR_MAROON 0x7800 /* 128, 0, 0 */
  12. #define GLCD_COLOR_PURPLE 0x780F /* 128, 0, 128 */
  13. #define GLCD_COLOR_OLIVE 0x7BE0 /* 128, 128, 0 */
  14. #define GLCD_COLOR_LIGHT_GREY 0xC618 /* 192, 192, 192 */
  15. #define GLCD_COLOR_DARK_GREY 0x7BEF /* 128, 128, 128 */
  16. #define GLCD_COLOR_BLUE 0x001F /* 0, 0, 255 */
  17. #define GLCD_COLOR_GREEN 0x07E0 /* 0, 255, 0 */
  18. #define GLCD_COLOR_CYAN 0x07FF /* 0, 255, 255 */
  19. #define GLCD_COLOR_RED 0xF800 /* 255, 0, 0 */
  20. #define GLCD_COLOR_MAGENTA 0xF81F /* 255, 0, 255 */
  21. #define GLCD_COLOR_YELLOW 0xFFE0 /* 255, 255, 0 */
  22. #define GLCD_COLOR_WHITE 0xFFFF /* 255, 255, 255 */
  23. /* declare dog tile */
  24. declare_tile(c_tiledog);
  25. static arm_2d_helper_pfb_t s_tPFBHelper;
  26. extern int32_t GLCD_DrawBitmap (uint32_t x,
  27. uint32_t y,
  28. uint32_t width,
  29. uint32_t height,
  30. const uint8_t *bitmap);
  31. static
  32. IMPL_PFB_ON_LOW_LV_RENDERING(__pfb_render_handler)
  33. {
  34. const arm_2d_tile_t *ptTile = &(ptPFB->tTile);
  35. ARM_2D_UNUSED(pTarget);
  36. ARM_2D_UNUSED(bIsNewFrame);
  37. GLCD_DrawBitmap(ptTile->tRegion.tLocation.iX,
  38. ptTile->tRegion.tLocation.iY,
  39. ptTile->tRegion.tSize.iWidth,
  40. ptTile->tRegion.tSize.iHeight,
  41. ptTile->pchBuffer);
  42. arm_2d_helper_pfb_report_rendering_complete(&s_tPFBHelper,
  43. (arm_2d_pfb_t *)ptPFB);
  44. }
  45. typedef struct {
  46. arm_2d_op_rotate_alpha_t tOP;
  47. const arm_2d_tile_t *ptTile;
  48. float fAngle;
  49. float fAngleSpeed;
  50. arm_2d_location_t tCentre;
  51. arm_2d_region_t tRegion;
  52. uint8_t chOpacity;
  53. } dog_head_t;
  54. static
  55. dog_head_t s_tDog[] =
  56. {
  57. {
  58. .ptTile = &c_tiledog,
  59. .fAngleSpeed = -5.0f,
  60. .tCentre = {
  61. .iX = 30,
  62. .iY = 30,
  63. },
  64. .tRegion = {
  65. .tLocation = {
  66. .iX = (90),
  67. .iY = (90),
  68. },
  69. .tSize = {
  70. .iWidth = 60,
  71. .iHeight = 60,
  72. },
  73. },
  74. .chOpacity = 255,
  75. },
  76. {
  77. .ptTile = &c_tiledog,
  78. .fAngleSpeed = 10.0f,
  79. .tCentre = {
  80. .iX = -30,
  81. .iY = -30,
  82. },
  83. .tRegion = {
  84. .tLocation = {
  85. .iX = (90),
  86. .iY = (90),
  87. },
  88. .tSize = {
  89. .iWidth = 60,
  90. .iHeight = 60,
  91. },
  92. },
  93. .chOpacity = 128,
  94. },
  95. };
  96. static uint32_t iWidth = 0;
  97. void timeout_entry(void *p)
  98. {
  99. iWidth++;
  100. if (iWidth > (2 * c_tiledog.tRegion.tSize.iWidth))
  101. iWidth = 0;
  102. }
  103. static
  104. IMPL_PFB_ON_DRAW(__pfb_draw_handler)
  105. {
  106. ARM_2D_UNUSED(pTarget);
  107. assert(NULL != ptTile);
  108. arm_2d_rgb16_fill_colour(ptTile, NULL, GLCD_COLOR_WHITE);
  109. arm_foreach (dog_head_t, s_tDog) {
  110. if (bIsNewFrame) {
  111. _->fAngle += ARM_2D_ANGLE(_->fAngleSpeed);
  112. _->fAngle = fmodf(_->fAngle, ARM_2D_ANGLE(360));
  113. }
  114. if (255 == _->chOpacity) {
  115. arm_2dp_rgb565_tile_rotation(
  116. (arm_2d_op_rotate_t *)&(_->tOP),
  117. _->ptTile, //!< source tile
  118. ptTile, //!< target tile
  119. &(_->tRegion), //!< target region
  120. _->tCentre, //!< center point
  121. _->fAngle, //!< rotation angle
  122. GLCD_COLOR_WHITE); //!< masking colour
  123. } else {
  124. arm_2dp_rgb565_tile_rotation_with_alpha(
  125. &(_->tOP),
  126. _->ptTile, //!< source tile
  127. ptTile, //!< target tile
  128. &(_->tRegion), //!< target region
  129. _->tCentre, //!< center point
  130. _->fAngle, //!< rotation angle
  131. GLCD_COLOR_WHITE, //!< masking colour
  132. _->chOpacity); //!< Opacity
  133. }
  134. }
  135. arm_2d_region_t tTargetRegion = c_tiledog.tRegion;
  136. arm_2d_region_t tBarRegion;
  137. tTargetRegion.tLocation.iX = (LCD_WIDTH - c_tiledog.tRegion.tSize.iWidth) / 2 - (c_tiledog.tRegion.tSize.iWidth / 2);
  138. tTargetRegion.tLocation.iY = (LCD_HEIGHT - c_tiledog.tRegion.tSize.iHeight) / 2;
  139. arm_2d_rgb16_tile_copy(&c_tiledog, ptTile, &tTargetRegion, ARM_2D_CP_MODE_COPY);
  140. tTargetRegion.tLocation.iX += c_tiledog.tRegion.tSize.iWidth;
  141. arm_2d_rgb16_tile_copy(&c_tiledog, ptTile, &tTargetRegion, ARM_2D_CP_MODE_COPY | ARM_2D_CP_MODE_X_MIRROR);
  142. arm_2d_rgb565_fill_colour_with_alpha(ptTile, &tTargetRegion, (arm_2d_color_rgb565_t){GLCD_COLOR_WHITE}, (uint_fast8_t)((255.0 / 120) * iWidth));
  143. tTargetRegion.tLocation.iX = (LCD_WIDTH - c_tiledog.tRegion.tSize.iWidth) / 2 - (c_tiledog.tRegion.tSize.iWidth / 2);
  144. tTargetRegion.tLocation.iY = (LCD_HEIGHT - c_tiledog.tRegion.tSize.iHeight) / 2;
  145. tTargetRegion.tLocation.iY += (c_tiledog.tRegion.tSize.iHeight + 10);
  146. tTargetRegion.tLocation.iX -= 1;
  147. tTargetRegion.tLocation.iY -= 1;
  148. tTargetRegion.tSize.iHeight = 12;
  149. tTargetRegion.tSize.iWidth = c_tiledog.tRegion.tSize.iWidth * 2 + 2;
  150. arm_2d_rgb16_fill_colour(ptTile, &tTargetRegion, GLCD_COLOR_LIGHT_GREY);
  151. tTargetRegion = c_tiledog.tRegion;
  152. tTargetRegion.tLocation.iX = (LCD_WIDTH - c_tiledog.tRegion.tSize.iWidth) / 2 - (c_tiledog.tRegion.tSize.iWidth / 2);
  153. tTargetRegion.tLocation.iY = (LCD_HEIGHT - c_tiledog.tRegion.tSize.iHeight) / 2;
  154. tTargetRegion.tLocation.iY += (c_tiledog.tRegion.tSize.iHeight + 10);
  155. tTargetRegion.tSize.iHeight = 10;
  156. tTargetRegion.tSize.iWidth = iWidth;
  157. arm_2d_rgb16_fill_colour(ptTile, &tTargetRegion, GLCD_COLOR_GREEN);
  158. tTargetRegion.tLocation.iX += iWidth;
  159. tTargetRegion.tSize.iWidth = c_tiledog.tRegion.tSize.iWidth * 2 - iWidth;
  160. arm_2d_rgb16_fill_colour(ptTile, &tTargetRegion, GLCD_COLOR_WHITE);
  161. tBarRegion.tLocation.iX = 0;
  162. tBarRegion.tLocation.iY = LCD_HEIGHT - 8*2;
  163. tBarRegion.tSize.iHeight = (2 * 8);
  164. tBarRegion.tSize.iWidth = LCD_WIDTH;
  165. arm_2d_rgb16_fill_colour(ptTile, &tBarRegion, GLCD_COLOR_BLACK);
  166. //! print info
  167. lcd_text_location(LCD_HEIGHT / 8 - 2, 0);
  168. lcd_printf("Dog1...Dog1...Dog1...Dog1...\n");
  169. lcd_printf("Dog2...Dog2...Dog2...Dog2...Dog2...\n");
  170. return arm_fsm_rt_cpl;
  171. }
  172. static
  173. IMPL_PFB_ON_DRAW(__pfb_draw_background_handler)
  174. {
  175. ARM_2D_UNUSED(pTarget);
  176. ARM_2D_UNUSED(bIsNewFrame);
  177. arm_2d_rgb16_fill_colour(ptTile, NULL, GLCD_COLOR_WHITE);
  178. return arm_fsm_rt_cpl;
  179. }
  180. void display_task(void)
  181. {
  182. /*! define dirty regions */
  183. IMPL_ARM_2D_REGION_LIST(s_tDirtyRegions, static const)
  184. /* a region for the dog */
  185. ADD_REGION_TO_LIST(s_tDirtyRegions,
  186. .tLocation = {(LCD_WIDTH - 150) /2,
  187. (LCD_HEIGHT - 150) / 2},
  188. .tSize = {
  189. .iWidth = 150,
  190. .iHeight = 150,
  191. },
  192. ),
  193. /* a region for the status bar on the bottom of the screen */
  194. ADD_LAST_REGION_TO_LIST(s_tDirtyRegions,
  195. .tLocation = {0,LCD_HEIGHT - 8*2},
  196. .tSize = {
  197. .iWidth = LCD_WIDTH,
  198. .iHeight = 8*2,
  199. },
  200. ),
  201. END_IMPL_ARM_2D_REGION_LIST()
  202. /*! define the partial-flushing area */
  203. //! call partial framebuffer helper service
  204. while(arm_fsm_rt_cpl != arm_2d_helper_pfb_task(
  205. &s_tPFBHelper,
  206. NULL));
  207. //(arm_2d_region_list_item_t *)s_tDirtyRegions));
  208. //! update performance info
  209. }
  210. void arm_2d_complex(void)
  211. {
  212. rt_timer_t t;
  213. arm_irq_safe {
  214. arm_2d_init();
  215. }
  216. t = rt_timer_create("t", timeout_entry, RT_NULL, 100, RT_TIMER_FLAG_PERIODIC | RT_TIMER_FLAG_SOFT_TIMER);
  217. if (t == RT_NULL)
  218. {
  219. rt_kprintf("Arm-2D create timer failed!\n");
  220. return;
  221. }
  222. rt_timer_start(t);
  223. //! initialize FPB helper
  224. if (ARM_2D_HELPER_PFB_INIT(
  225. &s_tPFBHelper, //!< FPB Helper object
  226. LCD_WIDTH, //!< screen width
  227. LCD_HEIGHT, //!< screen height
  228. uint16_t, //!< colour date type
  229. LCD_WIDTH, //!< PFB block width
  230. 8, //!< PFB block height
  231. 1, //!< number of PFB in the PFB pool
  232. {
  233. .evtOnLowLevelRendering = {
  234. //! callback for low level rendering
  235. .fnHandler = &__pfb_render_handler,
  236. },
  237. .evtOnDrawing = {
  238. //! callback for drawing GUI
  239. .fnHandler = &__pfb_draw_background_handler,
  240. },
  241. }
  242. ) < 0) {
  243. //! error detected
  244. assert(false);
  245. }
  246. //! draw background first
  247. while(arm_fsm_rt_cpl != arm_2d_helper_pfb_task(&s_tPFBHelper,NULL));
  248. //! update draw function
  249. ARM_2D_HELPER_PFB_UPDATE_ON_DRAW_HANDLER( &s_tPFBHelper,
  250. &__pfb_draw_handler);
  251. while (1) {
  252. display_task();
  253. }
  254. }
  255. MSH_CMD_EXPORT(arm_2d_complex, Arm-2D complex example.);