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