canvas.c 8.2 KB

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