sl@0: /******************************************************* sl@0: Demo Program for HIDAPI sl@3: sl@0: Alan Ott sl@0: Signal 11 Software sl@0: sl@0: 2010-07-20 sl@0: sl@0: Copyright 2010, All Rights Reserved sl@3: sl@0: This contents of this file may be used by anyone sl@0: for any reason without any conditions and may be sl@0: used as a starting point for your own applications sl@0: which use HIDAPI. sl@0: ********************************************************/ sl@0: sl@6: #include "MainWindow.h" sl@0: sl@0: sl@0: // FOX 1.7 changes the timeouts to all be nanoseconds. sl@0: // Fox 1.6 had all timeouts as milliseconds. sl@0: #if (FOX_MINOR >= 7) sl@0: const int timeout_scalar = 1000*1000; sl@0: #else sl@0: const int timeout_scalar = 1; sl@0: #endif sl@0: sl@0: sl@0: FXDEFMAP(MainWindow) MainWindowMap [] = { sl@0: FXMAPFUNC(SEL_COMMAND, MainWindow::ID_CONNECT, MainWindow::onConnect ), sl@0: FXMAPFUNC(SEL_COMMAND, MainWindow::ID_DISCONNECT, MainWindow::onDisconnect ), sl@0: FXMAPFUNC(SEL_COMMAND, MainWindow::ID_RESCAN, MainWindow::onRescan ), sl@0: FXMAPFUNC(SEL_COMMAND, MainWindow::ID_SEND_OUTPUT_REPORT, MainWindow::onSendOutputReport ), sl@0: FXMAPFUNC(SEL_COMMAND, MainWindow::ID_SEND_FEATURE_REPORT, MainWindow::onSendFeatureReport ), sl@0: FXMAPFUNC(SEL_COMMAND, MainWindow::ID_GET_FEATURE_REPORT, MainWindow::onGetFeatureReport ), sl@0: FXMAPFUNC(SEL_COMMAND, MainWindow::ID_CLEAR, MainWindow::onClear ), sl@2: FXMAPFUNC(SEL_COMMAND, MainWindow::ID_FUTABA_CLEAR_DISPLAY, MainWindow::onFutabaClearDisplay ), sl@2: FXMAPFUNC(SEL_COMMAND, MainWindow::ID_FUTABA_DIMMING, MainWindow::onFutabaDimming ), sl@2: FXMAPFUNC(SEL_COMMAND, MainWindow::ID_FUTABA_DISPLAY_DATA_INPUT, MainWindow::onFutabaDisplayDataInput ), sl@2: FXMAPFUNC(SEL_COMMAND, MainWindow::ID_FUTABA_READ_ID, MainWindow::onFutabaReadId ), sl@2: FXMAPFUNC(SEL_COMMAND, MainWindow::ID_FUTABA_READ_FIRMWARE_REVISION, MainWindow::onFutabaReadFirmwareRevision ), sl@2: FXMAPFUNC(SEL_COMMAND, MainWindow::ID_FUTABA_POWER_SUPPLY_MONITOR, MainWindow::onFutabaPowerSupplyMonitor ), sl@3: FXMAPFUNC(SEL_COMMAND, MainWindow::ID_FUTABA_SET_PIXEL, MainWindow::onFutabaSetPixel ), sl@4: FXMAPFUNC(SEL_COMMAND, MainWindow::ID_FUTABA_RESET_PIXEL, MainWindow::onFutabaResetPixel ), sl@3: FXMAPFUNC(SEL_COMMAND, MainWindow::ID_FUTABA_SET_ALL_PIXELS, MainWindow::onFutabaSetAllPixels ), sl@6: FXMAPFUNC(SEL_COMMAND, MainWindow::ID_SELECT_FONT, MainWindow::onSelectFont ), sl@0: FXMAPFUNC(SEL_TIMEOUT, MainWindow::ID_TIMER, MainWindow::onTimeout ), sl@0: FXMAPFUNC(SEL_TIMEOUT, MainWindow::ID_MAC_TIMER, MainWindow::onMacTimeout ), sl@0: }; sl@0: sl@0: FXIMPLEMENT(MainWindow, FXMainWindow, MainWindowMap, ARRAYNUMBER(MainWindowMap)); sl@0: sl@0: MainWindow::MainWindow(FXApp *app) sl@3: : FXMainWindow(app, "HIDAPI Test Application", NULL, NULL, DECOR_ALL, 200,100, 600,900) sl@0: { sl@2: iDimming=0x35; sl@0: devices = NULL; sl@0: connected_device = NULL; sl@0: sl@0: FXVerticalFrame *vf = new FXVerticalFrame(this, LAYOUT_FILL_Y|LAYOUT_FILL_X); sl@0: sl@0: FXLabel *label = new FXLabel(vf, "HIDAPI Test Tool"); sl@0: title_font = new FXFont(getApp(), "Arial", 14, FXFont::Bold); sl@0: label->setFont(title_font); sl@3: sl@0: new FXLabel(vf, sl@0: "Select a device and press Connect.", NULL, JUSTIFY_LEFT); sl@0: new FXLabel(vf, sl@0: "Output data bytes can be entered in the Output section, \n" sl@0: "separated by space, comma or brackets. Data starting with 0x\n" sl@0: "is treated as hex. Data beginning with a 0 is treated as \n" sl@0: "octal. All other data is treated as decimal.", NULL, JUSTIFY_LEFT); sl@0: new FXLabel(vf, sl@0: "Data received from the device appears in the Input section.", sl@0: NULL, JUSTIFY_LEFT); sl@0: new FXLabel(vf, sl@0: "Optionally, a report length may be specified. Extra bytes are\n" sl@0: "padded with zeros. If no length is specified, the length is \n" sl@0: "inferred from the data.", sl@0: NULL, JUSTIFY_LEFT); sl@0: new FXLabel(vf, ""); sl@0: sl@0: // Device List and Connect/Disconnect buttons sl@0: FXHorizontalFrame *hf = new FXHorizontalFrame(vf, LAYOUT_FILL_X); sl@0: //device_list = new FXList(new FXHorizontalFrame(hf,FRAME_SUNKEN|FRAME_THICK, 0,0,0,0, 0,0,0,0), NULL, 0, LISTBOX_NORMAL|LAYOUT_FILL_X|LAYOUT_FILL_Y|LAYOUT_FIX_WIDTH|LAYOUT_FIX_HEIGHT, 0,0,300,200); sl@0: device_list = new FXList(new FXHorizontalFrame(hf,FRAME_SUNKEN|FRAME_THICK|LAYOUT_FILL_X|LAYOUT_FILL_Y, 0,0,0,0, 0,0,0,0), NULL, 0, LISTBOX_NORMAL|LAYOUT_FILL_X|LAYOUT_FILL_Y, 0,0,300,200); sl@0: FXVerticalFrame *buttonVF = new FXVerticalFrame(hf); sl@0: connect_button = new FXButton(buttonVF, "Connect", NULL, this, ID_CONNECT, BUTTON_NORMAL|LAYOUT_FILL_X); sl@0: disconnect_button = new FXButton(buttonVF, "Disconnect", NULL, this, ID_DISCONNECT, BUTTON_NORMAL|LAYOUT_FILL_X); sl@0: disconnect_button->disable(); sl@0: rescan_button = new FXButton(buttonVF, "Re-Scan devices", NULL, this, ID_RESCAN, BUTTON_NORMAL|LAYOUT_FILL_X); sl@0: new FXHorizontalFrame(buttonVF, 0, 0,0,0,0, 0,0,50,0); sl@0: sl@0: connected_label = new FXLabel(vf, "Disconnected"); sl@3: sl@6: //Font group sl@6: new FXHorizontalFrame(vf); sl@6: FXGroupBox *gb = new FXGroupBox(vf, "Fonts", FRAME_GROOVE|LAYOUT_FILL_X); sl@6: FXMatrix *matrix = new FXMatrix(gb, 3, MATRIX_BY_COLUMNS|LAYOUT_FILL_X); sl@6: iButtonSelectFont = new FXButton(matrix, "Select Font", NULL, this, ID_SELECT_FONT, BUTTON_NORMAL|LAYOUT_FILL_X); sl@6: sl@2: //Futaba VFD commands sl@0: new FXHorizontalFrame(vf); sl@6: gb = new FXGroupBox(vf, "Futaba GP1212A01A", FRAME_GROOVE|LAYOUT_FILL_X); sl@6: matrix = new FXMatrix(gb, 3, MATRIX_BY_COLUMNS|LAYOUT_FILL_X); sl@2: iButtonClearDisplay = new FXButton(matrix, "Clear Display", NULL, this, ID_FUTABA_CLEAR_DISPLAY, BUTTON_NORMAL|LAYOUT_FILL_X); sl@2: iButtonDimming = new FXButton(matrix, "Dimming", NULL, this, ID_FUTABA_DIMMING, BUTTON_NORMAL|LAYOUT_FILL_X); sl@2: iButtonDisplayDataInput = new FXButton(matrix, "Display Data Input", NULL, this, ID_FUTABA_DISPLAY_DATA_INPUT, BUTTON_NORMAL|LAYOUT_FILL_X); sl@2: iButtonReadId = new FXButton(matrix, "Read Id", NULL, this, ID_FUTABA_READ_ID, BUTTON_NORMAL|LAYOUT_FILL_X); sl@2: iButtonReadFirmwareRevision = new FXButton(matrix, "Read Firmware Revision", NULL, this, ID_FUTABA_READ_FIRMWARE_REVISION, BUTTON_NORMAL|LAYOUT_FILL_X); sl@2: iButtonPowerSupplyMonitor = new FXButton(matrix, "Power Supply Monitor", NULL, this, ID_FUTABA_POWER_SUPPLY_MONITOR, BUTTON_NORMAL|LAYOUT_FILL_X); sl@3: new FXLabel(matrix, "X",NULL,LABEL_NORMAL|LAYOUT_FILL_X); sl@3: new FXLabel(matrix, "Y",NULL,LABEL_NORMAL|LAYOUT_FILL_X); sl@3: new FXLabel(matrix, "",NULL,LABEL_NORMAL|LAYOUT_FILL_X); sl@3: iTextFieldX = new FXTextField(matrix, 3, NULL, 0, TEXTFIELD_NORMAL|TEXTFIELD_INTEGER|LAYOUT_FILL_X); sl@3: iTextFieldY = new FXTextField(matrix, 3, NULL, 0, TEXTFIELD_NORMAL|TEXTFIELD_INTEGER|LAYOUT_FILL_X); sl@4: iButtonSetPixel = new FXButton(matrix, "Set Pixel", NULL, this, ID_FUTABA_SET_PIXEL, BUTTON_NORMAL|LAYOUT_FILL_X); sl@4: iButtonResetPixel = new FXButton(matrix, "Reset Pixel", NULL, this, ID_FUTABA_RESET_PIXEL, BUTTON_NORMAL|LAYOUT_FILL_X); sl@3: // sl@3: iButtonSetAllPixels = new FXButton(matrix, "Set All Pixels", NULL, this, ID_FUTABA_SET_ALL_PIXELS, BUTTON_NORMAL|LAYOUT_FILL_X); sl@6: // sl@6: sl@2: // sl@2: iButtonClearDisplay->disable(); sl@2: iButtonDimming->disable(); sl@2: iButtonDisplayDataInput->disable(); sl@2: iButtonReadId->disable(); sl@2: iButtonReadFirmwareRevision->disable(); sl@2: iButtonPowerSupplyMonitor->disable(); sl@3: iTextFieldX->disable(); sl@3: iTextFieldY->disable(); sl@3: iButtonSetPixel->disable(); sl@4: iButtonResetPixel->disable(); sl@3: iButtonSetAllPixels->disable(); sl@2: // sl@2: sl@0: // Output Group Box sl@3: new FXHorizontalFrame(vf); sl@2: gb = new FXGroupBox(vf, "Output", FRAME_GROOVE|LAYOUT_FILL_X); sl@2: matrix = new FXMatrix(gb, 3, MATRIX_BY_COLUMNS|LAYOUT_FILL_X); sl@0: new FXLabel(matrix, "Data"); sl@0: new FXLabel(matrix, "Length"); sl@0: new FXLabel(matrix, ""); sl@0: sl@0: //hf = new FXHorizontalFrame(gb, LAYOUT_FILL_X); sl@0: output_text = new FXTextField(matrix, 30, NULL, 0, TEXTFIELD_NORMAL|LAYOUT_FILL_X|LAYOUT_FILL_COLUMN); sl@0: output_text->setText("1 0x81 0"); sl@0: output_len = new FXTextField(matrix, 5, NULL, 0, TEXTFIELD_NORMAL|LAYOUT_FILL_X|LAYOUT_FILL_COLUMN); sl@0: output_button = new FXButton(matrix, "Send Output Report", NULL, this, ID_SEND_OUTPUT_REPORT, BUTTON_NORMAL|LAYOUT_FILL_X); sl@0: output_button->disable(); sl@0: //new FXHorizontalFrame(matrix, LAYOUT_FILL_X); sl@0: sl@0: //hf = new FXHorizontalFrame(gb, LAYOUT_FILL_X); sl@0: feature_text = new FXTextField(matrix, 30, NULL, 0, TEXTFIELD_NORMAL|LAYOUT_FILL_X|LAYOUT_FILL_COLUMN); sl@0: feature_len = new FXTextField(matrix, 5, NULL, 0, TEXTFIELD_NORMAL|LAYOUT_FILL_X|LAYOUT_FILL_COLUMN); sl@0: feature_button = new FXButton(matrix, "Send Feature Report", NULL, this, ID_SEND_FEATURE_REPORT, BUTTON_NORMAL|LAYOUT_FILL_X); sl@0: feature_button->disable(); sl@0: sl@0: get_feature_text = new FXTextField(matrix, 30, NULL, 0, TEXTFIELD_NORMAL|LAYOUT_FILL_X|LAYOUT_FILL_COLUMN); sl@0: new FXWindow(matrix); sl@0: get_feature_button = new FXButton(matrix, "Get Feature Report", NULL, this, ID_GET_FEATURE_REPORT, BUTTON_NORMAL|LAYOUT_FILL_X); sl@0: get_feature_button->disable(); sl@0: sl@0: sl@0: // Input Group Box sl@0: gb = new FXGroupBox(vf, "Input", FRAME_GROOVE|LAYOUT_FILL_X|LAYOUT_FILL_Y); sl@0: FXVerticalFrame *innerVF = new FXVerticalFrame(gb, LAYOUT_FILL_X|LAYOUT_FILL_Y); sl@0: input_text = new FXText(new FXHorizontalFrame(innerVF,LAYOUT_FILL_X|LAYOUT_FILL_Y|FRAME_SUNKEN|FRAME_THICK, 0,0,0,0, 0,0,0,0), NULL, 0, LAYOUT_FILL_X|LAYOUT_FILL_Y); sl@0: input_text->setEditable(false); sl@0: new FXButton(innerVF, "Clear", NULL, this, ID_CLEAR, BUTTON_NORMAL|LAYOUT_RIGHT); sl@3: sl@0: sl@0: } sl@0: sl@0: MainWindow::~MainWindow() sl@0: { sl@0: if (connected_device) sl@0: hid_close(connected_device); sl@0: hid_exit(); sl@0: delete title_font; sl@0: } sl@0: sl@0: void sl@0: MainWindow::create() sl@0: { sl@0: FXMainWindow::create(); sl@0: show(); sl@0: sl@0: onRescan(NULL, 0, NULL); sl@3: sl@0: sl@0: #ifdef __APPLE__ sl@0: init_apple_message_system(); sl@0: #endif sl@3: sl@0: getApp()->addTimeout(this, ID_MAC_TIMER, sl@0: 50 * timeout_scalar /*50ms*/); sl@0: } sl@0: sl@0: long sl@0: MainWindow::onConnect(FXObject *sender, FXSelector sel, void *ptr) sl@0: { sl@0: if (connected_device != NULL) sl@0: return 1; sl@3: sl@0: FXint cur_item = device_list->getCurrentItem(); sl@0: if (cur_item < 0) sl@0: return -1; sl@0: FXListItem *item = device_list->getItem(cur_item); sl@0: if (!item) sl@0: return -1; sl@0: struct hid_device_info *device_info = (struct hid_device_info*) item->getData(); sl@0: if (!device_info) sl@0: return -1; sl@3: sl@0: connected_device = hid_open_path(device_info->path); sl@3: sl@0: if (!connected_device) { sl@0: FXMessageBox::error(this, MBOX_OK, "Device Error", "Unable To Connect to Device"); sl@0: return -1; sl@0: } sl@3: sl@0: hid_set_nonblocking(connected_device, 1); sl@0: sl@0: getApp()->addTimeout(this, ID_TIMER, sl@0: 5 * timeout_scalar /*5ms*/); sl@3: sl@0: FXString s; sl@0: s.format("Connected to: %04hx:%04hx -", device_info->vendor_id, device_info->product_id); sl@0: s += FXString(" ") + device_info->manufacturer_string; sl@0: s += FXString(" ") + device_info->product_string; sl@0: connected_label->setText(s); sl@0: output_button->enable(); sl@0: feature_button->enable(); sl@0: get_feature_button->enable(); sl@0: connect_button->disable(); sl@0: disconnect_button->enable(); sl@0: input_text->setText(""); sl@2: // sl@2: iButtonClearDisplay->enable(); sl@2: iButtonDimming->enable(); sl@2: iButtonDisplayDataInput->enable(); sl@2: iButtonReadId->enable(); sl@2: iButtonReadFirmwareRevision->enable(); sl@2: iButtonPowerSupplyMonitor->enable(); sl@3: iTextFieldX->enable(); sl@3: iTextFieldY->enable(); sl@3: iButtonSetPixel->enable(); sl@4: iButtonResetPixel->enable(); sl@3: iButtonSetAllPixels->enable(); sl@3: sl@2: // sl@2: iOutputReportBuffer=new unsigned char[KFutabaOutputReportLength]; //TODO: use connected_device->output_report_length sl@0: sl@0: return 1; sl@0: } sl@0: sl@0: long sl@0: MainWindow::onDisconnect(FXObject *sender, FXSelector sel, void *ptr) sl@0: { sl@0: hid_close(connected_device); sl@0: connected_device = NULL; sl@0: connected_label->setText("Disconnected"); sl@0: output_button->disable(); sl@0: feature_button->disable(); sl@0: get_feature_button->disable(); sl@0: connect_button->enable(); sl@0: disconnect_button->disable(); sl@0: sl@0: getApp()->removeTimeout(this, ID_TIMER); sl@2: sl@2: // sl@2: iButtonClearDisplay->disable(); sl@2: iButtonDimming->disable(); sl@2: iButtonDisplayDataInput->disable(); sl@2: iButtonReadId->disable(); sl@2: iButtonReadFirmwareRevision->disable(); sl@2: iButtonPowerSupplyMonitor->disable(); sl@3: iTextFieldX->disable(); sl@3: iTextFieldY->disable(); sl@3: iButtonSetPixel->disable(); sl@4: iButtonResetPixel->disable(); sl@3: iButtonSetAllPixels->disable(); sl@2: // sl@2: sl@2: sl@2: delete iOutputReportBuffer; sl@2: iOutputReportBuffer=NULL; sl@3: sl@0: return 1; sl@0: } sl@0: sl@0: long sl@0: MainWindow::onRescan(FXObject *sender, FXSelector sel, void *ptr) sl@0: { sl@0: struct hid_device_info *cur_dev; sl@0: sl@0: device_list->clearItems(); sl@3: sl@2: //Define Futaba vendor ID to filter our list of device sl@2: const unsigned short KFutabaVendorId = 0x1008; sl@2: sl@0: // List the Devices sl@0: hid_free_enumeration(devices); sl@0: devices = hid_enumerate(0x0, 0x0); sl@3: cur_dev = devices; sl@0: while (cur_dev) { sl@2: // Add it to the List Box only if it is a Futaba device sl@3: if (cur_dev->vendor_id == KFutabaVendorId) sl@2: { sl@2: FXString s; sl@2: FXString usage_str; sl@2: s.format("%04hx:%04hx -", cur_dev->vendor_id, cur_dev->product_id); sl@2: s += FXString(" ") + cur_dev->manufacturer_string; sl@2: s += FXString(" ") + cur_dev->product_string; sl@2: usage_str.format(" (usage: %04hx:%04hx) ", cur_dev->usage_page, cur_dev->usage); sl@2: s += usage_str; sl@2: FXListItem *li = new FXListItem(s, NULL, cur_dev); sl@2: device_list->appendItem(li); sl@2: } sl@3: sl@0: cur_dev = cur_dev->next; sl@0: } sl@0: sl@0: if (device_list->getNumItems() == 0) sl@0: device_list->appendItem("*** No Devices Connected ***"); sl@0: else { sl@0: device_list->selectItem(0); sl@0: } sl@0: sl@0: return 1; sl@0: } sl@0: sl@0: size_t sl@0: MainWindow::getDataFromTextField(FXTextField *tf, char *buf, size_t len) sl@0: { sl@0: const char *delim = " ,{}\t\r\n"; sl@0: FXString data = tf->getText(); sl@0: const FXchar *d = data.text(); sl@0: size_t i = 0; sl@3: sl@0: // Copy the string from the GUI. sl@0: size_t sz = strlen(d); sl@0: char *str = (char*) malloc(sz+1); sl@0: strcpy(str, d); sl@3: sl@0: // For each token in the string, parse and store in buf[]. sl@0: char *token = strtok(str, delim); sl@0: while (token) { sl@0: char *endptr; sl@0: long int val = strtol(token, &endptr, 0); sl@0: buf[i++] = val; sl@0: token = strtok(NULL, delim); sl@0: } sl@3: sl@0: free(str); sl@0: return i; sl@0: } sl@0: sl@0: /* getLengthFromTextField() sl@0: Returns length: sl@0: 0: empty text field sl@0: >0: valid length sl@0: -1: invalid length */ sl@0: int sl@0: MainWindow::getLengthFromTextField(FXTextField *tf) sl@0: { sl@0: long int len; sl@0: FXString str = tf->getText(); sl@0: size_t sz = str.length(); sl@0: sl@0: if (sz > 0) { sl@0: char *endptr; sl@0: len = strtol(str.text(), &endptr, 0); sl@0: if (endptr != str.text() && *endptr == '\0') { sl@0: if (len <= 0) { sl@0: FXMessageBox::error(this, MBOX_OK, "Invalid length", "Enter a length greater than zero."); sl@0: return -1; sl@0: } sl@0: return len; sl@0: } sl@0: else sl@0: return -1; sl@0: } sl@0: sl@0: return 0; sl@0: } sl@0: sl@0: long sl@0: MainWindow::onSendOutputReport(FXObject *sender, FXSelector sel, void *ptr) sl@0: { sl@0: char buf[256]; sl@0: size_t data_len, len; sl@0: int textfield_len; sl@0: sl@0: memset(buf, 0x0, sizeof(buf)); sl@0: textfield_len = getLengthFromTextField(output_len); sl@0: data_len = getDataFromTextField(output_text, buf, sizeof(buf)); sl@0: sl@0: if (textfield_len < 0) { sl@0: FXMessageBox::error(this, MBOX_OK, "Invalid length", "Length field is invalid. Please enter a number in hex, octal, or decimal."); sl@0: return 1; sl@0: } sl@0: sl@0: if (textfield_len > sizeof(buf)) { sl@0: FXMessageBox::error(this, MBOX_OK, "Invalid length", "Length field is too long."); sl@0: return 1; sl@0: } sl@0: sl@0: len = (textfield_len)? textfield_len: data_len; sl@0: sl@0: int res = hid_write(connected_device, (const unsigned char*)buf, len); sl@0: if (res < 0) { sl@0: FXMessageBox::error(this, MBOX_OK, "Error Writing", "Could not write to device. Error reported was: %ls", hid_error(connected_device)); sl@0: } sl@3: sl@0: return 1; sl@0: } sl@0: sl@0: long sl@0: MainWindow::onSendFeatureReport(FXObject *sender, FXSelector sel, void *ptr) sl@0: { sl@0: char buf[256]; sl@0: size_t data_len, len; sl@0: int textfield_len; sl@0: sl@0: memset(buf, 0x0, sizeof(buf)); sl@0: textfield_len = getLengthFromTextField(feature_len); sl@0: data_len = getDataFromTextField(feature_text, buf, sizeof(buf)); sl@0: sl@0: if (textfield_len < 0) { sl@0: FXMessageBox::error(this, MBOX_OK, "Invalid length", "Length field is invalid. Please enter a number in hex, octal, or decimal."); sl@0: return 1; sl@0: } sl@0: sl@0: if (textfield_len > sizeof(buf)) { sl@0: FXMessageBox::error(this, MBOX_OK, "Invalid length", "Length field is too long."); sl@0: return 1; sl@0: } sl@0: sl@0: len = (textfield_len)? textfield_len: data_len; sl@0: sl@3: int res = hid_send_feature_report(connected_device, (const unsigned char*)buf, len); sl@0: if (res < 0) { sl@0: FXMessageBox::error(this, MBOX_OK, "Error Writing", "Could not send feature report to device. Error reported was: %ls", hid_error(connected_device)); sl@0: } sl@0: sl@0: return 1; sl@0: } sl@0: sl@0: long sl@0: MainWindow::onGetFeatureReport(FXObject *sender, FXSelector sel, void *ptr) sl@0: { sl@0: char buf[256]; sl@0: size_t len; sl@0: sl@0: memset(buf, 0x0, sizeof(buf)); sl@0: len = getDataFromTextField(get_feature_text, buf, sizeof(buf)); sl@0: sl@0: if (len != 1) { sl@0: FXMessageBox::error(this, MBOX_OK, "Too many numbers", "Enter only a single report number in the text field"); sl@0: } sl@0: sl@0: int res = hid_get_feature_report(connected_device, (unsigned char*)buf, sizeof(buf)); sl@0: if (res < 0) { sl@0: FXMessageBox::error(this, MBOX_OK, "Error Getting Report", "Could not get feature report from device. Error reported was: %ls", hid_error(connected_device)); sl@0: } sl@0: sl@0: if (res > 0) { sl@0: FXString s; sl@0: s.format("Returned Feature Report. %d bytes:\n", res); sl@0: for (int i = 0; i < res; i++) { sl@0: FXString t; sl@0: t.format("%02hhx ", buf[i]); sl@0: s += t; sl@0: if ((i+1) % 4 == 0) sl@0: s += " "; sl@0: if ((i+1) % 16 == 0) sl@0: s += "\n"; sl@0: } sl@0: s += "\n"; sl@0: input_text->appendText(s); sl@0: input_text->setBottomLine(INT_MAX); sl@0: } sl@3: sl@0: return 1; sl@0: } sl@0: sl@0: long sl@0: MainWindow::onClear(FXObject *sender, FXSelector sel, void *ptr) sl@0: { sl@0: input_text->setText(""); sl@0: return 1; sl@0: } sl@0: sl@2: sl@2: long sl@2: MainWindow::onFutabaClearDisplay(FXObject *sender, FXSelector sel, void *ptr) sl@2: { sl@2: memset(iOutputReportBuffer, 0x0, KFutabaOutputReportLength); sl@2: iOutputReportBuffer[0]=0x00; //Report ID sl@2: iOutputReportBuffer[1]=0x04; //Report length sl@2: iOutputReportBuffer[2]=0x1B; // sl@2: iOutputReportBuffer[3]=0x5B; // sl@2: iOutputReportBuffer[4]=0x32; // sl@2: iOutputReportBuffer[5]=0x4A; // sl@2: int res = hid_write(connected_device, iOutputReportBuffer, KFutabaOutputReportLength); sl@2: sl@2: return 1; sl@2: } sl@2: sl@2: long sl@2: MainWindow::onFutabaDimming(FXObject *sender, FXSelector sel, void *ptr) sl@2: { sl@2: memset(iOutputReportBuffer, 0x0, KFutabaOutputReportLength); sl@2: iOutputReportBuffer[0]=0x00; //Report ID sl@2: iOutputReportBuffer[1]=0x06; //Report length sl@2: iOutputReportBuffer[2]=0x1B; // sl@2: iOutputReportBuffer[3]=0x5C; // sl@2: iOutputReportBuffer[4]=0x3F; // sl@2: iOutputReportBuffer[5]=0x4C; // sl@2: iOutputReportBuffer[6]=0x44; // sl@2: iDimming = (iDimming==0x35?0x30:++iDimming); sl@2: iOutputReportBuffer[7]=iDimming; sl@2: int res = hid_write(connected_device, iOutputReportBuffer, KFutabaOutputReportLength); sl@2: sl@2: return 1; sl@2: } sl@2: sl@2: long sl@2: MainWindow::onFutabaDisplayDataInput(FXObject *sender, FXSelector sel, void *ptr) sl@2: { sl@2: //@1B 5B F0 00 00 07 00 01 FF sl@2: sl@2: memset(iOutputReportBuffer, 0x0, KFutabaOutputReportLength); sl@2: iOutputReportBuffer[0]=0x00; //Report ID sl@2: iOutputReportBuffer[1]=0x09; //Report length sl@2: iOutputReportBuffer[2]=0x1B; // sl@2: iOutputReportBuffer[3]=0x5B; // sl@2: iOutputReportBuffer[4]=0xF0; // sl@4: iOutputReportBuffer[5]=0x00; //X sl@4: iOutputReportBuffer[6]=0x00; //Y sl@2: iOutputReportBuffer[7]=0x07; // sl@2: iOutputReportBuffer[8]=0x00; // sl@2: iOutputReportBuffer[9]=0x01; // sl@2: iOutputReportBuffer[10]=0xFF; // sl@2: int res = hid_write(connected_device, iOutputReportBuffer, KFutabaOutputReportLength); sl@2: sl@3: sl@2: return 1; sl@2: } sl@2: sl@5: sl@5: sl@5: sl@4: /** sl@5: Set a single pixel to the specified value. sl@5: @param X coordinate of our pixel. sl@5: @param Y coordinate of our pixel. sl@5: @param The LSB defines our pixel value. sl@4: */ sl@4: void MainWindow::SetPixel(int aX, int aY, unsigned char aValue) sl@4: { sl@5: //Just specify a one pixel block sl@5: SetPixelBlock(aX,aY,0x00,0x01,aValue); sl@5: } sl@5: sl@5: /** sl@5: */ sl@5: void MainWindow::SetPixelBlock(int aX, int aY, int aHeight, int aSize, unsigned char aValue) sl@5: { sl@5: //Size must be 63 or below sl@4: memset(iOutputReportBuffer, 0x0, KFutabaOutputReportLength); sl@4: iOutputReportBuffer[0]=0x00; //Report ID sl@5: iOutputReportBuffer[1]=0x08+aSize; //Report length sl@4: iOutputReportBuffer[2]=0x1B; // sl@4: iOutputReportBuffer[3]=0x5B; // sl@4: iOutputReportBuffer[4]=0xF0; // sl@4: iOutputReportBuffer[5]=aX; //X sl@4: iOutputReportBuffer[6]=aY; //Y sl@6: iOutputReportBuffer[7]=aHeight; //Y length before return. Though outside the specs, setting this to zero apparently allows us to modify a single pixel without touching any other. sl@4: iOutputReportBuffer[8]=0x00; //Size of pixel data in bytes (MSB) sl@5: iOutputReportBuffer[9]=aSize; //Size of pixel data in bytes (LSB) sl@5: memset(iOutputReportBuffer+10, aValue, KFutabaOutputReportLength); sl@5: //iOutputReportBuffer[10]=aValue; //Pixel data sl@4: int res = hid_write(connected_device, iOutputReportBuffer, KFutabaOutputReportLength); sl@4: } sl@4: sl@5: sl@4: /** sl@4: */ sl@4: long MainWindow::onFutabaSetPixel(FXObject *sender, FXSelector sel, void *ptr) sl@4: { sl@4: int x=0; sl@4: int y=0; sl@4: iTextFieldX->getText().scan("%d",&x); sl@4: iTextFieldY->getText().scan("%d",&y); sl@4: SetPixel(x,y,0x01); sl@4: return 1; sl@4: } sl@4: sl@4: /** sl@4: */ sl@4: long MainWindow::onFutabaResetPixel(FXObject *sender, FXSelector sel, void *ptr) sl@4: { sl@4: int x=0; sl@4: int y=0; sl@4: iTextFieldX->getText().scan("%d",&x); sl@4: iTextFieldY->getText().scan("%d",&y); sl@4: SetPixel(x,y,0x00); sl@4: return 1; sl@4: } sl@4: sl@4: long MainWindow::onFutabaSetAllPixels(FXObject *sender, FXSelector sel, void *ptr) sl@4: { sl@5: //One pixel at a time sl@5: /* sl@4: for (int i=0;i<256;i++) sl@4: { sl@4: for (int j=0;j<64;j++) sl@4: { sl@4: SetPixel(i,j,0x01); sl@4: } sl@5: } sl@5: */ sl@5: //16x16=256 pixels at a time goes much faster sl@5: for (int i=0;i<256;i+=16) sl@5: { sl@5: for (int j=0;j<64;j+=16) sl@5: { sl@5: SetPixelBlock(i,j,15,32,0xFF); sl@5: //FXThread::sleep(1000000000); sl@5: } sl@4: } sl@4: sl@4: return 1; sl@4: } sl@4: sl@2: long sl@2: MainWindow::onFutabaReadId(FXObject *sender, FXSelector sel, void *ptr) sl@2: { sl@2: //1BH,5BH,63H,49H,44H sl@2: memset(iOutputReportBuffer, 0x0, KFutabaOutputReportLength); sl@2: iOutputReportBuffer[0]=0x00; //Report ID sl@2: iOutputReportBuffer[1]=0x05; //Report length sl@2: iOutputReportBuffer[2]=0x1B; // sl@2: iOutputReportBuffer[3]=0x5B; // sl@2: iOutputReportBuffer[4]=0x63; // sl@2: iOutputReportBuffer[5]=0x49; // sl@2: iOutputReportBuffer[6]=0x44; // sl@2: int res = hid_write(connected_device, iOutputReportBuffer, KFutabaOutputReportLength); sl@2: sl@2: return 1; sl@2: } sl@2: sl@2: long sl@2: MainWindow::onFutabaReadFirmwareRevision(FXObject *sender, FXSelector sel, void *ptr) sl@2: { sl@2: //1BH,5BH,63H,46H,52H sl@2: memset(iOutputReportBuffer, 0x0, KFutabaOutputReportLength); sl@2: iOutputReportBuffer[0]=0x00; //Report ID sl@2: iOutputReportBuffer[1]=0x05; //Report length sl@2: iOutputReportBuffer[2]=0x1B; // sl@2: iOutputReportBuffer[3]=0x5B; // sl@2: iOutputReportBuffer[4]=0x63; // sl@2: iOutputReportBuffer[5]=0x46; // sl@2: iOutputReportBuffer[6]=0x52; // sl@2: int res = hid_write(connected_device, iOutputReportBuffer, KFutabaOutputReportLength); sl@2: sl@2: return 1; sl@2: } sl@2: sl@2: long sl@2: MainWindow::onFutabaPowerSupplyMonitor(FXObject *sender, FXSelector sel, void *ptr) sl@2: { sl@2: //1BH,5BH,63H,50H,4DH sl@2: memset(iOutputReportBuffer, 0x0, KFutabaOutputReportLength); sl@2: iOutputReportBuffer[0]=0x00; //Report ID sl@2: iOutputReportBuffer[1]=0x05; //Report length sl@2: iOutputReportBuffer[2]=0x1B; // sl@2: iOutputReportBuffer[3]=0x5B; // sl@2: iOutputReportBuffer[4]=0x63; // sl@2: iOutputReportBuffer[5]=0x50; // sl@2: iOutputReportBuffer[6]=0x4D; // sl@2: int res = hid_write(connected_device, iOutputReportBuffer, KFutabaOutputReportLength); sl@2: sl@2: return 1; sl@2: } sl@2: sl@2: sl@2: sl@0: long sl@0: MainWindow::onTimeout(FXObject *sender, FXSelector sel, void *ptr) sl@0: { sl@0: unsigned char buf[256]; sl@0: int res = hid_read(connected_device, buf, sizeof(buf)); sl@3: sl@0: if (res > 0) { sl@0: FXString s; sl@0: s.format("Received %d bytes:\n", res); sl@0: for (int i = 0; i < res; i++) { sl@0: FXString t; sl@0: t.format("%02hhx ", buf[i]); sl@0: s += t; sl@0: if ((i+1) % 4 == 0) sl@0: s += " "; sl@0: if ((i+1) % 16 == 0) sl@0: s += "\n"; sl@0: } sl@0: s += "\n"; sl@0: input_text->appendText(s); sl@0: input_text->setBottomLine(INT_MAX); sl@0: } sl@0: if (res < 0) { sl@0: input_text->appendText("hid_read() returned error\n"); sl@0: input_text->setBottomLine(INT_MAX); sl@0: } sl@0: sl@0: getApp()->addTimeout(this, ID_TIMER, sl@0: 5 * timeout_scalar /*5ms*/); sl@0: return 1; sl@0: } sl@0: sl@0: long sl@0: MainWindow::onMacTimeout(FXObject *sender, FXSelector sel, void *ptr) sl@0: { sl@0: #ifdef __APPLE__ sl@0: check_apple_events(); sl@3: sl@0: getApp()->addTimeout(this, ID_MAC_TIMER, sl@0: 50 * timeout_scalar /*50ms*/); sl@0: #endif sl@0: sl@0: return 1; sl@0: } sl@0: sl@6: /** sl@6: sl@6: */ sl@6: long MainWindow::onSelectFont(FXObject *sender, FXSelector sel, void *ptr) sl@0: { sl@6: FXFontDialog* dlg=new FXFontDialog(this,"Pick a font"); sl@6: dlg->execute(); sl@6: dlg->getFontSelection(iCurrentFontDesc); sl@6: delete dlg; sl@6: return 1; sl@0: } sl@6: sl@6: sl@6: