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@15: FXMAPFUNC(SEL_COMMAND, MainWindow::ID_RESCAN, MainWindow::onScan ), 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@22: FXMAPFUNC(SEL_COMMAND, MainWindow::ID_FUTABA_SET_DISPLAY_POSITION, MainWindow::onFutabaSetDisplayPosition ), sl@23: FXMAPFUNC(SEL_COMMAND, MainWindow::ID_FUTABA_TOGGLE_OFF_SCREEN_MODE, MainWindow::onFutabaToggleOffScreenMode ), 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@7: : FXMainWindow(app, "HIDAPI Test Application", NULL, NULL, DECOR_ALL, 200,100, 600,900), sl@8: iCurrentFont(NULL), sl@24: iFontImage(NULL), sl@24: iPixelBuffer(NULL) sl@0: { sl@18: iBrightness=iVfd01.MaxBrightness(); 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@17: rescan_button = new FXButton(buttonVF, "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@22: iButtonBrightness = new FXButton(matrix, "Brightness", 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@22: iButtonSetDisplayPosition = new FXButton(matrix, "Set Display Position", NULL, this, ID_FUTABA_SET_DISPLAY_POSITION, BUTTON_NORMAL|LAYOUT_FILL_X); sl@23: iButtonToggleOffScreenMode = new FXButton(matrix, "Switch to on-screen", NULL, this, ID_FUTABA_TOGGLE_OFF_SCREEN_MODE, BUTTON_NORMAL|LAYOUT_FILL_X); sl@6: // sl@6: 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@15: SetConnectedStates(); sl@0: sl@0: } sl@0: sl@0: MainWindow::~MainWindow() sl@0: { sl@7: delete iCurrentFont; sl@7: iCurrentFont = NULL; sl@7: sl@8: delete iFontImage; sl@8: iFontImage = NULL; sl@8: sl@24: delete[] iPixelBuffer; sl@24: iPixelBuffer = NULL; sl@24: 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@15: onScan(NULL, 0, NULL); sl@3: sl@15: //Try to connect to our VFD from start-up sl@13: if (iVfd01.Open()) sl@13: { sl@22: //iVfd01.SetAllPixels(true); sl@22: //iVfd01.Close(); sl@15: SetConnectedStates(); sl@13: } 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@2: // sl@15: iOutputReportBuffer=new unsigned char[KFutabaMaxHidReportSize]; sl@0: sl@17: SetConnectedStates(); sl@17: sl@0: return 1; sl@0: } sl@0: sl@15: /** sl@15: Tell whether or not we are currently connected to an HID device. sl@15: */ sl@15: bool MainWindow::IsConnected() sl@15: { sl@15: return (connected_device||iVfd01.IsOpen()); sl@15: } sl@15: sl@15: /** sl@15: Update our UI states depending on whether or not we are connected to a device sl@15: */ sl@15: void MainWindow::SetConnectedStates() sl@15: { sl@15: if (IsConnected()) sl@15: { sl@15: //Start pulling for input report sl@15: getApp()->addTimeout(this, ID_TIMER, 5 * timeout_scalar /*5ms*/); sl@15: sl@15: FXString s; sl@15: //s.format("Connected to: %04hx:%04hx -", device_info->vendor_id, device_info->product_id); sl@15: s.format("Connected to: "); sl@15: if (connected_device) sl@15: { sl@15: wchar_t string[256]; sl@15: hid_get_manufacturer_string(connected_device,string,sizeof(string)); sl@15: s += FXString(" ") + string; sl@15: hid_get_product_string(connected_device,string,sizeof(string)); sl@15: s += FXString(" ") + string; sl@15: } sl@15: else sl@15: { sl@22: s += iVfd01.Vendor(); sl@22: s += iVfd01.Product(); sl@22: s += iVfd01.SerialNumber(); sl@15: } sl@15: connected_label->setText(s); sl@15: output_button->enable(); sl@15: feature_button->enable(); sl@15: get_feature_button->enable(); sl@15: connect_button->disable(); sl@15: disconnect_button->enable(); sl@15: input_text->setText(""); sl@15: // sl@17: if (iVfd01.IsOpen()) sl@17: { sl@17: //Those widgets are specific to our VFD sl@17: iButtonClearDisplay->enable(); sl@22: iButtonBrightness->enable(); sl@17: iButtonDisplayDataInput->enable(); sl@17: iButtonReadId->enable(); sl@17: iButtonReadFirmwareRevision->enable(); sl@17: iButtonPowerSupplyMonitor->enable(); sl@17: iTextFieldX->enable(); sl@17: iTextFieldY->enable(); sl@17: iButtonSetPixel->enable(); sl@17: iButtonResetPixel->enable(); sl@17: iButtonSetAllPixels->enable(); sl@21: iButtonSetDisplayPosition->enable(); sl@23: iButtonToggleOffScreenMode->enable(); sl@17: } sl@15: } sl@15: else sl@15: { sl@15: //Stop pulling for input report sl@15: getApp()->removeTimeout(this, ID_TIMER); sl@15: // sl@15: connected_label->setText("Disconnected"); sl@15: output_button->disable(); sl@15: feature_button->disable(); sl@15: get_feature_button->disable(); sl@15: connect_button->enable(); sl@15: disconnect_button->disable(); sl@17: sl@15: iButtonClearDisplay->disable(); sl@22: iButtonBrightness->disable(); sl@15: iButtonDisplayDataInput->disable(); sl@15: iButtonReadId->disable(); sl@15: iButtonReadFirmwareRevision->disable(); sl@15: iButtonPowerSupplyMonitor->disable(); sl@15: iTextFieldX->disable(); sl@15: iTextFieldY->disable(); sl@15: iButtonSetPixel->disable(); sl@15: iButtonResetPixel->disable(); sl@15: iButtonSetAllPixels->disable(); sl@21: iButtonSetDisplayPosition->disable(); sl@23: iButtonToggleOffScreenMode->disable(); sl@15: // sl@15: } sl@15: sl@15: } sl@15: sl@0: long sl@0: MainWindow::onDisconnect(FXObject *sender, FXSelector sel, void *ptr) sl@0: { sl@17: //Close all our devices. sl@17: //Closing devices which are not open won't harm. sl@0: hid_close(connected_device); sl@0: connected_device = NULL; sl@15: iVfd01.Close(); sl@2: sl@2: delete iOutputReportBuffer; sl@2: iOutputReportBuffer=NULL; sl@3: sl@15: SetConnectedStates(); sl@15: sl@0: return 1; sl@0: } sl@0: sl@0: long sl@15: MainWindow::onScan(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@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@17: //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@18: iVfd01.Clear(); sl@23: iVfd01.SwapBuffers(); sl@2: return 1; sl@2: } sl@2: sl@2: long sl@2: MainWindow::onFutabaDimming(FXObject *sender, FXSelector sel, void *ptr) sl@2: { sl@18: iBrightness = (iBrightness==iVfd01.MaxBrightness()?iVfd01.MinBrightness():++iBrightness); sl@18: iVfd01.SetBrightness(iBrightness); 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@10: memset(iOutputReportBuffer, 0x0, KFutabaMaxHidReportSize); 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@10: int res = hid_write(connected_device, iOutputReportBuffer, KFutabaMaxHidReportSize); sl@2: sl@3: sl@2: return 1; sl@2: } sl@2: 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@18: //SetPixel(x,y,0x01); sl@18: iVfd01.SetPixel(x,y,true); sl@23: iVfd01.SwapBuffers(); 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@18: //SetPixel(x,y,0x00); sl@18: iVfd01.SetPixel(x,y,false); sl@23: iVfd01.SwapBuffers(); sl@4: return 1; sl@4: } sl@4: sl@4: long MainWindow::onFutabaSetAllPixels(FXObject *sender, FXSelector sel, void *ptr) sl@4: { sl@23: iVfd01.SetAllPixels(0xFF); sl@23: iVfd01.SwapBuffers(); sl@4: return 1; sl@4: } sl@4: sl@21: sl@21: /** sl@21: sl@21: */ sl@21: long MainWindow::onFutabaSetDisplayPosition(FXObject *sender, FXSelector sel, void *ptr) sl@21: { sl@21: int x=0; sl@21: int y=0; sl@21: iTextFieldX->getText().scan("%d",&x); sl@21: iTextFieldY->getText().scan("%d",&y); sl@22: iVfd01.SetDisplayPosition(x,y); sl@21: return 1; sl@21: } sl@21: sl@21: sl@23: /** sl@23: sl@23: */ sl@23: long MainWindow::onFutabaToggleOffScreenMode(FXObject *sender, FXSelector sel, void *ptr) sl@23: { sl@23: iVfd01.ToggleOffScreenMode(); sl@23: if (iVfd01.OffScreenMode()) sl@23: { sl@23: iButtonToggleOffScreenMode->setText("Switch to on-screen"); sl@23: } sl@23: else sl@23: { sl@23: iButtonToggleOffScreenMode->setText("Switch to off-screen"); sl@23: } sl@23: return 1; sl@23: } sl@23: sl@23: sl@23: sl@23: sl@2: long sl@2: MainWindow::onFutabaReadId(FXObject *sender, FXSelector sel, void *ptr) sl@2: { sl@19: iVfd01.RequestId(); sl@2: return 1; sl@2: } sl@2: sl@2: long sl@2: MainWindow::onFutabaReadFirmwareRevision(FXObject *sender, FXSelector sel, void *ptr) sl@2: { sl@19: iVfd01.RequestFirmwareRevision(); sl@2: return 1; sl@2: } sl@2: sl@2: long sl@2: MainWindow::onFutabaPowerSupplyMonitor(FXObject *sender, FXSelector sel, void *ptr) sl@2: { sl@19: iVfd01.RequestPowerSupplyStatus(); 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@17: FutabaVfdReport report; sl@17: unsigned char buffer[256]; sl@17: unsigned char* buf=NULL; sl@17: int res = 0; sl@17: sl@17: //Use either our display or our generic device depending which one is connected sl@17: if (connected_device) sl@17: { sl@17: res = hid_read(connected_device, buffer, sizeof(buffer)); sl@17: buf=buffer; sl@17: } sl@17: else sl@17: { sl@17: res=iVfd01.Read(report); sl@17: buf=report.Buffer(); sl@17: } 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@17: //Keep on reading 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@7: { sl@6: FXFontDialog* dlg=new FXFontDialog(this,"Pick a font"); sl@7: if (dlg->execute()) sl@7: { sl@7: dlg->getFontSelection(iCurrentFontDesc); sl@7: delete iCurrentFont; sl@7: iCurrentFont = NULL; sl@7: iCurrentFont = new FXFont(getApp(),iCurrentFontDesc); sl@8: iCurrentFont->create(); sl@8: // sl@8: delete iFontImage; sl@8: iFontImage = NULL; sl@8: // sl@24: delete[] iPixelBuffer; sl@24: iPixelBuffer=NULL; sl@24: sl@24: //FXString text="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-[]{}();%$£&~#|_"; sl@24: FXString text="0123456789ABCDEF"; sl@8: //Create an image the proper size for our text sl@24: iFontImage = new FXTGAImage(getApp(),NULL,IMAGE_KEEP/*IMAGE_SHMI|IMAGE_SHMP*/,iCurrentFont->getTextWidth(text),iCurrentFont->getFontHeight()); sl@8: iFontImage->create(); sl@8: //Perform our drawing sl@8: { sl@8: FXDCWindow dc(iFontImage); sl@8: //dc.begin(iFontImage); sl@8: dc.setFont(iCurrentFont); sl@8: dc.setForeground(0xFFFFFFFF); sl@8: //dc.setBackground(0xFF000000); sl@8: //dc.setFillStyle(FILL_SOLID); sl@8: dc.fillRectangle(0,0,iFontImage->getWidth(),iFontImage->getHeight()); sl@8: dc.setForeground(0xFF000000); sl@8: dc.drawText(0,iCurrentFont->getFontAscent(),text); sl@8: //dc.end(); sl@8: } sl@24: sl@24: //Save to file sl@8: FXFileStream file; sl@8: file.open("fonttest.tga",FXStreamSave); sl@8: iFontImage->restore(); sl@8: iFontImage->savePixels(file); sl@8: file.close(); sl@8: sl@24: //Create display pixel buffer from our image pixels sl@24: int w=iFontImage->getWidth(); sl@24: int h=iFontImage->getHeight(); sl@25: int pixelCount=(w*h); sl@25: iPixelBuffer = new BitArray(pixelCount); sl@24: for (int i=0;igetPixel(i,j); sl@24: if (color!=0xffffffff) sl@24: { sl@25: iPixelBuffer->SetBit(i*h+j); sl@24: } sl@24: } sl@24: } sl@24: sl@24: if (iVfd01.IsOpen()) sl@24: { sl@25: iVfd01.BitBlit(*iPixelBuffer,w,h,0,0); sl@24: iVfd01.SwapBuffers(); sl@24: } sl@7: sl@7: } sl@7: sl@6: delete dlg; sl@6: return 1; sl@7: } sl@6: sl@6: sl@6: