gpio_controller.c 19 KB

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