canvas.c 11 KB

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