canvas.c 11 KB

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