gpio_controller.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. #include <furi.h>
  2. #include <furi_hal.h>
  3. #include <storage/storage.h>
  4. #include <toolbox/stream/stream.h>
  5. #include <toolbox/stream/file_stream.h>
  6. #include <gui/gui.h>
  7. #include <input/input.h>
  8. /* Magic happens here -- this file is generated by fbt.
  9. * Just set fap_icon_assets in application.fam and #include {APPID}_icons.h */
  10. #include "gpio_controller_icons.h"
  11. #include "app_defines.h"
  12. static void draw_main_view(Canvas* canvas, void* ctx);
  13. static void draw_config_menu_view(Canvas* canvas, void* ctx);
  14. static DrawView draw_view_funcs[] = {draw_main_view, draw_config_menu_view};
  15. static void handle_main_input(InputEvent* event, void* ctx);
  16. static void handle_config_menu_input(InputEvent* event, void* ctx);
  17. static HandleInput input_handlers[] = {handle_main_input, handle_config_menu_input};
  18. static ViewerState vstate;
  19. static int wiggle[] = {-1, 1, -1, 1};
  20. static uint32_t wiggle_frame_count = 4;
  21. static ViewElement elements[] = {
  22. {PIN_5V, PIN_3V, true, false, true, true, -1, 0, 0, "5V", (Icon*)&I_5v_pin, NULL},
  23. {PIN_A7, PIN_SWC, true, false, true, true, -1, 14, 0, "PA7", (Icon*)&I_a7_pin, NULL},
  24. {PIN_A6, NONE, true, false, true, true, -1, 28, 0, "PA6", (Icon*)&I_a6_pin, NULL},
  25. {PIN_A4, PIN_SIO, true, false, true, true, -1, 42, 0, "PA4", (Icon*)&I_a4_pin, NULL},
  26. {PIN_B3, PIN_TX, true, false, true, true, -1, 56, 0, "PB3", (Icon*)&I_b3_pin, NULL},
  27. {PIN_B2, PIN_RX, true, false, true, true, -1, 70, 0, "PB2", (Icon*)&I_b2_pin, NULL},
  28. {PIN_C3, PIN_C1, true, false, true, true, -1, 84, 0, "PC3", (Icon*)&I_c3_pin, NULL},
  29. {GEARIC,
  30. PIN_1W,
  31. true,
  32. true,
  33. true,
  34. false,
  35. -1,
  36. 112,
  37. 0,
  38. "Settings",
  39. (Icon*)&I_gear_unhighlighted,
  40. (Icon*)&I_gear_highlighted},
  41. {PIN_3V, PIN_5V, true, false, false, true, -1, 0, 48, "3.3V", (Icon*)&I_3v_pin, NULL},
  42. {PIN_SWC,
  43. PIN_A7,
  44. true,
  45. false,
  46. false,
  47. true,
  48. -1,
  49. 14,
  50. 48,
  51. "Serial Wire Clock",
  52. (Icon*)&I_swc_pin,
  53. NULL},
  54. {PIN_SIO, PIN_A4, true, false, false, true, -1, 42, 48, "Serial IO", (Icon*)&I_sio_pin, NULL},
  55. {PIN_TX, PIN_B3, true, false, false, true, -1, 56, 48, "UART - Transmit", (Icon*)&I_tx_pin, NULL},
  56. {PIN_RX, PIN_B2, true, false, false, true, -1, 70, 48, "UART - Receive", (Icon*)&I_rx_pin, NULL},
  57. {PIN_C1, PIN_C3, true, false, false, true, -1, 84, 48, "PC1", (Icon*)&I_c1_pin, NULL},
  58. {PIN_C0, NONE, true, false, false, true, -1, 98, 48, "PC0", (Icon*)&I_c0_pin, NULL},
  59. {PIN_1W, GEARIC, true, true, false, true, -1, 112, 48, "1-Wire", (Icon*)&I_1w_pin, NULL},
  60. {PIN_GND_08,
  61. NONE,
  62. false,
  63. false,
  64. true,
  65. false,
  66. -1,
  67. 98,
  68. -1,
  69. "GND (Ground)",
  70. (Icon*)&I_gnd_pin,
  71. NULL},
  72. {PIN_GND_11,
  73. NONE,
  74. false,
  75. false,
  76. false,
  77. false,
  78. -1,
  79. 28,
  80. 48,
  81. "GND (Ground)",
  82. (Icon*)&I_gnd_pin,
  83. NULL},
  84. {PIN_GND_18,
  85. NONE,
  86. false,
  87. false,
  88. false,
  89. false,
  90. -1,
  91. 126,
  92. 48,
  93. "GND (Ground)",
  94. (Icon*)&I_gnd_pin,
  95. NULL},
  96. };
  97. static GPIOPin gpio_pin_config[GPIO_PIN_COUNT];
  98. static int element_count =
  99. NONE; // The NONE enum will a value equal to the number of elements defined in enum_view_element
  100. size_t strnlen(const char* str, size_t maxlen) {
  101. size_t len = 0;
  102. while(len < maxlen && str[len] != '\0') {
  103. len++;
  104. }
  105. return len;
  106. }
  107. static void init_state() {
  108. vstate.selected = PIN_A7;
  109. vstate.wiggle_frame = -1;
  110. vstate.view = MAIN_VIEW;
  111. }
  112. static void init_gpio() {
  113. int count = 0;
  114. for(size_t i = 0; i < gpio_pins_count; i++) {
  115. if(!gpio_pins[i].debug) {
  116. for(int j = 0; j < element_count; j++) {
  117. if(strcmp(elements[j].name, gpio_pins[i].name) == 0) {
  118. gpio_pin_config[count].element_idx = j;
  119. gpio_pin_config[count].pin = gpio_pins[i].pin;
  120. gpio_pin_config[count].mode = GpioModeOutputPushPull;
  121. gpio_pin_config[count].pull = GpioPullNo;
  122. gpio_pin_config[count].speed = GpioSpeedVeryHigh;
  123. gpio_pin_config[count].value = 0;
  124. gpio_pin_config[count].name = gpio_pins[i].name;
  125. gpio_pin_config[count].unset = true;
  126. gpio_pin_config[count].found = true;
  127. gpio_pin_config[count].input = false;
  128. gpio_pin_config[count].user.mode = GPIO_MODE_UNSET;
  129. gpio_pin_config[count].user.value = GPIO_VALUE_FALSE;
  130. gpio_pin_config[count].user.gp_idx_input = -1;
  131. gpio_pin_config[count].user.changed = false;
  132. elements[j].gp_idx = i;
  133. elements[j].editable = true;
  134. count++;
  135. }
  136. }
  137. }
  138. }
  139. vstate.result = 0;
  140. }
  141. static void update_gpio() {
  142. // read from gpio pins
  143. for(int i = 0; i < GPIO_PIN_COUNT; i++) {
  144. GPIOPin* gpc = &gpio_pin_config[i];
  145. if(!gpc->unset) {
  146. if(gpc->mode == GpioModeInput) {
  147. gpc->value = furi_hal_gpio_read(gpc->pin) ? 1 : 0;
  148. }
  149. }
  150. }
  151. }
  152. #define TOGGLECOLOR(state, canvas, setting, selected_col, deselected_col) \
  153. canvas_set_color(canvas, (state == setting) ? selected_col : deselected_col)
  154. const char* gpio_user_mode_strs[] = {"INPUT", "INPUT_PULLUP", "OUTPUT", "UNSET"};
  155. const char* gpio_user_value_strs[] = {"TRUE", "FALSE", "INPUT"};
  156. static void draw_config_menu_view(Canvas* canvas, void* ctx) {
  157. UNUSED(ctx);
  158. int gp_idx = elements[vstate.selected].gp_idx;
  159. GPIOPin* gpc = &gpio_pin_config[gp_idx];
  160. UNUSED(gpc);
  161. canvas_set_font(canvas, FontSecondary);
  162. canvas_set_color(canvas, ColorBlack);
  163. canvas_draw_rframe(canvas, 1, 1, 126, 62, 0);
  164. TOGGLECOLOR(vstate.config_menu_selected, canvas, CONFIG_MENU_MODE, ColorBlack, ColorWhite);
  165. canvas_draw_box(canvas, 2, 2, 124, 15);
  166. TOGGLECOLOR(vstate.config_menu_selected, canvas, CONFIG_MENU_MODE, ColorWhite, ColorBlack);
  167. canvas_draw_str(canvas, 6, 12, "Mode");
  168. if(gpc->user.mode > 0) canvas_draw_str(canvas, 34, 12, "<");
  169. canvas_draw_str(canvas, 45, 12, gpio_user_mode_strs[gpc->user.mode]);
  170. if(gpc->user.mode < GPIO_MODE_UNSET) canvas_draw_str(canvas, 120, 12, ">");
  171. if(gpc->user.mode == GPIO_MODE_OUTPUT) {
  172. TOGGLECOLOR(
  173. vstate.config_menu_selected, canvas, CONFIG_MENU_VALUE, ColorBlack, ColorWhite);
  174. canvas_draw_box(canvas, 2, 16, 124, 15);
  175. TOGGLECOLOR(
  176. vstate.config_menu_selected, canvas, CONFIG_MENU_VALUE, ColorWhite, ColorBlack);
  177. canvas_draw_str(canvas, 6, 12 + 16, "Value");
  178. if(gpc->user.value > 0) canvas_draw_str(canvas, 34, 12 + 16, "<");
  179. canvas_draw_str(canvas, 45, 12 + 16, gpio_user_value_strs[gpc->user.value]);
  180. if(gpc->user.value < GPIO_VALUE_INPUT) canvas_draw_str(canvas, 120, 12 + 16, ">");
  181. }
  182. }
  183. // TODO: Determine the lowest frame delta we can get away with.
  184. // TODO: Redraw only what changes.
  185. // - clear previous (drawn) selected pin
  186. // - clear newly selected pin
  187. static void draw_main_view(Canvas* canvas, void* ctx) {
  188. UNUSED(ctx);
  189. canvas_clear(canvas);
  190. size_t current_frame_time = furi_get_tick();
  191. size_t delta_cycles =
  192. (current_frame_time > vstate.prev_frame_time ?
  193. current_frame_time - vstate.prev_frame_time :
  194. 0);
  195. size_t delta_time_ms = delta_cycles * 1000 / furi_kernel_get_tick_frequency();
  196. // delay until desired delta time and recalculate
  197. if(delta_time_ms < FRAME_TIME) {
  198. furi_delay_ms(FRAME_TIME - delta_time_ms);
  199. current_frame_time = furi_get_tick();
  200. delta_cycles =
  201. (current_frame_time > vstate.prev_frame_time ?
  202. current_frame_time - vstate.prev_frame_time :
  203. 0);
  204. delta_time_ms = delta_cycles * 1000 / furi_kernel_get_tick_frequency();
  205. }
  206. vstate.elapsed_time += delta_time_ms;
  207. vstate.prev_frame_time = current_frame_time;
  208. canvas_set_font(canvas, FontSecondary);
  209. char hex_string[3];
  210. // draw values
  211. for(int i = 0; i < GPIO_PIN_COUNT; i++) {
  212. if(!gpio_pin_config[i].unset) {
  213. ViewElement e = elements[gpio_pin_config[i].element_idx];
  214. // draw wire
  215. if(e.top_row) {
  216. canvas_draw_line(canvas, e.x_pos + 6, e.y_pos + 16, e.x_pos + 6, e.y_pos + 16 + 8);
  217. } else {
  218. canvas_draw_line(canvas, e.x_pos + 6, e.y_pos, e.x_pos + 6, e.y_pos - 8);
  219. }
  220. if(gpio_pin_config[i].mode == GpioModeAnalog) {
  221. snprintf(hex_string, sizeof(hex_string), "%02X", (int)gpio_pin_config[i].value);
  222. if(e.top_row) {
  223. canvas_draw_icon(canvas, e.x_pos - 1, e.y_pos + 20, &I_analog_box);
  224. canvas_draw_str(canvas, e.x_pos + 1, e.y_pos + 22 + 7, hex_string);
  225. } else {
  226. canvas_draw_icon(canvas, e.x_pos - 1, e.y_pos - 15, &I_analog_box);
  227. canvas_draw_str(canvas, e.x_pos + 1, e.y_pos - 6, hex_string);
  228. }
  229. } else {
  230. const Icon* icon = (int)gpio_pin_config[i].value ? &I_digi_one : &I_digi_zero;
  231. if(e.top_row) {
  232. canvas_draw_icon(canvas, e.x_pos + 2, e.y_pos + 20, icon);
  233. } else {
  234. canvas_draw_icon(canvas, e.x_pos + 2, e.y_pos - 13, icon);
  235. }
  236. }
  237. }
  238. }
  239. for(int i = 0; i < element_count; i++) {
  240. ViewElement e = elements[i];
  241. int x = e.x_pos;
  242. int y = e.y_pos + (e.top_row && e.pull_out ? -3 : 0);
  243. Icon* icon = e.icon;
  244. if(vstate.selected == i) {
  245. if(e.pull_out) {
  246. y += e.top_row ? 3 : -3;
  247. }
  248. if(e.selected_icon != NULL) {
  249. icon = e.selected_icon;
  250. }
  251. if(vstate.wiggle_frame >= 0) {
  252. x += wiggle[vstate.wiggle_frame];
  253. if(vstate.elapsed_time >= ANIMATE_FRAME_TIME_MS) {
  254. vstate.wiggle_frame++;
  255. if((unsigned int)(vstate.wiggle_frame) >= wiggle_frame_count) {
  256. vstate.wiggle_frame = -1;
  257. }
  258. vstate.elapsed_time = 0;
  259. }
  260. }
  261. }
  262. canvas_draw_icon(canvas, x, y, icon);
  263. }
  264. // draw arrows
  265. for(int i = 0; i < GPIO_PIN_COUNT; i++) {
  266. if(!gpio_pin_config[i].unset) {
  267. ViewElement e = elements[gpio_pin_config[i].element_idx];
  268. bool selected = vstate.selected == gpio_pin_config[i].element_idx;
  269. // draw arrow
  270. if(e.top_row) {
  271. int offset = selected ? 3 : 0;
  272. const Icon* arrow_icon = gpio_pin_config[i].input ? &I_arrow_up : &I_arrow_down;
  273. canvas_draw_icon(canvas, e.x_pos + 3, e.y_pos + 8 + offset, arrow_icon);
  274. } else {
  275. int offset = selected ? 0 : 3;
  276. const Icon* arrow_icon = gpio_pin_config[i].input ? &I_arrow_down : &I_arrow_up;
  277. canvas_draw_icon(canvas, e.x_pos + 3, e.y_pos + -1 + offset, arrow_icon);
  278. }
  279. }
  280. }
  281. canvas_set_font(canvas, FontSecondary);
  282. canvas_draw_str(canvas, 0, 42, elements[vstate.selected].name);
  283. }
  284. static void handle_main_input(InputEvent* event, void* ctx) {
  285. if(vstate.wiggle_frame < 0) {
  286. furi_assert(ctx);
  287. FuriMessageQueue* event_queue = ctx;
  288. // place in queue to handle backing out of app
  289. furi_message_queue_put(event_queue, event, FuriWaitForever);
  290. if((event->type == InputTypePress || event->type == InputTypeRelease) &&
  291. event->key == InputKeyOk) {
  292. if(event->type == InputTypePress && elements[vstate.selected].gp_idx < 0) {
  293. vstate.wiggle_frame = 0;
  294. vstate.elapsed_time = 0;
  295. } else if(
  296. elements[vstate.selected].gp_idx >= 0 &&
  297. (event->type == InputTypePress || event->type == InputTypeRelease)) {
  298. int gp_idx = elements[vstate.selected].gp_idx;
  299. gpio_pin_config[gp_idx].user.prev_mode = gpio_pin_config[gp_idx].user.mode;
  300. vstate.view = CONFIG_MENU_VIEW;
  301. vstate.config_menu_selected = CONFIG_MENU_MODE;
  302. }
  303. } else if(event->type == InputTypePress || event->type == InputTypeRepeat) {
  304. switch(event->key) {
  305. case InputKeyLeft:
  306. vstate.selected--;
  307. if(vstate.selected == GEARIC)
  308. vstate.selected = PIN_1W;
  309. else if(vstate.selected < 0)
  310. vstate.selected = GEARIC;
  311. break;
  312. case InputKeyRight:
  313. if(vstate.selected <= GEARIC) {
  314. vstate.selected++;
  315. vstate.selected = vstate.selected > GEARIC ? PIN_5V : vstate.selected;
  316. } else {
  317. vstate.selected++;
  318. vstate.selected = vstate.selected > PIN_1W ? PIN_3V : vstate.selected;
  319. }
  320. break;
  321. case InputKeyUp:
  322. case InputKeyDown:
  323. if(elements[vstate.selected].opposite != NONE)
  324. vstate.selected = elements[vstate.selected].opposite;
  325. break;
  326. default:
  327. break;
  328. }
  329. }
  330. }
  331. }
  332. static void set_GPIO_pin_via_user(int gp_idx) {
  333. GPIOPin* gpc = &gpio_pin_config[gp_idx];
  334. if(gpc->user.changed) {
  335. // update attributes
  336. switch(gpc->user.mode) {
  337. case GPIO_MODE_INPUT:
  338. gpc->mode = GpioModeInput;
  339. gpc->pull = GpioPullNo;
  340. gpc->input = true;
  341. break;
  342. case GPIO_MODE_INPUT_PULLUP:
  343. gpc->mode = GpioModeInput;
  344. gpc->pull = GpioPullUp;
  345. gpc->input = true;
  346. break;
  347. case GPIO_MODE_OUTPUT:
  348. gpc->mode = GpioModeOutputPushPull;
  349. gpc->pull = GpioPullNo;
  350. gpc->input = false;
  351. break;
  352. default:
  353. break;
  354. }
  355. switch(gpc->user.value) {
  356. case GPIO_VALUE_TRUE:
  357. gpc->value = (double)1.0;
  358. break;
  359. case GPIO_VALUE_FALSE:
  360. case GPIO_VALUE_INPUT:
  361. case GPIO_VALUE_NONE:
  362. gpc->value = (double)0.0;
  363. break;
  364. default:
  365. break;
  366. }
  367. furi_hal_gpio_write(gpc->pin, gpc->value != (double)0.0 ? true : false);
  368. if(gpc->user.mode != gpc->user.prev_mode) {
  369. furi_hal_gpio_init(gpc->pin, gpc->mode, gpc->pull, gpc->speed);
  370. gpc->unset = false;
  371. }
  372. gpc->user.changed = false;
  373. }
  374. }
  375. static void handle_config_menu_input(InputEvent* event, void* ctx) {
  376. UNUSED(ctx);
  377. int gp_idx = elements[vstate.selected].gp_idx;
  378. GPIOPin* gpc = &gpio_pin_config[gp_idx];
  379. if(event->type == InputTypePress || event->type == InputTypeRepeat) {
  380. switch(event->key) {
  381. case InputKeyLeft:
  382. switch(vstate.config_menu_selected) {
  383. case CONFIG_MENU_MODE:
  384. if(gpc->user.mode > 0) {
  385. gpc->user.mode--;
  386. gpc->user.changed = true;
  387. }
  388. break;
  389. case CONFIG_MENU_VALUE:
  390. if(gpc->user.value > 0) {
  391. gpc->user.value--;
  392. gpc->user.changed = true;
  393. }
  394. break;
  395. case CONFIG_MENU_INPUT:
  396. break;
  397. default:
  398. break;
  399. }
  400. break;
  401. case InputKeyRight:
  402. switch(vstate.config_menu_selected) {
  403. case CONFIG_MENU_MODE:
  404. if(gpc->user.mode < GPIO_MODE_UNSET) {
  405. gpc->user.mode++;
  406. gpc->user.changed = true;
  407. }
  408. break;
  409. case CONFIG_MENU_VALUE:
  410. if(gpc->user.value < GPIO_VALUE_FALSE) {
  411. gpc->user.value++;
  412. gpc->user.changed = true;
  413. }
  414. break;
  415. case CONFIG_MENU_INPUT:
  416. break;
  417. default:
  418. break;
  419. }
  420. break;
  421. case InputKeyUp:
  422. if(gpc->user.mode == GPIO_MODE_OUTPUT) {
  423. if(vstate.config_menu_selected == 0)
  424. vstate.config_menu_selected = CONFIG_MENU_VALUE;
  425. else
  426. vstate.config_menu_selected--;
  427. }
  428. break;
  429. case InputKeyDown:
  430. if(gpc->user.mode == GPIO_MODE_OUTPUT) {
  431. if(vstate.config_menu_selected == CONFIG_MENU_VALUE)
  432. vstate.config_menu_selected = 0;
  433. else
  434. vstate.config_menu_selected++;
  435. }
  436. break;
  437. case InputKeyBack:
  438. // Set new pin configuration
  439. set_GPIO_pin_via_user(gp_idx);
  440. vstate.view = MAIN_VIEW;
  441. break;
  442. default:
  443. break;
  444. }
  445. }
  446. }
  447. static void app_draw_callback(Canvas* canvas, void* ctx) {
  448. draw_view_funcs[vstate.view](canvas, ctx);
  449. }
  450. static void app_input_callback(InputEvent* input_event, void* ctx) {
  451. input_handlers[vstate.view](input_event, ctx);
  452. }
  453. int32_t gpio_controller_main(void* p) {
  454. UNUSED(p);
  455. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  456. // Configure view port
  457. ViewPort* view_port = view_port_alloc();
  458. view_port_draw_callback_set(view_port, app_draw_callback, view_port);
  459. view_port_input_callback_set(view_port, app_input_callback, event_queue);
  460. // Register view port in GUI
  461. Gui* gui = furi_record_open(RECORD_GUI);
  462. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  463. InputEvent event;
  464. init_state();
  465. init_gpio();
  466. vstate.prev_frame_time = furi_get_tick();
  467. vstate.elapsed_time = 0;
  468. bool running = true;
  469. while(running) {
  470. if(furi_message_queue_get(event_queue, &event, 100) == FuriStatusOk) {
  471. if(event.type == InputTypePress || event.type == InputTypeRepeat) {
  472. switch(event.key) {
  473. case InputKeyBack:
  474. running = false;
  475. break;
  476. default:
  477. break;
  478. }
  479. }
  480. }
  481. update_gpio();
  482. view_port_update(view_port);
  483. }
  484. view_port_enabled_set(view_port, false);
  485. gui_remove_view_port(gui, view_port);
  486. view_port_free(view_port);
  487. furi_message_queue_free(event_queue);
  488. furi_record_close(RECORD_GUI);
  489. return 0;
  490. }