mp_flipper_runtime.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #include <furi.h>
  2. #include <storage/storage.h>
  3. #include <py/mperrno.h>
  4. #include <mp_flipper_runtime.h>
  5. #include <mp_flipper_modflipperzero.h>
  6. #include "mp_flipper_context.h"
  7. static void on_input_callback(const InputEvent* event, void* ctx) {
  8. uint16_t button = 1 << event->key;
  9. uint16_t type = 1 << (InputKeyMAX + event->type);
  10. mp_flipper_on_input(button, type);
  11. }
  12. void mp_flipper_save_file(const char* file_path, const char* data, size_t size) {
  13. mp_flipper_context_t* ctx = mp_flipper_context;
  14. File* file = storage_file_alloc(ctx->storage);
  15. do {
  16. if(!storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
  17. storage_file_free(file);
  18. mp_flipper_raise_os_error_with_filename(MP_ENOENT, file_path);
  19. break;
  20. }
  21. storage_file_write(file, data, size);
  22. } while(false);
  23. storage_file_free(file);
  24. }
  25. inline void mp_flipper_nlr_jump_fail(void* val) {
  26. furi_crash();
  27. }
  28. inline void mp_flipper_assert(const char* file, int line, const char* func, const char* expr) {
  29. }
  30. inline void mp_flipper_fatal_error(const char* msg) {
  31. furi_crash(msg);
  32. }
  33. const char* mp_flipper_print_get_data(void* data) {
  34. return furi_string_get_cstr(data);
  35. }
  36. size_t mp_flipper_print_get_data_length(void* data) {
  37. return furi_string_size(data);
  38. }
  39. void* mp_flipper_print_data_alloc() {
  40. return furi_string_alloc();
  41. }
  42. void mp_flipper_print_strn(void* data, const char* str, size_t length) {
  43. for(size_t i = 0; i < length; i++) {
  44. furi_string_push_back(data, str[i]);
  45. }
  46. }
  47. void mp_flipper_print_data_free(void* data) {
  48. furi_string_free(data);
  49. }
  50. void* mp_flipper_context_alloc() {
  51. mp_flipper_context_t* ctx = malloc(sizeof(mp_flipper_context_t));
  52. ctx->gui = furi_record_open(RECORD_GUI);
  53. ctx->view_port = view_port_alloc();
  54. ctx->input_event_queue = furi_record_open(RECORD_INPUT_EVENTS);
  55. ctx->input_event = furi_pubsub_subscribe(ctx->input_event_queue, on_input_callback, NULL);
  56. gui_add_view_port(ctx->gui, ctx->view_port, GuiLayerFullscreen);
  57. ctx->canvas = gui_direct_draw_acquire(ctx->gui);
  58. ctx->dialog_message = dialog_message_alloc();
  59. ctx->dialog_message_button_left = NULL;
  60. ctx->dialog_message_button_center = NULL;
  61. ctx->dialog_message_button_right = NULL;
  62. ctx->storage = furi_record_open(RECORD_STORAGE);
  63. ctx->adc_handle = NULL;
  64. // GPIO
  65. ctx->gpio_pins = malloc(MP_FLIPPER_GPIO_PINS * sizeof(mp_flipper_gpio_pin_t));
  66. for(uint8_t pin = 0; pin < MP_FLIPPER_GPIO_PINS; pin++) {
  67. ctx->gpio_pins[pin] = MP_FLIPPER_GPIO_PIN_OFF;
  68. }
  69. // infrared rx
  70. ctx->infrared_rx = malloc(sizeof(mp_flipper_infrared_rx_t));
  71. ctx->infrared_rx->size = MP_FLIPPER_INFRARED_RX_BUFFER_SIZE;
  72. ctx->infrared_rx->buffer = calloc(ctx->infrared_rx->size, sizeof(uint16_t));
  73. ctx->infrared_rx->pointer = 0;
  74. ctx->infrared_rx->running = true;
  75. // infrared tx
  76. ctx->infrared_tx = malloc(sizeof(mp_flipper_infrared_tx_t));
  77. ctx->infrared_tx->index = 0;
  78. ctx->infrared_tx->provider = NULL;
  79. ctx->infrared_tx->repeat = 0;
  80. ctx->infrared_tx->signal = NULL;
  81. ctx->infrared_tx->size = 0;
  82. ctx->infrared_tx->level = false;
  83. return ctx;
  84. }
  85. void mp_flipper_context_free(void* context) {
  86. mp_flipper_context_t* ctx = context;
  87. gui_direct_draw_release(ctx->gui);
  88. furi_pubsub_unsubscribe(ctx->input_event_queue, ctx->input_event);
  89. gui_remove_view_port(ctx->gui, ctx->view_port);
  90. view_port_free(ctx->view_port);
  91. dialog_message_free(ctx->dialog_message);
  92. furi_record_close(RECORD_GUI);
  93. furi_record_close(RECORD_INPUT_EVENTS);
  94. furi_record_close(RECORD_STORAGE);
  95. // disable ADC handle
  96. if(ctx->adc_handle) {
  97. furi_hal_adc_release(ctx->adc_handle);
  98. }
  99. // de-initialize all GPIO pins
  100. for(uint8_t pin = 0; pin < MP_FLIPPER_GPIO_PINS; pin++) {
  101. mp_flipper_gpio_deinit_pin(pin);
  102. }
  103. // stop running PWM output
  104. mp_flipper_pwm_stop(MP_FLIPPER_GPIO_PIN_PA4);
  105. mp_flipper_pwm_stop(MP_FLIPPER_GPIO_PIN_PA7);
  106. free(ctx->gpio_pins);
  107. // stop infrared
  108. free(ctx->infrared_rx->buffer);
  109. free(ctx->infrared_rx);
  110. free(ctx->infrared_tx);
  111. free(ctx);
  112. }