diff --git a/main.c b/main.c index de5e6a1..6efb6b0 100644 --- a/main.c +++ b/main.c @@ -63,7 +63,22 @@ char shell_line[VNA_SHELL_MAX_LENGTH]; char *shell_args[VNA_SHELL_MAX_ARGUMENTS + 1]; uint16_t shell_nargs; static volatile vna_shellcmd_t shell_function = 0; -static volatile bool shell_function_ui; // shell_function may also run in a UI wait loop (CMD_RUN_IN_UI) +static volatile uint8_t shell_function_flags; // CMD_xxx of shell_function: CMD_RUN_IN_UI also runs it in a UI wait loop + +// Some commands can executed only in sweep thread, not in main cycle +#define CMD_WAIT_MUTEX 1 +#define CMD_RUN_IN_LOAD 2 +#define CMD_RUN_IN_UI 4 +#ifdef __USE_USB_MSC__ +#define CMD_DISK_OK 8 // allowed in USB DISK mode, does not use SPI1 or spi_buffer +#else +#define CMD_DISK_OK 0 +#endif +#ifdef __UI_OFF__ +#define CMD_UI_ON 16 // needs the user at the tinySA: ui on before it runs (with CMD_WAIT_MUTEX) +#else +#define CMD_UI_ON 0 +#endif //#define ENABLED_DUMP // Allow get threads debug info @@ -90,6 +105,7 @@ void update_frequencies(void); static void set_frequencies(freq_t start, freq_t stop, uint16_t points); static bool sweep(bool break_on_operation); static long_t my_atoi(const char *p); +static bool shell_check_connect(void); uint8_t sweep_mode = SWEEP_ENABLE; uint16_t sweep_once_count = 1; @@ -152,11 +168,12 @@ void shell_run_pending(bool ui_wait) { chSysLock(); vna_shellcmd_t local_func = shell_function; - if (ui_wait && !shell_function_ui) + uint8_t local_flags = shell_function_flags; + if (ui_wait && !(local_flags & CMD_RUN_IN_UI)) local_func = 0; else { shell_function = 0; - shell_function_ui = false; + shell_function_flags = 0; // The shell sets OP_CONSOLE together with shell_function, a request without a // command (direct command, timed out command) has done its job: the sweep stopped operation_requested &= ~OP_CONSOLE; @@ -166,6 +183,10 @@ void shell_run_pending(bool ui_wait) if (local_func) { if (!ui_wait) // a UI wait loop keeps a pending touch or lever event operation_requested = OP_NONE; // otherwise commands will be aborted +#ifdef __UI_OFF__ + if (local_flags & CMD_UI_ON) // the command needs the user at the tinySA + set_ui(true); +#endif local_func(shell_nargs - 1, &shell_args[1]); if (ui_wait || operation_requested == OP_NONE) // Don't prompt if aborted @@ -315,6 +336,12 @@ static THD_FUNCTION(Thread1, arg) usb_disk_mode(); } #endif +#ifdef __UI_OFF__ + if (ui_off && !shell_check_connect()) { // an unplugged tinySA must not stay blind + set_ui(true); + ui_on_notice = false; // no PC to tell + } +#endif // START_PROFILE // Process UI inputs if (!(sweep_mode & SWEEP_SELFTEST)) @@ -1039,6 +1066,21 @@ VNA_SHELL_FUNCTION(cmd_release) #endif +#ifdef __UI_OFF__ +VNA_SHELL_FUNCTION(cmd_ui) +{ + if (argc == 0) { + shell_printf("ui %s" VNA_SHELL_NEWLINE_STR, ui_off ? "off" : "on"); + return; + } + int m = generic_option_cmd("ui", "off|on", argc, argv[0]); + if (m >= 0) { + set_ui(m); + ui_on_notice = false; // the PC asked for it + } +} +#endif + #ifdef TINYSA4 // Row y of the screen for a capture, y counting up from 0: the screen is read as one DMA stream, // the next row into the other half of buf (2 * LCD_WIDTH pixels) while the caller uses row y. @@ -2758,15 +2800,6 @@ typedef struct { } VNAShellCommand; #pragma pack(pop) -// Some commands can executed only in sweep thread, not in main cycle -#define CMD_WAIT_MUTEX 1 -#define CMD_RUN_IN_LOAD 2 -#define CMD_RUN_IN_UI 4 -#ifdef __USE_USB_MSC__ -#define CMD_DISK_OK 8 // allowed in USB DISK mode, does not use SPI1 or spi_buffer -#else -#define CMD_DISK_OK 0 -#endif static const VNAShellCommand commands[] = { {"version" , cmd_version , CMD_DISK_OK}, @@ -2799,8 +2832,8 @@ static const VNAShellCommand commands[] = {"zero" , cmd_zero , CMD_WAIT_MUTEX | CMD_RUN_IN_LOAD}, // Will set the scanraw measured value offset (128 or 174) {"sweep" , cmd_sweep , CMD_WAIT_MUTEX | CMD_RUN_IN_LOAD}, {"test" , cmd_test , 0}, - {"touchcal" , cmd_touchcal , CMD_WAIT_MUTEX}, - {"touchtest" , cmd_touchtest , CMD_WAIT_MUTEX}, + {"touchcal" , cmd_touchcal , CMD_WAIT_MUTEX | CMD_UI_ON}, + {"touchtest" , cmd_touchtest , CMD_WAIT_MUTEX | CMD_UI_ON}, {"pause" , cmd_pause , CMD_WAIT_MUTEX | CMD_RUN_IN_LOAD}, #ifdef TINYSA4 {"restart" , cmd_restart , CMD_WAIT_MUTEX | CMD_RUN_IN_LOAD}, @@ -2830,6 +2863,9 @@ static const VNAShellCommand commands[] = {"refresh" , cmd_refresh , CMD_DISK_OK}, {"touch" , cmd_touch , CMD_DISK_OK}, {"release" , cmd_release , CMD_DISK_OK}, +#endif +#ifdef __UI_OFF__ + {"ui" , cmd_ui , CMD_WAIT_MUTEX}, // draws: runs in the sweep thread #endif {"vbat" , cmd_vbat , CMD_WAIT_MUTEX}, // Uses same adc as touch!!!!! #ifdef ENABLE_VBAT_OFFSET_COMMAND @@ -2876,10 +2912,14 @@ static const VNAShellCommand commands[] = { "ext_gain", cmd_ext_gain, CMD_RUN_IN_LOAD}, { "output", cmd_output, CMD_RUN_IN_LOAD }, { "deviceid", cmd_deviceid, CMD_RUN_IN_LOAD }, +#ifdef __UI_OFF__ + { "selftest", cmd_selftest, CMD_WAIT_MUTEX | CMD_UI_ON }, // ui on: in the sweep thread +#else { "selftest", cmd_selftest, 0 }, +#endif { "correction", cmd_correction, CMD_RUN_IN_LOAD }, { "calc", cmd_calc, CMD_WAIT_MUTEX | CMD_RUN_IN_LOAD}, - { "menu", cmd_menu, CMD_WAIT_MUTEX | CMD_RUN_IN_LOAD }, + { "menu", cmd_menu, CMD_WAIT_MUTEX | CMD_RUN_IN_LOAD | CMD_UI_ON }, { "text", cmd_text, CMD_RUN_IN_LOAD}, { "remark", cmd_remark, CMD_RUN_IN_LOAD }, #ifdef ENABLE_SD_CARD_CMD @@ -3198,6 +3238,14 @@ static int VNAShell_readLine(char *line, int max_size) // static void VNAShell_executeLine(char *line) { +#ifdef __UI_OFF__ + // The UI came back without "ui on" (touch, jog, a command that needs the user): tell the PC + // here, in this thread before the reply, so it never splits other console output + if (ui_on_notice) { + ui_on_notice = false; + shell_printf("ui on" VNA_SHELL_NEWLINE_STR); + } +#endif // Execute line const VNAShellCommand *scp = VNAShell_parceLine(line); if (scp) { @@ -3232,7 +3280,7 @@ static void VNAShell_executeLine(char *line) } if (cmd_flag & CMD_WAIT_MUTEX) { chSysLock(); - shell_function_ui = cmd_flag & CMD_RUN_IN_UI; + shell_function_flags = cmd_flag; shell_function = scp->sc_function; operation_requested|=OP_CONSOLE; // this will abort current sweep to give priority to the new request chSysUnlock(); diff --git a/nanovna.h b/nanovna.h index e49a30f..11f1b13 100644 --- a/nanovna.h +++ b/nanovna.h @@ -119,6 +119,7 @@ #define __VBW__ #define __SWEEP_RESTART__ #define __BURST__ // Burst modulation: TIM8 on PB9 keys the OOK of the Si4468 (GPIO1, normally CTS) +#define __UI_OFF__ // ui off: no LCD drawing while a PC is in control, a touch gives the UI back // #define DIRECT_CORRECTION // Not enough space for config in one flash page. #define DB_PER_DEGREE_BELOW 0.056 #define DB_PER_DEGREE_ABOVE 0.069 @@ -419,7 +420,9 @@ extern freq_t ultra_start; //extern bool ultra; #endif #ifdef TINYSA4 -extern float measured_noise_figure; +#ifdef __NOISE_FIGURE__ +float noise_figure(void); +#endif extern float *drive_dBm; extern bool level_error; extern bool depth_error; @@ -1672,6 +1675,14 @@ void refresh_sweep_menu(int i); void save_to_sd(int mask); void drawMessageBox(const char *header, char *text, uint32_t delay); bool isFullScreenMode(void); +#ifdef __UI_OFF__ +extern bool ui_off; // the screen shows the ui off message, the drawing functions return at once +extern volatile bool ui_on_notice; +void set_ui(bool on); +#define UI_OFF ui_off +#else +#define UI_OFF false +#endif int btn_side(void); extern int si5351_available; diff --git a/plot.c b/plot.c index 9348ada..5230f8d 100644 --- a/plot.c +++ b/plot.c @@ -404,6 +404,24 @@ marker_to_value(const int i) return(v); } +#ifdef __NOISE_FIGURE__ +// Noise figure of marker 0 in the NF measurements, for the marker info and for nf_store +float noise_figure(void) +{ + float nf = marker_to_value(0) + 173.93f - nf_gain; // measured noise figure at 20C + if (setting.measurement != M_NF_TINYSA) { + float mnf = expf(nf/10.0f * logf(10.0f)); // measure noise factor + float tnf = expf(config.noise_figure/10.0f * logf(10.0f)); // tinySA noise factor + float amp_gain = expf(nf_gain/10.0f * logf(10.0f)); + float anf = mnf - (tnf - 1.0f)/amp_gain; + nf = 10.0f*logf(anf)/logf(10.0f); + } + // powf(10,x) = expf(x * logf(10)) + // log10f(x) = logf(x)/logf(10) + return nf; +} +#endif + #ifdef TINYSA3 float sa_sqrtf(const float x) { @@ -1391,6 +1409,7 @@ draw_cell(int m, int n) static void draw_all_cells(bool flush_markmap) { + if (UI_OFF) return; // redraw_marker(), redraw_trigger(): draw_all() is gated already int m, n; // START_PROFILE for (n = 0; n < (area_height+CELLHEIGHT-1) / CELLHEIGHT; n++){ @@ -1941,34 +1960,21 @@ static void cell_draw_marker_info(int x0, int y0) } #ifdef __NOISE_FIGURE__ } else if (i>=2 && (setting.measurement == M_NF_TINYSA || setting.measurement == M_NF_VALIDATE || setting.measurement == M_NF_AMPLIFIER) && markers[0].enabled) { - float aNP = 0; - aNP = marker_to_value(0); - measured_noise_figure = aNP + 173.93 - nf_gain; // measured noise figure at 20C - if (setting.measurement != M_NF_TINYSA) { - float mnf = expf(measured_noise_figure/10.0 * logf(10)); // measure noise factor - float tnf = expf(config.noise_figure/10.0 * logf(10.0)); // tinySA noise factor - float amp_gain = expf(nf_gain/10.0 * logf(10.0)); - float anf = mnf - (tnf - 1.0)/amp_gain; - measured_noise_figure = 10.0*logf(anf)/logf(10.0); - } - // powf(10,x) = expf(x * logf(10)) - // log10f(x) = logf(x)/logf(10) - - + float nf = noise_figure(); ili9341_set_foreground(marker_color(0)); // j = 1; int xpos = 1 + (j%2)*(WIDTH/2) + CELLOFFSETX - x0; int ypos = 1 + (j/2)*(16) - y0; if (setting.measurement == M_NF_TINYSA) { - cell_printf(xpos, ypos, FONT_b"TINYSA NF: %4.1f", measured_noise_figure); + cell_printf(xpos, ypos, FONT_b"TINYSA NF: %4.1f", nf); } else { if (setting.measurement == M_NF_VALIDATE) - cell_printf(xpos, ypos, FONT_b"TINYSA NF ERROR: %4.1f", measured_noise_figure); + cell_printf(xpos, ypos, FONT_b"TINYSA NF ERROR: %4.1f", nf); else - cell_printf(xpos, ypos, FONT_b"GAIN: %4.1fdB NF: %4.1f", nf_gain, measured_noise_figure); + cell_printf(xpos, ypos, FONT_b"GAIN: %4.1fdB NF: %4.1f", nf_gain, nf); } #ifdef __LEVEL_METER__ - plot_printf(level_text, sizeof(level_text), "%4.1f", measured_noise_figure); + plot_printf(level_text, sizeof(level_text), "%4.1f", nf); #endif break; @@ -2051,7 +2057,7 @@ draw_frequencies(void) { char buf1[40]; char buf2[32]; - if (MODE_OUTPUT(setting.mode)) // No frequencies during output + if (MODE_OUTPUT(setting.mode) || UI_OFF) // No frequencies during output or ui off return; if (current_menu_is_form() && !in_selftest) return; @@ -2207,6 +2213,7 @@ request_to_redraw_grid(void) void redraw_frame(void) { + if (UI_OFF) return; ili9341_set_background(LCD_BG_COLOR); ili9341_clear_screen(); draw_frequencies(); @@ -2473,7 +2480,7 @@ set_level_meter_or_waterfall(void) else graph_bottom = NO_WATERFALL; _grid_y = graph_bottom / NGRIDY; area_height = AREA_HEIGHT_NORMAL; - if (setting.waterfall != W_OFF || setting.level_meter){ + if ((setting.waterfall != W_OFF || setting.level_meter) && !UI_OFF){ ili9341_set_background(LCD_BG_COLOR); ili9341_fill(OFFSETX, graph_bottom, LCD_WIDTH - OFFSETX, CHART_BOTTOM - graph_bottom); } diff --git a/sa_cmd.c b/sa_cmd.c index 8d462fc..8c11736 100644 --- a/sa_cmd.c +++ b/sa_cmd.c @@ -426,9 +426,25 @@ VNA_SHELL_FUNCTION(cmd_level) set_level(f); } +#ifdef __UI_OFF__ +// The actual RBW and sweep time of changed settings. While paused the cal status calculates +// them, but with the UI off it does not draw. +static void update_actual_values(void) +{ + if (dirty) { + update_rbw(); + calculate_step_delay(); + setting.actual_sweep_time_us = calc_min_sweep_time_us(); + } +} +#endif + VNA_SHELL_FUNCTION(cmd_sweeptime) { if (argc != 1 || argv[0][0] == '?') { +#ifdef __UI_OFF__ + update_actual_values(); +#endif usage_printf("sweeptime 0.003..60\r\n%5.3Fs\r\n",((float)setting.actual_sweep_time_us)/ONE_SECOND_TIME); return; } @@ -618,6 +634,9 @@ VNA_SHELL_FUNCTION(cmd_rbw) { if (argc != 1 || argv[0][0] == '?') { usage: +#ifdef __UI_OFF__ + update_actual_values(); +#endif #ifdef TINYSA4 usage_printf("rbw 0.2..850|auto\r\n%.1FHz\r\n", actual_rbw_x10*100.0); #else @@ -1376,9 +1395,11 @@ again: dirty = true; // adc_stop_analog_watchdog(); int oldpos = 0; - ili9341_set_background(LCD_BG_COLOR); - ili9341_fill(OFFSETX, CHART_BOTTOM+1, WIDTH, 1); - ili9341_set_background(LCD_SWEEP_LINE_COLOR); + if (!UI_OFF) { // ui off: no progress bar + ili9341_set_background(LCD_BG_COLOR); + ili9341_fill(OFFSETX, CHART_BOTTOM+1, WIDTH, 1); + ili9341_set_background(LCD_SWEEP_LINE_COLOR); + } #define BUFFER_SIZE 64 uint8_t buf[BUFFER_SIZE]; int idx = 0; @@ -1395,7 +1416,7 @@ again: idx = 0; } int pos = i * (WIDTH+1) / points; - if (pos - oldpos > 8) { + if (pos - oldpos > 8 && !UI_OFF) { ili9341_fill(OFFSETX + oldpos, CHART_BOTTOM+1, pos - oldpos, 1); // update sweep progress bar oldpos = pos; } @@ -1405,8 +1426,10 @@ again: // adc_start_analog_watchdog(); if ((unbuffered & 2) && !operation_requested) goto again; - ili9341_set_background(LCD_BG_COLOR); - ili9341_fill(OFFSETX, CHART_BOTTOM+1, WIDTH, 1); + if (!UI_OFF) { + ili9341_set_background(LCD_BG_COLOR); + ili9341_fill(OFFSETX, CHART_BOTTOM+1, WIDTH, 1); + } setting.frequency_step = old_step; dirty = true; redraw_request = 0; // disable screen update in this mode diff --git a/ui.c b/ui.c index 305b206..6a92fbc 100644 --- a/ui.c +++ b/ui.c @@ -193,7 +193,44 @@ static void draw_button(uint16_t x, uint16_t y, uint16_t w, uint16_t h, ui_butto //svoid save_csv(uint8_t mask); //#endif +#ifdef __UI_OFF__ +bool ui_off = false; +volatile bool ui_on_notice = false; // the UI is back: "ui on" starts the next console reply + +// ui off: the screen shows a message and the console never waits for the screen. A touch or +// the jog (ui_process), ui on, a command that needs the user (CMD_UI_ON), USB DISK mode or a +// lost connection give the UI back. +void set_ui(bool on) +{ + if (on == !ui_off) // no change + return; + if (on) { + ui_off = false; + ui_on_notice = true; // cleared by "ui on" and a lost connection: no need to tell + redraw_frame(); + request_to_redraw_grid(); + if (ui_mode != UI_NORMAL) // the menu of the output modes + draw_menu(); + return; + } + static const char msg[] = "Remote only mode. Touch screen to enable UI"; + int w = 0; + for (const char *p = msg; *p; p++) + w += wFONT_GET_WIDTH(*p); +#ifdef __SD_FILE_BROWSER__ + if (ui_mode == UI_BROWSER) // set_ui(true) can not redraw the browser + ui_mode_normal(); +#endif + ili9341_set_foreground(LCD_FG_COLOR); + ili9341_set_background(LCD_BG_COLOR); + ili9341_clear_screen(); + ili9341_drawstring_10x14(msg, (LCD_WIDTH - w)/2, (LCD_HEIGHT - wFONT_GET_HEIGHT)/2); + ui_off = true; +} +#endif + bool isFullScreenMode(void) { + if (UI_OFF) return true; // draw_all() and the sweep progress bar draw nothing #ifdef __SD_FILE_BROWSER__ return ui_mode == UI_BROWSER; #else @@ -1880,8 +1917,6 @@ UI_FUNCTION_CALLBACK(menu_curve_confirm_cb) menu_move_back(false); } -float measured_noise_figure; - #if 0 UI_FUNCTION_CALLBACK(menu_noise_figure_confirm_cb) { @@ -3038,6 +3073,11 @@ static UI_FUNCTION_ADV_CALLBACK(menu_measure_acb) return; } menu_move_back(false); +#ifdef __NOISE_FIGURE__ + // Store the NF of a running TINYSA NF measurement: here, before markers_reset(), as the marker + // info would show it (with the UI off nothing draws it) + float nf = (data == M_NF_STORE && setting.measurement == M_NF_TINYSA) ? noise_figure() : 0; +#endif markers_reset(); if ((data != M_OFF && setting.measurement != M_OFF) || data == M_OFF ) @@ -3262,8 +3302,8 @@ static UI_FUNCTION_ADV_CALLBACK(menu_measure_acb) nf_gain = 0; goto noise_figure; case M_NF_STORE: - if (measured_noise_figure > 2 && measured_noise_figure < 20) { - config.noise_figure = measured_noise_figure; + if (nf > 2 && nf < 20) { + config.noise_figure = nf; config_save(); data = M_NF_VALIDATE; // Continue to validate goto validate; @@ -4520,6 +4560,9 @@ void usb_disk_mode(void) if ((disk_initialize(0) & STA_NOINIT) || disk_ioctl(0, GET_SECTOR_COUNT, &blocks) != RES_OK || blocks == 0) error = "SD card error"; } +#ifdef __UI_OFF__ + set_ui(true); // this screen needs the user +#endif ili9341_set_foreground(LCD_FG_COLOR); ili9341_set_background(LCD_BG_COLOR); ili9341_clear_screen(); @@ -6582,6 +6625,7 @@ const char *month[] = { "Jan", "Feb", "Mar","Apr","May","Jun","Jul","Aug","Sep", void draw_cal_status(void) { + if (UI_OFF) return; #define BLEN 7 char buf[BLEN+1]; buf[6]=0; @@ -7163,6 +7207,12 @@ draw_button(uint16_t x, uint16_t y, uint16_t w, uint16_t h, ui_button_t *b) } void drawMessageBox(const char *header, char *text, uint32_t delay){ +#ifdef __UI_OFF__ + if (ui_off) { // the PC in control gets the message, without the delay + shell_printf("%s: %s\r\n", header, text); + return; + } +#endif ui_button_t b; b.bg = LCD_MENU_COLOR; b.fg = LCD_MENU_TEXT_COLOR; @@ -7424,6 +7474,7 @@ static const uint8_t check_box[] = { static void draw_menu_buttons(const menuitem_t *menu, uint32_t mask) { + if (UI_OFF) return; int i, y; ui_button_t button; const menuitem_t *m = menu; @@ -7840,7 +7891,7 @@ leave_ui_mode() if (setting.waterfall) set_level_meter_or_waterfall(); #ifdef __LEVEL_METER__ - if (setting.level_meter) + if (setting.level_meter && !UI_OFF) ili9341_fill(LCD_WIDTH-MENU_BUTTON_WIDTH, area_height, MENU_BUTTON_WIDTH, menu_button_height*MENU_BUTTON_MAX - area_height); #endif redraw_request|=REDRAW_AREA | REDRAW_FREQUENCY | REDRAW_CAL_STATUS | REDRAW_BATTERY; @@ -8834,7 +8885,7 @@ ui_process(void) { #ifdef __USE_SD_CARD__ // SD icon repeat, only from the normal screen: a capture would close an open menu - if (sd_repeat && setting.interval && ui_mode == UI_NORMAL && + if (sd_repeat && setting.interval && ui_mode == UI_NORMAL && !UI_OFF && chVTGetSystemTimeX() - sd_repeat_start >= (systime_t)setting.interval * CH_CFG_ST_FREQUENCY) sd_icon_save(); #endif @@ -8844,9 +8895,17 @@ ui_process(void) selection = -1; // hide keyboard mode selection ui_mode_menu(); } +#ifdef __UI_OFF__ + bool wake = false; // ui off: a jog press or a touch only gives the UI back +#endif // Clear only the handled request: the shell may have set OP_CONSOLE meanwhile for a // waiting command, without it an output mode sweep never stops to run that command if ((operation_requested&OP_LEVER) || previous_button_state != button_state) { +#ifdef __UI_OFF__ + if (ui_off) + wake = btn_check() != 0; // a press, also left or right, the release is no event + else +#endif ui_process_lever(); previous_button_state = button_state; chSysLock(); @@ -8854,11 +8913,23 @@ ui_process(void) chSysUnlock(); } if (operation_requested&OP_TOUCH) { +#ifdef __UI_OFF__ + if (ui_off) { + if (touch_check() != EVT_TOUCH_NONE) { + touch_wait_release(); + wake = true; + } + } else +#endif ui_process_touch(); chSysLock(); operation_requested &= ~OP_TOUCH; chSysUnlock(); } +#ifdef __UI_OFF__ + if (wake) + set_ui(true); +#endif touch_start_watchdog(); }