Add analog clockface display, command dana <hour> <minute> <day#>
[metawatch.git] / metawatch.c
index a4ac234..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);
 
@@ -105,25 +106,20 @@ int mw_send_frame(mwdevice_t *mwdevice, unsigned char msg_type, unsigned char op
  * ---------------------------------------------------------------------- */
 
 
-int mw_set_rtc(mwdevice_t *mwdevice)
+int mw_set_rtc(mwdevice_t *mwdevice, struct tm *mw_tm)
 {
-       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;  
+       year = mw_tm->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[2] = mw_tm->tm_mon + 1;
+       data[3] = mw_tm->tm_mday;
+       data[4] = mw_tm->tm_wday;
+       data[5] = mw_tm->tm_hour;
+       data[6] = mw_tm->tm_min;
+       data[7] = mw_tm->tm_sec;
 
        return mw_send_frame(mwdevice, MW_SET_REAL_TIME_CLOCK, 0, data, 8);
 }
@@ -307,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
@@ -398,19 +409,19 @@ int mw_get_battery_voltage_response(mwdevice_t *mwdevice, unsigned char *batrsp,
        unsigned char power_good = batrsp[0];
        unsigned char bat_charging = batrsp[1];
        unsigned short voltage = *(unsigned short *)(batrsp+2);
-       unsigned short voltage_average = *(unsigned short *)(batrsp+4);
+       unsigned short voltage_avg = *(unsigned short *)(batrsp+4);
 
 #ifdef DEBUG
-       fprintf(stderr, "battery is at %dmV (average is %dmV), %s and %s\n", voltage, voltage_average, power_good ? "power is good" : "power fault", bat_charging ? "charging" : "not charging");
+       fprintf(stderr, "battery is at %dmV (average is %dmV), %s and %s\n", voltage, voltage_avg, power_good ? "power is good" : "power fault", bat_charging ? "charging" : "not charging");
 #endif
 
        if (mwdevice->mw_get_battery_voltage_response_cb != NULL)
-               mwdevice->mw_get_battery_voltage_response_cb(mwdevice, &voltage, &power_good, &bat_charging, mwdevice->mw_gbatvrsp_data);
+               mwdevice->mw_get_battery_voltage_response_cb(mwdevice, &voltage, &voltage_avg, &power_good, &bat_charging, mwdevice->mw_gbatvrsp_data);
 
        return 0;
 }
 
-void mw_set_get_battery_voltage_response_cb(mwdevice_t *mwdevice, void (*mw_get_battery_voltage_response_cb) (mwdevice_t *mwdevice, unsigned short *voltage, unsigned char *pgood, unsigned char *charging, void *user_data), void *user_data)
+void mw_set_get_battery_voltage_response_cb(mwdevice_t *mwdevice, void (*mw_get_battery_voltage_response_cb) (mwdevice_t *mwdevice, unsigned short *voltage, unsigned short *voltage_avg, unsigned char *pgood, unsigned char *charging, void *user_data), void *user_data)
 {
        if (mw_get_battery_voltage_response_cb != NULL)
                mwdevice->mw_get_battery_voltage_response_cb = mw_get_battery_voltage_response_cb;
@@ -418,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
@@ -437,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)
 {
@@ -449,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)
@@ -468,21 +481,20 @@ void mw_set_button_event_message_cb(mwdevice_t *mwdevice, void (*mw_button_event
 
 int mw_read_light_sensor_response(mwdevice_t *mwdevice, unsigned char *lightrsp, int len)
 {
-       unsigned char power_good = lightrsp[0];
-       unsigned char bat_charging = lightrsp[1];
-       unsigned short light_level = *(unsigned short *)(lightrsp+2);
+       unsigned short light_level = *(unsigned short *)(lightrsp);
+       unsigned short light_level_avg = *(unsigned short *)(lightrsp+2);
 
 #ifdef DEBUG
-       fprintf(stderr, "light sensor is at %d, power stat: %s and %s\n", light_level, power_good ? "power is good" : "power fault", bat_charging ? "charging" : "not charging");
+       fprintf(stderr, "light sensor is at %d, average at %d\n", light_level, light_level_avg);
 #endif
 
        if (mwdevice->mw_read_light_sensor_response_cb != NULL)
-               mwdevice->mw_read_light_sensor_response_cb(mwdevice, &light_level, mwdevice->mw_rlsrsp_data);
+               mwdevice->mw_read_light_sensor_response_cb(mwdevice, &light_level, &light_level_avg, mwdevice->mw_rlsrsp_data);
 
        return 0;
 }
 
-void mw_set_read_light_sensor_response_cb(mwdevice_t *mwdevice, void (*mw_read_light_sensor_response_cb) (mwdevice_t *mwdevice, unsigned short *light_level, void *user_data), void *user_data)
+void mw_set_read_light_sensor_response_cb(mwdevice_t *mwdevice, void (*mw_read_light_sensor_response_cb) (mwdevice_t *mwdevice, unsigned short *light_level, unsigned short *light_level_avg, void *user_data), void *user_data)
 {
        if (mw_read_light_sensor_response_cb != NULL)
                mwdevice->mw_read_light_sensor_response_cb = mw_read_light_sensor_response_cb;
@@ -513,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
@@ -561,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 - ");
@@ -601,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;
        };