/* * (c) 2011 Siegen, Germany by Nils Faerber * * license LGPL */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include /* #include #include */ #include "metawatch.h" #include "crc16ccitt.h" #include "mw_utility.h" #include "bt_helper.h" typedef struct { GMainLoop *mloop; mwdevice_t mwdevice; unsigned char rcvbuf[128]; int rcvbuf_pos; int con_fd; /* console input fd */ char cmdline[128]; int cmdline_pos; } mwdata_t; void bitmap_test(mwdevice_t *mwdevice) { /* a nice checker-board pattern */ unsigned char checkbuf[24] = { 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, }; mw_write_buffer(mwdevice, MW_SCREEN_MODE_IDLE, 0, 31, checkbuf, 24); mw_write_buffer(mwdevice, MW_SCREEN_MODE_IDLE, 0, 33, checkbuf, 24); mw_write_buffer(mwdevice, MW_SCREEN_MODE_IDLE, 0, 35, checkbuf, 24); mw_write_buffer(mwdevice, MW_SCREEN_MODE_IDLE, 0, 37, checkbuf, 24); mw_update_display(mwdevice, MW_SCREEN_MODE_IDLE, 1); } void bitmap_test2(mwdevice_t *mwdevice) { mw_buffer *mwbuf1, *mwbuf2; unsigned char *bbuf; int len, x; mwbuf1 = mw_alloc_pbuffer(96, 66, 1); mwbuf2 = mw_alloc_pbuffer(96, 66, 1); mw_buf_clear(mwbuf1, MW_BLACK); mw_buf_clear(mwbuf2, MW_WHITE); for (x=0; x<66; x++) { //mw_buf_clear(mwbuf1, MW_BLACK); mw_buf_draw_pixel(mwbuf1, x, x, MW_WHITE); mw_dump_mw_buffer(mwbuf1); bbuf = mw_make_mw_buffer(mwbuf1, &len); mw_send_bitmap(mwdevice, MW_SCREEN_MODE_IDLE, 96, 65, 31, bbuf, len); mw_update_display(mwdevice, MW_SCREEN_MODE_IDLE, 1); getchar(); }; mw_free_pbuffer(mwbuf1); mw_free_pbuffer(mwbuf2); } void text_test(mwdevice_t *mwdevice) { mw_buffer *mwbuf; unsigned char *bbuf; int len; mwbuf = mw_alloc_pbuffer(96, 66, 1); mw_buf_clear(mwbuf, MW_BLACK); mw_buf_print(mwbuf, 1, 10, "Font 0", 0, MW_WHITE, MW_BLACK); mw_buf_print(mwbuf, 1, 20, "Font 1", 1, MW_WHITE, MW_BLACK); mw_buf_print(mwbuf, 1, 30, "Font 2", 2, MW_WHITE, MW_BLACK); mw_buf_print(mwbuf, 1, 45, "Big Fat Clipping", 2, MW_BLACK, MW_WHITE); bbuf = mw_make_mw_buffer(mwbuf, &len); mw_send_bitmap(mwdevice, MW_SCREEN_MODE_IDLE, 96, 65, 31, bbuf, len); mw_update_display(mwdevice, MW_SCREEN_MODE_IDLE, 1); mw_free_pbuffer(mwbuf); } void line_test(mwdevice_t *mwdevice) { mw_buffer *mwbuf; unsigned char *bbuf; int len, p; mwbuf = mw_alloc_pbuffer(96, 66, 1); mw_buf_clear(mwbuf, MW_BLACK); for (p=0; p<=96; p+=8) mw_buf_draw_line_bresenham(mwbuf, p, 0, 95-p, 65, MW_WHITE); bbuf = mw_make_mw_buffer(mwbuf, &len); mw_send_bitmap(mwdevice, MW_SCREEN_MODE_IDLE, 96, 65, 31, bbuf, len); mw_update_display(mwdevice, MW_SCREEN_MODE_IDLE, 1); mw_free_pbuffer(mwbuf); } void bitmap_read(mwdevice_t *mwdevice) { int ffd, ret; char rbuf[256]; unsigned int width, height, i; #ifdef DEBUG unsigned int x, y; #endif unsigned int rowlength; unsigned char *bmapbuf; // unsigned char mw_buf[24]; ffd = open("test.pbm", O_RDONLY); if (ffd < 0) { perror("open"); return; }; ret = read(ffd, rbuf, 3); if (rbuf[0] != 'P' || rbuf[1] != '4') { fprintf(stderr, "not a PBM file\n"); return; } memset(rbuf, 0, 256); i = 0; do { ret = read(ffd, (rbuf+i), 1); } while (!isspace(rbuf[i++])); width = atoi(rbuf); memset(rbuf, 0, 256); i = 0; do { ret = read(ffd, (rbuf+i), 1); } while (!isspace(rbuf[i++])); height = atoi(rbuf); rowlength = ((width / 8) + 1); bmapbuf = malloc(rowlength * height); ret = read(ffd, bmapbuf, rowlength * height); close(ffd); #ifdef DEBUG fprintf(stderr, "row length = %d bytes\n", rowlength); fprintf(stderr, "bitmap resolution is %d x %d\n", width, height); fprintf(stderr, "read %d of %d bytes\n", ret, rowlength * height); fprintf(stderr, "\n"); for (y=0; y=16 || i>=strlen(body)) { mw_buf_print(mwbuf, 0, r*9, sstr, 0, MW_WHITE, MW_BLACK); memset(sstr,0,32); c=0; r++; if (r>10) break; }; }; bbuf = mw_make_mw_buffer(mwbuf, &len); mw_send_bitmap(mwdevice, MW_SCREEN_MODE_APPLICATION, 96, 96, 0, bbuf, len); mw_update_display(mwdevice, MW_SCREEN_MODE_APPLICATION, 1); mw_free_pbuffer(mwbuf); mw_set_vibrate_mode(mwdevice, 1, 300, 300, 2); } void test_application(mwdevice_t *mwdevice) { mw_buffer *mwbuf; unsigned char *bbuf; int len; mw_enable_button(mwdevice, MW_SCREEN_MODE_APPLICATION, MW_BUTTON_A, MW_BUTTON_IMMEDIATE, MW_BUTTON_EVENT_MESSAGE, MW_BUTTON_A); mw_configure_watch_mode(mwdevice, MW_SCREEN_MODE_APPLICATION, 0, 30, 0); mwbuf = mw_alloc_pbuffer(96, 96, 1); mw_buf_clear(mwbuf, MW_BLACK); mw_buf_print(mwbuf, 0, 0, " Application ", 0, MW_BLACK, MW_WHITE); mw_buf_print(mwbuf, 0, 9, "0123456789012345", 0, MW_WHITE, MW_BLACK); mw_buf_print(mwbuf, 0, 18, "0123456789g12345", 0, MW_WHITE, MW_BLACK); mw_buf_print(mwbuf, 0, 27, "0123456789012345", 0, MW_WHITE, MW_BLACK); mw_buf_print(mwbuf, 0, 36, "0123456789012345", 0, MW_WHITE, MW_BLACK); mw_buf_print(mwbuf, 0, 45, "0123456789012345", 0, MW_WHITE, MW_BLACK); mw_buf_print(mwbuf, 0, 54, "0123456789g12345", 0, MW_WHITE, MW_BLACK); mw_buf_print(mwbuf, 0, 63, "0123456789012345", 0, MW_WHITE, MW_BLACK); mw_buf_print(mwbuf, 0, 72, "0123456789012345", 0, MW_WHITE, MW_BLACK); mw_buf_print(mwbuf, 0, 81, "0123456789012345", 0, MW_WHITE, MW_BLACK); bbuf = mw_make_mw_buffer(mwbuf, &len); mw_send_bitmap(mwdevice, MW_SCREEN_MODE_APPLICATION, 96, 96, 0, bbuf, len); mw_update_display(mwdevice, MW_SCREEN_MODE_APPLICATION, 1); mw_free_pbuffer(mwbuf); } void test_oled(mwdevice_t *mwdevice, unsigned char oled) { mw_buffer *mwbuf; unsigned char *bbuf; int len, i; mwbuf = mw_alloc_pbuffer(80, 16, 1); mw_buf_clear(mwbuf, MW_BLACK); mw_buf_print(mwbuf, 0, 0, "Application", 0, MW_WHITE, MW_BLACK); mw_buf_print(mwbuf, 0, 8, "012345678901234", 0, MW_WHITE, MW_BLACK); bbuf = mw_make_mw_oled_buffer(mwbuf, &len); mw_write_oled_buffer(mwdevice, 0, oled, 80, 0, bbuf, len); mw_free_pbuffer(mwbuf); } void print_help(void) { } void process_cmd(char *cmdline, int clinep, mwdata_t *mwdata) { unsigned char mdata[32]; unsigned int intopt; fprintf(stderr, "command: '%s'\n", cmdline); if (strncmp(cmdline, "quit", 4) == 0) { //close(mw_fd); //exit(0); g_main_loop_quit (mwdata->mloop); } if (strncmp(cmdline, "help", 4) == 0) { print_help(); } if (strncmp(cmdline, "ahand", 5) == 0) { intopt = atoi(cmdline+5); g_print("Advance analog hands by %d minutes\n", intopt); mdata[0] = intopt / 60; mdata[1] = intopt % 60; mdata[2] = 0; mw_send_frame(&mwdata->mwdevice, MW_ADVANCE_WATCH_HANDS, 0, mdata, 3); } if (strncmp(cmdline, "srtc", 4) == 0) { fprintf(stderr, "Setting RTC from system time..."); mw_set_rtc(&mwdata->mwdevice, MW_RTC_CLOCK_12HR, MW_RTC_DATE_MMDD); fprintf(stderr, "OK\n"); } if (strncmp(cmdline, "grtc", 4) == 0) { mw_send_frame(&mwdata->mwdevice, MW_GET_REAL_TIME_CLOCK, 0, NULL, 0); } if (strncmp(cmdline, "gistr", 5) == 0) { intopt = cmdline[6]-0x30; if (intopt>=0 && intopt <=3) mdata[0] = intopt; else mdata[0] = 0; mw_send_frame(&mwdata->mwdevice, MW_GET_INFORMATION_STRING, 0, mdata, 1); } if (strncmp(cmdline, "gdtype", 6) == 0) { mw_send_frame(&mwdata->mwdevice, MW_GET_DEVICE_TYPE, 0, NULL, 0); } if (strncmp(cmdline, "rvbat", 5) == 0) { mw_send_frame(&mwdata->mwdevice, MW_READ_BATTERY_VOLTAGE_MSG, 0, NULL, 0); } if (strncmp(cmdline, "rlight", 6) == 0) { mw_send_frame(&mwdata->mwdevice, MW_READ_LIGHT_SENSOR_MSG, 0, NULL, 0); } if (strncmp(cmdline, "modecfg", 6) == 0) { mw_configure_watch_mode(&mwdata->mwdevice, MW_SCREEN_MODE_IDLE, 0, 4, 1); mw_update_display(&mwdata->mwdevice, MW_SCREEN_MODE_IDLE, 0); } if (strncmp(cmdline, "rbtcfg", 6) == 0) { intopt = cmdline[7]-0x30; mdata[0] = 0; /* idle screen */ mdata[1] = intopt; /* button index */ /* for reading the config those are unnecessary */ mdata[2] = 0; /* button press type */ mdata[3] = 0; /* callback message type */ mdata[4] = 0; /* callback message option */ mw_send_frame(&mwdata->mwdevice, MW_READ_BUTTON_CONFIG, 0, mdata, 5); } if (strncmp(cmdline, "svib", 4) == 0) { mw_set_vibrate_mode(&mwdata->mwdevice, 1, 300, 300, 5); } if (strncmp(cmdline, "tbmp", 4) == 0) { bitmap_test(&mwdata->mwdevice); } if (strncmp(cmdline, "t2bmp", 5) == 0) { bitmap_test2(&mwdata->mwdevice); } if (strncmp(cmdline, "text", 4) == 0) { text_test(&mwdata->mwdevice); } if (strncmp(cmdline, "tline", 5) == 0) { line_test(&mwdata->mwdevice); } if (strncmp(cmdline, "rbmp", 4) == 0) { bitmap_read(&mwdata->mwdevice); } if (strncmp(cmdline, "tnote", 5) == 0) { // test_notification(&mwdata->mwdevice); mw_do_notification(&mwdata->mwdevice, "TestNotification", "This is a pretty long text that needs to be broken and torn", 1); } if (strncmp(cmdline, "tapp", 4) == 0) { test_application(&mwdata->mwdevice); } if (strncmp(cmdline, "eoled", 5) == 0) { intopt = cmdline[6]-0x30; if (intopt == MW_OLED_UPPER || intopt == MW_OLED_LOWER) mw_send_frame(&mwdata->mwdevice, MW_ENABLE_OLED_DISPLAY_MSG, intopt, NULL, 0); } if (strncmp(cmdline, "toled", 5) == 0) { intopt = cmdline[6]-0x30; test_oled(&mwdata->mwdevice, intopt); //mw_write_oled_buffer(mwdevice, 0, 80, 0, mdata, 10); //mw_send_frame(mwdevice, MW_UPDATE_OLED_DISPLAY_MSG, 0, NULL, 0); } } int feed_menu(mwdata_t *mdata) { int rcvd; rcvd = read(0, (mdata->cmdline+mdata->cmdline_pos), 1); if (rcvd > 0) { if (mdata->cmdline[mdata->cmdline_pos] == '\r') { printf("\n"); mdata->cmdline[mdata->cmdline_pos--] = '\0'; process_cmd(mdata->cmdline, mdata->cmdline_pos, mdata); mdata->cmdline_pos = 0; memset(mdata->cmdline, 0, 128); } else { mdata->cmdline_pos++; if (mdata->cmdline_pos > 75) mdata->cmdline_pos = 75; printf("\r> %s", mdata->cmdline); fflush(stdout); } } return 0; } gboolean handle_mw_io(GIOChannel *mw_io, GIOCondition condition, gpointer udata) { mwdata_t *mdata = (mwdata_t *)udata; int rcvd; rcvd = read(mdata->mwdevice.mw_fd, mdata->rcvbuf+mdata->rcvbuf_pos, 64); #ifdef DEBUG fprintf(stderr, "read %d bytes:\n", rcvd); #endif if (rcvd > 0) { #ifdef DEBUG dump_frame(mdata->rcvbuf, rcvd); #endif decode_frame(&mdata->mwdevice, mdata->rcvbuf, rcvd); mdata->rcvbuf_pos = 0; } return TRUE; } gboolean handle_min_io(GIOChannel *mw_io, GIOCondition condition, gpointer udata) { mwdata_t *mdata = (mwdata_t *)udata; feed_menu(mdata); return TRUE; } static DBusHandlerResult signal_filter (DBusConnection *connection, DBusMessage *message, void *user_data) { //GMainLoop *loop = user_data; mwdata_t *mdata = (mwdata_t *)user_data; DBusError error; char *app_name, *app_icon, *summary, *body; // **actions; int replace_id; // dict, expire; /* A signal from the bus saying we are about to be disconnected */ if (dbus_message_is_signal(message, "org.freedesktop.Local", "Disconnected")) { /* Tell the main loop to quit */ g_main_loop_quit (mdata->mloop); /* We have handled this message, don't pass it on */ return DBUS_HANDLER_RESULT_HANDLED; } if (dbus_message_is_method_call (message,"org.freedesktop.Notifications", "Notify")) { g_print("method call\n"); dbus_error_init (&error); if (dbus_message_get_args(message, &error, DBUS_TYPE_STRING, &app_name, DBUS_TYPE_UINT32, &replace_id, DBUS_TYPE_STRING, &app_icon, DBUS_TYPE_STRING, &summary, DBUS_TYPE_STRING, &body, /* G_TYPE_STRV, &actions, DBUS_TYPE_DICT_ENTRY_AS_STRING, &dict, DBUS_TYPE_INT32, &expire, */ DBUS_TYPE_INVALID)) { g_print("Notify received: from app %s, icon %s:\nSummary: %s\nBody: %s\n", app_name, app_icon, summary, body); mw_send_notify(&mdata->mwdevice, summary, body); /* we just listen, we do not handle it here */ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; } else { g_print("Notify received, but error getting message: %s\n", error.message); dbus_error_free (&error); } } else { g_print("Not signal received\n"); return DBUS_HANDLER_RESULT_HANDLED; }; return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; } int main(int argc, char **argv) { int mw_fd; mwdata_t mdata; struct termios tconfd, otconfd, tmwfd; bdaddr_t btaddr; GIOChannel *mw_io, *m_in; DBusConnection *bus; DBusError error; if (argc != 2) { fprintf(stderr, "Usage:\n\t%s \n", argv[0]); return 1; }; g_type_init(); mdata.mloop = g_main_loop_new (NULL, FALSE); crc16ccitt_init(); #if USE_RFCOMM_FILE mw_fd = open(argv[1], O_RDWR); if (mw_fd < 0) { perror("open"); return 1; }; #else if (str2ba(argv[1], &btaddr)) return 1; mw_fd = open_socket(&btaddr, 1); if (mw_fd < 0) { return 1; } else { fprintf(stderr, "connected to %s\n", argv[1]); }; #endif /* we have a connection, RFCOMM socket is on mw_fd */ /* make the tty raw */ tcgetattr(mw_fd, &tmwfd); cfmakeraw(&tmwfd); tmwfd.c_oflag |= ONLCR | OPOST; tmwfd.c_lflag |= ISIG; tcsetattr(mw_fd, TCSANOW, &tmwfd); mdata.mwdevice.mw_fd = mw_fd; mdata.rcvbuf_pos = 0; memset(mdata.rcvbuf, 0, 128); dbus_error_init (&error); bus = dbus_bus_get (DBUS_BUS_SESSION, &error); if (!bus) { g_warning ("Failed to connect to the D-BUS daemon: %s", error.message); dbus_error_free (&error); return 1; } dbus_connection_setup_with_g_main (bus, NULL); dbus_bus_add_match (bus, "interface='org.freedesktop.Notifications'", &error); dbus_connection_add_filter (bus, signal_filter, &mdata, NULL); // menu(mw_fd); mw_io = g_io_channel_unix_new(mw_fd); g_io_add_watch(mw_io, G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP, handle_mw_io, &mdata); tcgetattr(0, &tconfd); tcgetattr(0, &otconfd); cfmakeraw(&tconfd); tconfd.c_oflag |= ONLCR | OPOST; tconfd.c_lflag |= ISIG; tcsetattr(0, TCSANOW, &tconfd); mdata.con_fd = 0; m_in = g_io_channel_unix_new(0); /* stdin for kbd */ g_io_add_watch(m_in, G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP, handle_min_io, &mdata); memset(mdata.cmdline, 0, 128); mdata.cmdline_pos = 0; mw_init(&mdata.mwdevice, mw_fd); g_main_loop_run (mdata.mloop); fsync(mw_fd); close(mw_fd); /* reset the controlling tty again */ tcsetattr(0, TCSANOW, &otconfd); return 0; };