Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.85 KB | None | 0 0
  1. #include "lvgl_ui.hpp"
  2. #include "../fonts/RobotoBold40px.hpp"
  3. #include "../icons/HeartrateIcon.h"
  4.  
  5. #include "lvgl.h"
  6.  
  7. #include "MetaUtils.hpp"
  8. #include "CallbackConnector.hpp"
  9.  
  10. #include <random>
  11. #include <charconv>
  12. #include <system_error>
  13. #include <string_view>
  14. #include <cstdint>
  15.  
  16. #include <any>
  17. #include <type_traits>
  18.  
  19. namespace UiConstants::Display
  20. {
  21. static const std::uint32_t Width = LV_HOR_RES;
  22. static const std::uint32_t Height = LV_VER_RES;
  23. }
  24.  
  25. namespace
  26. {
  27.  
  28. auto drawInyan( lv_obj_t* _parent )
  29. {
  30. auto createAlignedRect = [_parent]( auto _aligmentType, const lv_style_t* _style )
  31. {
  32. lv_obj_t* pObject{ nullptr };
  33. pObject = lv_obj_create( _parent, nullptr );
  34. lv_obj_set_size(
  35. pObject
  36. , UiConstants::Display::Width / 2
  37. , UiConstants::Display::Height
  38. );
  39.  
  40. lv_obj_set_style( pObject, _style );
  41. lv_obj_align( pObject, nullptr, _aligmentType, 0, 0 );
  42.  
  43. return pObject;
  44. };
  45.  
  46. static lv_style_t pInyStyle;
  47. static lv_style_t pYanStyle;
  48.  
  49. lv_style_copy( &pInyStyle, &lv_style_plain_color );
  50. pInyStyle.body.main_color = lv_color_make( 0x00,0x00,0x00 );
  51. pInyStyle.body.grad_color = pInyStyle.body.main_color;
  52.  
  53. lv_style_copy(&pYanStyle, &lv_style_plain_color);
  54. pYanStyle.body.main_color = lv_color_make( 0xFF, 0xFF, 0xFF );
  55. pYanStyle.body.grad_color = pYanStyle.body.main_color;
  56.  
  57. lv_obj_t* pIny = createAlignedRect(LV_ALIGN_IN_RIGHT_MID, &pInyStyle);
  58. lv_obj_t* pYan = createAlignedRect( LV_ALIGN_IN_LEFT_MID, &pYanStyle );
  59.  
  60. auto createAlignedCircle = [ _parent ](auto _aligmentType, const lv_style_t* _style)
  61. {
  62. lv_obj_t* pCircle = lv_obj_create( _parent, nullptr );
  63.  
  64. lv_obj_set_size(
  65. pCircle
  66. , UiConstants::Display::Width / 2
  67. , UiConstants::Display::Height / 2
  68. );
  69.  
  70. lv_obj_set_style( pCircle, _style );
  71. lv_obj_align( pCircle, nullptr, _aligmentType, 0, 0);
  72.  
  73. return pCircle;
  74. };
  75.  
  76. static lv_style_t pInyCircleStyle;
  77. lv_style_copy( &pInyCircleStyle, &lv_style_plain_color );
  78.  
  79. pInyCircleStyle.body.main_color = lv_color_make(0x00, 0x00, 0x00);
  80. pInyCircleStyle.body.grad_color = pInyStyle.body.main_color;
  81. pInyCircleStyle.body.radius = LV_RADIUS_CIRCLE;
  82.  
  83. static lv_style_t pYanCircleStyle;
  84.  
  85. lv_style_copy( &pYanCircleStyle, &pInyCircleStyle );
  86. pYanCircleStyle.body.main_color = lv_color_make(0xFF, 0xFF, 0xFF);
  87. pYanCircleStyle.body.grad_color = pYanCircleStyle.body.main_color;
  88.  
  89. lv_obj_t* pInyCircle = createAlignedCircle( LV_ALIGN_IN_BOTTOM_MID, &pInyCircleStyle );
  90. lv_obj_t* pYanCircle = createAlignedCircle( LV_ALIGN_IN_TOP_MID, &pYanCircleStyle );
  91.  
  92. return std::tuple( pIny, pInyCircle, pYan, pYanCircle );
  93. }
  94.  
  95. auto drawClocks( lv_obj_t* _parent )
  96. {
  97. static lv_style_t hoursLabelStyle;
  98. lv_style_copy( &hoursLabelStyle, &lv_style_plain_color );
  99.  
  100. lv_obj_t* pHoursLabel = lv_label_create( _parent, nullptr );
  101. hoursLabelStyle.text.font = &RobotoFont40px;
  102. hoursLabelStyle.text.color = lv_color_make( 0x00, 0x00, 0x00 );
  103.  
  104. lv_label_set_style( pHoursLabel, LV_LABEL_STYLE_MAIN, &hoursLabelStyle );
  105. lv_obj_align(
  106. pHoursLabel
  107. , nullptr
  108. , LV_ALIGN_IN_TOP_MID
  109. , 0
  110. , static_cast<lv_coord_t>( UiConstants::Display::Height / 3.25f )
  111. );
  112.  
  113. lv_label_set_text( pHoursLabel, "11" );
  114.  
  115.  
  116. static lv_style_t minutesLabelStyle;
  117. lv_obj_t* pMinutesLabel = lv_label_create( _parent, nullptr );
  118.  
  119. lv_style_copy( &minutesLabelStyle, &lv_style_plain_color );
  120. minutesLabelStyle.text.font = &RobotoFont40px;
  121. minutesLabelStyle.text.color = lv_color_make(0xFF, 0xFF, 0xFF);
  122.  
  123. lv_label_set_style( pMinutesLabel, LV_LABEL_STYLE_MAIN, &minutesLabelStyle );
  124. lv_obj_align(
  125. pMinutesLabel
  126. , nullptr
  127. , LV_ALIGN_CENTER
  128. , 0
  129. , UiConstants::Display::Height / 8
  130. );
  131.  
  132. lv_label_set_text( pMinutesLabel, "27" );
  133.  
  134.  
  135. static lv_style_t secondsLabelStyle;
  136. lv_obj_t* pSecondsLabel = lv_label_create( _parent, nullptr );
  137.  
  138. lv_style_copy( &secondsLabelStyle, &minutesLabelStyle );
  139. secondsLabelStyle.text.font = &lv_font_roboto_16;
  140.  
  141. lv_label_set_style( pSecondsLabel, LV_LABEL_STYLE_MAIN, &secondsLabelStyle );
  142. lv_obj_align(
  143. pSecondsLabel
  144. , nullptr
  145. , LV_ALIGN_IN_TOP_RIGHT
  146. , 0
  147. , UiConstants::Display::Height / 3 + UiConstants::Display::Height / 24
  148. );
  149.  
  150. lv_label_set_text( pSecondsLabel, "27" );
  151.  
  152. return std::tuple( pHoursLabel, pMinutesLabel, pSecondsLabel );
  153. }
  154.  
  155. auto drawHeartrate( lv_obj_t* _parent )
  156. {
  157. lv_obj_t* pHeartRateIcon = lv_img_create( _parent, nullptr );
  158. lv_img_set_src( pHeartRateIcon, &HeartrateIcon );
  159. lv_obj_align( pHeartRateIcon, nullptr, LV_ALIGN_IN_TOP_MID, 0, 20 );
  160.  
  161.  
  162. static lv_style_t pHeartRateStyle;
  163. lv_obj_t* pHeartRateLabel = lv_label_create( _parent, nullptr );
  164.  
  165. lv_style_copy( &pHeartRateStyle, &lv_style_plain_color );
  166. pHeartRateStyle.text.font = &RobotoFont40px;
  167. pHeartRateStyle.text.color = lv_color_make( 0xFF, 0xFF, 0xFF );
  168.  
  169. lv_label_set_style( pHeartRateLabel, LV_LABEL_STYLE_MAIN, &pHeartRateStyle );
  170. lv_obj_align(
  171. pHeartRateLabel
  172. , nullptr
  173. , LV_ALIGN_CENTER
  174. , 0
  175. , UiConstants::Display::Height / 8
  176. );
  177.  
  178.  
  179. lv_label_set_text( pHeartRateLabel, "..." );
  180.  
  181. std::random_device randomDev;
  182. std::mt19937 generator(randomDev() );
  183.  
  184. std::uniform_int_distribution<> dis( 60, 120 );
  185.  
  186. auto randomHeartrateGenerator = cbc::obtain_connector(
  187. [ pHeartRateLabel,dis, generator](lv_task_t* _pTask) mutable
  188. {
  189. std::int32_t heartRateValue{ dis( generator ) };
  190.  
  191. constexpr size_t LabelSize = 4;
  192. std::array<char, LabelSize> str{};
  193.  
  194. if (auto [p, ec] = std::to_chars( str.data(), str.data() + str.size(), heartRateValue );
  195. ec == std::errc())
  196. lv_label_set_text(
  197. pHeartRateLabel
  198. , std::string_view( str.data(), p - str.data() ).data()
  199. );
  200. }
  201. );
  202.  
  203. lv_task_t* pTaskSwitch = lv_task_create(
  204. randomHeartrateGenerator
  205. , 600
  206. , LV_TASK_PRIO_MID
  207. , nullptr
  208. );
  209.  
  210. return std::tuple( pHeartRateIcon, pHeartRateLabel );
  211. }
  212.  
  213. }
  214.  
  215. namespace LvglUi
  216. {
  217.  
  218. void createWidgetsDemo()
  219. {
  220.  
  221. //lv_theme_t* pTheme = lv_theme_mono_init(0, nullptr);
  222. lv_theme_t* pTheme = lv_theme_material_init( 0, nullptr );
  223. lv_theme_set_current( pTheme );
  224.  
  225. static std::array<lv_point_t, 3> validPos
  226. = {
  227. lv_point_t{ 0, 0 }
  228. , lv_point_t{ 0, 1 }
  229. , lv_point_t{ 1, 1 }
  230. };
  231.  
  232. lv_obj_t* pTileView;
  233. pTileView = lv_tileview_create( lv_scr_act(), nullptr );
  234. lv_tileview_set_valid_positions(
  235. pTileView
  236. , validPos.data()
  237. , static_cast<std::uint16_t>( validPos.size() )
  238. );
  239.  
  240. lv_tileview_set_edge_flash( pTileView, false );
  241.  
  242. auto createClockTile = [pTileView]
  243. {
  244.  
  245. lv_obj_t* tileClock = lv_obj_create( pTileView, nullptr );
  246. lv_obj_set_size( tileClock, LV_HOR_RES, LV_VER_RES );
  247. lv_obj_set_style( tileClock, &lv_style_plain );
  248.  
  249. /*Clock Tile: just labels*/
  250. auto iniYan = drawInyan( tileClock );
  251. auto clocks = drawClocks( tileClock );
  252. auto clockTileWidgets = std::tuple_cat( iniYan, clocks );
  253. lv_tileview_add_element( pTileView, tileClock );
  254.  
  255. Meta::tupleApply(
  256. [&pTileView]( lv_obj_t* _pClockWidget )
  257. {
  258. lv_tileview_add_element( pTileView , _pClockWidget );
  259. }
  260. , clockTileWidgets
  261. );
  262.  
  263. return clockTileWidgets;
  264. };
  265. auto heartrateCreator = [pTileView]
  266. {
  267. /*Tile2: a back menu*/
  268. lv_obj_t* tileHeartrate = lv_obj_create( pTileView, nullptr );
  269. lv_obj_set_size( tileHeartrate, LV_HOR_RES, LV_VER_RES );
  270. lv_obj_set_style( tileHeartrate, &lv_style_plain );
  271. lv_obj_set_pos( tileHeartrate, 0, LV_VER_RES);
  272. lv_tileview_add_element( pTileView, tileHeartrate );
  273.  
  274. auto iniYanReversed = drawInyan( tileHeartrate );
  275. auto heartRateWidgets = drawHeartrate( tileHeartrate );
  276. auto heartRateTile = std::tuple_cat( iniYanReversed, heartRateWidgets );
  277.  
  278.  
  279. Meta::tupleApply(
  280. [&pTileView]( lv_obj_t* _pOptionsWidget )
  281. {
  282. lv_tileview_add_element( pTileView, _pOptionsWidget );
  283. }
  284. , heartRateTile
  285. );
  286.  
  287. return heartRateTile;
  288. };
  289.  
  290.  
  291. auto switcherTask = cbc::obtain_connector(
  292. [pTileView, heartrateCreator, createClockTile]( lv_task_t* _pTask )
  293. {
  294. static bool activeScreen{ true };
  295. static std::any pActiveScreenTuple;
  296.  
  297. using THeartrateCall = std::invoke_result_t<decltype( heartrateCreator )>;
  298. using ClockTileCall = std::invoke_result_t<decltype( createClockTile )>;
  299.  
  300.  
  301. if( activeScreen )
  302. {
  303. if(!pActiveScreenTuple.has_value() )
  304. pActiveScreenTuple = createClockTile();
  305. else
  306. {
  307. auto pSreenDeinit = std::any_cast<typename THeartrateCall>( pActiveScreenTuple );
  308.  
  309. Meta::tupleApply(
  310. [](lv_obj_t* _pWidget)
  311. {
  312. lv_obj_del( _pWidget );
  313. }
  314. , pSreenDeinit
  315. );
  316. pActiveScreenTuple = createClockTile();
  317. }
  318. }
  319. else
  320. {
  321. if ( !pActiveScreenTuple.has_value() )
  322. pActiveScreenTuple = heartrateCreator();
  323. else
  324. {
  325. auto pSreenDeinit = std::any_cast<typename ClockTileCall>( pActiveScreenTuple );
  326.  
  327. Meta::tupleApply(
  328. [](lv_obj_t* _pWidget)
  329. {
  330. lv_obj_del( _pWidget );
  331. }
  332. , pSreenDeinit
  333. );
  334. pActiveScreenTuple = heartrateCreator();
  335. }
  336. }
  337. activeScreen = !activeScreen;
  338. lv_tileview_set_tile_act(pTileView, 0, activeScreen, true);
  339.  
  340. }
  341. );
  342.  
  343. switcherTask(nullptr);
  344.  
  345. lv_task_t* pTaskSwitch = lv_task_create(
  346. switcherTask
  347. , 2500
  348. , LV_TASK_PRIO_MID
  349. , nullptr
  350. );
  351. //lv_task_once( pTaskSwitch );
  352. }
  353.  
  354. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement