X-Git-Url: https://git.kernelconcepts.de/?p=metawatch.git;a=blobdiff_plain;f=metawatch.c;h=08d5aca2bd08861837a07dd5b6fa64ef273c83e5;hp=348140a49c52920e068a876c190cc1a2f3410c98;hb=36bbcbe1824acc18b9bc48426de62d862283dfc0;hpb=59068bbd135c3d2ed046be5fd64850166ca04a5c diff --git a/metawatch.c b/metawatch.c index 348140a..08d5aca 100644 --- a/metawatch.c +++ b/metawatch.c @@ -51,6 +51,10 @@ const char *mw_status_string[] = { #define MW_FRAME_DELAY 0x00 +/* ---------------------------------------------------------------------- + * Debugging helpers + * ---------------------------------------------------------------------- */ + void dump_frame(unsigned char *frame, int len) { int i; @@ -61,7 +65,7 @@ void dump_frame(unsigned char *frame, int len) } -int mw_send_frame(int mw_fd, unsigned char msg_type, unsigned char options, unsigned char *data, unsigned char len) +int mw_send_frame(mwdevice_t *mwdevice, unsigned char msg_type, unsigned char options, unsigned char *data, unsigned char len) { unsigned short crc; unsigned char frame[64]; @@ -83,7 +87,7 @@ int mw_send_frame(int mw_fd, unsigned char msg_type, unsigned char options, unsi dump_frame(frame, tlen); #endif - while (((ret = write(mw_fd, frame, tlen)) >= 0) && (tlen > 0)) + while (((ret = write(mwdevice->mw_fd, frame, tlen)) >= 0) && (tlen > 0)) tlen -= ret; if (MW_FRAME_DELAY) @@ -101,7 +105,7 @@ int mw_send_frame(int mw_fd, unsigned char msg_type, unsigned char options, unsi * ---------------------------------------------------------------------- */ -void mw_set_rtc(int mw_fd, unsigned char clk1224, unsigned char date_fmt) +int mw_set_rtc(mwdevice_t *mwdevice, unsigned char clk1224, unsigned char date_fmt) { time_t mtime; struct tm mtm; @@ -123,10 +127,10 @@ void mw_set_rtc(int mw_fd, unsigned char clk1224, unsigned char date_fmt) data[8] = clk1224; data[9] = date_fmt; - mw_send_frame(mw_fd, MW_SET_REAL_TIME_CLOCK, 0, data, 10); + return mw_send_frame(mwdevice, MW_SET_REAL_TIME_CLOCK, 0, data, 10); } -void mw_set_vibrate_mode(int mw_fd, unsigned char enable, unsigned short on_time, unsigned short off_time, unsigned char cycles) +int mw_set_vibrate_mode(mwdevice_t *mwdevice, unsigned char enable, unsigned short on_time, unsigned short off_time, unsigned char cycles) { unsigned char mdata[7]; @@ -134,32 +138,35 @@ void mw_set_vibrate_mode(int mw_fd, unsigned char enable, unsigned short on_time *(unsigned short *)(mdata+1) = on_time; /* miliseconds */ *(unsigned short *)(mdata+3) = off_time; /* miliseconds */ mdata[5] = cycles; - mw_send_frame(mw_fd, MW_SET_VIBRATE_MODE, 0, mdata, 6); + + return mw_send_frame(mwdevice, MW_SET_VIBRATE_MODE, 0, mdata, 6); } -void mw_configure_watch_mode(int mw_fd, unsigned char mode, unsigned char save, unsigned char timeout, unsigned char invert) +int mw_configure_watch_mode(mwdevice_t *mwdevice, unsigned char mode, unsigned char save, unsigned char timeout, unsigned char invert) { unsigned char mdata[3]; mdata[0] = timeout; /* seconds */ mdata[1] = invert; /* 0=normal, 1=invert */ - mw_send_frame(mw_fd, MW_CONFIGURE_MODE, (mode & 0x0f) | ((save & 0x01) << 4), mdata, 2); + + return mw_send_frame(mwdevice, MW_CONFIGURE_MODE, (mode & 0x0f) | ((save & 0x01) << 4), mdata, 2); } -void mw_update_display(int mw_fd, unsigned char mode, unsigned char copy) +int mw_update_display(mwdevice_t *mwdevice, unsigned char mode, unsigned char copy) { - mw_send_frame(mw_fd, MW_UPDATE_DISPLAY, (mode & 0x0f) | ((copy & 0x01) << 4), NULL, 0); + return mw_send_frame(mwdevice, MW_UPDATE_DISPLAY, (mode & 0x0f) | ((copy & 0x01) << 4), NULL, 0); } -void mw_load_template(int mw_fd, unsigned char mode, unsigned char template_select) +int mw_load_template(mwdevice_t *mwdevice, unsigned char mode, unsigned char template_select) { - mw_send_frame(mw_fd, MW_LOAD_TEMPLATE, (mode & 0x0f), &template_select, 1); + return mw_send_frame(mwdevice, MW_LOAD_TEMPLATE, (mode & 0x0f), &template_select, 1); } /* * send line for screen-mode mode from *buffer to watch, starting at display row row_offset + * this is only for digital LCD watch */ -void mw_write_buffer(int mw_fd, +int mw_write_buffer(mwdevice_t *mwdevice, 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 */ @@ -167,10 +174,13 @@ void mw_write_buffer(int mw_fd, { unsigned char mdata[32]; + if (mwdevice->devtype != MW_DEVICE_TYPE_DIGITAL && mwdevice->devtype != MW_DEVICE_TYPE_DEVB_DIGI) + return -1; + 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; + return -1; }; memset(mdata, 0, 32); mdata[0] = row_offset; @@ -179,7 +189,8 @@ void mw_write_buffer(int mw_fd, 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); + + return mw_send_frame(mwdevice, MW_WRITE_BUFFER, (mode & 0x0f) | (((numlines & 0x01)<< 4) & 0x10), mdata, numlines ? 13 : 26); } /* @@ -198,7 +209,7 @@ void mw_write_buffer(int mw_fd, 0: Start index col 1..: data */ -void mw_write_oled_buffer(int mw_fd, +int mw_write_oled_buffer(mwdevice_t *mwdevice, unsigned char mode, /* idle or scroll */ unsigned char oled, /* which OLED */ unsigned char numcols, @@ -207,23 +218,27 @@ void mw_write_oled_buffer(int mw_fd, { unsigned char mdata[32]; int i; + + if (mwdevice->devtype != MW_DEVICE_TYPE_ANA_DIGI && mwdevice->devtype != MW_DEVICE_TYPE_DEVB_ANA_DIGI) + return -1; /* lower row first since display wil be updated after completion of upper row */ if (buflen > 80) { for (i=80; imw_get_real_time_clock_response_cb != NULL) + mwdevice->mw_get_real_time_clock_response_cb(mwdevice, &mtm, mwdevice->mw_grtcrsp_data); + + return 0; } -void mw_get_battery_voltage_response(int mw_fd, unsigned char *batrsp, int len) +void mw_set_get_real_time_clock_response_cb(mwdevice_t *mwdevice, void (*mw_get_real_time_clock_response_cb) (mwdevice_t *mwdevice, struct tm *mw_tm, void *user_data), void *user_data) +{ + if (mw_get_real_time_clock_response_cb != NULL) + mwdevice->mw_get_real_time_clock_response_cb = mw_get_real_time_clock_response_cb; + if (user_data != NULL) + mwdevice->mw_grtcrsp_data = user_data; +} + + +int mw_get_battery_voltage_response(mwdevice_t *mwdevice, unsigned char *batrsp, int len) { unsigned short voltage = *(unsigned short *)batrsp; unsigned char power_good = batrsp[2]; unsigned char bat_charging = batrsp[3]; - fprintf(stderr, "battery is at %dV, %s and %s\n", voltage, power_good ? "power is good" : "power fault", bat_charging ? "charging" : "not charging"); +#ifdef DEBUG + fprintf(stderr, "battery is at %dmV, %s and %s\n", voltage, 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); + + 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) +{ + if (mw_get_battery_voltage_response_cb != NULL) + mwdevice->mw_get_battery_voltage_response_cb = mw_get_battery_voltage_response_cb; + if (user_data != NULL) + mwdevice->mw_gbatvrsp_data = user_data; } -void mw_read_button_config_response(int mw_fd, unsigned char *btnrsp, int len) +int mw_read_button_config_response(mwdevice_t *mwdevice, unsigned char *btnrsp, int len) { +#ifdef DEBUG 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]); @@ -308,22 +356,63 @@ void mw_read_button_config_response(int mw_fd, unsigned char *btnrsp, int len) fprintf(stderr, ")\n"); fprintf(stderr, "callback msg type: 0x%02x\n", btnrsp[3]); fprintf(stderr, "callback msg opts: 0x%02d\n", btnrsp[4]); +#endif + + return 0; } -void mw_read_light_sensor_response(int mw_fd, unsigned char *lightrsp, int len) +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) +{ + if (mw_read_button_config_response_cb != NULL) + mwdevice->mw_read_button_config_response_cb = mw_read_button_config_response_cb; + if (user_data != NULL) + mwdevice->mw_rbtncnfrsp_data = user_data; +} + +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 voltage = *(unsigned short *)(lightrsp+2); + unsigned short light_level = *(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"); +#endif + + if (mwdevice->mw_read_light_sensor_response_cb != NULL) + mwdevice->mw_read_light_sensor_response_cb(mwdevice, &light_level, mwdevice->mw_rlsrsp_data); - 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"); + return 0; } -void mw_status_change_event(int mw_fd, unsigned char option, unsigned char *statrsp, int len) +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) { + if (mw_read_light_sensor_response_cb != NULL) + mwdevice->mw_read_light_sensor_response_cb = mw_read_light_sensor_response_cb; + if (user_data != NULL) + mwdevice->mw_rlsrsp_data = user_data; +} + +int mw_status_change_event(mwdevice_t *mwdevice, unsigned char option, unsigned char *statrsp, int len) +{ + unsigned char mode = (option & 0x0f); + unsigned char status = statrsp[0]; #ifdef DEBUG fprintf(stderr, "Status change event for mode %s: %s\n", mw_screen_mode_names[option&0x0f], mw_status_string[statrsp[0]]); #endif + + if (mwdevice->mw_status_change_event_cb != NULL) + mwdevice->mw_status_change_event_cb(mwdevice, &mode, &status, mwdevice->mw_stchev_data); + + return 0; +} + +void mw_set_status_change_event_cb(mwdevice_t *mwdevice, void (*mw_status_change_event_cb) (mwdevice_t *mwdevice, unsigned char *scrmode, unsigned char *status, void *user_data), void *user_data) +{ + if (mw_status_change_event_cb != NULL) + mwdevice->mw_status_change_event_cb = mw_status_change_event_cb; + if (user_data != NULL) + mwdevice->mw_stchev_data = user_data; } @@ -331,8 +420,7 @@ void mw_status_change_event(int mw_fd, unsigned char option, unsigned char *stat * Protocol handling * ---------------------------------------------------------------------- */ - -int decode_frame(int mw_fd, unsigned char *buf, int len) +int decode_frame(mwdevice_t *mwdevice, unsigned char *buf, int len) { unsigned short crc; unsigned char msglen; @@ -345,7 +433,7 @@ int decode_frame(int mw_fd, unsigned char *buf, int len) crc = *(unsigned short *)(buf+len-2); if (crc != crc16ccitt(buf, len-2)) { fprintf(stderr, "decode frame CRC error\n"); - return 1; + return -1; } #ifdef DEBUG else @@ -353,7 +441,7 @@ int decode_frame(int mw_fd, unsigned char *buf, int len) #endif if (buf[0] != MW_SOF) { fprintf(stderr, "decode frame SOF not found\n"); - return 1; + return -1; } #ifdef DEBUG else @@ -389,25 +477,26 @@ int decode_frame(int mw_fd, unsigned char *buf, int len) fprintf(stderr, "unknown %d\n", msgdata[0]); break; }; + mwdevice->devtype = msgdata[0]; break; case MW_GET_INFORMATION_STRING_RSP: 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); + mw_get_real_time_clock_response(mwdevice, msgdata, msgdatalen); break; case MW_READ_BATTERY_VOLTAGE_RSP: - mw_get_battery_voltage_response(mw_fd, msgdata, msgdatalen); + mw_get_battery_voltage_response(mwdevice, msgdata, msgdatalen); break; case MW_READ_LIGHT_SENSOR_RSP: - mw_read_light_sensor_response(mw_fd, msgdata, msgdatalen); + 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; case MW_READ_BUTTON_CONFIG_RSP: - mw_read_button_config_response(mw_fd, msgdata, msgdatalen); + mw_read_button_config_response(mwdevice, msgdata, msgdatalen); break; case MW_BUTTON_EVENT_MESSAGE: fprintf(stderr, "Button event message\n"); @@ -416,21 +505,62 @@ int decode_frame(int mw_fd, unsigned char *buf, int len) 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); + mw_status_change_event(mwdevice, msgopt, msgdata, msgdatalen); break; default: fprintf(stderr, "Unkown msgtype 0x%02x\n", msgtype); break; }; - return 0; + return msglen; } +/* ---------------------------------------------------------------------- + * General code usage + * ---------------------------------------------------------------------- */ + +int mw_init(mwdevice_t *mwdevice, int mw_fd) +{ + memset(mwdevice, 0, sizeof(mwdevice_t)); + mwdevice->mw_fd = mw_fd; + + /* figure out which device we run with */ + mw_send_frame(mwdevice, MW_GET_DEVICE_TYPE, 0, NULL, 0); + + return 0; +} + /* ---------------------------------------------------------------------- * Convenience functions not strictly part of the protocol * ---------------------------------------------------------------------- */ +int mw_get_resolution(mwdevice_t *mwdevice, unsigned int *width, unsigned int *height) +{ + if (width == NULL || height == NULL) + return -1; + + switch (mwdevice->devtype) { + case MW_DEVICE_TYPE_RESERVED: + return -1; + case MW_DEVICE_TYPE_ANA_DIGI: + case MW_DEVICE_TYPE_DEVB_ANA_DIGI: + *width = 80; + *height = 16; + break; + case MW_DEVICE_TYPE_DIGITAL: + case MW_DEVICE_TYPE_DEVB_DIGI: + *width = 96; + *height = 96; + break; + default: + break; + }; + + return 0; +} + + /* if flip=1 bits in each byte are inverted 7->1, 6->2, 5->3,... if invert=1 each byte is inverted */ @@ -453,7 +583,7 @@ 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) +void mw_send_bitmap(mwdevice_t *mwdevice, unsigned char mode, int width, int height, int offset, unsigned char *bmapbuf, int buflen) { #ifdef DEBUG unsigned int i, x; @@ -483,7 +613,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, offset+y, mw_buf, 24); + mw_write_buffer(mwdevice, mode, 0, offset+y, mw_buf, 24); } }