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 *(unsigned char *)(mwbuf->pbuf+((y*mwbuf->res_x)+x)) = clr;
161 void mw_buf_print(mw_buffer *mwbuf, unsigned int x, unsigned int y, char *text, unsigned char fsize, mw_color fgclr, mw_color bgclr)
164 const unsigned char *data, *font_style;
165 unsigned char mask,xme,yme,offset;
167 if (text==NULL || strlen(text) == 0)
172 data = (const unsigned char *)FONT6x8;
173 font_style = (const unsigned char *)FONT6x8;
176 data = (const unsigned char *)FONT8x8F;
177 font_style = (const unsigned char *)FONT8x8F;
180 data = (const unsigned char *)FONT8x16;
181 font_style = (const unsigned char *)FONT8x16;
184 data = (const unsigned char *)FONT6x8;
185 font_style = (const unsigned char *)FONT6x8;
194 data = (font_style + offset) + (offset * (int)(*text - 32));
195 for (i=0; i < yme; i++) {
197 for (j=x; j < (x + xme); j++) {
199 if ((z < mwbuf->res_y) && (j < mwbuf->res_x)) {
201 mw_buf_draw_pixel(mwbuf, j, z, fgclr);
203 mw_buf_draw_pixel(mwbuf, j, z, bgclr);
212 } while (*text != '\0');
215 void mw_buf_draw_line_bresenham(mw_buffer *mwbuf, unsigned int xstart, unsigned int ystart, unsigned int xend, unsigned int yend, mw_color clr)
217 int x, y, t, dx, dy, incx, incy, pdx, pdy, ddx, ddy, es, el, err;
222 incx = (dx >= 0) ? 1 : -1;
223 incy = (dy >= 0) ? 1 : -1;
243 mw_buf_draw_pixel(mwbuf, x, y, clr);
245 for (t = 0; t < el; ++t) {
255 mw_buf_draw_pixel(mwbuf, x, y, clr);
260 /* ----------------------------------------------------------------------
261 * Complex combined functions, for user convenience
262 * ---------------------------------------------------------------------- */
265 * send a text notification, automatically take care of device type (ana/digi)
266 * char *title is displayed inverse in top line
267 * char *text is the notification text
268 * vibrate is the number of 300ms vibrations, 0 for none
270 void mw_do_notification(mwdevice_t *mwdevice, char *title, char *text, unsigned char vibrate)
277 // fprintf(stderr, "do_notify devtype=%d, title='%s', text='%s', vibrate=%d\n", mwdevice->devtype, title, text, vibrate);
278 if (mwdevice->devtype == MW_DEVICE_TYPE_DIGITAL || mwdevice->devtype == MW_DEVICE_TYPE_DEVB_DIGI) {
279 mwbuf = mw_alloc_pbuffer(96, 96, 1);
280 mw_buf_clear(mwbuf, MW_BLACK);
282 mw_buf_print(mwbuf, 0, 0, title, 0, MW_BLACK, MW_WHITE);
287 while (i<strlen(text)) {
288 sstr[c++] = text[i++];
289 if (c>=16 || i>=strlen(text)) {
290 mw_buf_print(mwbuf, 0, r*9, sstr, 0, MW_WHITE, MW_BLACK);
298 bbuf = mw_make_mw_buffer(mwbuf, &len);
299 mw_send_bitmap(mwdevice, MW_SCREEN_MODE_NOTIFICATION, 96, 96, 0, bbuf, len);
300 mw_update_display(mwdevice, MW_SCREEN_MODE_NOTIFICATION, 1);
301 mw_free_pbuffer(mwbuf);
302 } else if (mwdevice->devtype == MW_DEVICE_TYPE_ANA_DIGI || mwdevice->devtype == MW_DEVICE_TYPE_DEVB_ANA_DIGI) {
303 fprintf(stderr, "do notify OLED\n");
304 mwbuf = mw_alloc_pbuffer(80, 16, 1);
305 mw_buf_clear(mwbuf, MW_BLACK);
307 mw_buf_print(mwbuf, 0, 0, title, 0, MW_BLACK, MW_WHITE);
312 while (i<strlen(text) && r<2) {
313 sstr[c++] = text[i++];
314 if (c>=13 || i>=strlen(text)) {
315 mw_buf_print(mwbuf, 0, r*9, sstr, 0, MW_WHITE, MW_BLACK);
321 bbuf = mw_make_mw_oled_buffer(mwbuf, &len);
322 mw_write_oled_buffer(mwdevice, 0, MW_OLED_UPPER, 80, 0, bbuf, len);
324 mw_buf_clear(mwbuf, MW_BLACK);
327 while (i<strlen(text) && r<2) {
328 sstr[c++] = text[i++];
329 if (c>=13 || i>=strlen(text)) {
330 mw_buf_print(mwbuf, 0, r*9, sstr, 0, MW_WHITE, MW_BLACK);
338 bbuf = mw_make_mw_oled_buffer(mwbuf, &len);
339 mw_write_oled_buffer(mwdevice, 0, MW_OLED_LOWER, 80, 0, bbuf, len);
341 mw_free_pbuffer(mwbuf);
343 fprintf(stderr, "Watch type not set - forgot to call mw_init()?\n");
346 mw_set_vibrate_mode(mwdevice, 1, 300, 300, vibrate);