metawatch/mw_utility.c

255 lines
5.5 KiB
C
Raw Normal View History

2011-07-30 18:21:10 +02:00
/*
* (c) 2011 Siegen, Germany by Nils Faerber <nils.faerber@kernelconcepts.de>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
2011-07-30 18:21:10 +02:00
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "mw_utility.h"
#include "metawatch.h"
#include "fonts.h"
2011-07-30 18:21:10 +02:00
/*
* The pixmap buffer has at least one byte per pixel, even for monochrome (bpp=1)
* bitmaps
*/
mw_buffer *mw_alloc_pbuffer(unsigned int res_x, unsigned int res_y, unsigned int bpp)
{
mw_buffer *nmwbuf;
int pbuf_size;
nmwbuf = (mw_buffer *) malloc(sizeof(mw_buffer));
if (!nmwbuf)
return NULL;
nmwbuf->res_x = res_x;
nmwbuf->res_y = res_y;
nmwbuf->bpp = bpp;
pbuf_size = nmwbuf->res_x * nmwbuf->res_y * ((nmwbuf->bpp / 8) + 1);
nmwbuf->pbuf = malloc(pbuf_size);
if (!nmwbuf->pbuf) {
free(nmwbuf);
return NULL;
} else {
memset(nmwbuf->pbuf, 0, pbuf_size);
return nmwbuf;
}
}
void mw_free_pbuffer(mw_buffer *mwbuf)
{
if (!mwbuf)
return;
free(mwbuf->pbuf);
free(mwbuf);
}
/*
2011-08-10 20:13:00 +02:00
* makes a buffer for sending to the LCD watch from the drawing buffer, e.g.
* mw_send_bitmap(mw_fd, MW_SCREEN_MODE_IDLE, 96, 65, 31, bbuf, len);
2011-08-10 20:13:00 +02:00
*
* NOT reentrant !
*/
unsigned char *mw_make_mw_buffer(mw_buffer *mwbuf, int *buflen)
{
static unsigned char wbuf[96*12];
int x, y;
unsigned char clr;
memset(wbuf, 0, 96*12);
for (y = 0; y < mwbuf->res_y; y++) {
for (x = 0; x < mwbuf->res_x; x++) {
clr = *(unsigned char *)(mwbuf->pbuf+((y*mwbuf->res_x)+x));
if (clr) {
*(unsigned char *)(wbuf+((y*12)+(x/8))) |= 1 << (x%8);
};
};
};
*buflen = mwbuf->res_y * 12;
return wbuf;
}
2011-08-10 20:13:00 +02:00
unsigned char *mw_make_mw_oled_buffer(mw_buffer *mwbuf, int *buflen)
{
static unsigned char wbuf[2*80]; /* size of one OLED, two rows */
int x, y;
unsigned char clr;
memset(wbuf, 0, 2*80);
for (x=0; x<mwbuf->res_x; x++) {
for (y=0; y<mwbuf->res_y; y++) {
clr = *(unsigned char *)(mwbuf->pbuf+((y*mwbuf->res_x)+x));
if (clr) {
*(unsigned char *)(wbuf+(x+80*(y/8))) |= 1 << (7-(y%8));
}
}
}
*buflen = (mwbuf->res_y / 8) * 80;
return wbuf;
}
2011-07-30 18:21:10 +02:00
void mw_dump_mw_buffer(mw_buffer *mwbuf)
{
int x, y;
unsigned char clr;
for (y = 0; y < mwbuf->res_y; y++) {
for (x = 0; x < mwbuf->res_x; x++) {
clr = *(unsigned char *)(mwbuf->pbuf+((y*mwbuf->res_x)+x));
if (clr)
fprintf(stderr, ".");
else
fprintf(stderr, " ");
};
fprintf(stderr, "\n");
};
}
/* clear/fill entire buffer with color */
void mw_buf_clear(mw_buffer *mwbuf, mw_color clr)
{
int pbuf_size;
if (!mwbuf)
return;
if (clr == MW_TRANSPARENT)
return;
2011-07-30 18:21:10 +02:00
pbuf_size = mwbuf->res_x * mwbuf->res_y * ((mwbuf->bpp / 8) + 1);
memset(mwbuf->pbuf, clr, pbuf_size);
}
/* draw a single pixel */
void mw_buf_draw_pixel(mw_buffer *mwbuf, unsigned int x, unsigned int y, mw_color clr)
{
if (!mwbuf)
return;
if (clr == MW_TRANSPARENT)
return;
2011-07-30 18:21:10 +02:00
*(unsigned char *)(mwbuf->pbuf+((y*mwbuf->res_x)+x)) = clr;
}
void mw_buf_print(mw_buffer *mwbuf, unsigned int x, unsigned int y, char *text, unsigned char fsize, mw_color fgclr, mw_color bgclr)
{
unsigned int i,j,z;
const unsigned char *data, *font_style;
unsigned char mask,xme,yme,offset;
if (text==NULL || strlen(text) == 0)
return;
switch (fsize) {
case 0:
data = (const unsigned char *)FONT6x8;
font_style = (const unsigned char *)FONT6x8;
break;
case 1:
data = (const unsigned char *)FONT8x8F;
font_style = (const unsigned char *)FONT8x8F;
break;
case 2:
data = (const unsigned char *)FONT8x16;
font_style = (const unsigned char *)FONT8x16;
break;
default:
data = (const unsigned char *)FONT6x8;
font_style = (const unsigned char *)FONT6x8;
break;
};
xme = *data++;
yme = *data++;
offset = *data;
do {
mask = 0x00;
data = (font_style + offset) + (offset * (int)(*text - 32));
for (i=0; i < yme; i++) {
mask |=0x80;
for (j=x; j < (x + xme); j++) {
z = y + i;
if ((z < mwbuf->res_y) && (j < mwbuf->res_x)) {
if (*data & mask) {
mw_buf_draw_pixel(mwbuf, j, z, fgclr);
} else {
mw_buf_draw_pixel(mwbuf, j, z, bgclr);
}
}
mask >>= 1;
}
data++;
}
x += xme;
text++;
} while (*text != '\0');
}
void mw_buf_draw_line_bresenham(mw_buffer *mwbuf, unsigned int xstart, unsigned int ystart, unsigned int xend, unsigned int yend, mw_color clr)
{
int x, y, t, dx, dy, incx, incy, pdx, pdy, ddx, ddy, es, el, err;
dx = xend - xstart;
dy = yend - ystart;
incx = (dx >= 0) ? 1 : -1;
incy = (dy >= 0) ? 1 : -1;
if (dx<0)
dx = -dx;
if (dy<0)
dy = -dy;
if (dx>dy) {
pdx = incx; pdy = 0;
ddx=incx; ddy=incy;
es =dy; el =dx;
} else {
pdx=0; pdy=incy;
ddx=incx; ddy=incy;
es =dx; el =dy;
}
x = xstart;
y = ystart;
err = el/2;
mw_buf_draw_pixel(mwbuf, x, y, clr);
for (t = 0; t < el; ++t) {
err -= es;
if (err < 0) {
err += el;
x += ddx;
y += ddy;
} else {
x += pdx;
y += pdy;
}
mw_buf_draw_pixel(mwbuf, x, y, clr);
}
}
2011-07-30 18:21:10 +02:00