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.
21 #include <sys/types.h>
31 #include "metawatch.h"
32 #include "metawatch_protocol.h"
33 #include "crc16ccitt.h"
37 const char *mw_screen_mode_names[] = {
40 "notification screen",
44 const char *mw_status_string[] = {
52 #define MW_FRAME_DELAY 0x00
54 void dump_frame(unsigned char *frame, int len)
59 fprintf(stderr, "0x%02x ", frame[i]);
60 fprintf(stderr, "\n");
64 int mw_send_frame(int mw_fd, unsigned char msg_type, unsigned char options, unsigned char *data, unsigned char len)
67 unsigned char frame[64];
68 int tlen = len + 6; /* payload + 6 bytes frameing */
76 if (data != NULL && len > 0)
77 memcpy(frame+4, data, len);
79 crc = crc16ccitt(frame, len+4);
80 *(unsigned short *)(frame+len+4) = crc;
83 dump_frame(frame, tlen);
86 while (((ret = write(mw_fd, frame, tlen)) >= 0) && (tlen > 0))
90 usleep(MW_FRAME_DELAY);
92 if (tlen == 0 && ret >= 0)
99 /* ----------------------------------------------------------------------
100 * Host to watch commands
101 * ---------------------------------------------------------------------- */
104 void mw_set_rtc(int mw_fd, unsigned char clk1224, unsigned char date_fmt)
109 unsigned char data[32];
112 localtime_r(&mtime, &mtm);
114 year = mtm.tm_year + 1900;
115 data[0] = (year & 0x0f00) >> 8;
116 data[1] = (year & 0x00ff);
117 data[2] = mtm.tm_mon + 1;
118 data[3] = mtm.tm_mday;
119 data[4] = mtm.tm_wday;
120 data[5] = mtm.tm_hour;
121 data[6] = mtm.tm_min;
122 data[7] = mtm.tm_sec;
126 mw_send_frame(mw_fd, MW_SET_REAL_TIME_CLOCK, 0, data, 10);
129 void mw_set_vibrate_mode(int mw_fd, unsigned char enable, unsigned short on_time, unsigned short off_time, unsigned char cycles)
131 unsigned char mdata[7];
134 *(unsigned short *)(mdata+1) = on_time; /* miliseconds */
135 *(unsigned short *)(mdata+3) = off_time; /* miliseconds */
137 mw_send_frame(mw_fd, MW_SET_VIBRATE_MODE, 0, mdata, 6);
140 void mw_configure_watch_mode(int mw_fd, unsigned char mode, unsigned char save, unsigned char timeout, unsigned char invert)
142 unsigned char mdata[3];
144 mdata[0] = timeout; /* seconds */
145 mdata[1] = invert; /* 0=normal, 1=invert */
146 mw_send_frame(mw_fd, MW_CONFIGURE_MODE, (mode & 0x0f) | ((save & 0x01) << 4), mdata, 2);
149 void mw_update_display(int mw_fd, unsigned char mode, unsigned char copy)
151 mw_send_frame(mw_fd, MW_UPDATE_DISPLAY, (mode & 0x0f) | ((copy & 0x01) << 4), NULL, 0);
154 void mw_load_template(int mw_fd, unsigned char mode, unsigned char template_select)
156 mw_send_frame(mw_fd, MW_LOAD_TEMPLATE, (mode & 0x0f), &template_select, 1);
160 * send line for screen-mode mode from *buffer to watch, starting at display row row_offset
162 void mw_write_buffer(int mw_fd,
164 unsigned char numlines, /* number of lines, 0=two lines or 1=one line */
165 unsigned char row_offset, /* start at row_offset in display, e.g. lower part in idle @31 */
166 unsigned char *buffer, int buflen)
168 unsigned char mdata[32];
170 buflen = 12 * (buflen / 12); /* crop to 12 bytes */
171 if ((numlines == 0 && buflen < 12) || (numlines == 1 && buflen < 24)) {
172 fprintf(stderr, "mw_write_buffer: bufferlen does not match number of lines\n");
175 memset(mdata, 0, 32);
176 mdata[0] = row_offset;
177 memcpy((mdata+1), buffer, 12);
179 mdata[13] = row_offset+1;
180 memcpy((mdata+14), (buffer+12), 12);
182 mw_send_frame(mw_fd, MW_WRITE_BUFFER, (mode & 0x0f) | (((numlines & 0x01)<< 4) & 0x10), mdata, numlines ? 13 : 26);
187 B0 : row select, 0 first row, 1 second row
188 B1 : display select, 0 upper OLED, 1 lower OLED
189 B2 : if 1 send an event upon completion
193 B0: First buffer in a chain of scroll buffers
194 B1: Any buffer except first or last
195 B2: Last buffer of a chain
196 B3: reserved / unused
201 void mw_write_oled_buffer(int mw_fd,
202 unsigned char mode, /* idle or scroll */
203 unsigned char oled, /* which OLED */
204 unsigned char numcols,
205 unsigned char col_index, /* starting index */
206 unsigned char *buffer, int buflen)
208 unsigned char mdata[32];
211 /* lower row first since display wil be updated after completion of upper row */
213 for (i=80; i<buflen; i+=20) {
215 memcpy((mdata+1), (buffer+i), 20);
216 mw_send_frame(mw_fd, MW_WRITE_OLED_IDLE_DISPLAY_MSG, 0 | (oled ? 2 : 0), mdata, 21);
219 for (i=0; i<80; i+=20) {
221 memcpy((mdata+1), (buffer+i), 20);
222 mw_send_frame(mw_fd, MW_WRITE_OLED_IDLE_DISPLAY_MSG, 1 | (oled ? 2 : 0), mdata, 21);
226 void mw_enable_button(int mw_fd,
228 unsigned char button_index,
229 unsigned char press_type,
230 unsigned char callback_type,
231 unsigned char callback_option)
233 unsigned char mdata[32];
235 memset(mdata, 0, 32);
237 mdata[1] = button_index;
238 mdata[2] = press_type;
239 mdata[3] = callback_type;
240 mdata[4] = callback_option;
241 mw_send_frame(mw_fd, MW_ENABLE_BUTTON, 0, mdata, 5);
244 void mw_disable_button(int mw_fd,
246 unsigned char button_index,
247 unsigned char press_type)
249 unsigned char mdata[32];
251 memset(mdata, 0, 32);
253 mdata[1] = button_index;
254 mdata[2] = press_type;
255 mw_send_frame(mw_fd, MW_ENABLE_BUTTON, 0, mdata, 3);
258 /* ----------------------------------------------------------------------
259 * Watch responses, events or notifications
260 * ---------------------------------------------------------------------- */
263 void mw_get_real_time_clock_response(int mw_fd, unsigned char *rtcrsp, int len)
267 unsigned char clk1224, date_fmt;
270 fprintf(stderr, "get real time clock response length wrong %d != 10\n", len);
274 year = *(unsigned short *)rtcrsp;
275 mtm.tm_year = year - 1900;
276 mtm.tm_mon = rtcrsp[2] - 1;
277 mtm.tm_mday = rtcrsp[3];
278 mtm.tm_wday = rtcrsp[4];
279 mtm.tm_hour = rtcrsp[5];
280 mtm.tm_min = rtcrsp[6];
281 mtm.tm_sec = rtcrsp[7];
283 date_fmt = rtcrsp[9];
285 fprintf(stderr, "watch RTC is %s, clock format is %s, date format is %s\n", asctime(&mtm), clk1224 ? "24h" : "AM/PM", date_fmt ? "DD/MM" : "MM/DD");
288 void mw_get_battery_voltage_response(int mw_fd, unsigned char *batrsp, int len)
290 unsigned short voltage = *(unsigned short *)batrsp;
291 unsigned char power_good = batrsp[2];
292 unsigned char bat_charging = batrsp[3];
294 fprintf(stderr, "battery is at %dV, %s and %s\n", voltage, power_good ? "power is good" : "power fault", bat_charging ? "charging" : "not charging");
297 void mw_read_button_config_response(int mw_fd, unsigned char *btnrsp, int len)
299 fprintf(stderr, "read button config response\n");
300 fprintf(stderr, "screen mode : 0x%02x\n", btnrsp[0]);
301 fprintf(stderr, "button index : 0x%02x\n", btnrsp[1]);
302 fprintf(stderr, "mask table : 0x%02x (", btnrsp[2]);
303 fprintf(stderr, "%s ", (btnrsp[2] & 0x01) ? "Absolute, " : "");
304 fprintf(stderr, "%s ", (btnrsp[2] & 0x02) ? "Press&Release, " : "");
305 fprintf(stderr, "%s ", (btnrsp[2] & 0x04) ? "Press&Hold, " : "");
306 fprintf(stderr, "%s ", (btnrsp[2] & 0x08) ? "Press&LongHold, " : "");
307 fprintf(stderr, "%s ", (btnrsp[2] & 0x10) ? "Immediate" : "");
308 fprintf(stderr, ")\n");
309 fprintf(stderr, "callback msg type: 0x%02x\n", btnrsp[3]);
310 fprintf(stderr, "callback msg opts: 0x%02d\n", btnrsp[4]);
313 void mw_read_light_sensor_response(int mw_fd, unsigned char *lightrsp, int len)
315 unsigned char power_good = lightrsp[0];
316 unsigned char bat_charging = lightrsp[1];
317 unsigned short voltage = *(unsigned short *)(lightrsp+2);
319 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");
322 void mw_status_change_event(int mw_fd, unsigned char option, unsigned char *statrsp, int len)
325 fprintf(stderr, "Status change event for mode %s: %s\n", mw_screen_mode_names[option&0x0f], mw_status_string[statrsp[0]]);
330 /* ----------------------------------------------------------------------
332 * ---------------------------------------------------------------------- */
335 int decode_frame(int mw_fd, unsigned char *buf, int len)
338 unsigned char msglen;
339 unsigned char msgtype;
340 unsigned char msgopt;
341 unsigned char *msgdata;
342 unsigned char msgdatalen;
345 crc = *(unsigned short *)(buf+len-2);
346 if (crc != crc16ccitt(buf, len-2)) {
347 fprintf(stderr, "decode frame CRC error\n");
352 fprintf(stderr, "decode frame CRC OK\n");
354 if (buf[0] != MW_SOF) {
355 fprintf(stderr, "decode frame SOF not found\n");
360 fprintf(stderr, "decode frame found SOF\n");
367 msgdatalen = msglen - 4 - 2;
370 case MW_GET_DEVICE_TYPE_RSP:
371 fprintf(stderr, "Got device type ");
374 fprintf(stderr, "Reserved\n");
377 fprintf(stderr, "Ana-Digi\n");
380 fprintf(stderr, "Digital\n");
383 fprintf(stderr, "Development Board Digital\n");
386 fprintf(stderr, "Development Board Ana-Digi\n");
389 fprintf(stderr, "unknown %d\n", msgdata[0]);
393 case MW_GET_INFORMATION_STRING_RSP:
395 fprintf(stderr, "Got info string '%s'\n", msgdata);
397 case MW_GET_REAL_TIME_CLOCK_RSP:
398 mw_get_real_time_clock_response(mw_fd, msgdata, msgdatalen);
400 case MW_READ_BATTERY_VOLTAGE_RSP:
401 mw_get_battery_voltage_response(mw_fd, msgdata, msgdatalen);
403 case MW_READ_LIGHT_SENSOR_RSP:
404 mw_read_light_sensor_response(mw_fd, msgdata, msgdatalen);
406 case MW_LOW_BATTERY_WARNING_MSG:
407 fprintf(stderr, "Watch battery low, please connect charger\n");
409 case MW_READ_BUTTON_CONFIG_RSP:
410 mw_read_button_config_response(mw_fd, msgdata, msgdatalen);
412 case MW_BUTTON_EVENT_MESSAGE:
413 fprintf(stderr, "Button event message\n");
415 case MW_LOW_BATTERY_BT_OFF_MSG:
416 fprintf(stderr, "Watch battery extremely low - radio will turn off\n");
418 case MW_STATUS_CHANGE_EVENT:
419 mw_status_change_event(mw_fd, msgopt, msgdata, msgdatalen);
422 fprintf(stderr, "Unkown msgtype 0x%02x\n", msgtype);
430 /* ----------------------------------------------------------------------
431 * Convenience functions not strictly part of the protocol
432 * ---------------------------------------------------------------------- */
434 /* if flip=1 bits in each byte are inverted 7->1, 6->2, 5->3,...
435 if invert=1 each byte is inverted
437 void bmap_buffer_flipinvert(unsigned char flip, unsigned char invert, unsigned char *buf, int len)
446 tmp |= ((*buf & (1<<i)) >> i) << (7-i);
447 // fprintf(stderr, "0x%02x -> 0x%02x\n", *buf, tmp);
450 *buf = invert ? ~tmp : tmp;
456 void mw_send_bitmap(int mw_fd, unsigned char mode, int width, int height, int offset, unsigned char *bmapbuf, int buflen)
461 unsigned int y, rowlength;
462 unsigned char mw_buf[24];
464 rowlength = (((width-1) / 8) + 1);
465 if ((height + offset) > 96)
466 height = 96 - offset;
469 fprintf(stderr, "row length = %d bytes\n", rowlength);
470 fprintf(stderr, "bitmap resolution is %d x %d\n", width, height);
471 fprintf(stderr, "\n");
472 for (y=0; y<height; y++) {
473 for (x=0; x<rowlength; x++) {
475 fprintf(stderr, "%c", (bmapbuf[(y*rowlength)+x] & (1<<(7-i))) ? '.' : ' ');
477 fprintf(stderr, "\n");
479 fprintf(stderr, "\n");
482 for (y=0; y<height; y+=2) {
483 memset(mw_buf, 0, 24);
484 memcpy(mw_buf, (bmapbuf+(y*rowlength)), (rowlength > 12) ? 12 : rowlength);
485 memcpy((mw_buf+12), (bmapbuf+((y+1)*rowlength)), (rowlength > 12) ? 12 : rowlength);
486 mw_write_buffer(mw_fd, mode, 0, offset+y, mw_buf, 24);