/*
- * (c) 2011 Siegen, Germany by Nils Faerber
+ * (c) 2011 Siegen, Germany by Nils Faerber <nils.faerber@kernelconcepts.de>
*
- * license GPL
+ * 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 <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <time.h>
#include <termios.h>
+#include <ctype.h>
+#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;
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
- */
+
+/* ----------------------------------------------------------------------
+ * Host to watch commands
+ * ---------------------------------------------------------------------- */
+
void mw_set_rtc(int mw_fd, unsigned char clk1224, unsigned char date_fmt)
{
mdata[0] = timeout; /* seconds */
mdata[1] = invert; /* 0=normal, 1=invert */
- mw_send_frame(mw_fd, MW_CONFIGURE_MODE, (mode & 0x0f) | ((save & 0x01) << 3), mdata, 2);
+ 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) << 3), NULL, 0);
+ 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);
}
/*
- * watch responses, events or notifications
+ * 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);
+}
+
+void mw_enable_button(int mw_fd,
+ unsigned char mode,
+ unsigned char button_index,
+ unsigned char press_type,
+ unsigned char callback_type,
+ unsigned char callback_option)
+{
+ unsigned char mdata[32];
+
+ memset(mdata, 0, 32);
+ mdata[0] = mode;
+ mdata[1] = button_index;
+ mdata[2] = press_type;
+ mdata[3] = callback_type;
+ mdata[4] = callback_option;
+ mw_send_frame(mw_fd, MW_ENABLE_BUTTON, 0, mdata, 5);
+}
+
+void mw_disable_button(int mw_fd,
+ unsigned char mode,
+ unsigned char button_index,
+ unsigned char press_type)
+{
+ unsigned char mdata[32];
+
+ memset(mdata, 0, 32);
+ mdata[0] = mode;
+ mdata[1] = button_index;
+ mdata[2] = press_type;
+ mw_send_frame(mw_fd, MW_ENABLE_BUTTON, 0, mdata, 3);
+}
+
+/* ----------------------------------------------------------------------
+ * Watch responses, events or notifications
+ * ---------------------------------------------------------------------- */
+
+
void mw_get_real_time_clock_response(int mw_fd, unsigned char *rtcrsp, int len)
{
struct tm mtm;
unsigned char clk1224, date_fmt;
if (len != 10) {
- fprintf(stderr, "get real time clock response too short %d != 10\n", len);
+ fprintf(stderr, "get real time clock response length wrong %d != 10\n", len);
return;
}
fprintf(stderr, "battery is at %dV, %s and %s\n", voltage, power_good ? "power is good" : "power fault", bat_charging ? "charging" : "not charging");
}
+void mw_read_button_config_response(int mw_fd, unsigned char *btnrsp, int len)
+{
+ fprintf(stderr, "read button config response\n");
+ fprintf(stderr, "screen mode : 0x%02x\n", btnrsp[0]);
+ fprintf(stderr, "button index : 0x%02x\n", btnrsp[1]);
+ fprintf(stderr, "mask table : 0x%02x (", btnrsp[2]);
+ fprintf(stderr, "%s ", (btnrsp[2] & 0x01) ? "Absolute, " : "");
+ fprintf(stderr, "%s ", (btnrsp[2] & 0x02) ? "Press&Release, " : "");
+ fprintf(stderr, "%s ", (btnrsp[2] & 0x04) ? "Press&Hold, " : "");
+ fprintf(stderr, "%s ", (btnrsp[2] & 0x08) ? "Press&LongHold, " : "");
+ fprintf(stderr, "%s ", (btnrsp[2] & 0x10) ? "Immediate" : "");
+ fprintf(stderr, ")\n");
+ fprintf(stderr, "callback msg type: 0x%02x\n", btnrsp[3]);
+ fprintf(stderr, "callback msg opts: 0x%02d\n", btnrsp[4]);
+}
+
+void mw_read_light_sensor_response(int mw_fd, unsigned char *lightrsp, int len)
+{
+ unsigned char power_good = lightrsp[0];
+ unsigned char bat_charging = lightrsp[1];
+ unsigned short voltage = *(unsigned short *)(lightrsp+2);
+
+ fprintf(stderr, "light sensor is at %d, power stat: %s and %s\n", voltage, power_good ? "power is good" : "power fault", bat_charging ? "charging" : "not charging");
+}
+
+void mw_status_change_event(int mw_fd, unsigned char option, unsigned char *statrsp, int len)
+{
+#ifdef DEBUG
+ fprintf(stderr, "Status change event for mode %s: %s\n", mw_screen_mode_names[option&0x0f], mw_status_string[statrsp[0]]);
+#endif
+}
+
+
+/* ----------------------------------------------------------------------
+ * Protocol handling
+ * ---------------------------------------------------------------------- */
+
int decode_frame(int mw_fd, unsigned char *buf, int len)
{
unsigned char msgtype;
unsigned char msgopt;
unsigned char *msgdata;
+ unsigned char msgdatalen;
/* check frame */
crc = *(unsigned short *)(buf+len-2);
if (crc != crc16ccitt(buf, len-2)) {
fprintf(stderr, "decode frame CRC error\n");
return 1;
- } else
+ }
+#ifdef DEBUG
+ else
fprintf(stderr, "decode frame CRC OK\n");
+#endif
if (buf[0] != MW_SOF) {
fprintf(stderr, "decode frame SOF not found\n");
return 1;
- } else
+ }
+#ifdef DEBUG
+ else
fprintf(stderr, "decode frame found SOF\n");
+#endif
msglen = buf[1];
msgtype = buf[2];
msgopt = buf[3];
msgdata = (buf+4);
+ msgdatalen = msglen - 4 - 2;
switch (msgtype) {
- case MW_GET_REAL_TIME_CLOCK_RSP:
- mw_get_real_time_clock_response(mw_fd, msgdata, len-4-2);
+ case MW_GET_DEVICE_TYPE_RSP:
+ fprintf(stderr, "Got device type ");
+ switch(msgdata[0]) {
+ case 0:
+ fprintf(stderr, "Reserved\n");
+ break;
+ case 1:
+ fprintf(stderr, "Ana-Digi\n");
+ break;
+ case 2:
+ fprintf(stderr, "Digital\n");
+ break;
+ case 3:
+ fprintf(stderr, "Development Board Digital\n");
+ break;
+ case 4:
+ fprintf(stderr, "Development Board Ana-Digi\n");
+ break;
+ default:
+ fprintf(stderr, "unknown %d\n", msgdata[0]);
+ break;
+ };
break;
case MW_GET_INFORMATION_STRING_RSP:
- msgdata[len-4-2] = 0;
- fprintf(stderr, "Got info string '%s'\n", (msgdata+4));
+ msgdata[len-2] = 0;
+ fprintf(stderr, "Got info string '%s'\n", msgdata);
+ break;
+ case MW_GET_REAL_TIME_CLOCK_RSP:
+ mw_get_real_time_clock_response(mw_fd, msgdata, msgdatalen);
break;
case MW_READ_BATTERY_VOLTAGE_RSP:
- mw_get_battery_voltage_response(mw_fd, msgdata, len-4-2);
+ mw_get_battery_voltage_response(mw_fd, msgdata, msgdatalen);
+ break;
+ case MW_READ_LIGHT_SENSOR_RSP:
+ mw_read_light_sensor_response(mw_fd, msgdata, msgdatalen);
break;
case MW_LOW_BATTERY_WARNING_MSG:
fprintf(stderr, "Watch battery low, please connect charger\n");
break;
+ case MW_READ_BUTTON_CONFIG_RSP:
+ mw_read_button_config_response(mw_fd, msgdata, msgdatalen);
+ break;
+ case MW_BUTTON_EVENT_MESSAGE:
+ fprintf(stderr, "Button event message\n");
+ break;
case MW_LOW_BATTERY_BT_OFF_MSG:
fprintf(stderr, "Watch battery extremely low - radio will turn off\n");
break;
+ case MW_STATUS_CHANGE_EVENT:
+ mw_status_change_event(mw_fd, msgopt, msgdata, msgdatalen);
+ break;
default:
fprintf(stderr, "Unkown msgtype 0x%02x\n", msgtype);
break;
}
-void process_cmd(char *cmdline, int clinep, int mw_fd)
-{
- unsigned char mdata[32];
-
- fprintf(stderr, "command: '%s'\n", cmdline);
+/* ----------------------------------------------------------------------
+ * Convenience functions not strictly part of the protocol
+ * ---------------------------------------------------------------------- */
- if (strncmp(cmdline, "quit", 4) == 0) {
- close(mw_fd);
- exit(0);
- }
-
- if (strncmp(cmdline, "srtc", 4) == 0) {
- fprintf(stderr, "Setting RTC from system time...");
- mw_set_rtc(mw_fd, MW_RTC_CLOCK_24HR, MW_RTC_DATE_DDMM);
- fprintf(stderr, "OK\n");
- }
- if (strncmp(cmdline, "grtc", 4) == 0) {
- mw_send_frame(mw_fd, MW_GET_REAL_TIME_CLOCK, 0, NULL, 0);
- }
- if (strncmp(cmdline, "gistr", 5) == 0) {
- mdata[0] = 0;
- mw_send_frame(mw_fd, MW_GET_INFORMATION_STRING, 0, mdata, 1);
- }
- if (strncmp(cmdline, "gdtype", 6) == 0) {
- mw_send_frame(mw_fd, MW_GET_DEVICE_TYPE, 0, NULL, 0);
- }
- if (strncmp(cmdline, "rvbat", 5) == 0) {
- mw_send_frame(mw_fd, MW_READ_BATTERY_VOLTAGE_MSG, 0, NULL, 0);
- }
- if (strncmp(cmdline, "modecfg", 6) == 0) {
- mw_configure_watch_mode(mw_fd, MW_SCREEN_MODE_IDLE, 0, 4, 1);
- mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 0);
- }
- if (strncmp(cmdline, "rbtcfg", 6) == 0) {
- mdata[0] = 0; /* idle screen */
- mdata[1] = 1; /* button index */
- mdata[2] = 2; /* button press type */
- mdata[3] = 3; /* callback message type */
- mdata[4] = 4; /* callback message option */
- mw_send_frame(mw_fd, MW_READ_BUTTON_CONFIG, 0, NULL, 0);
- }
- if (strncmp(cmdline, "svib", 4) == 0) {
- mw_set_vibrate_mode(mw_fd, 1, 300, 300, 5);
+/* if flip=1 bits in each byte are inverted 7->1, 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)) >> i) << (7-i);
+ // fprintf(stderr, "0x%02x -> 0x%02x\n", *buf, tmp);
+ } else
+ tmp = *buf;
+ *buf = invert ? ~tmp : tmp;
+ buf++;
}
}
-int menu(int mw_fd)
+void mw_send_bitmap(int mw_fd, unsigned char mode, int width, int height, int offset, unsigned char *bmapbuf, int buflen)
{
- fd_set mfds;
- struct termios tconfd;
- char cmdline[128];
- unsigned char msg_buf[64];
- unsigned char clinep = 0;
- int rcvd;
-
- tcgetattr(0, &tconfd);
- cfmakeraw(&tconfd);
- tconfd.c_oflag |= ONLCR | OPOST;
- tconfd.c_lflag |= ISIG;
- tcsetattr(0, TCSANOW, &tconfd);
-
- FD_ZERO(&mfds);
- FD_SET(0, &mfds);
- FD_SET(mw_fd, &mfds);
-
- memset(cmdline, 0, 128);
-
- do {
- rcvd = 0;
- if (select(mw_fd+1, &mfds, NULL, NULL, NULL) > 0) {
- if (FD_ISSET(mw_fd, &mfds)) {
- rcvd = read(mw_fd, msg_buf, 64);
- fprintf(stderr, "read %d bytes:\n", rcvd);
- if (rcvd > 0) {
- dump_frame(msg_buf, rcvd);
- decode_frame(mw_fd, msg_buf, rcvd);
- }
- };
- if (FD_ISSET(0, &mfds)) {
- rcvd = read(0, (cmdline+clinep), 1);
- if (rcvd > 0) {
- if (cmdline[clinep] == '\r') {
- printf("\n");
- cmdline[clinep--] = '\0';
- process_cmd(cmdline, clinep, mw_fd);
- clinep = 0;
- memset(cmdline, 0, 128);
- } else {
- clinep++;
- if (clinep > 75)
- clinep = 75;
- printf("\r> %s", cmdline);
- fflush(stdout);
- }
- }
- };
- } else
- break;
- FD_ZERO(&mfds);
- FD_SET(0, &mfds);
- FD_SET(mw_fd, &mfds);
- } while (rcvd > 0);
+#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<height; y++) {
+ for (x=0; x<rowlength; x++) {
+ for (i=0; i<8; i++)
+ fprintf(stderr, "%c", (bmapbuf[(y*rowlength)+x] & (1<<(7-i))) ? '.' : ' ');
+ }
+ fprintf(stderr, "\n");
+ }
+ fprintf(stderr, "\n");
+#endif
- return 0;
+ for (y=0; y<height; y+=2) {
+ memset(mw_buf, 0, 24);
+ memcpy(mw_buf, (bmapbuf+(y*rowlength)), (rowlength > 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);
+ }
}
-int main(int argc, char **argv)
-{
- int mw_fd;
-
- if (argc != 2) {
- fprintf(stderr, "Usage:\n\t%s <devicename>\n", argv[0]);
- return 1;
- };
-
- crc16ccitt_init();
-
- mw_fd = open(argv[1], O_RDWR);
- if (mw_fd < 0) {
- perror("open");
- return 1;
- };
-
- menu(mw_fd);
-
- fsync(mw_fd);
-
- close(mw_fd);
-
- return 0;
-};
-