Add analog clockface display, command dana <hour> <minute> <day#>
[metawatch.git] / metawatch.c
index 2bf028c..e093168 100644 (file)
@@ -90,6 +90,7 @@ int mw_send_frame(mwdevice_t *mwdevice, unsigned char msg_type, unsigned char op
        while (((ret = write(mwdevice->mw_fd, frame, tlen)) >= 0) && (tlen > 0))
                tlen -= ret;
 
+       fsync(mwdevice->mw_fd);
        if (MW_FRAME_DELAY)
                usleep(MW_FRAME_DELAY);
 
@@ -302,6 +303,21 @@ int mw_advance_watch_hands(mwdevice_t *mwdevice, unsigned char hours, unsigned c
        return mw_send_frame(mwdevice, MW_ADVANCE_WATCH_HANDS, 0, mdata, 3);
 }
 
+int mw_battery_configuration(mwdevice_t *mwdevice, unsigned char warn_lvl, unsigned char bt_off_lvl)
+{
+       unsigned char mdata[4];
+       
+       if (warn_lvl < 28 || warn_lvl > 42)
+               return -1;
+       if (bt_off_lvl < 28 || bt_off_lvl > 42)
+               return -1;
+
+       mdata[0] = warn_lvl;
+       mdata[1] = bt_off_lvl;
+
+       return mw_send_frame(mwdevice, MW_BATTERY_CONFIG_MSG, 0, mdata, 2);
+}
+
 
 /* ----------------------------------------------------------------------
  * Watch responses, events or notifications
@@ -413,6 +429,7 @@ void mw_set_get_battery_voltage_response_cb(mwdevice_t *mwdevice, void (*mw_get_
                mwdevice->mw_gbatvrsp_data = user_data;
 }
 
+#if 0
 int mw_read_button_config_response(mwdevice_t *mwdevice, unsigned char *btnrsp, int len)
 {
 #ifdef DEBUG
@@ -432,6 +449,7 @@ int mw_read_button_config_response(mwdevice_t *mwdevice, unsigned char *btnrsp,
 
        return 0;
 }
+#endif
 
 void mw_set_read_button_config_response_cb(mwdevice_t *mwdevice, void (*mw_read_button_config_response_cb) (mwdevice_t *mwdevice, void *user_data), void *user_data)
 {
@@ -444,7 +462,7 @@ void mw_set_read_button_config_response_cb(mwdevice_t *mwdevice, void (*mw_read_
 int mw_button_event_message(mwdevice_t *mwdevice, unsigned char *btnevt, int len, unsigned char opts)
 {
 #ifdef DEBUG
-       fprintf(stderr, "  button %d options %d\n", btnevt[0], opts);
+       fprintf(stderr, "Button event message, button %d options %d\n", btnevt[0], opts);
 #endif
 
        if (mwdevice->mw_button_event_message_cb != NULL)
@@ -507,6 +525,43 @@ void mw_set_status_change_event_cb(mwdevice_t *mwdevice, void (*mw_status_change
                mwdevice->mw_stchev_data = user_data;
 }
 
+int mw_low_battery_warning_message(mwdevice_t *mwdevice)
+{
+#ifdef DEBUG
+       fprintf(stderr, "Watch battery low, please connect charger\n");
+#endif
+       if (mwdevice->mw_low_battery_warning_message_cb != NULL)
+               mwdevice->mw_low_battery_warning_message_cb(mwdevice, mwdevice->mw_lbatwarnmsg_data);
+
+       return 0;
+}
+
+void mw_set_low_battery_warning_message_cb(mwdevice_t *mwdevice, void (*mw_low_battery_warning_message_cb) (mwdevice_t *mwdevice, void *user_data), void *user_data)
+{
+       if (mw_low_battery_warning_message_cb != NULL)
+               mwdevice->mw_low_battery_warning_message_cb = mw_low_battery_warning_message_cb;
+       if (user_data != NULL)
+               mwdevice->mw_lbatwarnmsg_data = user_data;
+}
+
+int mw_low_battery_bt_off_message(mwdevice_t *mwdevice)
+{
+#ifdef DEBUG
+       fprintf(stderr, "Watch battery extremely low - radio will turn off\n");
+#endif
+       if (mwdevice->mw_low_battery_bt_off_message_cb != NULL)
+               mwdevice->mw_low_battery_bt_off_message_cb(mwdevice, mwdevice->mw_lbatbtoff_data);
+
+       return 0;
+}
+
+void mw_set_low_battery_bt_off_message_cb(mwdevice_t *mwdevice, void (*mw_low_battery_bt_off_message_cb) (mwdevice_t *mwdevice, void *user_data), void *user_data)
+{
+       if (mw_low_battery_bt_off_message_cb != NULL)
+               mwdevice->mw_low_battery_bt_off_message_cb = mw_low_battery_bt_off_message_cb;
+       if (user_data != NULL)
+               mwdevice->mw_lbatbtoff_data = user_data;
+}
 
 /* ----------------------------------------------------------------------
  * Protocol handling
@@ -555,18 +610,19 @@ int mw_decode_frame(mwdevice_t *mwdevice, unsigned char *buf, int len)
                case MW_READ_LIGHT_SENSOR_RSP:
                        mw_read_light_sensor_response(mwdevice, msgdata, msgdatalen);
                        break;
-               case MW_LOW_BATTERY_WARNING_MSG:
-                       fprintf(stderr, "Watch battery low, please connect charger\n");
-                       break;
+#if 0
                case MW_READ_BUTTON_CONFIG_RSP:
                        mw_read_button_config_response(mwdevice, msgdata, msgdatalen);
                        break;
+#endif
                case MW_BUTTON_EVENT_MESSAGE:
-                       fprintf(stderr, "Button event message\n");
                        mw_button_event_message(mwdevice, msgdata, msgdatalen, msgopt);
                        break;
+               case MW_LOW_BATTERY_WARNING_MSG:
+                       mw_low_battery_warning_message(mwdevice);
+                       break;
                case MW_LOW_BATTERY_BT_OFF_MSG:
-                       fprintf(stderr, "Watch battery extremely low - radio will turn off\n");
+                       mw_low_battery_bt_off_message(mwdevice);
                        break;
                case MW_NVAL_OPERATION_RSP:
                        fprintf(stderr, "NVAL operation response - ");
@@ -595,7 +651,9 @@ int mw_decode_frame(mwdevice_t *mwdevice, unsigned char *buf, int len)
                        mw_status_change_event(mwdevice, msgopt, msgdata, msgdatalen);
                        break;
                default:
+#ifdef DEBUG
                        fprintf(stderr, "Unkown msgtype 0x%02x\n", msgtype);
+#endif
                        break;
        };