canvas.c 12 KB

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