/* * (c) 2011 Siegen, Germany by Nils Faerber * * 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. */ #include #include #include #include #include #include #include #include #include #include #include "metawatch.h" #include "metawatch_protocol.h" #include "crc16ccitt.h" #ifdef DEBUG const char *mw_screen_mode_names[] = { "idle screen", "application screen", "notification screen", "scroll" }; const char *mw_status_string[] = { "Reserved", "Mode Change", "Display Timeout" }; #endif #define MW_FRAME_DELAY 0x00 void dump_frame(unsigned char *frame, int len) { int i; for (i=0; i 0) memcpy(frame+4, data, len); crc = crc16ccitt(frame, len+4); *(unsigned short *)(frame+len+4) = crc; #ifdef DEBUG dump_frame(frame, tlen); #endif while (((ret = write(mw_fd, frame, tlen)) >= 0) && (tlen > 0)) tlen -= ret; if (MW_FRAME_DELAY) usleep(MW_FRAME_DELAY); if (tlen == 0 && ret >= 0) return 0; else return ret; } /* ---------------------------------------------------------------------- * Host to watch commands * ---------------------------------------------------------------------- */ void mw_set_rtc(int mw_fd, unsigned char clk1224, unsigned char date_fmt) { time_t mtime; struct tm mtm; unsigned short year; unsigned char data[32]; mtime = time(NULL); localtime_r(&mtime, &mtm); year = mtm.tm_year + 1900; data[0] = (year & 0x0f00) >> 8; data[1] = (year & 0x00ff); data[2] = mtm.tm_mon + 1; data[3] = mtm.tm_mday; data[4] = mtm.tm_wday; data[5] = mtm.tm_hour; data[6] = mtm.tm_min; data[7] = mtm.tm_sec; data[8] = clk1224; data[9] = date_fmt; mw_send_frame(mw_fd, MW_SET_REAL_TIME_CLOCK, 0, data, 10); } void mw_set_vibrate_mode(int mw_fd, unsigned char enable, unsigned short on_time, unsigned short off_time, unsigned char cycles) { unsigned char mdata[7]; mdata[0] = enable; *(unsigned short *)(mdata+1) = on_time; /* miliseconds */ *(unsigned short *)(mdata+3) = off_time; /* miliseconds */ mdata[5] = cycles; mw_send_frame(mw_fd, MW_SET_VIBRATE_MODE, 0, mdata, 6); } void mw_configure_watch_mode(int mw_fd, unsigned char mode, unsigned char save, unsigned char timeout, unsigned char invert) { unsigned char mdata[3]; mdata[0] = timeout; /* seconds */ mdata[1] = invert; /* 0=normal, 1=invert */ mw_send_frame(mw_fd, MW_CONFIGURE_MODE, (mode & 0x0f) | ((save & 0x01) << 4), mdata, 2); } void mw_update_display(int mw_fd, unsigned char mode, unsigned char copy) { mw_send_frame(mw_fd, MW_UPDATE_DISPLAY, (mode & 0x0f) | ((copy & 0x01) << 4), NULL, 0); } void mw_load_template(int mw_fd, unsigned char mode, unsigned char template_select) { mw_send_frame(mw_fd, MW_LOAD_TEMPLATE, (mode & 0x0f), &template_select, 1); } /* * send line for screen-mode mode from *buffer to watch, starting at display row row_offset */ void mw_write_buffer(int mw_fd, unsigned char mode, unsigned char numlines, /* number of lines, 0=two lines or 1=one line */ unsigned char row_offset, /* start at row_offset in display, e.g. lower part in idle @31 */ unsigned char *buffer, int buflen) { unsigned char mdata[32]; buflen = 12 * (buflen / 12); /* crop to 12 bytes */ if ((numlines == 0 && buflen < 12) || (numlines == 1 && buflen < 24)) { fprintf(stderr, "mw_write_buffer: bufferlen does not match number of lines\n"); return; }; memset(mdata, 0, 32); mdata[0] = row_offset; memcpy((mdata+1), buffer, 12); if (numlines == 0) { mdata[13] = row_offset+1; memcpy((mdata+14), (buffer+12), 12); }; mw_send_frame(mw_fd, MW_WRITE_BUFFER, (mode & 0x0f) | (((numlines & 0x01)<< 4) & 0x10), mdata, numlines ? 13 : 26); } /* Options: B0 : row select, 0 first row, 1 second row B1 : display select, 0 upper OLED, 1 lower OLED B2 : if 1 send an event upon completion B3..4: scroll type, B5..7: unused Scroll types: B0: First buffer in a chain of scroll buffers B1: Any buffer except first or last B2: Last buffer of a chain B3: reserved / unused Payload: 0: Start index col 1..: data */ void mw_write_oled_buffer(int mw_fd, unsigned char mode, /* idle or scroll */ unsigned char oled, /* which OLED */ unsigned char numcols, unsigned char col_index, /* starting index */ unsigned char *buffer, int buflen) { unsigned char mdata[32]; int i; /* lower row first since display wil be updated after completion of upper row */ if (buflen > 80) { for (i=80; i1, 6->2, 5->3,... if invert=1 each byte is inverted */ void bmap_buffer_flipinvert(unsigned char flip, unsigned char invert, unsigned char *buf, int len) { int i; unsigned char tmp; while (len--) { tmp = 0; if (flip) { for (i=0; i<8; i++) tmp |= ((*buf & (1<> i) << (7-i); // fprintf(stderr, "0x%02x -> 0x%02x\n", *buf, tmp); } else tmp = *buf; *buf = invert ? ~tmp : tmp; buf++; } } void mw_send_bitmap(int mw_fd, unsigned char mode, int width, int height, int offset, unsigned char *bmapbuf, int buflen) { #ifdef DEBUG unsigned int i, x; #endif unsigned int y, rowlength; unsigned char mw_buf[24]; rowlength = (((width-1) / 8) + 1); if ((height + offset) > 96) height = 96 - offset; #ifdef DEBUG fprintf(stderr, "row length = %d bytes\n", rowlength); fprintf(stderr, "bitmap resolution is %d x %d\n", width, height); fprintf(stderr, "\n"); for (y=0; y 12) ? 12 : rowlength); memcpy((mw_buf+12), (bmapbuf+((y+1)*rowlength)), (rowlength > 12) ? 12 : rowlength); mw_write_buffer(mw_fd, mode, 0, offset+y, mw_buf, 24); } }