diff --git a/nanovna.h b/nanovna.h index cb667d5..858ed16 100644 --- a/nanovna.h +++ b/nanovna.h @@ -1238,6 +1238,7 @@ int check_touched(void); int invoke_quick_menu(int); bool ui_process_listen_lever(void); void refresh_sweep_menu(int i); +void save_to_sd(int mask); // Irq operation process set #define OP_NONE 0x00 diff --git a/ui.c b/ui.c index 4b75a51..dfb1920 100644 --- a/ui.c +++ b/ui.c @@ -2913,6 +2913,33 @@ static const uint8_t bmp_header_v4[14+56] = { 0x00, 0x00, 0x00, 0x00 // A mask = 0b00000000 00000000 }; +FRESULT open_file(char *ext) +{ + FRESULT res = f_mount(fs_volume, "", 1); + // fs_volume, fs_file and fs_filename stored at end of spi_buffer!!!!! +// shell_printf("Mount = %d\r\n", res); + if (res != FR_OK) + return res; +#if FF_USE_LFN >= 1 + uint32_t tr = rtc_get_tr_bcd(); // TR read first + uint32_t dr = rtc_get_dr_bcd(); // DR read second + plot_printf(fs_filename, FF_LFN_BUF, "SA_%06x_%06x.%s", dr, tr, ext); +#else + plot_printf(fs_filename, FF_LFN_BUF, "%08x.%s", rtc_get_FAT(), ext); +#endif + res = f_open(fs_file, fs_filename, FA_CREATE_ALWAYS | FA_READ | FA_WRITE); + return res; +} + +void close_file(FRESULT res) +{ + if (res == FR_OK) + res = f_close(fs_file); +// time = chVTGetSystemTimeX() - time; +// shell_printf("Total time: %dms (write %d byte/sec)\r\n", time/10, (LCD_WIDTH*LCD_HEIGHT*sizeof(uint16_t)+sizeof(bmp_header_v4))*10000/time); + drawMessageBox("Save", res == FR_OK ? fs_filename : " Write failed ", 2000); + redraw_request|= REDRAW_AREA; +} static bool made_screenshot(int touch_x, int touch_y) { @@ -2925,20 +2952,8 @@ made_screenshot(int touch_x, int touch_y) touch_wait_release(); // uint32_t time = chVTGetSystemTimeX(); // shell_printf("Screenshot\r\n"); - FRESULT res = f_mount(fs_volume, "", 1); - // fs_volume, fs_file and fs_filename stored at end of spi_buffer!!!!! + FRESULT res = open_file("bmp"); uint16_t *buf = (uint16_t *)spi_buffer; -// shell_printf("Mount = %d\r\n", res); - if (res != FR_OK) - return TRUE; -#if FF_USE_LFN >= 1 - uint32_t tr = rtc_get_tr_bcd(); // TR read first - uint32_t dr = rtc_get_dr_bcd(); // DR read second - plot_printf(fs_filename, FF_LFN_BUF, "SA_%06x_%06x.bmp", dr, tr); -#else - plot_printf(fs_filename, FF_LFN_BUF, "%08x.bmp", rtc_get_FAT()); -#endif - res = f_open(fs_file, fs_filename, FA_CREATE_ALWAYS | FA_READ | FA_WRITE); // shell_printf("Open %s, result = %d\r\n", fs_filename, res); if (res == FR_OK){ res = f_write(fs_file, bmp_header_v4, sizeof(bmp_header_v4), &size); @@ -2952,12 +2967,30 @@ made_screenshot(int touch_x, int touch_y) // shell_printf("Close %d\r\n", res); // testLog(); } -// time = chVTGetSystemTimeX() - time; -// shell_printf("Total time: %dms (write %d byte/sec)\r\n", time/10, (LCD_WIDTH*LCD_HEIGHT*sizeof(uint16_t)+sizeof(bmp_header_v4))*10000/time); - drawMessageBox("SCREENSHOT", res == FR_OK ? fs_filename : " Write failed ", 2000); - redraw_request|= REDRAW_AREA; + close_file(res); return TRUE; } + +void save_to_sd(int mask) +{ + FRESULT res = open_file("csv"); + UINT size; + if (res == FR_OK) { + for (int i = 0; i < sweep_points; i++) { + char *buf = (char *)spi_buffer; + if (mask & 1) buf += plot_printf(buf, 100, "%U, ", frequencies[i]); + if (mask & 2) buf += plot_printf(buf, 100, "%f ", value(measured[TRACE_ACTUAL][i])); + if (mask & 4) buf += plot_printf(buf, 100, "%f ", value(measured[TRACE_STORED][i])); + if (mask & 8) buf += plot_printf(buf, 100, "%f", value(measured[TRACE_TEMP][i])); + buf += plot_printf(buf, 100, "\r\n"); + res = f_write(fs_file, (char *)spi_buffer, buf - (char *)spi_buffer, &size); + if (res != FR_OK) + break; + } + } + close_file(res); +} + #endif static int diff --git a/ui_sa.c b/ui_sa.c index 4a622f2..133c187 100644 --- a/ui_sa.c +++ b/ui_sa.c @@ -1376,6 +1376,14 @@ static UI_FUNCTION_ADV_CALLBACK(menu_storage_acb) } else set_auto_reflevel(true); break; +#ifdef TINYSA4 + case 4: + save_to_sd(1+2); // frequencies + actual + break; + case 5: + save_to_sd(1+4); // frequencies + stored + break; +#endif } ui_mode_normal(); // draw_cal_status(); @@ -2529,6 +2537,10 @@ static const menuitem_t menu_storage[] = { { MT_ADV_CALLBACK,1, "CLEAR\nSTORED", menu_storage_acb}, { MT_ADV_CALLBACK,2, "SUBTRACT\nSTORED",menu_storage_acb}, { MT_ADV_CALLBACK,3, "NORMALIZE", menu_storage_acb}, +#ifdef TINYSA4 + { MT_ADV_CALLBACK,4, "ACTUAL\n"S_RARROW"SD", menu_storage_acb}, + { MT_ADV_CALLBACK,5, "STORED\n"S_RARROW"SD", menu_storage_acb}, +#endif { MT_CANCEL, 0, S_LARROW" BACK", NULL }, { MT_NONE, 0, NULL, NULL } // sentinel };