]> git.kernelconcepts.de Git - metawatch.git/blobdiff - mw_utility.c
Ooops - fix typo
[metawatch.git] / mw_utility.c
index baf3d0a07d94eb30da3adfc5776403674e77a12e..fa7d3420b5f089c6f9973b570266dead9e0324c6 100644 (file)
 #include <string.h>
 #include <stdio.h>
 
-#include "mw_utility.h"
 #include "metawatch.h"
+#include "mw_utility.h"
 
 #include "fonts.h"
 
+/* ----------------------------------------------------------------------
+ * Generic drawing functions
+ * ---------------------------------------------------------------------- */
+
 /*
  * The pixmap buffer has at least one byte per pixel, even for monochrome (bpp=1)
  * bitmaps
@@ -64,8 +68,10 @@ void mw_free_pbuffer(mw_buffer *mwbuf)
 }
 
 /*
- * makes a buffer for sending to the watch from the drawing buffer, e.g.
+ * 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);
+ *
+ * NOT reentrant !
  */
 unsigned char *mw_make_mw_buffer(mw_buffer *mwbuf, int *buflen)
 {
@@ -88,6 +94,27 @@ unsigned char *mw_make_mw_buffer(mw_buffer *mwbuf, int *buflen)
        return wbuf;
 }
 
+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;
+}
+
 void mw_dump_mw_buffer(mw_buffer *mwbuf)
 {
        int x, y;
@@ -128,6 +155,9 @@ void mw_buf_draw_pixel(mw_buffer *mwbuf, unsigned int x, unsigned int y, mw_colo
        if (clr == MW_TRANSPARENT)
                return;
 
+       if (x < 0 || x > 128 || y < 0 || y > 128)
+               return;
+
        *(unsigned char *)(mwbuf->pbuf+((y*mwbuf->res_x)+x)) = clr;
 }
 
@@ -229,3 +259,148 @@ void mw_buf_draw_line_bresenham(mw_buffer *mwbuf, unsigned int xstart, unsigned
        }
 }
 
+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)
+{
+       int i, 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 (i=1; i<thickness; i++) {
+               mw_buf_draw_pixel(mwbuf, x-i, y, clr);
+               mw_buf_draw_pixel(mwbuf, x+i, y, clr);
+               mw_buf_draw_pixel(mwbuf, x, y-i, clr);
+               mw_buf_draw_pixel(mwbuf, x, y+i, 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);
+               for (i=1; i<thickness; i++) {
+                       mw_buf_draw_pixel(mwbuf, x-i, y, clr);
+                       mw_buf_draw_pixel(mwbuf, x+i, y, clr);
+                       mw_buf_draw_pixel(mwbuf, x, y-i, clr);
+                       mw_buf_draw_pixel(mwbuf, x, y+i, clr);
+               }
+       }
+}
+
+/* ----------------------------------------------------------------------
+ * Complex combined functions, for user convenience
+ * ---------------------------------------------------------------------- */
+
+/*
+ * send a text notification, automatically take care of device type (ana/digi)
+ * char *title is displayed inverse in top line
+ * char *text is the notification text
+ * vibrate is the number of 300ms vibrations, 0 for none
+ */
+void mw_do_notification(mwdevice_t *mwdevice, char *title, char *text, unsigned char vibrate)
+{
+       mw_buffer *mwbuf;
+       unsigned char *bbuf;
+       int len,i,c,r;
+       char sstr[32];
+
+       // fprintf(stderr, "do_notify devtype=%d, title='%s', text='%s', vibrate=%d\n", mwdevice->devtype, title, text, vibrate);
+       if (mwdevice->devtype == MW_DEVICE_TYPE_DIGITAL || mwdevice->devtype == MW_DEVICE_TYPE_DEVB_DIGI) {
+               mwbuf = mw_alloc_pbuffer(96, 96, 1);
+               mw_buf_clear(mwbuf, MW_BLACK);
+
+               mw_buf_print(mwbuf, 0,  0, title, 0, MW_BLACK, MW_WHITE);
+
+               i=0;
+               c=0; r=1;
+               memset(sstr,0,32);
+               while (i<strlen(text)) {
+                       sstr[c++] = text[i++];
+                       if (c>=16 || i>=strlen(text)) {
+                               mw_buf_print(mwbuf, 0,  r*9, sstr, 0, MW_WHITE, MW_BLACK);
+                               memset(sstr,0,32);
+                               c=0; r++;
+                               if (r>10)
+                                       break;
+                       };
+               };
+
+               bbuf = mw_make_mw_buffer(mwbuf, &len);
+               mw_send_bitmap(mwdevice, MW_SCREEN_MODE_NOTIFICATION, 96, 96, 0, bbuf, len);
+               mw_update_display(mwdevice, MW_SCREEN_MODE_NOTIFICATION, 1);
+               mw_free_pbuffer(mwbuf);
+       } else if (mwdevice->devtype == MW_DEVICE_TYPE_ANA_DIGI || mwdevice->devtype == MW_DEVICE_TYPE_DEVB_ANA_DIGI) {
+               fprintf(stderr, "do notify OLED\n");
+               mwbuf = mw_alloc_pbuffer(80, 16, 1);
+               mw_buf_clear(mwbuf, MW_BLACK);
+
+               mw_buf_print(mwbuf, 0,  0, title, 0, MW_BLACK, MW_WHITE);
+
+               i=0;
+               c=0; r=1;
+               memset(sstr,0,32);
+               while (i<strlen(text) && r<2) {
+                       sstr[c++] = text[i++];
+                       if (c>=13 || i>=strlen(text)) {
+                               mw_buf_print(mwbuf, 0,  r*9, sstr, 0, MW_WHITE, MW_BLACK);
+                               memset(sstr,0,32);
+                               c=0; r++;
+                       };
+               };
+
+               bbuf = mw_make_mw_oled_buffer(mwbuf, &len);
+               mw_write_oled_buffer(mwdevice, 0, MW_OLED_UPPER, 80, 0, bbuf, len);
+
+               mw_buf_clear(mwbuf, MW_BLACK);
+               c=0; r=0;
+               memset(sstr,0,32);
+               while (i<strlen(text) && r<2) {
+                       sstr[c++] = text[i++];
+                       if (c>=13 || i>=strlen(text)) {
+                               mw_buf_print(mwbuf, 0,  r*9, sstr, 0, MW_WHITE, MW_BLACK);
+                               memset(sstr,0,32);
+                               c=0; r++;
+                               if (r>2)
+                                       break;
+                       };
+               };
+
+               bbuf = mw_make_mw_oled_buffer(mwbuf, &len);
+               mw_write_oled_buffer(mwdevice, 0, MW_OLED_LOWER, 80, 0, bbuf, len);
+
+               mw_free_pbuffer(mwbuf);
+       } else
+               fprintf(stderr, "Watch type not set - forgot to call mw_init()?\n");
+
+       if (vibrate)
+               mw_set_vibrate_mode(mwdevice, 1, 300, 300, vibrate);
+}
+