bt_test.c 13 KB

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