Commit 1cac90ee authored by Matthieu Cattin's avatar Matthieu Cattin

sw: add 2d plot widget.

parent abdf9648
OBJS=gfx.o ui.o ui_pc.o font_helv17.o font_luct38.o font_xm16x25b.o digital_watch.o font_helv38b.o font_helv29.o font_helv22b.o font_xm4x6.o font_xm5x8.o font_helv11.o
OBJS=plot_widget.o gfx.o ui.o ui_pc.o font_helv17.o font_luct38.o font_xm16x25b.o digital_watch.o font_helv38b.o font_helv29.o font_helv22b.o font_xm4x6.o font_xm5x8.o font_helv11.o
CFLAGS=`sdl-config --cflags` -I. -g -O0
LDFLAGS =`sdl-config --libs` -g -O0
......
#include "ui.h"
#include "../bitmaps/battery_charging.h"
#include "../bitmaps/battery.h"
#include "../bitmaps/gps_disconnected.h"
#include "../bitmaps/gps_searching.h"
#include "../bitmaps/gps_receiving.h"
#include "plot_widget.h"
//#include "../bitmaps/bitmaps.h"
extern int64_t sys_get_tics();
......@@ -189,6 +185,7 @@ struct ui_widget home_screen = {
static void status_bar_event(struct ui_widget *w, struct ui_event event)
{
/*
static int8_t level;
switch(event.type)
......@@ -202,7 +199,7 @@ static void status_bar_event(struct ui_widget *w, struct ui_event event)
gfx_box(&w->dc, 127-15, 1, 127-3, 11, COLOR_WHITE);
gfx_box(&w->dc, 127-15, 1, 127-15+((level * 12)/100), 11, COLOR_BLACK);
gfx_box(&w->dc, 0, 0, 15, 15, COLOR_WHITE);
gfx_draw_bitmap(&w->dc, 0, 0, &gps_disconnected);
//gfx_draw_bitmap(&w->dc, 0, 0, &gps_disconnected);
w->flags |= WF_DIRTY;
break;
}
......@@ -215,7 +212,7 @@ static void status_bar_event(struct ui_widget *w, struct ui_event event)
gfx_box(&w->dc, 127-15, 1, 127-3, 11, COLOR_WHITE);
gfx_box(&w->dc, 127-15, 1, 127-15+((level * 12)/100), 11, COLOR_BLACK);
gfx_box(&w->dc, 0, 0, 15, 15, COLOR_WHITE);
gfx_draw_bitmap(&w->dc, 0, 0, &gps_receiving);
//gfx_draw_bitmap(&w->dc, 0, 0, &gps_receiving);
w->flags |= WF_DIRTY;
break;
}
......@@ -224,13 +221,14 @@ static void status_bar_event(struct ui_widget *w, struct ui_event event)
gfx_box(&w->dc, 127-15, 1, 127-3, 11, COLOR_WHITE);
gfx_draw_bitmap(&w->dc, 127-15, 0, &battery_charging);
gfx_box(&w->dc, 0, 0, 15, 15, COLOR_WHITE);
gfx_draw_bitmap(&w->dc, 0, 0, &gps_searching);
//gfx_draw_bitmap(&w->dc, 0, 0, &gps_searching);
w->flags |= WF_DIRTY;
break;
}
default:
break;
}
*/
}
static void status_bar_redraw(struct ui_widget *w)
......@@ -238,7 +236,7 @@ static void status_bar_redraw(struct ui_widget *w)
//gfx_line(&w->dc, 0, w->pos.y1, 127, w->pos.y1, COLOR_BLACK);
gfx_round_box(&w->dc, 30, -10, 127-30, 10, 9, COLOR_BLACK);
gfx_centered_text(&w->dc, &font_helv11, 0, "Home");
gfx_draw_bitmap(&w->dc, 127-15, 0, &battery);
//gfx_draw_bitmap(&w->dc, 127-15, 0, &battery);
}
struct ui_widget status_bar = {
......@@ -249,17 +247,21 @@ struct ui_widget status_bar = {
WF_ACTIVE | WF_VISIBLE
};
void home_screen_create()
{
ui_init_widget (&home_screen);
ui_init_widget (&digital_watch);
ui_init_widget (&pls_viewer);
//ui_init_widget (&pls_viewer);
ui_init_widget (&hist_plot);
ui_add_widget(&digital_watch);
ui_add_widget(&pls_viewer);
//ui_add_widget(&pls_viewer);
ui_add_widget (&hist_plot);
ui_add_child(&home_screen, &digital_watch);
ui_add_child(&home_screen, &pls_viewer);
//ui_add_child(&home_screen, &pls_viewer);
ui_add_child (&home_screen, &hist_plot);
ui_add_widget(&home_screen);
ui_init_widget (&status_bar);
......
#include "plot_widget.h"
void find_min_max(int16_t* d, uint16_t d_size, int16_t* min, int16_t* max)
{
uint16_t i;
*min = d[0];
*max = d[0];
for(i=0; i<d_size; i++)
{
if(d[i] < *min)
*min = d[i];
if(d[i] > *max)
*max = d[i];
}
}
void draw_plot(struct surface* surf, plot* p)
{
uint8_t i;
int16_t y_min, y_max;
uint8_t x_scale, y_scale, x_win;
int8_t y_pos, y_pos_next;
char buf[20];
float t;
// Find data min/max
find_min_max(p->d, p->d_size, &y_min, &y_max);
DBG("min: %d\nmax: %d\nmax-min: %d\n",y_min, y_max,(y_max - y_min));
// Compute x scaling factors
// If data size is smaller than the plot size, don't stretch plot
if(p->d_size < p->x_size){
x_scale = 1;
x_win = p->d_size;
}
else{
x_scale = p->d_size / p->x_size;
x_win = p->x_size;
}
// Compute y scaling factor (rounded up by adding 1)
y_scale = (y_max - y_min) / p->y_size + 1;
DBG("x_scale: %d\ny_scale: %d\n",x_scale, y_scale);
// Rescale and plot data
for(i=0; i<x_win-1; i++){
//y_pos = (d[i*x_scale])/y_scale + y_size/2;
//y_pos_next = (d[(i+1)*x_scale])/y_scale + y_size/2;
y_pos = (p->d[i*x_scale] - y_min)/y_scale;
y_pos_next = (p->d[(i+1)*x_scale] - y_min)/y_scale;
//DBG("x=%3d, y=%3d (%d)\n", i, y_pos, d[i*x_scale]);
if(p->line)
gfx_line(surf, i, p->y_size-y_pos, i+1, p->y_size-y_pos_next, 1);
else
gfx_set_pixel(surf, i, p->y_size-y_pos, 1);
}
// Print data zero (dotted line)
//DBG("zero: %d\n", -y_min/y_scale);
if(p->zero){
for(i=0; i<=10; i++){
gfx_line(surf, (p->x_size/10)*i, p->y_size-(-y_min/y_scale),(p->x_size/10)*i+(p->x_size/20), p->y_size-(-y_min/y_scale), 1);
}
}
// Print plot title
DBG("title: %s",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_xm4x6, p->y_size+1, p->title);
// Print min max
sprintf(buf,"min:%d",y_min);
gfx_text(surf, &font_xm4x6, 1, p->y_size+1, buf);
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);
// Print frame, if enabled
if(p->frame)
{
gfx_line(surf, 0, 0, p->x_size, 0, 1);
gfx_line(surf, p->x_size, 0, p->x_size, 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);
}
}
static void hist_plot_event(struct ui_widget *w, struct ui_event event)
{
switch(event.type)
{
case EVT_NEXT_CYCLE:
w->flags |= WF_DIRTY;
break;
default:
break;
}
}
static void hist_plot_redraw(struct ui_widget *w)
{
#define D_SIZE 400
int16_t data[D_SIZE];
uint16_t i;
char title[20];
float x;
static float a=0;
plot p;
// Clear widget
gfx_clear(&w->dc, 0);
// Generate test data
sprintf(title, "test plot\n");
x = 0;
for(i=0; i<D_SIZE; i++)
{
x+=0.03;
data[i] = (int16_t)((200+a*100) * sin(a+x));
//data[i] = -100 + i;
}
a+=0.8;
// Draw plot
p.d = &data[0];
p.d_size = D_SIZE;
p.x_size = 127;
p.y_size = 60;
p.title = title;
p.frame = 1;
p.line = 1;
p.zero = 1;
draw_plot(&w->dc, &p);
}
struct ui_widget hist_plot = {
hist_plot_redraw,
hist_plot_event,
{ 0, 60, 127, 127 },
0,
WF_ACTIVE | WF_VISIBLE
};
#include "ui.h"
#include "math.h"
extern struct ui_widget hist_plot;
typedef struct plot{
int16_t* d; // pointer on data array
uint16_t d_size; // data array size
uint8_t x_size; // X plot size (pixel)
uint8_t y_size; // Y plot size (pixel)
char* title; // pointer on plot title string
uint8_t frame; // draw frame around plot
uint8_t line; // draw lines between data points
uint8_t zero; // draw a line showing data zero value
}plot;
......@@ -23,6 +23,7 @@
#define EVT_UP 8
#define EVT_DOWN 9
#define EVT_RIGHT 10
#define EVT_LEFT 11
#define UI_TICK_RATE 30 /* UI refresh ticks = 30 milliseconds */
......
......@@ -79,6 +79,9 @@ int sys_update()
case SDLK_RIGHT:
ui_post_event(EVT_RIGHT, 0);
break;
case SDLK_LEFT:
ui_post_event(EVT_LEFT, 0);
break;
#if 0
case SDLK_LEFT: cur_key=KEY_L; break;
case SDLK_RIGHT: cur_key=KEY_R; break;
......
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