canvas.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 if(font == FontBigNumbers) {
  96. u8g2_SetFont(&canvas->fb, u8g2_font_profont22_tn);
  97. } else {
  98. furi_crash(NULL);
  99. }
  100. }
  101. void canvas_draw_str(Canvas* canvas, uint8_t x, uint8_t y, const char* str) {
  102. furi_assert(canvas);
  103. if(!str) return;
  104. x += canvas->offset_x;
  105. y += canvas->offset_y;
  106. u8g2_DrawStr(&canvas->fb, x, y, str);
  107. }
  108. void canvas_draw_str_aligned(
  109. Canvas* canvas,
  110. uint8_t x,
  111. uint8_t y,
  112. Align horizontal,
  113. Align vertical,
  114. const char* str) {
  115. furi_assert(canvas);
  116. if(!str) return;
  117. x += canvas->offset_x;
  118. y += canvas->offset_y;
  119. switch(horizontal) {
  120. case AlignLeft:
  121. break;
  122. case AlignRight:
  123. x -= u8g2_GetStrWidth(&canvas->fb, str);
  124. break;
  125. case AlignCenter:
  126. x -= (u8g2_GetStrWidth(&canvas->fb, str) / 2);
  127. break;
  128. default:
  129. furi_crash(NULL);
  130. break;
  131. }
  132. switch(vertical) {
  133. case AlignTop:
  134. y += u8g2_GetAscent(&canvas->fb);
  135. break;
  136. case AlignBottom:
  137. break;
  138. case AlignCenter:
  139. y += (u8g2_GetAscent(&canvas->fb) / 2);
  140. break;
  141. default:
  142. furi_crash(NULL);
  143. break;
  144. }
  145. u8g2_DrawStr(&canvas->fb, x, y, str);
  146. }
  147. uint16_t canvas_string_width(Canvas* canvas, const char* str) {
  148. furi_assert(canvas);
  149. if(!str) return 0;
  150. return u8g2_GetStrWidth(&canvas->fb, str);
  151. }
  152. uint8_t canvas_glyph_width(Canvas* canvas, char symbol) {
  153. furi_assert(canvas);
  154. return u8g2_GetGlyphWidth(&canvas->fb, symbol);
  155. }
  156. void canvas_draw_icon_animation(
  157. Canvas* canvas,
  158. uint8_t x,
  159. uint8_t y,
  160. IconAnimation* icon_animation) {
  161. furi_assert(canvas);
  162. furi_assert(icon_animation);
  163. x += canvas->offset_x;
  164. y += canvas->offset_y;
  165. u8g2_DrawXBM(
  166. &canvas->fb,
  167. x,
  168. y,
  169. icon_animation_get_width(icon_animation),
  170. icon_animation_get_height(icon_animation),
  171. icon_animation_get_data(icon_animation));
  172. }
  173. void canvas_draw_icon(Canvas* canvas, uint8_t x, uint8_t y, const Icon* icon) {
  174. furi_assert(canvas);
  175. furi_assert(icon);
  176. x += canvas->offset_x;
  177. y += canvas->offset_y;
  178. u8g2_DrawXBM(
  179. &canvas->fb, x, y, icon_get_width(icon), icon_get_height(icon), icon_get_data(icon));
  180. }
  181. void canvas_draw_dot(Canvas* canvas, uint8_t x, uint8_t y) {
  182. furi_assert(canvas);
  183. x += canvas->offset_x;
  184. y += canvas->offset_y;
  185. u8g2_DrawPixel(&canvas->fb, x, y);
  186. }
  187. void canvas_draw_box(Canvas* canvas, uint8_t x, uint8_t y, uint8_t width, uint8_t height) {
  188. furi_assert(canvas);
  189. x += canvas->offset_x;
  190. y += canvas->offset_y;
  191. u8g2_DrawBox(&canvas->fb, x, y, width, height);
  192. }
  193. void canvas_draw_rbox(
  194. Canvas* canvas,
  195. uint8_t x,
  196. uint8_t y,
  197. uint8_t width,
  198. uint8_t height,
  199. uint8_t radius) {
  200. furi_assert(canvas);
  201. x += canvas->offset_x;
  202. y += canvas->offset_y;
  203. u8g2_DrawRBox(&canvas->fb, x, y, width, height, radius);
  204. }
  205. void canvas_draw_frame(Canvas* canvas, uint8_t x, uint8_t y, uint8_t width, uint8_t height) {
  206. furi_assert(canvas);
  207. x += canvas->offset_x;
  208. y += canvas->offset_y;
  209. u8g2_DrawFrame(&canvas->fb, x, y, width, height);
  210. }
  211. void canvas_draw_rframe(
  212. Canvas* canvas,
  213. uint8_t x,
  214. uint8_t y,
  215. uint8_t width,
  216. uint8_t height,
  217. uint8_t radius) {
  218. furi_assert(canvas);
  219. x += canvas->offset_x;
  220. y += canvas->offset_y;
  221. u8g2_DrawRFrame(&canvas->fb, x, y, width, height, radius);
  222. }
  223. void canvas_draw_line(Canvas* canvas, uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2) {
  224. furi_assert(canvas);
  225. x1 += canvas->offset_x;
  226. y1 += canvas->offset_y;
  227. x2 += canvas->offset_x;
  228. y2 += canvas->offset_y;
  229. u8g2_DrawLine(&canvas->fb, x1, y1, x2, y2);
  230. }
  231. void canvas_draw_circle(Canvas* canvas, uint8_t x, uint8_t y, uint8_t radius) {
  232. furi_assert(canvas);
  233. x += canvas->offset_x;
  234. y += canvas->offset_y;
  235. u8g2_DrawCircle(&canvas->fb, x, y, radius, U8G2_DRAW_ALL);
  236. }
  237. void canvas_draw_disc(Canvas* canvas, uint8_t x, uint8_t y, uint8_t radius) {
  238. furi_assert(canvas);
  239. x += canvas->offset_x;
  240. y += canvas->offset_y;
  241. u8g2_DrawDisc(&canvas->fb, x, y, radius, U8G2_DRAW_ALL);
  242. }
  243. void canvas_draw_xbm(
  244. Canvas* canvas,
  245. uint8_t x,
  246. uint8_t y,
  247. uint8_t w,
  248. uint8_t h,
  249. const uint8_t* bitmap) {
  250. furi_assert(canvas);
  251. x += canvas->offset_x;
  252. y += canvas->offset_y;
  253. u8g2_DrawXBM(&canvas->fb, x, y, w, h, bitmap);
  254. }
  255. void canvas_draw_glyph(Canvas* canvas, uint8_t x, uint8_t y, uint16_t ch) {
  256. furi_assert(canvas);
  257. x += canvas->offset_x;
  258. y += canvas->offset_y;
  259. u8g2_DrawGlyph(&canvas->fb, x, y, ch);
  260. }
  261. void canvas_set_bitmap_mode(Canvas* canvas, bool alpha) {
  262. u8g2_SetBitmapMode(&canvas->fb, alpha ? 1 : 0);
  263. }
  264. void canvas_set_orientation(Canvas* canvas, CanvasOrientation orientation) {
  265. furi_assert(canvas);
  266. if(canvas->orientation != orientation) {
  267. canvas->orientation = orientation;
  268. if(canvas->orientation == CanvasOrientationHorizontal) {
  269. FURI_SWAP(canvas->width, canvas->height);
  270. u8g2_SetDisplayRotation(&canvas->fb, U8G2_R0);
  271. } else if(canvas->orientation == CanvasOrientationVertical) {
  272. FURI_SWAP(canvas->width, canvas->height);
  273. u8g2_SetDisplayRotation(&canvas->fb, U8G2_R3);
  274. } else {
  275. furi_assert(0);
  276. }
  277. }
  278. }
  279. CanvasOrientation canvas_get_orientation(const Canvas* canvas) {
  280. return canvas->orientation;
  281. }