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 "metawatch.h"
25 #include "mw_utility.h"
29 /* ----------------------------------------------------------------------
30 * Generic drawing functions
31 * ---------------------------------------------------------------------- */
34 * The pixmap buffer has at least one byte per pixel, even for monochrome (bpp=1)
37 mw_buffer *mw_alloc_pbuffer(unsigned int res_x, unsigned int res_y, unsigned int bpp)
42 nmwbuf = (mw_buffer *) malloc(sizeof(mw_buffer));
46 nmwbuf->res_x = res_x;
47 nmwbuf->res_y = res_y;
50 pbuf_size = nmwbuf->res_x * nmwbuf->res_y * ((nmwbuf->bpp / 8) + 1);
51 nmwbuf->pbuf = malloc(pbuf_size);
56 memset(nmwbuf->pbuf, 0, pbuf_size);
61 void mw_free_pbuffer(mw_buffer *mwbuf)
71 * makes a buffer for sending to the LCD watch from the drawing buffer, e.g.
72 * mw_send_bitmap(mw_fd, MW_SCREEN_MODE_IDLE, 96, 65, 31, bbuf, len);
76 unsigned char *mw_make_mw_buffer(mw_buffer *mwbuf, int *buflen)
78 static unsigned char wbuf[96*12];
82 memset(wbuf, 0, 96*12);
84 for (y = 0; y < mwbuf->res_y; y++) {
85 for (x = 0; x < mwbuf->res_x; x++) {
86 clr = *(unsigned char *)(mwbuf->pbuf+((y*mwbuf->res_x)+x));
88 *(unsigned char *)(wbuf+((y*12)+(x/8))) |= 1 << (x%8);
92 *buflen = mwbuf->res_y * 12;
97 unsigned char *mw_make_mw_oled_buffer(mw_buffer *mwbuf, int *buflen)
99 static unsigned char wbuf[2*80]; /* size of one OLED, two rows */
103 memset(wbuf, 0, 2*80);
105 for (x=0; x<mwbuf->res_x; x++) {
106 for (y=0; y<mwbuf->res_y; y++) {
107 clr = *(unsigned char *)(mwbuf->pbuf+((y*mwbuf->res_x)+x));
109 *(unsigned char *)(wbuf+(x+80*(y/8))) |= 1 << (7-(y%8));
113 *buflen = (mwbuf->res_y / 8) * 80;
118 void mw_dump_mw_buffer(mw_buffer *mwbuf)
123 for (y = 0; y < mwbuf->res_y; y++) {
124 for (x = 0; x < mwbuf->res_x; x++) {
125 clr = *(unsigned char *)(mwbuf->pbuf+((y*mwbuf->res_x)+x));
127 fprintf(stderr, ".");
129 fprintf(stderr, " ");
131 fprintf(stderr, "\n");
136 /* clear/fill entire buffer with color */
137 void mw_buf_clear(mw_buffer *mwbuf, mw_color clr)
143 if (clr == MW_TRANSPARENT)
146 pbuf_size = mwbuf->res_x * mwbuf->res_y * ((mwbuf->bpp / 8) + 1);
147 memset(mwbuf->pbuf, clr, pbuf_size);
150 /* draw a single pixel */
151 void mw_buf_draw_pixel(mw_buffer *mwbuf, unsigned int x, unsigned int y, mw_color clr)
155 if (clr == MW_TRANSPARENT)
158 if (x < 0 || x > 128 || y < 0 || y > 128)
161 *(unsigned char *)(mwbuf->pbuf+((y*mwbuf->res_x)+x)) = clr;
164 void mw_buf_print(mw_buffer *mwbuf, unsigned int x, unsigned int y, char *text, unsigned char fsize, mw_color fgclr, mw_color bgclr)
167 const unsigned char *data, *font_style;
168 unsigned char mask,xme,yme,offset;
170 if (text==NULL || strlen(text) == 0)
175 data = (const unsigned char *)FONT6x8;
176 font_style = (const unsigned char *)FONT6x8;
179 data = (const unsigned char *)FONT8x8F;
180 font_style = (const unsigned char *)FONT8x8F;
183 data = (const unsigned char *)FONT8x16;
184 font_style = (const unsigned char *)FONT8x16;
187 data = (const unsigned char *)FONT6x8;
188 font_style = (const unsigned char *)FONT6x8;
197 data = (font_style + offset) + (offset * (int)(*text - 32));
198 for (i=0; i < yme; i++) {
200 for (j=x; j < (x + xme); j++) {
202 if ((z < mwbuf->res_y) && (j < mwbuf->res_x)) {
204 mw_buf_draw_pixel(mwbuf, j, z, fgclr);
206 mw_buf_draw_pixel(mwbuf, j, z, bgclr);
215 } while (*text != '\0');
218 void mw_buf_draw_line_bresenham(mw_buffer *mwbuf, unsigned int xstart, unsigned int ystart, unsigned int xend, unsigned int yend, mw_color clr)
220 int x, y, t, dx, dy, incx, incy, pdx, pdy, ddx, ddy, es, el, err;
225 incx = (dx >= 0) ? 1 : -1;
226 incy = (dy >= 0) ? 1 : -1;
246 mw_buf_draw_pixel(mwbuf, x, y, clr);
248 for (t = 0; t < el; ++t) {
258 mw_buf_draw_pixel(mwbuf, x, y, clr);
262 void mw_buf_draw_line_bresenham_w(mw_buffer *mwbuf, unsigned int xstart, unsigned int ystart, unsigned int xend, unsigned int yend, unsigned char thickness, mw_color clr)
264 int i, x, y, t, dx, dy, incx, incy, pdx, pdy, ddx, ddy, es, el, err;
269 incx = (dx >= 0) ? 1 : -1;
270 incy = (dy >= 0) ? 1 : -1;
290 mw_buf_draw_pixel(mwbuf, x, y, clr);
291 for (i=1; i<thickness; i++) {
292 mw_buf_draw_pixel(mwbuf, x-i, y, clr);
293 mw_buf_draw_pixel(mwbuf, x+i, y, clr);
294 mw_buf_draw_pixel(mwbuf, x, y-i, clr);
295 mw_buf_draw_pixel(mwbuf, x, y+i, clr);
298 for (t = 0; t < el; ++t) {
308 mw_buf_draw_pixel(mwbuf, x, y, clr);
309 for (i=1; i<thickness; i++) {
310 mw_buf_draw_pixel(mwbuf, x-i, y, clr);
311 mw_buf_draw_pixel(mwbuf, x+i, y, clr);
312 mw_buf_draw_pixel(mwbuf, x, y-i, clr);
313 mw_buf_draw_pixel(mwbuf, x, y+i, clr);
318 /* ----------------------------------------------------------------------
319 * Complex combined functions, for user convenience
320 * ---------------------------------------------------------------------- */
323 * send a text notification, automatically take care of device type (ana/digi)
324 * char *title is displayed inverse in top line
325 * char *text is the notification text
326 * vibrate is the number of 300ms vibrations, 0 for none
328 void mw_do_notification(mwdevice_t *mwdevice, char *title, char *text, unsigned char vibrate)
335 // fprintf(stderr, "do_notify devtype=%d, title='%s', text='%s', vibrate=%d\n", mwdevice->devtype, title, text, vibrate);
336 if (mwdevice->devtype == MW_DEVICE_TYPE_DIGITAL || mwdevice->devtype == MW_DEVICE_TYPE_DEVB_DIGI) {
337 mwbuf = mw_alloc_pbuffer(96, 96, 1);
338 mw_buf_clear(mwbuf, MW_BLACK);
340 mw_buf_print(mwbuf, 0, 0, title, 0, MW_BLACK, MW_WHITE);
345 while (i<strlen(text)) {
346 sstr[c++] = text[i++];
347 if (c>=16 || i>=strlen(text)) {
348 mw_buf_print(mwbuf, 0, r*9, sstr, 0, MW_WHITE, MW_BLACK);
356 bbuf = mw_make_mw_buffer(mwbuf, &len);
357 mw_send_bitmap(mwdevice, MW_SCREEN_MODE_NOTIFICATION, 96, 96, 0, bbuf, len);
358 mw_update_display(mwdevice, MW_SCREEN_MODE_NOTIFICATION, 1);
359 mw_free_pbuffer(mwbuf);
360 } else if (mwdevice->devtype == MW_DEVICE_TYPE_ANA_DIGI || mwdevice->devtype == MW_DEVICE_TYPE_DEVB_ANA_DIGI) {
361 fprintf(stderr, "do notify OLED\n");
362 mwbuf = mw_alloc_pbuffer(80, 16, 1);
363 mw_buf_clear(mwbuf, MW_BLACK);
365 mw_buf_print(mwbuf, 0, 0, title, 0, MW_BLACK, MW_WHITE);
370 while (i<strlen(text) && r<2) {
371 sstr[c++] = text[i++];
372 if (c>=13 || i>=strlen(text)) {
373 mw_buf_print(mwbuf, 0, r*9, sstr, 0, MW_WHITE, MW_BLACK);
379 bbuf = mw_make_mw_oled_buffer(mwbuf, &len);
380 mw_write_oled_buffer(mwdevice, 0, MW_OLED_UPPER, 80, 0, bbuf, len);
382 mw_buf_clear(mwbuf, MW_BLACK);
385 while (i<strlen(text) && r<2) {
386 sstr[c++] = text[i++];
387 if (c>=13 || i>=strlen(text)) {
388 mw_buf_print(mwbuf, 0, r*9, sstr, 0, MW_WHITE, MW_BLACK);
396 bbuf = mw_make_mw_oled_buffer(mwbuf, &len);
397 mw_write_oled_buffer(mwdevice, 0, MW_OLED_LOWER, 80, 0, bbuf, len);
399 mw_free_pbuffer(mwbuf);
401 fprintf(stderr, "Watch type not set - forgot to call mw_init()?\n");
404 mw_set_vibrate_mode(mwdevice, 1, 300, 300, vibrate);