Commit 16d82f58 authored by Matthieu Cattin's avatar Matthieu Cattin

sw: First working prototype of the path widget.

parent dc1a284e
#include "path_widget.h" #include "path_widget.h"
#include <stdlib.h>
#include <stdio.h>
void find_path_min_max(struct coord *d, uint16_t d_size, struct coord *min, struct coord *max)
{
uint16_t i;
uint8_t j;
min->lat = d[0].lat;
max->lat = d[0].lat;
min->lon = d[0].lon;
max->lon = d[0].lon;
min->alt = d[0].alt;
max->alt = d_size;
for(i=0; i<d_size; i++){
if(d[i].lat < min->lat)
min->lat = d[i].lat;
if(d[i].lat > max->lat)
max->lat = d[i].lat;
}
DBG("find_min_max: d_size=%d\n",d_size);
}
void draw_path(struct surface *surf, path *p) void draw_path(struct surface *surf, path *p)
{ {
uint8_t i; uint8_t i;
struct coord min; coord min;
struct coord max; coord max;
struct coord *p_min; double x_scale, y_scale;
struct coord *p_max;
uint8_t x_scale, y_scale, x_win;
int8_t y_pos, y_pos_next; int8_t y_pos, y_pos_next;
int8_t x_pos, x_pos_next;
char buf[20]; char buf[20];
p_min = &min; min.lat = p->d[0].lat;
p_max = &max; max.lat = p->d[0].lat;
min.lon = p->d[0].lon;
max.lon = p->d[0].lon;
min.alt = p->d[0].alt;
max.alt = p->d[0].alt;
for(i=0; i<p->d_size; i++) for(i=0; i<p->d_size; i++)
{ {
DBG("data: %8d, %8d, %8d\n",p->d[i].lat, p->d[i].lon, p->d[i].alt); DBG("data: %f, %f, %f\n",p->d[i].lat, p->d[i].lon, p->d[i].alt);
} }
DBG("draw_path: d_size=%d, i=%d\n",p->d_size, i); DBG("draw_path: d_size=%d, i=%d\n",p->d_size, i);
// Find data min/max // Find data min/max
find_min_max(p->d, p->d_size, p_min, p_max); for(i=0; i<p->d_size; i++){
DBG("lat: min: %8d, max: %8d, max-min: %8d\n", min.lat, max.lat, (max.lat - min.lat)); if(p->d[i].lat < min.lat)
DBG("lon: min: %8d, max: %8d, max-min: %8d\n", min.lon, max.lon, (max.lon - min.lon)); min.lat = p->d[i].lat;
DBG("alt: min: %8d, max: %8d, max-min: %8d\n", min.alt, max.alt, (max.alt - min.alt)); if(p->d[i].lat > max.lat)
max.lat = p->d[i].lat;
/*
// Compute x scaling factors if(p->d[i].lon < min.lon)
// If data size is smaller than the plot size, don't stretch data min.lon = p->d[i].lon;
if(p->d_size < p->x_size){ if(p->d[i].lon > max.lon)
x_scale = 1; max.lon = p->d[i].lon;
x_win = p->d_size;
} if(p->d[i].alt < min.alt)
else{ min.alt = p->d[i].alt;
x_scale = p->d_size / p->x_size; if(p->d[i].alt > max.alt)
x_win = p->x_size; max.alt = p->d[i].alt;
} }
//find_min_max(p->d, p->d_size, p_min, p_max);
DBG("lat: min: %f, max: %f, max-min: %f\n", min.lat, max.lat, (max.lat - min.lat));
DBG("lon: min: %f, max: %f, max-min: %f\n", min.lon, max.lon, (max.lon - min.lon));
DBG("alt: min: %f, max: %f, max-min: %f\n", min.alt, max.alt, (max.alt - min.alt));
// Compute x scaling factor (rounded up by adding 1)
x_scale = (max.lat - min.lat) / p->x_size + 1;
// Compute y scaling factor (rounded up by adding 1) // Compute y scaling factor (rounded up by adding 1)
y_scale = (y_max - y_min) / p->y_size + 1; y_scale = (max.lon - min.lon) / p->y_size + 1;
DBG("x_scale: %d\ny_scale: %d\n",x_scale, y_scale); DBG("x_scale: %f\ny_scale: %f\n",x_scale, y_scale);
// Rescale and plot data // Rescale and plot data
for(i=0; i<x_win-1; i++){ for(i=0; i<p->d_size-1; i++){
//y_pos = (d[i*x_scale])/y_scale + y_size/2; x_pos = (p->d[i].lat - min.lat)/x_scale;
//y_pos_next = (d[(i+1)*x_scale])/y_scale + y_size/2; x_pos_next = (p->d[i+1].lat - min.lat)/x_scale;
y_pos = (p->d[i*x_scale] - y_min)/y_scale; y_pos = (p->d[i].lon - min.lon)/y_scale;
y_pos_next = (p->d[(i+1)*x_scale] - y_min)/y_scale; y_pos_next = (p->d[i+1].lon - min.lon)/y_scale;
//DBG("x=%3d, y=%3d (%d)\n", i, y_pos, d[i*x_scale]); DBG("x=%4d, y=%4d\n", x_pos, y_pos);
if(p->line) if(p->line)
gfx_line(surf, i, p->y_size-y_pos, i+1, p->y_size-y_pos_next, 1); gfx_line(surf, x_pos, p->y_size-y_pos, x_pos_next, p->y_size-y_pos_next, 1);
else else
gfx_set_pixel(surf, i, p->y_size-y_pos, 1); gfx_set_pixel(surf, x_pos, p->y_size-y_pos, 1);
} }
// Print plot title // Print plot title
DBG("title: %s",p->title); DBG("title: %s",p->title);
//gfx_text(surf, &font_xm5x8, 0, 0, p->title); //gfx_text(surf, &font_xm5x8, 0, 0, p->title);
//gfx_centered_text(surf, &font_helv11, 0, p->title); //gfx_centered_text(surf, &font_helv11, 0, p->title);
gfx_centered_text(surf, &font_xm4x6, p->y_size+1, p->title); gfx_centered_text(surf, &font_xm4x6, p->y_size+1, p->title);
/*
// Print min max // Print min max
sprintf(buf,"min:%d",y_min); sprintf(buf,"min:%d",y_min);
gfx_text(surf, &font_xm4x6, 1, p->y_size+1, buf); gfx_text(surf, &font_xm4x6, 1, p->y_size+1, buf);
sprintf(buf,"max:%d",y_max); sprintf(buf,"max:%d",y_max);
gfx_text(surf, &font_xm4x6, p->x_size-gfx_text_width(&font_xm4x6,buf), p->y_size+1, buf); gfx_text(surf, &font_xm4x6, p->x_size-gfx_text_width(&font_xm4x6,buf), p->y_size+1, buf);
*/
// Print frame, if enabled // Print frame, if enabled
if(p->frame) if(p->frame)
{ {
...@@ -105,7 +96,7 @@ void draw_path(struct surface *surf, path *p) ...@@ -105,7 +96,7 @@ void draw_path(struct surface *surf, path *p)
gfx_line(surf, p->x_size, p->y_size, 0, p->y_size, 1); gfx_line(surf, p->x_size, p->y_size, 0, p->y_size, 1);
gfx_line(surf, 0, p->y_size, 0, 0, 1); gfx_line(surf, 0, p->y_size, 0, 0, 1);
} }
*/
} }
...@@ -127,9 +118,11 @@ static void path_redraw(struct ui_widget *w) ...@@ -127,9 +118,11 @@ static void path_redraw(struct ui_widget *w)
coord data[D_SIZE]; coord data[D_SIZE];
uint16_t i; uint16_t i;
char title[20]; char title[20];
float x;
static float a=0; static float a=0;
path p; path p;
int sign;
#define DIV 1000000.0
// Clear widget // Clear widget
gfx_clear(&w->dc, 0); gfx_clear(&w->dc, 0);
...@@ -137,13 +130,23 @@ static void path_redraw(struct ui_widget *w) ...@@ -137,13 +130,23 @@ static void path_redraw(struct ui_widget *w)
// Generate test data // Generate test data
sprintf(title, "test path\n"); sprintf(title, "test path\n");
x = 0; data[0].lat = rand()/DIV;
for(i=0; i<D_SIZE; i++) data[0].lon = rand()/DIV;
data[0].alt = rand() % 1000;
for(i=1; i<D_SIZE; i++)
{ {
x+=100; sign = rand() % 2;
data[i].lat = x*i; if(sign)
data[i].lon = 100+(i*x-50); data[i].lat = data[i-1].lat + rand()/(DIV);
data[i].alt = 10+i; else
data[i].lat = data[i-1].lat - rand()/(DIV);
sign = rand() % 2;
if(sign)
data[i].lon = data[i-1].lon + rand()/(DIV);
else
data[i].lon = data[i-1].lon - rand()/(DIV);
data[i].alt = rand() % 1000;
//DBG("data: %8d, %8d, %8d\n",data[i].lat, data[i].lon, data[i].alt); //DBG("data: %8d, %8d, %8d\n",data[i].lat, data[i].lon, data[i].alt);
} }
......
...@@ -5,9 +5,9 @@ ...@@ -5,9 +5,9 @@
extern struct ui_widget path_widget; extern struct ui_widget path_widget;
typedef struct coord{ typedef struct coord{
int32_t lat; double lat;
int32_t lon; double lon;
int32_t alt; double alt;
}coord; }coord;
typedef struct path{ typedef struct path{
...@@ -19,3 +19,4 @@ typedef struct path{ ...@@ -19,3 +19,4 @@ typedef struct path{
uint8_t frame; // draw frame around plot uint8_t frame; // draw frame around plot
uint8_t line; // draw lines between data points uint8_t line; // draw lines between data points
}path; }path;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment