]> git.kernelconcepts.de Git - metawatch.git/blobdiff - metawatch.c
Add button messages and handling
[metawatch.git] / metawatch.c
index 6c93469d42ab23aea1d6bfdf9ce9c80150efa817..dd3af80b80a7bedde765fd8cea43bb168ea95d42 100644 (file)
@@ -1,7 +1,20 @@
 /*
  * (c) 2011 Siegen, Germany by Nils Faerber <nils.faerber@kernelconcepts.de>
  *
- * license LGPL
+ * 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>
@@ -169,6 +182,37 @@ void mw_write_buffer(int mw_fd,
        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
@@ -209,9 +253,36 @@ void mw_get_battery_voltage_response(int mw_fd, unsigned char *batrsp, int len)
        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
 }
 
 
@@ -233,13 +304,19 @@ int decode_frame(int mw_fd, unsigned char *buf, int len)
        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];
@@ -247,24 +324,56 @@ int decode_frame(int mw_fd, unsigned char *buf, int len)
        msgdata = (buf+4);
 
        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, len-2);
                        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, len-2);
+                       break;
+               case MW_READ_LIGHT_SENSOR_RSP:
+                       mw_read_light_sensor_response(mw_fd, msgdata, len-2);
                        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, len-2);
+                       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, len-4-2);
+                       mw_status_change_event(mw_fd, msgopt, msgdata, len-2);
                        break;
                default:
                        fprintf(stderr, "Unkown msgtype 0x%02x\n", msgtype);
@@ -303,12 +412,13 @@ void bmap_buffer_flipinvert(unsigned char flip, unsigned char invert, unsigned c
 
 void mw_send_bitmap(int mw_fd, unsigned char mode, int width, int height, int offset, unsigned char *bmapbuf, int buflen)
 {
-       char rbuf[256];
-       unsigned int i, x, y;
-       unsigned int rowlength;
+#ifdef DEBUG
+       unsigned int i, x;
+#endif
+       unsigned int y, rowlength;
        unsigned char mw_buf[24];
 
-       rowlength = ((width / 8) + 1);
+       rowlength = (((width-1) / 8) + 1);
        if ((height + offset) > 96)
                height = 96 - offset;
 
@@ -330,7 +440,7 @@ void mw_send_bitmap(int mw_fd, unsigned char mode, int width, int height, int of
                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, 31+y, mw_buf, 24);
+               mw_write_buffer(mw_fd, mode, 0, offset+y, mw_buf, 24);
        }
 }