bt_test.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. #include "bt_test.h"
  2. #include <gui/canvas.h>
  3. #include <gui/elements.h>
  4. #include <lib/toolbox/float_tools.h>
  5. #include <m-array.h>
  6. #include <furi.h>
  7. #include <inttypes.h>
  8. #include <stdint.h>
  9. struct BtTestParam {
  10. const char* label;
  11. uint8_t current_value_index;
  12. FuriString* current_value_text;
  13. uint8_t values_count;
  14. BtTestParamChangeCallback change_callback;
  15. void* context;
  16. };
  17. ARRAY_DEF(BtTestParamArray, BtTestParam, M_POD_OPLIST);
  18. struct BtTest {
  19. View* view;
  20. BtTestChangeStateCallback change_state_callback;
  21. BtTestBackCallback back_callback;
  22. void* context;
  23. };
  24. typedef struct {
  25. BtTestState state;
  26. BtTestParamArray_t params;
  27. uint8_t position;
  28. uint8_t window_position;
  29. const char* message;
  30. float rssi;
  31. uint32_t packets_num_rx;
  32. uint32_t packets_num_tx;
  33. } BtTestModel;
  34. #define BT_TEST_START_MESSAGE "Ok - Start"
  35. #define BT_TEST_STOP_MESSAGE "Ok - Stop"
  36. static void bt_test_process_up(BtTest* bt_test);
  37. static void bt_test_process_down(BtTest* bt_test);
  38. static void bt_test_process_left(BtTest* bt_test);
  39. static void bt_test_process_right(BtTest* bt_test);
  40. static void bt_test_process_ok(BtTest* bt_test);
  41. static void bt_test_process_back(BtTest* bt_test);
  42. static void bt_test_draw_callback(Canvas* canvas, void* _model) {
  43. BtTestModel* model = _model;
  44. char info_str[32];
  45. const uint8_t param_height = 16;
  46. const uint8_t param_width = 123;
  47. canvas_clear(canvas);
  48. uint8_t position = 0;
  49. BtTestParamArray_it_t it;
  50. canvas_set_font(canvas, FontSecondary);
  51. for(BtTestParamArray_it(it, model->params); !BtTestParamArray_end_p(it);
  52. BtTestParamArray_next(it)) {
  53. uint8_t param_position = position - model->window_position;
  54. uint8_t params_on_screen = 3;
  55. uint8_t y_offset = 0;
  56. if(param_position < params_on_screen) {
  57. const BtTestParam* param = BtTestParamArray_cref(it);
  58. uint8_t param_y = y_offset + (param_position * param_height);
  59. uint8_t param_text_y = param_y + param_height - 4;
  60. if(position == model->position) {
  61. canvas_set_color(canvas, ColorBlack);
  62. elements_slightly_rounded_box(
  63. canvas, 0, param_y + 1, param_width, param_height - 2);
  64. canvas_set_color(canvas, ColorWhite);
  65. } else {
  66. canvas_set_color(canvas, ColorBlack);
  67. }
  68. canvas_draw_str(canvas, 6, param_text_y, param->label);
  69. if(param->current_value_index > 0) {
  70. canvas_draw_str(canvas, 50, param_text_y, "<");
  71. }
  72. canvas_draw_str(
  73. canvas, 61, param_text_y, furi_string_get_cstr(param->current_value_text));
  74. if(param->current_value_index < (param->values_count - 1)) {
  75. canvas_draw_str(canvas, 113, param_text_y, ">");
  76. }
  77. }
  78. position++;
  79. }
  80. elements_scrollbar(canvas, model->position, BtTestParamArray_size(model->params));
  81. canvas_draw_str(canvas, 6, 60, model->message);
  82. if(model->state == BtTestStateStarted) {
  83. if(!float_is_equal(model->rssi, 0.0f)) {
  84. snprintf(info_str, sizeof(info_str), "RSSI:%3.1f dB", (double)model->rssi);
  85. canvas_draw_str_aligned(canvas, 124, 60, AlignRight, AlignBottom, info_str);
  86. }
  87. } else if(model->state == BtTestStateStopped) {
  88. if(model->packets_num_rx) {
  89. snprintf(info_str, sizeof(info_str), "%" PRIu32 " pack rcv", model->packets_num_rx);
  90. canvas_draw_str_aligned(canvas, 124, 60, AlignRight, AlignBottom, info_str);
  91. } else if(model->packets_num_tx) {
  92. snprintf(info_str, sizeof(info_str), "%" PRIu32 " pack sent", model->packets_num_tx);
  93. canvas_draw_str_aligned(canvas, 124, 60, AlignRight, AlignBottom, info_str);
  94. }
  95. }
  96. }
  97. static bool bt_test_input_callback(InputEvent* event, void* context) {
  98. BtTest* bt_test = context;
  99. furi_assert(bt_test);
  100. bool consumed = false;
  101. if(event->type == InputTypeShort) {
  102. switch(event->key) {
  103. case InputKeyUp:
  104. consumed = true;
  105. bt_test_process_up(bt_test);
  106. break;
  107. case InputKeyDown:
  108. consumed = true;
  109. bt_test_process_down(bt_test);
  110. break;
  111. case InputKeyLeft:
  112. consumed = true;
  113. bt_test_process_left(bt_test);
  114. break;
  115. case InputKeyRight:
  116. consumed = true;
  117. bt_test_process_right(bt_test);
  118. break;
  119. case InputKeyOk:
  120. consumed = true;
  121. bt_test_process_ok(bt_test);
  122. break;
  123. case InputKeyBack:
  124. consumed = false;
  125. bt_test_process_back(bt_test);
  126. break;
  127. default:
  128. break;
  129. }
  130. }
  131. return consumed;
  132. }
  133. void bt_test_process_up(BtTest* bt_test) {
  134. with_view_model( // -V658
  135. bt_test->view,
  136. BtTestModel * model,
  137. {
  138. uint8_t params_on_screen = 3;
  139. if(model->position > 0) {
  140. model->position--;
  141. if(((model->position - model->window_position) < 1) &&
  142. model->window_position > 0) {
  143. model->window_position--;
  144. }
  145. } else {
  146. model->position = BtTestParamArray_size(model->params) - 1;
  147. if(model->position > (params_on_screen - 1)) {
  148. model->window_position = model->position - (params_on_screen - 1);
  149. }
  150. }
  151. },
  152. true);
  153. }
  154. void bt_test_process_down(BtTest* bt_test) {
  155. with_view_model(
  156. bt_test->view,
  157. BtTestModel * model,
  158. {
  159. uint8_t params_on_screen = 3;
  160. if(model->position < (BtTestParamArray_size(model->params) - 1)) {
  161. model->position++;
  162. if((model->position - model->window_position) > (params_on_screen - 2) &&
  163. model->window_position <
  164. (BtTestParamArray_size(model->params) - params_on_screen)) {
  165. model->window_position++;
  166. }
  167. } else {
  168. model->position = 0;
  169. model->window_position = 0;
  170. }
  171. },
  172. true);
  173. }
  174. BtTestParam* bt_test_get_selected_param(BtTestModel* model) {
  175. BtTestParam* param = NULL;
  176. BtTestParamArray_it_t it;
  177. uint8_t position = 0;
  178. for(BtTestParamArray_it(it, model->params); !BtTestParamArray_end_p(it);
  179. BtTestParamArray_next(it)) {
  180. if(position == model->position) {
  181. break;
  182. }
  183. position++;
  184. }
  185. param = BtTestParamArray_ref(it);
  186. furi_assert(param);
  187. return param;
  188. }
  189. void bt_test_process_left(BtTest* bt_test) {
  190. BtTestParam* param;
  191. with_view_model(
  192. bt_test->view,
  193. BtTestModel * model,
  194. {
  195. param = bt_test_get_selected_param(model);
  196. if(param->current_value_index > 0) {
  197. param->current_value_index--;
  198. if(param->change_callback) {
  199. model->state = BtTestStateStopped;
  200. model->message = BT_TEST_START_MESSAGE;
  201. model->rssi = 0.0f;
  202. model->packets_num_rx = 0;
  203. model->packets_num_tx = 0;
  204. }
  205. }
  206. },
  207. true);
  208. if(param->change_callback) {
  209. param->change_callback(param);
  210. }
  211. }
  212. void bt_test_process_right(BtTest* bt_test) {
  213. BtTestParam* param;
  214. with_view_model(
  215. bt_test->view,
  216. BtTestModel * model,
  217. {
  218. param = bt_test_get_selected_param(model);
  219. if(param->current_value_index < (param->values_count - 1)) {
  220. param->current_value_index++;
  221. if(param->change_callback) {
  222. model->state = BtTestStateStopped;
  223. model->message = BT_TEST_START_MESSAGE;
  224. model->rssi = 0.0f;
  225. model->packets_num_rx = 0;
  226. model->packets_num_tx = 0;
  227. }
  228. }
  229. },
  230. true);
  231. if(param->change_callback) {
  232. param->change_callback(param);
  233. }
  234. }
  235. void bt_test_process_ok(BtTest* bt_test) {
  236. BtTestState state;
  237. with_view_model(
  238. bt_test->view,
  239. BtTestModel * model,
  240. {
  241. if(model->state == BtTestStateStarted) {
  242. model->state = BtTestStateStopped;
  243. model->message = BT_TEST_START_MESSAGE;
  244. model->rssi = 0.0f;
  245. model->packets_num_rx = 0;
  246. model->packets_num_tx = 0;
  247. } else if(model->state == BtTestStateStopped) {
  248. model->state = BtTestStateStarted;
  249. model->message = BT_TEST_STOP_MESSAGE;
  250. }
  251. state = model->state;
  252. },
  253. true);
  254. if(bt_test->change_state_callback) {
  255. bt_test->change_state_callback(state, bt_test->context);
  256. }
  257. }
  258. void bt_test_process_back(BtTest* bt_test) {
  259. with_view_model(
  260. bt_test->view,
  261. BtTestModel * model,
  262. {
  263. model->state = BtTestStateStopped;
  264. model->rssi = 0.0f;
  265. model->packets_num_rx = 0;
  266. model->packets_num_tx = 0;
  267. },
  268. false);
  269. if(bt_test->back_callback) {
  270. bt_test->back_callback(bt_test->context);
  271. }
  272. }
  273. BtTest* bt_test_alloc() {
  274. BtTest* bt_test = malloc(sizeof(BtTest));
  275. bt_test->view = view_alloc();
  276. view_set_context(bt_test->view, bt_test);
  277. view_allocate_model(bt_test->view, ViewModelTypeLocking, sizeof(BtTestModel));
  278. view_set_draw_callback(bt_test->view, bt_test_draw_callback);
  279. view_set_input_callback(bt_test->view, bt_test_input_callback);
  280. with_view_model(
  281. bt_test->view,
  282. BtTestModel * model,
  283. {
  284. model->state = BtTestStateStopped;
  285. model->message = "Ok - Start";
  286. BtTestParamArray_init(model->params);
  287. model->position = 0;
  288. model->window_position = 0;
  289. model->rssi = 0.0f;
  290. model->packets_num_tx = 0;
  291. model->packets_num_rx = 0;
  292. },
  293. true);
  294. return bt_test;
  295. }
  296. void bt_test_free(BtTest* bt_test) {
  297. furi_assert(bt_test);
  298. with_view_model(
  299. bt_test->view,
  300. BtTestModel * model,
  301. {
  302. BtTestParamArray_it_t it;
  303. for(BtTestParamArray_it(it, model->params); !BtTestParamArray_end_p(it);
  304. BtTestParamArray_next(it)) {
  305. furi_string_free(BtTestParamArray_ref(it)->current_value_text);
  306. }
  307. BtTestParamArray_clear(model->params);
  308. },
  309. false);
  310. view_free(bt_test->view);
  311. free(bt_test);
  312. }
  313. View* bt_test_get_view(BtTest* bt_test) {
  314. furi_assert(bt_test);
  315. return bt_test->view;
  316. }
  317. BtTestParam* bt_test_param_add(
  318. BtTest* bt_test,
  319. const char* label,
  320. uint8_t values_count,
  321. BtTestParamChangeCallback change_callback,
  322. void* context) {
  323. BtTestParam* param = NULL;
  324. furi_assert(label);
  325. furi_assert(bt_test);
  326. with_view_model(
  327. bt_test->view,
  328. BtTestModel * model,
  329. {
  330. param = BtTestParamArray_push_new(model->params);
  331. param->label = label;
  332. param->values_count = values_count;
  333. param->change_callback = change_callback;
  334. param->context = context;
  335. param->current_value_index = 0;
  336. param->current_value_text = furi_string_alloc();
  337. },
  338. true);
  339. return param;
  340. }
  341. void bt_test_set_rssi(BtTest* bt_test, float rssi) {
  342. furi_assert(bt_test);
  343. with_view_model(
  344. bt_test->view, BtTestModel * model, { model->rssi = rssi; }, true);
  345. }
  346. void bt_test_set_packets_tx(BtTest* bt_test, uint32_t packets_num) {
  347. furi_assert(bt_test);
  348. with_view_model(
  349. bt_test->view, BtTestModel * model, { model->packets_num_tx = packets_num; }, true);
  350. }
  351. void bt_test_set_packets_rx(BtTest* bt_test, uint32_t packets_num) {
  352. furi_assert(bt_test);
  353. with_view_model(
  354. bt_test->view, BtTestModel * model, { model->packets_num_rx = packets_num; }, true);
  355. }
  356. void bt_test_set_change_state_callback(BtTest* bt_test, BtTestChangeStateCallback callback) {
  357. furi_assert(bt_test);
  358. furi_assert(callback);
  359. bt_test->change_state_callback = callback;
  360. }
  361. void bt_test_set_back_callback(BtTest* bt_test, BtTestBackCallback callback) {
  362. furi_assert(bt_test);
  363. furi_assert(callback);
  364. bt_test->back_callback = callback;
  365. }
  366. void bt_test_set_context(BtTest* bt_test, void* context) {
  367. furi_assert(bt_test);
  368. bt_test->context = context;
  369. }
  370. void bt_test_set_current_value_index(BtTestParam* param, uint8_t current_value_index) {
  371. param->current_value_index = current_value_index;
  372. }
  373. void bt_test_set_current_value_text(BtTestParam* param, const char* current_value_text) {
  374. furi_string_set(param->current_value_text, current_value_text);
  375. }
  376. uint8_t bt_test_get_current_value_index(BtTestParam* param) {
  377. return param->current_value_index;
  378. }
  379. void* bt_test_get_context(BtTestParam* param) {
  380. return param->context;
  381. }