Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-tenma-white committed Oct 31, 2020
2 parents 9898491 + db2d505 commit 1328dd5
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 124 deletions.
1 change: 1 addition & 0 deletions globals.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ extern int16_t lastsaveid;
extern properties_t *active_props;
extern properties_t current_props;

#define MARKER_INVALID -1
extern int8_t previous_marker;


Expand Down
32 changes: 30 additions & 2 deletions main2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ volatile uint32_t systemTimeCounter = 0;
static FIFO<small_function<void()>, 8> eventQueue;

static volatile bool usbDataMode = false;
static volatile bool usbCaptureMode = false;

static freqHz_t currFreqHz = 0; // current hardware tx frequency
int currThruGain = 0; // gain setting used for this thru measurement
Expand Down Expand Up @@ -828,6 +829,27 @@ static void setVNASweepToUSB() {
#endif
}
static void cmdRegisterWrite(int address) {
if(address == 0xee) {
usbCaptureMode = true;

constexpr struct {
uint16_t width;
uint16_t height;
uint8_t pixelFormat;
} meta = { LCD_WIDTH, LCD_HEIGHT, 16 };

serial.print((char*) &meta, sizeof(meta));

// use uint16_t ili9341_spi_buffers for read buffer
static_assert(meta.width * 2 <= sizeof(ili9341_spi_buffers));
for (int y=0; y < meta.height; y+=1){
ili9341_read_memory(0, y, meta.width, 1, meta.width*2, ili9341_spi_buffers);
serial.print((char*) ili9341_spi_buffers, meta.width * 2 * 1);
}

usbCaptureMode = false;
return;
}
if(!usbDataMode)
enterUSBDataMode();
if(address == 0x00 || address == 0x10 || address == 0x20 || address == 0x22) {
Expand Down Expand Up @@ -1246,10 +1268,10 @@ cal_interpolate(void)
return;

freqHz_t src_start = src->startFreqHz();
freqHz_t src_stop = src->stopFreqHz();
//freqHz_t src_stop = src->stopFreqHz();
freqHz_t src_step = src->stepFreqHz();
freqHz_t dst_start = dst->startFreqHz();
freqHz_t dst_stop = dst->stopFreqHz();
//freqHz_t dst_stop = dst->stopFreqHz();
freqHz_t dst_step = dst->stepFreqHz();

// lower than start freq of src range
Expand Down Expand Up @@ -1601,6 +1623,10 @@ int main(void) {
while(true) {
// process any outstanding commands from usb
cmdInputFIFO.drain();
if (usbCaptureMode) {
continue;
}

if(usbDataMode) {
if(outputRawSamples)
usb_transmit_rawSamples();
Expand Down Expand Up @@ -2067,6 +2093,8 @@ namespace UIActions {
}

void application_doSingleEvent() {
// process any outstanding commands from usb
cmdInputFIFO.drain();
if(eventQueue.readable()) {
auto callback = eventQueue.read();
eventQueue.dequeue();
Expand Down
2 changes: 1 addition & 1 deletion mculib
45 changes: 20 additions & 25 deletions plot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1171,14 +1171,6 @@ draw_marker(int x, int y, int c, int ch)
}
}

void
marker_position(int m, int t, int *x, int *y)
{
uint32_t index = trace_index[t][markers[m].index];
*x = CELL_X(index);
*y = CELL_Y(index);
}

typedef int (*compare_t)(int x, int y);
static int greater(int x, int y) { return x > y; }
static int lesser(int x, int y) { return x < y; }
Expand Down Expand Up @@ -1274,21 +1266,23 @@ marker_search_right(MarkerSearchModes mode, int from)
}


int
distance_to_index(int8_t t, uint16_t idx, int16_t x, int16_t y)
{
uint32_t index = trace_index[t][idx];
x-= CELL_X(index);
y-= CELL_Y(index);
return x*x + y*y;
}

int
search_nearest_index(int x, int y, int t)
{
uint32_t *index = trace_index[t];
int min_i = -1;
int min_d = 1000;
int min_d = MARKER_PICKUP_DISTANCE * MARKER_PICKUP_DISTANCE;
int i;
for (i = 0; i < sweep_points; i++) {
int16_t dx = x - CELL_X(index[i]);
int16_t dy = y - CELL_Y(index[i]);
if (dx < 0) dx = -dx;
if (dy < 0) dy = -dy;
if (dx > 20 || dy > 20)
continue;
int d = dx*dx + dy*dy;
int d = distance_to_index(t, i, x , y);
if (d < min_d) {
min_d = d;
min_i = i;
Expand Down Expand Up @@ -1539,7 +1533,7 @@ draw_all(bool flush)
void
request_to_redraw_marker(int marker)
{
if (marker < 0)
if (marker == MARKER_INVALID)
return;
// mark map on new position of marker
redraw_request |= REDRAW_MARKER;
Expand Down Expand Up @@ -1609,11 +1603,11 @@ cell_draw_marker_info(int x0, int y0)
{
char buf[24];
int t;
if (active_marker < 0)
if (active_marker == MARKER_INVALID)
return;
int idx = markers[active_marker].index;
int j = 0;
if (active_marker != -1 && previous_marker != -1 && uistat.current_trace != -1) {
if (previous_marker != MARKER_INVALID && uistat.current_trace != -1) {
int t = uistat.current_trace;
int mk;
for (mk = 0; mk < MARKERS_MAX; mk++) {
Expand Down Expand Up @@ -1646,15 +1640,16 @@ cell_draw_marker_info(int x0, int y0)
}

// draw marker delta
if (!uistat.marker_delta && previous_marker >= 0 && active_marker != previous_marker && markers[previous_marker].enabled) {
if (!uistat.marker_delta && active_marker != previous_marker) {
int idx0 = markers[previous_marker].index;
int xpos = (WIDTH/2+30) + CELLOFFSETX - x0;
int ypos = 1 + (j/2)*(FONT_STR_HEIGHT) - y0;
strcpy(buf, S_DELTA "1:"); if (mk == active_marker) buf[0] = S_SARROW[0];
buf[1] += previous_marker;
strcpy(buf, S_DELTA "1-1:"); if (mk == active_marker) buf[0] = S_SARROW[0];
buf[1] += active_marker;
buf[3] += previous_marker;
ili9341_set_foreground(0xFFFF);
cell_drawstring(buf, xpos, ypos);
xpos += 3*FONT_WIDTH + 3;
xpos += 5*FONT_WIDTH + 5;
if ((domain_mode & DOMAIN_MODE) == DOMAIN_FREQ) {
frequency_string(buf, sizeof buf, freqAt(idx) - freqAt(idx0));
} else {
Expand Down Expand Up @@ -1869,7 +1864,7 @@ draw_cal_status(void)
ili9341_drawstring(c, x, y);
}
y += FONT_STR_HEIGHT;
int i;
uint16_t i;
static const struct {char text[2]; uint16_t mask;} calibration_text[]={
{"S", CALSTAT_SHORT},
{"O", CALSTAT_OPEN},
Expand Down
2 changes: 1 addition & 1 deletion plot.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ void draw_cal_status(void);

void markmap_all_markers(void);

void marker_position(int m, int t, int *x, int *y);
int distance_to_index(int8_t t, uint16_t idx, int16_t x, int16_t y);
int search_nearest_index(int x, int y, int t);

int marker_search(MarkerSearchModes mode);
Expand Down
21 changes: 20 additions & 1 deletion python/nanovna.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,26 @@ def scan(self):
return (self.data(0), self.data(1))

def capture(self):
raise NotImplementedError()
from PIL import Image

self.open()

# reset protocol to known state
self.serial.write([0,0,0,0,0,0,0,0])

self.serial.write([0x20, 0xee, 0x00])
meta = self.serial.read(2 + 2 + 1)
print(meta)

self.serial.timeout = 10
width, height, pixel = struct.unpack('<HHB', meta)
b = self.serial.read(width * height * 2)
x = struct.unpack(">" + str(width * height) + "H", b)

# convert pixel format from 565(RGB) to 8888(RGBA)
arr = np.array(x, dtype=np.uint32)
arr = 0xFF000000 + ((arr & 0xF800) >> 8) + ((arr & 0x07E0) << 5) + ((arr & 0x001F) << 19)
return Image.frombuffer('RGBA', (width, height), arr, 'raw', 'RGBA', 0, 1)



Expand Down
Loading

0 comments on commit 1328dd5

Please sign in to comment.