coffee_eeprom.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #include <furi.h>
  2. #include <furi_hal.h>
  3. #include <gui/elements.h>
  4. #include <gui/gui.h>
  5. #include <gui/view_port.h>
  6. #include <stdlib.h>
  7. #include "coffee.h"
  8. #include <math.h>
  9. #include <storage/storage.h>
  10. #include <dialogs/dialogs.h>
  11. #include <lib/toolbox/name_generator.h>
  12. #define TAG "COFFEE EEPROM"
  13. #define MAX_CREDIT 655.35
  14. #define MIN_CREDIT 0.01
  15. typedef struct {
  16. Gui* gui;
  17. ViewPort* view_port;
  18. FuriMessageQueue* event_queue;
  19. bool editor_mode;
  20. float digit_editor;
  21. float credit;
  22. FuriString* msg;
  23. FuriString* status;
  24. } CoffeeContext;
  25. static void coffee_render_callback(Canvas* const canvas, void* ctx) {
  26. CoffeeContext* context = ctx;
  27. canvas_clear(canvas);
  28. canvas_set_color(canvas, ColorBlack);
  29. canvas_set_font(canvas, FontSecondary);
  30. if(context->credit >= 0.0){
  31. furi_string_printf(context->msg, "Credit: %.2f EUR", (double) context->credit);
  32. elements_button_left(canvas, "Load");
  33. elements_button_right(canvas, "Save");
  34. elements_button_center(canvas, "Edit (Hold)");
  35. canvas_set_font(canvas, FontPrimary);
  36. canvas_draw_str_aligned(canvas, 64, 8, AlignCenter, AlignCenter, furi_string_get_cstr(context->msg));
  37. }else{
  38. furi_string_printf(context->status, "EEPROM not connected!");
  39. }
  40. canvas_set_font(canvas, FontKeyboard);
  41. canvas_draw_str_aligned(canvas, 64, 26, AlignCenter, AlignCenter, furi_string_get_cstr(context->status));
  42. }
  43. /* This function is called from the GUI thread. All it does is put the event
  44. into the application's queue so it can be processed later. */
  45. static void coffee_input_callback(InputEvent* event, void* ctx) {
  46. CoffeeContext* context = ctx;
  47. furi_message_queue_put(context->event_queue, event, FuriWaitForever);
  48. }
  49. /* Allocate the memory and initialise the variables */
  50. static CoffeeContext* coffee_context_alloc() {
  51. CoffeeContext* context = malloc(sizeof(CoffeeContext));
  52. context->view_port = view_port_alloc();
  53. view_port_draw_callback_set(context->view_port, coffee_render_callback, context);
  54. view_port_input_callback_set(context->view_port, coffee_input_callback, context);
  55. context->event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  56. context->gui = furi_record_open(RECORD_GUI);
  57. gui_add_view_port(context->gui, context->view_port, GuiLayerFullscreen);
  58. context->msg = furi_string_alloc();
  59. context->status = furi_string_alloc();
  60. return context;
  61. }
  62. void load_file_dump(){
  63. FuriString* file_path = furi_string_alloc();
  64. do {
  65. DialogsFileBrowserOptions browser_options;
  66. dialog_file_browser_set_basic_options(
  67. &browser_options, ".bin", NULL);
  68. browser_options.hide_ext = false;
  69. browser_options.base_path = "/ext";
  70. DialogsApp* dialogs = furi_record_open(RECORD_DIALOGS);
  71. bool res = dialog_file_browser_show(dialogs, file_path, file_path, &browser_options);
  72. furi_record_close(RECORD_DIALOGS);
  73. if(!res) {
  74. FURI_LOG_E(TAG, "No file selected");
  75. break;
  76. }
  77. // Open storage
  78. Storage* storage = furi_record_open(RECORD_STORAGE);
  79. // Allocate file
  80. File* file = storage_file_alloc(storage);
  81. // Open file, write data and close it
  82. if(!storage_file_open(file, furi_string_get_cstr(file_path), FSAM_READ, FSOM_OPEN_EXISTING)) {
  83. FURI_LOG_E(TAG, "Failed to open file");
  84. }
  85. uint8_t buffer[256] = {0};
  86. uint16_t read = 0;
  87. uint16_t ret = 0;
  88. do {
  89. uint8_t temp[128] = {0};
  90. read += ret;
  91. ret = storage_file_read(file, temp, sizeof(temp) - 1);
  92. for(size_t i = 0; i < ret; i++) {
  93. buffer[i+read] = temp[i];
  94. }
  95. } while(ret > 0);
  96. storage_file_close(file);
  97. // Deallocate file
  98. storage_file_free(file);
  99. // Close storage
  100. furi_record_close(RECORD_STORAGE);
  101. if (read % 128 == 0){
  102. FuriString* dump = furi_string_alloc();
  103. FURI_LOG_E(TAG, "START READ DUMP");
  104. for (size_t i = 0; i < read; i++){
  105. furi_string_cat_printf(dump, "%.2X", buffer[i]);
  106. }
  107. FURI_LOG_E(TAG, "%s", furi_string_get_cstr(dump));
  108. FURI_LOG_E(TAG, "END READ DUMP");
  109. write_dump(buffer, (size_t) read);
  110. break;
  111. }
  112. } while(1);
  113. }
  114. FuriString* save_file_dump(float credit){
  115. char file_name_buf[64];
  116. name_generator_make_random(file_name_buf, 64);
  117. // Open storag
  118. char* file_path = APP_DATA_PATH("Dump");
  119. FuriString* file_name = furi_string_alloc_printf("%s_%s_%d.bin", file_path, file_name_buf, (int) (credit * 100));
  120. Storage* storage = furi_record_open(RECORD_STORAGE);
  121. // Allocate file
  122. File* file = storage_file_alloc(storage);
  123. // Get the path to the current application data folder
  124. // That is: /ext/apps_data/<app_name>
  125. // And it will create folders in the path if they don't exist
  126. // In this example it will create /ext/apps_data/example_apps_data
  127. // And file will be /ext/apps_data/example_apps_data/test.txt
  128. // Open file, write data and close it
  129. if(!storage_file_open(file, furi_string_get_cstr(file_name), FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
  130. FURI_LOG_E(TAG, "Failed to open file");
  131. }
  132. uint8_t out[256];
  133. dump(out);
  134. if(!storage_file_write(file, out, 256)) {
  135. FURI_LOG_E(TAG, "Failed to write to file");
  136. }
  137. storage_file_close(file);
  138. // Deallocate file
  139. storage_file_free(file);
  140. // Close storage
  141. furi_record_close(RECORD_STORAGE);
  142. return file_name;
  143. }
  144. /* Starts the reader thread and handles the input */
  145. static void coffee_run(CoffeeContext* context) {
  146. /* Start the reader thread. It will talk to the thermometer in the background. */
  147. context->credit = read_credit();
  148. context->digit_editor = 0.01;
  149. /* An endless loop which handles the input*/
  150. for(bool is_running = true; is_running;) {
  151. InputEvent event;
  152. /* Wait for an input event. Input events come from the GUI thread via a callback. */
  153. const FuriStatus status =
  154. furi_message_queue_get(context->event_queue, &event, FuriWaitForever);
  155. if(status == FuriStatusOk) {
  156. if(event.type == InputTypePress) {
  157. switch(event.key) {
  158. case InputKeyUp:
  159. if(context->editor_mode && context->credit + context->digit_editor <= MAX_CREDIT){
  160. context->credit += context->digit_editor;
  161. FURI_LOG_E(TAG, "%.2f %.2f", (double) context->credit, (double) context->digit_editor);
  162. }
  163. break;
  164. case InputKeyDown:
  165. if(context->editor_mode && context->credit - context->digit_editor >= MIN_CREDIT){
  166. context->credit -= context->digit_editor;
  167. FURI_LOG_E(TAG, "%.2f %.2f", (double) context->credit, (double) context->digit_editor);
  168. }
  169. break;
  170. case InputKeyMAX:
  171. break;
  172. case InputKeyRight:
  173. if(context->editor_mode && context->digit_editor >= 0.01){
  174. context->digit_editor /= 10;
  175. FURI_LOG_E(TAG, "%.2f %.2f", (double) context->credit, (double) context->digit_editor);
  176. }else {
  177. save_file_dump(context->credit);
  178. furi_string_printf(context->status, "Dump saved!");
  179. }
  180. break;
  181. case InputKeyLeft:
  182. if(context->editor_mode && context->digit_editor <= 100){
  183. context->digit_editor *= 10;
  184. FURI_LOG_E(TAG, "%.2f %.2f", (double) context->credit, (double) context->digit_editor);
  185. }else{
  186. //virgin();
  187. load_file_dump();
  188. context->credit = read_credit();
  189. furi_string_printf(context->status, "Dump write done!");
  190. }
  191. break;
  192. case InputKeyOk:
  193. if(context->editor_mode){
  194. write_credit(context->credit);
  195. context->credit = read_credit();
  196. furi_string_printf(context->status, "Write done!");
  197. context->editor_mode = false;
  198. }
  199. break;
  200. case InputKeyBack:
  201. if(context->editor_mode){
  202. furi_string_reset(context->status);
  203. context->editor_mode = false;
  204. }else{
  205. is_running = false;
  206. }
  207. break;
  208. }
  209. }else if(event.type == InputTypeLong && event.key == InputKeyOk){
  210. furi_string_printf(context->status, "Editor Mode");
  211. context->editor_mode = true;
  212. }
  213. }
  214. }
  215. }
  216. /* Release the unused resources and deallocate memory */
  217. static void coffee_context_free(CoffeeContext* context) {
  218. furi_string_free(context->msg);
  219. furi_string_free(context->status);
  220. view_port_enabled_set(context->view_port, false);
  221. gui_remove_view_port(context->gui, context->view_port);
  222. furi_message_queue_free(context->event_queue);
  223. view_port_free(context->view_port);
  224. furi_record_close(RECORD_GUI);
  225. }
  226. /* The application's entry point. Execution starts from here. */
  227. int32_t coffee_eeprom_main(void* p) {
  228. UNUSED(p);
  229. /* Allocate all of the necessary structures */
  230. CoffeeContext* context = coffee_context_alloc();
  231. /* Start the applicaton's main loop. It won't return until the application was requested to exit. */
  232. coffee_run(context);
  233. /* Release all unneeded resources */
  234. coffee_context_free(context);
  235. return 0;
  236. }