2 * (c) 2011 Siegen, Germany by Nils Faerber <nils.faerber@kernelconcepts.de>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
24 #include "mw_utility.h"
25 #include "metawatch.h"
30 * The pixmap buffer has at least one byte per pixel, even for monochrome (bpp=1)
33 mw_buffer *mw_alloc_pbuffer(unsigned int res_x, unsigned int res_y, unsigned int bpp)
38 nmwbuf = (mw_buffer *) malloc(sizeof(mw_buffer));
42 nmwbuf->res_x = res_x;
43 nmwbuf->res_y = res_y;
46 pbuf_size = nmwbuf->res_x * nmwbuf->res_y * ((nmwbuf->bpp / 8) + 1);
47 nmwbuf->pbuf = malloc(pbuf_size);
52 memset(nmwbuf->pbuf, 0, pbuf_size);
57 void mw_free_pbuffer(mw_buffer *mwbuf)
67 * makes a buffer for sending to the LCD watch from the drawing buffer, e.g.
68 * mw_send_bitmap(mw_fd, MW_SCREEN_MODE_IDLE, 96, 65, 31, bbuf, len);
72 unsigned char *mw_make_mw_buffer(mw_buffer *mwbuf, int *buflen)
74 static unsigned char wbuf[96*12];
78 memset(wbuf, 0, 96*12);
80 for (y = 0; y < mwbuf->res_y; y++) {
81 for (x = 0; x < mwbuf->res_x; x++) {
82 clr = *(unsigned char *)(mwbuf->pbuf+((y*mwbuf->res_x)+x));
84 *(unsigned char *)(wbuf+((y*12)+(x/8))) |= 1 << (x%8);
88 *buflen = mwbuf->res_y * 12;
93 unsigned char *mw_make_mw_oled_buffer(mw_buffer *mwbuf, int *buflen)
95 static unsigned char wbuf[2*80]; /* size of one OLED, two rows */
99 memset(wbuf, 0, 2*80);
101 for (x=0; x<mwbuf->res_x; x++) {
102 for (y=0; y<mwbuf->res_y; y++) {
103 clr = *(unsigned char *)(mwbuf->pbuf+((y*mwbuf->res_x)+x));
105 *(unsigned char *)(wbuf+(x+80*(y/8))) |= 1 << (7-(y%8));
109 *buflen = (mwbuf->res_y / 8) * 80;
114 void mw_dump_mw_buffer(mw_buffer *mwbuf)
119 for (y = 0; y < mwbuf->res_y; y++) {
120 for (x = 0; x < mwbuf->res_x; x++) {
121 clr = *(unsigned char *)(mwbuf->pbuf+((y*mwbuf->res_x)+x));
123 fprintf(stderr, ".");
125 fprintf(stderr, " ");
127 fprintf(stderr, "\n");
132 /* clear/fill entire buffer with color */
133 void mw_buf_clear(mw_buffer *mwbuf, mw_color clr)
139 if (clr == MW_TRANSPARENT)
142 pbuf_size = mwbuf->res_x * mwbuf->res_y * ((mwbuf->bpp / 8) + 1);
143 memset(mwbuf->pbuf, clr, pbuf_size);
146 /* draw a single pixel */
147 void mw_buf_draw_pixel(mw_buffer *mwbuf, unsigned int x, unsigned int y, mw_color clr)
151 if (clr == MW_TRANSPARENT)
154 *(unsigned char *)(mwbuf->pbuf+((y*mwbuf->res_x)+x)) = clr;
157 void mw_buf_print(mw_buffer *mwbuf, unsigned int x, unsigned int y, char *text, unsigned char fsize, mw_color fgclr, mw_color bgclr)
160 const unsigned char *data, *font_style;
161 unsigned char mask,xme,yme,offset;
163 if (text==NULL || strlen(text) == 0)
168 data = (const unsigned char *)FONT6x8;
169 font_style = (const unsigned char *)FONT6x8;
172 data = (const unsigned char *)FONT8x8F;
173 font_style = (const unsigned char *)FONT8x8F;
176 data = (const unsigned char *)FONT8x16;
177 font_style = (const unsigned char *)FONT8x16;
180 data = (const unsigned char *)FONT6x8;
181 font_style = (const unsigned char *)FONT6x8;
190 data = (font_style + offset) + (offset * (int)(*text - 32));
191 for (i=0; i < yme; i++) {
193 for (j=x; j < (x + xme); j++) {
195 if ((z < mwbuf->res_y) && (j < mwbuf->res_x)) {
197 mw_buf_draw_pixel(mwbuf, j, z, fgclr);
199 mw_buf_draw_pixel(mwbuf, j, z, bgclr);
208 } while (*text != '\0');
211 void mw_buf_draw_line_bresenham(mw_buffer *mwbuf, unsigned int xstart, unsigned int ystart, unsigned int xend, unsigned int yend, mw_color clr)
213 int x, y, t, dx, dy, incx, incy, pdx, pdy, ddx, ddy, es, el, err;
218 incx = (dx >= 0) ? 1 : -1;
219 incy = (dy >= 0) ? 1 : -1;
239 mw_buf_draw_pixel(mwbuf, x, y, clr);
241 for (t = 0; t < el; ++t) {
251 mw_buf_draw_pixel(mwbuf, x, y, clr);