mp_flipper_runtime.c 4.1 KB

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