canvas.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. uint8_t u8g2_gpio_and_delay_stm32(u8x8_t* u8x8, uint8_t msg, uint8_t arg_int, void* arg_ptr);
  7. uint8_t u8x8_hw_spi_stm32(u8x8_t* u8x8, uint8_t msg, uint8_t arg_int, void* arg_ptr);
  8. Canvas* canvas_init() {
  9. Canvas* canvas = furi_alloc(sizeof(Canvas));
  10. furi_hal_power_insomnia_enter();
  11. canvas->orientation = CanvasOrientationHorizontal;
  12. u8g2_Setup_st7565_erc12864_alt_f(
  13. &canvas->fb, U8G2_R0, u8x8_hw_spi_stm32, u8g2_gpio_and_delay_stm32);
  14. // send init sequence to the display, display is in sleep mode after this
  15. u8g2_InitDisplay(&canvas->fb);
  16. u8g2_SetContrast(&canvas->fb, 36);
  17. // wake up display
  18. u8g2_ClearBuffer(&canvas->fb);
  19. u8g2_SetPowerSave(&canvas->fb, 0);
  20. u8g2_SendBuffer(&canvas->fb);
  21. furi_hal_power_insomnia_exit();
  22. return canvas;
  23. }
  24. void canvas_free(Canvas* canvas) {
  25. furi_assert(canvas);
  26. free(canvas);
  27. }
  28. void canvas_reset(Canvas* canvas) {
  29. furi_assert(canvas);
  30. canvas_clear(canvas);
  31. canvas_set_color(canvas, ColorBlack);
  32. canvas_set_font(canvas, FontSecondary);
  33. }
  34. void canvas_commit(Canvas* canvas) {
  35. furi_assert(canvas);
  36. u8g2_SetPowerSave(&canvas->fb, 0); // wake up display
  37. u8g2_SendBuffer(&canvas->fb);
  38. }
  39. uint8_t* canvas_get_buffer(Canvas* canvas) {
  40. furi_assert(canvas);
  41. return u8g2_GetBufferPtr(&canvas->fb);
  42. }
  43. size_t canvas_get_buffer_size(Canvas* canvas) {
  44. furi_assert(canvas);
  45. return u8g2_GetBufferTileWidth(&canvas->fb) * u8g2_GetBufferTileHeight(&canvas->fb) * 8;
  46. }
  47. void canvas_frame_set(
  48. Canvas* canvas,
  49. uint8_t offset_x,
  50. uint8_t offset_y,
  51. uint8_t width,
  52. uint8_t height) {
  53. furi_assert(canvas);
  54. canvas->offset_x = offset_x;
  55. canvas->offset_y = offset_y;
  56. canvas->width = width;
  57. canvas->height = height;
  58. }
  59. uint8_t canvas_width(Canvas* canvas) {
  60. furi_assert(canvas);
  61. return canvas->width;
  62. }
  63. uint8_t canvas_height(Canvas* canvas) {
  64. furi_assert(canvas);
  65. return canvas->height;
  66. }
  67. uint8_t canvas_current_font_height(Canvas* canvas) {
  68. furi_assert(canvas);
  69. uint8_t font_height = u8g2_GetMaxCharHeight(&canvas->fb);
  70. if(canvas->fb.font == u8g2_font_haxrcorp4089_tr) {
  71. font_height += 1;
  72. }
  73. return font_height;
  74. }
  75. void canvas_clear(Canvas* canvas) {
  76. furi_assert(canvas);
  77. u8g2_ClearBuffer(&canvas->fb);
  78. }
  79. void canvas_set_color(Canvas* canvas, Color color) {
  80. furi_assert(canvas);
  81. u8g2_SetDrawColor(&canvas->fb, color);
  82. }
  83. void canvas_invert_color(Canvas* canvas) {
  84. canvas->fb.draw_color = !canvas->fb.draw_color;
  85. }
  86. void canvas_set_font(Canvas* canvas, Font font) {
  87. furi_assert(canvas);
  88. u8g2_SetFontMode(&canvas->fb, 1);
  89. if(font == FontPrimary) {
  90. u8g2_SetFont(&canvas->fb, u8g2_font_helvB08_tf);
  91. } else if(font == FontSecondary) {
  92. u8g2_SetFont(&canvas->fb, u8g2_font_haxrcorp4089_tr);
  93. } else if(font == FontKeyboard) {
  94. u8g2_SetFont(&canvas->fb, u8g2_font_profont11_mf);
  95. } else {
  96. furi_check(0);
  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_check(0);
  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_check(0);
  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. u8g2_SetDisplayRotation(&canvas->fb, U8G2_R0);
  268. else if(canvas->orientation == CanvasOrientationVertical)
  269. u8g2_SetDisplayRotation(&canvas->fb, U8G2_R3);
  270. else
  271. furi_assert(0);
  272. }
  273. }
  274. CanvasOrientation canvas_get_orientation(const Canvas* canvas) {
  275. return canvas->orientation;
  276. }