canvas.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. #include "canvas_i.h"
  2. #include "icon_i.h"
  3. #include "icon_animation_i.h"
  4. #include <furi.h>
  5. #include <furi-hal.h>
  6. #include <u8g2_glue.h>
  7. Canvas* canvas_init() {
  8. Canvas* canvas = furi_alloc(sizeof(Canvas));
  9. furi_hal_power_insomnia_enter();
  10. // Setup u8g2
  11. u8g2_Setup_st756x_flipper(&canvas->fb, U8G2_R0, u8x8_hw_spi_stm32, u8g2_gpio_and_delay_stm32);
  12. canvas->orientation = CanvasOrientationHorizontal;
  13. // Initialize display
  14. u8g2_InitDisplay(&canvas->fb);
  15. // Wake up display
  16. u8g2_SetPowerSave(&canvas->fb, 0);
  17. // Clear buffer and send to device
  18. canvas_clear(canvas);
  19. canvas_commit(canvas);
  20. furi_hal_power_insomnia_exit();
  21. return canvas;
  22. }
  23. void canvas_free(Canvas* canvas) {
  24. furi_assert(canvas);
  25. free(canvas);
  26. }
  27. void canvas_reset(Canvas* canvas) {
  28. furi_assert(canvas);
  29. canvas_clear(canvas);
  30. canvas_set_color(canvas, ColorBlack);
  31. canvas_set_font(canvas, FontSecondary);
  32. canvas_set_font_direction(canvas, CanvasFontDirectionLeftToRight);
  33. }
  34. void canvas_commit(Canvas* canvas) {
  35. furi_assert(canvas);
  36. u8g2_SendBuffer(&canvas->fb);
  37. }
  38. uint8_t* canvas_get_buffer(Canvas* canvas) {
  39. furi_assert(canvas);
  40. return u8g2_GetBufferPtr(&canvas->fb);
  41. }
  42. size_t canvas_get_buffer_size(Canvas* canvas) {
  43. furi_assert(canvas);
  44. return u8g2_GetBufferTileWidth(&canvas->fb) * u8g2_GetBufferTileHeight(&canvas->fb) * 8;
  45. }
  46. void canvas_frame_set(
  47. Canvas* canvas,
  48. uint8_t offset_x,
  49. uint8_t offset_y,
  50. uint8_t width,
  51. uint8_t height) {
  52. furi_assert(canvas);
  53. canvas->offset_x = offset_x;
  54. canvas->offset_y = offset_y;
  55. canvas->width = width;
  56. canvas->height = height;
  57. }
  58. uint8_t canvas_width(Canvas* canvas) {
  59. furi_assert(canvas);
  60. return canvas->width;
  61. }
  62. uint8_t canvas_height(Canvas* canvas) {
  63. furi_assert(canvas);
  64. return canvas->height;
  65. }
  66. uint8_t canvas_current_font_height(Canvas* canvas) {
  67. furi_assert(canvas);
  68. uint8_t font_height = u8g2_GetMaxCharHeight(&canvas->fb);
  69. if(canvas->fb.font == u8g2_font_haxrcorp4089_tr) {
  70. font_height += 1;
  71. }
  72. return font_height;
  73. }
  74. void canvas_clear(Canvas* canvas) {
  75. furi_assert(canvas);
  76. u8g2_ClearBuffer(&canvas->fb);
  77. }
  78. void canvas_set_color(Canvas* canvas, Color color) {
  79. furi_assert(canvas);
  80. u8g2_SetDrawColor(&canvas->fb, color);
  81. }
  82. void canvas_set_font_direction(Canvas* canvas, CanvasFontDirection dir) {
  83. furi_assert(canvas);
  84. u8g2_SetFontDirection(&canvas->fb, dir);
  85. }
  86. void canvas_invert_color(Canvas* canvas) {
  87. canvas->fb.draw_color = !canvas->fb.draw_color;
  88. }
  89. void canvas_set_font(Canvas* canvas, Font font) {
  90. furi_assert(canvas);
  91. u8g2_SetFontMode(&canvas->fb, 1);
  92. if(font == FontPrimary) {
  93. u8g2_SetFont(&canvas->fb, u8g2_font_helvB08_tr);
  94. } else if(font == FontSecondary) {
  95. u8g2_SetFont(&canvas->fb, u8g2_font_haxrcorp4089_tr);
  96. } else if(font == FontKeyboard) {
  97. u8g2_SetFont(&canvas->fb, u8g2_font_profont11_mr);
  98. } else if(font == FontBigNumbers) {
  99. u8g2_SetFont(&canvas->fb, u8g2_font_profont22_tn);
  100. } else {
  101. furi_crash(NULL);
  102. }
  103. }
  104. void canvas_draw_str(Canvas* canvas, uint8_t x, uint8_t y, const char* str) {
  105. furi_assert(canvas);
  106. if(!str) return;
  107. x += canvas->offset_x;
  108. y += canvas->offset_y;
  109. u8g2_DrawStr(&canvas->fb, x, y, str);
  110. }
  111. void canvas_draw_str_aligned(
  112. Canvas* canvas,
  113. uint8_t x,
  114. uint8_t y,
  115. Align horizontal,
  116. Align vertical,
  117. const char* str) {
  118. furi_assert(canvas);
  119. if(!str) return;
  120. x += canvas->offset_x;
  121. y += canvas->offset_y;
  122. switch(horizontal) {
  123. case AlignLeft:
  124. break;
  125. case AlignRight:
  126. x -= u8g2_GetStrWidth(&canvas->fb, str);
  127. break;
  128. case AlignCenter:
  129. x -= (u8g2_GetStrWidth(&canvas->fb, str) / 2);
  130. break;
  131. default:
  132. furi_crash(NULL);
  133. break;
  134. }
  135. switch(vertical) {
  136. case AlignTop:
  137. y += u8g2_GetAscent(&canvas->fb);
  138. break;
  139. case AlignBottom:
  140. break;
  141. case AlignCenter:
  142. y += (u8g2_GetAscent(&canvas->fb) / 2);
  143. break;
  144. default:
  145. furi_crash(NULL);
  146. break;
  147. }
  148. u8g2_DrawStr(&canvas->fb, x, y, str);
  149. }
  150. uint16_t canvas_string_width(Canvas* canvas, const char* str) {
  151. furi_assert(canvas);
  152. if(!str) return 0;
  153. return u8g2_GetStrWidth(&canvas->fb, str);
  154. }
  155. uint8_t canvas_glyph_width(Canvas* canvas, char symbol) {
  156. furi_assert(canvas);
  157. return u8g2_GetGlyphWidth(&canvas->fb, symbol);
  158. }
  159. void canvas_draw_icon_animation(
  160. Canvas* canvas,
  161. uint8_t x,
  162. uint8_t y,
  163. IconAnimation* icon_animation) {
  164. furi_assert(canvas);
  165. furi_assert(icon_animation);
  166. x += canvas->offset_x;
  167. y += canvas->offset_y;
  168. uint8_t* icon_data = NULL;
  169. furi_hal_compress_icon_decode(icon_animation_get_data(icon_animation), &icon_data);
  170. u8g2_DrawXBM(
  171. &canvas->fb,
  172. x,
  173. y,
  174. icon_animation_get_width(icon_animation),
  175. icon_animation_get_height(icon_animation),
  176. icon_data);
  177. }
  178. void canvas_draw_icon(Canvas* canvas, uint8_t x, uint8_t y, const Icon* icon) {
  179. furi_assert(canvas);
  180. furi_assert(icon);
  181. x += canvas->offset_x;
  182. y += canvas->offset_y;
  183. uint8_t* icon_data = NULL;
  184. furi_hal_compress_icon_decode(icon_get_data(icon), &icon_data);
  185. u8g2_DrawXBM(&canvas->fb, x, y, icon_get_width(icon), icon_get_height(icon), icon_data);
  186. }
  187. void canvas_draw_dot(Canvas* canvas, uint8_t x, uint8_t y) {
  188. furi_assert(canvas);
  189. x += canvas->offset_x;
  190. y += canvas->offset_y;
  191. u8g2_DrawPixel(&canvas->fb, x, y);
  192. }
  193. void canvas_draw_box(Canvas* canvas, uint8_t x, uint8_t y, uint8_t width, uint8_t height) {
  194. furi_assert(canvas);
  195. x += canvas->offset_x;
  196. y += canvas->offset_y;
  197. u8g2_DrawBox(&canvas->fb, x, y, width, height);
  198. }
  199. void canvas_draw_rbox(
  200. Canvas* canvas,
  201. uint8_t x,
  202. uint8_t y,
  203. uint8_t width,
  204. uint8_t height,
  205. uint8_t radius) {
  206. furi_assert(canvas);
  207. x += canvas->offset_x;
  208. y += canvas->offset_y;
  209. u8g2_DrawRBox(&canvas->fb, x, y, width, height, radius);
  210. }
  211. void canvas_draw_frame(Canvas* canvas, uint8_t x, uint8_t y, uint8_t width, uint8_t height) {
  212. furi_assert(canvas);
  213. x += canvas->offset_x;
  214. y += canvas->offset_y;
  215. u8g2_DrawFrame(&canvas->fb, x, y, width, height);
  216. }
  217. void canvas_draw_rframe(
  218. Canvas* canvas,
  219. uint8_t x,
  220. uint8_t y,
  221. uint8_t width,
  222. uint8_t height,
  223. uint8_t radius) {
  224. furi_assert(canvas);
  225. x += canvas->offset_x;
  226. y += canvas->offset_y;
  227. u8g2_DrawRFrame(&canvas->fb, x, y, width, height, radius);
  228. }
  229. void canvas_draw_line(Canvas* canvas, uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2) {
  230. furi_assert(canvas);
  231. x1 += canvas->offset_x;
  232. y1 += canvas->offset_y;
  233. x2 += canvas->offset_x;
  234. y2 += canvas->offset_y;
  235. u8g2_DrawLine(&canvas->fb, x1, y1, x2, y2);
  236. }
  237. void canvas_draw_circle(Canvas* canvas, uint8_t x, uint8_t y, uint8_t radius) {
  238. furi_assert(canvas);
  239. x += canvas->offset_x;
  240. y += canvas->offset_y;
  241. u8g2_DrawCircle(&canvas->fb, x, y, radius, U8G2_DRAW_ALL);
  242. }
  243. void canvas_draw_disc(Canvas* canvas, uint8_t x, uint8_t y, uint8_t radius) {
  244. furi_assert(canvas);
  245. x += canvas->offset_x;
  246. y += canvas->offset_y;
  247. u8g2_DrawDisc(&canvas->fb, x, y, radius, U8G2_DRAW_ALL);
  248. }
  249. void canvas_draw_xbm(
  250. Canvas* canvas,
  251. uint8_t x,
  252. uint8_t y,
  253. uint8_t w,
  254. uint8_t h,
  255. const uint8_t* bitmap) {
  256. furi_assert(canvas);
  257. x += canvas->offset_x;
  258. y += canvas->offset_y;
  259. u8g2_DrawXBM(&canvas->fb, x, y, w, h, bitmap);
  260. }
  261. void canvas_draw_glyph(Canvas* canvas, uint8_t x, uint8_t y, uint16_t ch) {
  262. furi_assert(canvas);
  263. x += canvas->offset_x;
  264. y += canvas->offset_y;
  265. u8g2_DrawGlyph(&canvas->fb, x, y, ch);
  266. }
  267. void canvas_set_bitmap_mode(Canvas* canvas, bool alpha) {
  268. u8g2_SetBitmapMode(&canvas->fb, alpha ? 1 : 0);
  269. }
  270. void canvas_set_orientation(Canvas* canvas, CanvasOrientation orientation) {
  271. furi_assert(canvas);
  272. if(canvas->orientation != orientation) {
  273. canvas->orientation = orientation;
  274. if(canvas->orientation == CanvasOrientationHorizontal) {
  275. FURI_SWAP(canvas->width, canvas->height);
  276. u8g2_SetDisplayRotation(&canvas->fb, U8G2_R0);
  277. } else if(canvas->orientation == CanvasOrientationVertical) {
  278. FURI_SWAP(canvas->width, canvas->height);
  279. u8g2_SetDisplayRotation(&canvas->fb, U8G2_R3);
  280. } else {
  281. furi_assert(0);
  282. }
  283. }
  284. }
  285. CanvasOrientation canvas_get_orientation(const Canvas* canvas) {
  286. return canvas->orientation;
  287. }