src/test.cpp
author sl
Sat, 24 May 2014 00:43:18 +0200
changeset 25 233a997193b8
parent 24 c6b5c552980a
child 26 df50d7cb4dd0
permissions -rw-r--r--
Now using host frame buffer and sending the whole frame upon swap buffers.
BitArray saved the day to implement proper BitBlit.
Any font now drawing nicely.
sl@0
     1
/*******************************************************
sl@0
     2
 Demo Program for HIDAPI
sl@3
     3
sl@0
     4
 Alan Ott
sl@0
     5
 Signal 11 Software
sl@0
     6
sl@0
     7
 2010-07-20
sl@0
     8
sl@0
     9
 Copyright 2010, All Rights Reserved
sl@3
    10
sl@0
    11
 This contents of this file may be used by anyone
sl@0
    12
 for any reason without any conditions and may be
sl@0
    13
 used as a starting point for your own applications
sl@0
    14
 which use HIDAPI.
sl@0
    15
********************************************************/
sl@0
    16
sl@6
    17
#include "MainWindow.h"
sl@0
    18
sl@0
    19
sl@0
    20
// FOX 1.7 changes the timeouts to all be nanoseconds.
sl@0
    21
// Fox 1.6 had all timeouts as milliseconds.
sl@0
    22
#if (FOX_MINOR >= 7)
sl@0
    23
	const int timeout_scalar = 1000*1000;
sl@0
    24
#else
sl@0
    25
	const int timeout_scalar = 1;
sl@0
    26
#endif
sl@0
    27
sl@0
    28
sl@0
    29
FXDEFMAP(MainWindow) MainWindowMap [] = {
sl@0
    30
	FXMAPFUNC(SEL_COMMAND, MainWindow::ID_CONNECT, MainWindow::onConnect ),
sl@0
    31
	FXMAPFUNC(SEL_COMMAND, MainWindow::ID_DISCONNECT, MainWindow::onDisconnect ),
sl@15
    32
	FXMAPFUNC(SEL_COMMAND, MainWindow::ID_RESCAN, MainWindow::onScan ),
sl@0
    33
	FXMAPFUNC(SEL_COMMAND, MainWindow::ID_SEND_OUTPUT_REPORT, MainWindow::onSendOutputReport ),
sl@0
    34
	FXMAPFUNC(SEL_COMMAND, MainWindow::ID_SEND_FEATURE_REPORT, MainWindow::onSendFeatureReport ),
sl@0
    35
	FXMAPFUNC(SEL_COMMAND, MainWindow::ID_GET_FEATURE_REPORT, MainWindow::onGetFeatureReport ),
sl@0
    36
	FXMAPFUNC(SEL_COMMAND, MainWindow::ID_CLEAR, MainWindow::onClear ),
sl@2
    37
	FXMAPFUNC(SEL_COMMAND, MainWindow::ID_FUTABA_CLEAR_DISPLAY, MainWindow::onFutabaClearDisplay ),
sl@2
    38
	FXMAPFUNC(SEL_COMMAND, MainWindow::ID_FUTABA_DIMMING, MainWindow::onFutabaDimming ),
sl@2
    39
	FXMAPFUNC(SEL_COMMAND, MainWindow::ID_FUTABA_DISPLAY_DATA_INPUT, MainWindow::onFutabaDisplayDataInput ),
sl@2
    40
	FXMAPFUNC(SEL_COMMAND, MainWindow::ID_FUTABA_READ_ID, MainWindow::onFutabaReadId ),
sl@2
    41
	FXMAPFUNC(SEL_COMMAND, MainWindow::ID_FUTABA_READ_FIRMWARE_REVISION, MainWindow::onFutabaReadFirmwareRevision ),
sl@2
    42
	FXMAPFUNC(SEL_COMMAND, MainWindow::ID_FUTABA_POWER_SUPPLY_MONITOR, MainWindow::onFutabaPowerSupplyMonitor ),
sl@3
    43
    FXMAPFUNC(SEL_COMMAND, MainWindow::ID_FUTABA_SET_PIXEL, MainWindow::onFutabaSetPixel ),
sl@4
    44
	FXMAPFUNC(SEL_COMMAND, MainWindow::ID_FUTABA_RESET_PIXEL, MainWindow::onFutabaResetPixel ),
sl@3
    45
    FXMAPFUNC(SEL_COMMAND, MainWindow::ID_FUTABA_SET_ALL_PIXELS, MainWindow::onFutabaSetAllPixels ),
sl@22
    46
	FXMAPFUNC(SEL_COMMAND, MainWindow::ID_FUTABA_SET_DISPLAY_POSITION, MainWindow::onFutabaSetDisplayPosition ),
sl@23
    47
	FXMAPFUNC(SEL_COMMAND, MainWindow::ID_FUTABA_TOGGLE_OFF_SCREEN_MODE, MainWindow::onFutabaToggleOffScreenMode ),	
sl@6
    48
    FXMAPFUNC(SEL_COMMAND, MainWindow::ID_SELECT_FONT, MainWindow::onSelectFont ),
sl@0
    49
	FXMAPFUNC(SEL_TIMEOUT, MainWindow::ID_TIMER, MainWindow::onTimeout ),
sl@0
    50
	FXMAPFUNC(SEL_TIMEOUT, MainWindow::ID_MAC_TIMER, MainWindow::onMacTimeout ),
sl@0
    51
};
sl@0
    52
sl@0
    53
FXIMPLEMENT(MainWindow, FXMainWindow, MainWindowMap, ARRAYNUMBER(MainWindowMap));
sl@0
    54
sl@0
    55
MainWindow::MainWindow(FXApp *app)
sl@7
    56
	: FXMainWindow(app, "HIDAPI Test Application", NULL, NULL, DECOR_ALL, 200,100, 600,900),
sl@8
    57
    iCurrentFont(NULL),
sl@24
    58
    iFontImage(NULL),
sl@24
    59
	iPixelBuffer(NULL)
sl@0
    60
{
sl@18
    61
	iBrightness=iVfd01.MaxBrightness();
sl@0
    62
	devices = NULL;
sl@0
    63
	connected_device = NULL;
sl@0
    64
sl@0
    65
	FXVerticalFrame *vf = new FXVerticalFrame(this, LAYOUT_FILL_Y|LAYOUT_FILL_X);
sl@0
    66
sl@0
    67
	FXLabel *label = new FXLabel(vf, "HIDAPI Test Tool");
sl@0
    68
	title_font = new FXFont(getApp(), "Arial", 14, FXFont::Bold);
sl@0
    69
	label->setFont(title_font);
sl@3
    70
sl@0
    71
	new FXLabel(vf,
sl@0
    72
		"Select a device and press Connect.", NULL, JUSTIFY_LEFT);
sl@0
    73
	new FXLabel(vf,
sl@0
    74
		"Output data bytes can be entered in the Output section, \n"
sl@0
    75
		"separated by space, comma or brackets. Data starting with 0x\n"
sl@0
    76
		"is treated as hex. Data beginning with a 0 is treated as \n"
sl@0
    77
		"octal. All other data is treated as decimal.", NULL, JUSTIFY_LEFT);
sl@0
    78
	new FXLabel(vf,
sl@0
    79
		"Data received from the device appears in the Input section.",
sl@0
    80
		NULL, JUSTIFY_LEFT);
sl@0
    81
	new FXLabel(vf,
sl@0
    82
		"Optionally, a report length may be specified. Extra bytes are\n"
sl@0
    83
		"padded with zeros. If no length is specified, the length is \n"
sl@0
    84
		"inferred from the data.",
sl@0
    85
		NULL, JUSTIFY_LEFT);
sl@0
    86
	new FXLabel(vf, "");
sl@0
    87
sl@0
    88
	// Device List and Connect/Disconnect buttons
sl@0
    89
	FXHorizontalFrame *hf = new FXHorizontalFrame(vf, LAYOUT_FILL_X);
sl@0
    90
	//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
    91
	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
    92
	FXVerticalFrame *buttonVF = new FXVerticalFrame(hf);
sl@0
    93
	connect_button = new FXButton(buttonVF, "Connect", NULL, this, ID_CONNECT, BUTTON_NORMAL|LAYOUT_FILL_X);
sl@0
    94
	disconnect_button = new FXButton(buttonVF, "Disconnect", NULL, this, ID_DISCONNECT, BUTTON_NORMAL|LAYOUT_FILL_X);
sl@0
    95
	disconnect_button->disable();
sl@17
    96
	rescan_button = new FXButton(buttonVF, "Scan devices", NULL, this, ID_RESCAN, BUTTON_NORMAL|LAYOUT_FILL_X);
sl@0
    97
	new FXHorizontalFrame(buttonVF, 0, 0,0,0,0, 0,0,50,0);
sl@0
    98
sl@0
    99
	connected_label = new FXLabel(vf, "Disconnected");
sl@3
   100
sl@6
   101
    //Font group
sl@6
   102
    new FXHorizontalFrame(vf);
sl@6
   103
    FXGroupBox *gb = new FXGroupBox(vf, "Fonts", FRAME_GROOVE|LAYOUT_FILL_X);
sl@6
   104
    FXMatrix *matrix = new FXMatrix(gb, 3, MATRIX_BY_COLUMNS|LAYOUT_FILL_X);
sl@6
   105
    iButtonSelectFont = new FXButton(matrix, "Select Font", NULL, this, ID_SELECT_FONT, BUTTON_NORMAL|LAYOUT_FILL_X);
sl@6
   106
sl@2
   107
	//Futaba VFD commands
sl@0
   108
	new FXHorizontalFrame(vf);
sl@6
   109
	gb = new FXGroupBox(vf, "Futaba GP1212A01A", FRAME_GROOVE|LAYOUT_FILL_X);
sl@6
   110
	matrix = new FXMatrix(gb, 3, MATRIX_BY_COLUMNS|LAYOUT_FILL_X);
sl@2
   111
	iButtonClearDisplay = new FXButton(matrix, "Clear Display", NULL, this, ID_FUTABA_CLEAR_DISPLAY, BUTTON_NORMAL|LAYOUT_FILL_X);
sl@22
   112
	iButtonBrightness = new FXButton(matrix, "Brightness", NULL, this, ID_FUTABA_DIMMING, BUTTON_NORMAL|LAYOUT_FILL_X);
sl@2
   113
	iButtonDisplayDataInput = new FXButton(matrix, "Display Data Input", NULL, this, ID_FUTABA_DISPLAY_DATA_INPUT, BUTTON_NORMAL|LAYOUT_FILL_X);
sl@2
   114
	iButtonReadId = new FXButton(matrix, "Read Id", NULL, this, ID_FUTABA_READ_ID, BUTTON_NORMAL|LAYOUT_FILL_X);
sl@2
   115
	iButtonReadFirmwareRevision =  new FXButton(matrix, "Read Firmware Revision", NULL, this, ID_FUTABA_READ_FIRMWARE_REVISION, BUTTON_NORMAL|LAYOUT_FILL_X);
sl@2
   116
	iButtonPowerSupplyMonitor = new FXButton(matrix, "Power Supply Monitor", NULL, this, ID_FUTABA_POWER_SUPPLY_MONITOR, BUTTON_NORMAL|LAYOUT_FILL_X);
sl@3
   117
    new FXLabel(matrix, "X",NULL,LABEL_NORMAL|LAYOUT_FILL_X);
sl@3
   118
    new FXLabel(matrix, "Y",NULL,LABEL_NORMAL|LAYOUT_FILL_X);
sl@3
   119
    new FXLabel(matrix, "",NULL,LABEL_NORMAL|LAYOUT_FILL_X);
sl@3
   120
    iTextFieldX = new FXTextField(matrix, 3, NULL, 0, TEXTFIELD_NORMAL|TEXTFIELD_INTEGER|LAYOUT_FILL_X);
sl@3
   121
    iTextFieldY = new FXTextField(matrix, 3, NULL, 0, TEXTFIELD_NORMAL|TEXTFIELD_INTEGER|LAYOUT_FILL_X);
sl@4
   122
    iButtonSetPixel = new FXButton(matrix, "Set Pixel", NULL, this, ID_FUTABA_SET_PIXEL, BUTTON_NORMAL|LAYOUT_FILL_X);
sl@4
   123
	iButtonResetPixel = new FXButton(matrix, "Reset Pixel", NULL, this, ID_FUTABA_RESET_PIXEL, BUTTON_NORMAL|LAYOUT_FILL_X);
sl@3
   124
    //
sl@3
   125
    iButtonSetAllPixels = new FXButton(matrix, "Set All Pixels", NULL, this, ID_FUTABA_SET_ALL_PIXELS, BUTTON_NORMAL|LAYOUT_FILL_X);
sl@22
   126
    iButtonSetDisplayPosition = new FXButton(matrix, "Set Display Position", NULL, this, ID_FUTABA_SET_DISPLAY_POSITION, BUTTON_NORMAL|LAYOUT_FILL_X);
sl@23
   127
	iButtonToggleOffScreenMode = new FXButton(matrix, "Switch to on-screen", NULL, this, ID_FUTABA_TOGGLE_OFF_SCREEN_MODE, BUTTON_NORMAL|LAYOUT_FILL_X);
sl@6
   128
    //
sl@6
   129
sl@0
   130
	// Output Group Box
sl@3
   131
	new FXHorizontalFrame(vf);
sl@2
   132
	gb = new FXGroupBox(vf, "Output", FRAME_GROOVE|LAYOUT_FILL_X);
sl@2
   133
	matrix = new FXMatrix(gb, 3, MATRIX_BY_COLUMNS|LAYOUT_FILL_X);
sl@0
   134
	new FXLabel(matrix, "Data");
sl@0
   135
	new FXLabel(matrix, "Length");
sl@0
   136
	new FXLabel(matrix, "");
sl@0
   137
sl@0
   138
	//hf = new FXHorizontalFrame(gb, LAYOUT_FILL_X);
sl@0
   139
	output_text = new FXTextField(matrix, 30, NULL, 0, TEXTFIELD_NORMAL|LAYOUT_FILL_X|LAYOUT_FILL_COLUMN);
sl@0
   140
	output_text->setText("1 0x81 0");
sl@0
   141
	output_len = new FXTextField(matrix, 5, NULL, 0, TEXTFIELD_NORMAL|LAYOUT_FILL_X|LAYOUT_FILL_COLUMN);
sl@0
   142
	output_button = new FXButton(matrix, "Send Output Report", NULL, this, ID_SEND_OUTPUT_REPORT, BUTTON_NORMAL|LAYOUT_FILL_X);
sl@0
   143
	output_button->disable();
sl@0
   144
	//new FXHorizontalFrame(matrix, LAYOUT_FILL_X);
sl@0
   145
sl@0
   146
	//hf = new FXHorizontalFrame(gb, LAYOUT_FILL_X);
sl@0
   147
	feature_text = new FXTextField(matrix, 30, NULL, 0, TEXTFIELD_NORMAL|LAYOUT_FILL_X|LAYOUT_FILL_COLUMN);
sl@0
   148
	feature_len = new FXTextField(matrix, 5, NULL, 0, TEXTFIELD_NORMAL|LAYOUT_FILL_X|LAYOUT_FILL_COLUMN);
sl@0
   149
	feature_button = new FXButton(matrix, "Send Feature Report", NULL, this, ID_SEND_FEATURE_REPORT, BUTTON_NORMAL|LAYOUT_FILL_X);
sl@0
   150
	feature_button->disable();
sl@0
   151
sl@0
   152
	get_feature_text = new FXTextField(matrix, 30, NULL, 0, TEXTFIELD_NORMAL|LAYOUT_FILL_X|LAYOUT_FILL_COLUMN);
sl@0
   153
	new FXWindow(matrix);
sl@0
   154
	get_feature_button = new FXButton(matrix, "Get Feature Report", NULL, this, ID_GET_FEATURE_REPORT, BUTTON_NORMAL|LAYOUT_FILL_X);
sl@0
   155
	get_feature_button->disable();
sl@0
   156
sl@0
   157
sl@0
   158
	// Input Group Box
sl@0
   159
	gb = new FXGroupBox(vf, "Input", FRAME_GROOVE|LAYOUT_FILL_X|LAYOUT_FILL_Y);
sl@0
   160
	FXVerticalFrame *innerVF = new FXVerticalFrame(gb, LAYOUT_FILL_X|LAYOUT_FILL_Y);
sl@0
   161
	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
   162
	input_text->setEditable(false);
sl@0
   163
	new FXButton(innerVF, "Clear", NULL, this, ID_CLEAR, BUTTON_NORMAL|LAYOUT_RIGHT);
sl@3
   164
sl@15
   165
    SetConnectedStates();
sl@0
   166
sl@0
   167
}
sl@0
   168
sl@0
   169
MainWindow::~MainWindow()
sl@0
   170
{
sl@7
   171
    delete iCurrentFont;
sl@7
   172
    iCurrentFont = NULL;
sl@7
   173
sl@8
   174
    delete iFontImage;
sl@8
   175
    iFontImage = NULL;
sl@8
   176
sl@24
   177
	delete[] iPixelBuffer;
sl@24
   178
	iPixelBuffer = NULL;
sl@24
   179
sl@0
   180
	if (connected_device)
sl@0
   181
		hid_close(connected_device);
sl@0
   182
	hid_exit();
sl@0
   183
	delete title_font;
sl@0
   184
}
sl@0
   185
sl@0
   186
void
sl@0
   187
MainWindow::create()
sl@0
   188
{
sl@0
   189
	FXMainWindow::create();
sl@0
   190
	show();
sl@0
   191
sl@15
   192
	onScan(NULL, 0, NULL);
sl@3
   193
sl@15
   194
	//Try to connect to our VFD from start-up
sl@13
   195
	if (iVfd01.Open())
sl@13
   196
        {
sl@22
   197
	    //iVfd01.SetAllPixels(true);
sl@22
   198
	    //iVfd01.Close();
sl@15
   199
        SetConnectedStates();
sl@13
   200
        }
sl@0
   201
sl@0
   202
#ifdef __APPLE__
sl@0
   203
	init_apple_message_system();
sl@0
   204
#endif
sl@3
   205
sl@0
   206
	getApp()->addTimeout(this, ID_MAC_TIMER,
sl@0
   207
		50 * timeout_scalar /*50ms*/);
sl@0
   208
}
sl@0
   209
sl@0
   210
long
sl@0
   211
MainWindow::onConnect(FXObject *sender, FXSelector sel, void *ptr)
sl@0
   212
{
sl@0
   213
	if (connected_device != NULL)
sl@0
   214
		return 1;
sl@3
   215
sl@0
   216
	FXint cur_item = device_list->getCurrentItem();
sl@0
   217
	if (cur_item < 0)
sl@0
   218
		return -1;
sl@0
   219
	FXListItem *item = device_list->getItem(cur_item);
sl@0
   220
	if (!item)
sl@0
   221
		return -1;
sl@0
   222
	struct hid_device_info *device_info = (struct hid_device_info*) item->getData();
sl@0
   223
	if (!device_info)
sl@0
   224
		return -1;
sl@3
   225
sl@0
   226
	connected_device =  hid_open_path(device_info->path);
sl@3
   227
sl@0
   228
	if (!connected_device) {
sl@0
   229
		FXMessageBox::error(this, MBOX_OK, "Device Error", "Unable To Connect to Device");
sl@0
   230
		return -1;
sl@0
   231
	}
sl@3
   232
sl@0
   233
	hid_set_nonblocking(connected_device, 1);
sl@0
   234
sl@2
   235
	//
sl@15
   236
	iOutputReportBuffer=new unsigned char[KFutabaMaxHidReportSize];
sl@0
   237
sl@17
   238
    SetConnectedStates();
sl@17
   239
sl@0
   240
	return 1;
sl@0
   241
}
sl@0
   242
sl@15
   243
/**
sl@15
   244
Tell whether or not we are currently connected to an HID device.
sl@15
   245
*/
sl@15
   246
bool MainWindow::IsConnected()
sl@15
   247
    {
sl@15
   248
    return (connected_device||iVfd01.IsOpen());
sl@15
   249
    }
sl@15
   250
sl@15
   251
/**
sl@15
   252
Update our UI states depending on whether or not we are connected to a device
sl@15
   253
*/
sl@15
   254
void MainWindow::SetConnectedStates()
sl@15
   255
    {
sl@15
   256
    if (IsConnected())
sl@15
   257
        {
sl@15
   258
        //Start pulling for input report
sl@15
   259
        getApp()->addTimeout(this, ID_TIMER, 5 * timeout_scalar /*5ms*/);
sl@15
   260
sl@15
   261
        FXString s;
sl@15
   262
        //s.format("Connected to: %04hx:%04hx -", device_info->vendor_id, device_info->product_id);
sl@15
   263
        s.format("Connected to: ");
sl@15
   264
        if (connected_device)
sl@15
   265
            {
sl@15
   266
            wchar_t string[256];
sl@15
   267
            hid_get_manufacturer_string(connected_device,string,sizeof(string));
sl@15
   268
            s += FXString(" ") + string;
sl@15
   269
            hid_get_product_string(connected_device,string,sizeof(string));
sl@15
   270
            s += FXString(" ") + string;
sl@15
   271
            }
sl@15
   272
        else
sl@15
   273
            {
sl@22
   274
            s += iVfd01.Vendor();
sl@22
   275
			s += iVfd01.Product();
sl@22
   276
			s += iVfd01.SerialNumber();
sl@15
   277
            }
sl@15
   278
        connected_label->setText(s);
sl@15
   279
        output_button->enable();
sl@15
   280
        feature_button->enable();
sl@15
   281
        get_feature_button->enable();
sl@15
   282
        connect_button->disable();
sl@15
   283
        disconnect_button->enable();
sl@15
   284
        input_text->setText("");
sl@15
   285
        //
sl@17
   286
        if (iVfd01.IsOpen())
sl@17
   287
            {
sl@17
   288
            //Those widgets are specific to our VFD
sl@17
   289
            iButtonClearDisplay->enable();
sl@22
   290
            iButtonBrightness->enable();
sl@17
   291
            iButtonDisplayDataInput->enable();
sl@17
   292
            iButtonReadId->enable();
sl@17
   293
            iButtonReadFirmwareRevision->enable();
sl@17
   294
            iButtonPowerSupplyMonitor->enable();
sl@17
   295
            iTextFieldX->enable();
sl@17
   296
            iTextFieldY->enable();
sl@17
   297
            iButtonSetPixel->enable();
sl@17
   298
            iButtonResetPixel->enable();
sl@17
   299
            iButtonSetAllPixels->enable();
sl@21
   300
            iButtonSetDisplayPosition->enable();
sl@23
   301
			iButtonToggleOffScreenMode->enable();
sl@17
   302
            }
sl@15
   303
        }
sl@15
   304
    else
sl@15
   305
        {
sl@15
   306
        //Stop pulling for input report
sl@15
   307
        getApp()->removeTimeout(this, ID_TIMER);
sl@15
   308
        //
sl@15
   309
        connected_label->setText("Disconnected");
sl@15
   310
        output_button->disable();
sl@15
   311
        feature_button->disable();
sl@15
   312
        get_feature_button->disable();
sl@15
   313
        connect_button->enable();
sl@15
   314
        disconnect_button->disable();
sl@17
   315
sl@15
   316
        iButtonClearDisplay->disable();
sl@22
   317
        iButtonBrightness->disable();
sl@15
   318
        iButtonDisplayDataInput->disable();
sl@15
   319
        iButtonReadId->disable();
sl@15
   320
        iButtonReadFirmwareRevision->disable();
sl@15
   321
        iButtonPowerSupplyMonitor->disable();
sl@15
   322
        iTextFieldX->disable();
sl@15
   323
        iTextFieldY->disable();
sl@15
   324
        iButtonSetPixel->disable();
sl@15
   325
        iButtonResetPixel->disable();
sl@15
   326
        iButtonSetAllPixels->disable();
sl@21
   327
        iButtonSetDisplayPosition->disable();
sl@23
   328
		iButtonToggleOffScreenMode->disable();
sl@15
   329
        //
sl@15
   330
        }
sl@15
   331
sl@15
   332
    }
sl@15
   333
sl@0
   334
long
sl@0
   335
MainWindow::onDisconnect(FXObject *sender, FXSelector sel, void *ptr)
sl@0
   336
{
sl@17
   337
    //Close all our devices.
sl@17
   338
    //Closing devices which are not open won't harm.
sl@0
   339
	hid_close(connected_device);
sl@0
   340
	connected_device = NULL;
sl@15
   341
    iVfd01.Close();
sl@2
   342
sl@2
   343
	delete iOutputReportBuffer;
sl@2
   344
	iOutputReportBuffer=NULL;
sl@3
   345
sl@15
   346
    SetConnectedStates();
sl@15
   347
sl@0
   348
	return 1;
sl@0
   349
}
sl@0
   350
sl@0
   351
long
sl@15
   352
MainWindow::onScan(FXObject *sender, FXSelector sel, void *ptr)
sl@0
   353
{
sl@0
   354
	struct hid_device_info *cur_dev;
sl@0
   355
sl@0
   356
	device_list->clearItems();
sl@3
   357
sl@0
   358
	// List the Devices
sl@0
   359
	hid_free_enumeration(devices);
sl@0
   360
	devices = hid_enumerate(0x0, 0x0);
sl@3
   361
	cur_dev = devices;
sl@0
   362
	while (cur_dev) {
sl@2
   363
		// Add it to the List Box only if it is a Futaba device
sl@17
   364
		//if (cur_dev->vendor_id == KFutabaVendorId)
sl@2
   365
			{
sl@2
   366
			FXString s;
sl@2
   367
			FXString usage_str;
sl@2
   368
			s.format("%04hx:%04hx -", cur_dev->vendor_id, cur_dev->product_id);
sl@2
   369
			s += FXString(" ") + cur_dev->manufacturer_string;
sl@2
   370
			s += FXString(" ") + cur_dev->product_string;
sl@2
   371
			usage_str.format(" (usage: %04hx:%04hx) ", cur_dev->usage_page, cur_dev->usage);
sl@2
   372
			s += usage_str;
sl@2
   373
			FXListItem *li = new FXListItem(s, NULL, cur_dev);
sl@2
   374
			device_list->appendItem(li);
sl@2
   375
			}
sl@3
   376
sl@0
   377
		cur_dev = cur_dev->next;
sl@0
   378
	}
sl@0
   379
sl@0
   380
	if (device_list->getNumItems() == 0)
sl@0
   381
		device_list->appendItem("*** No Devices Connected ***");
sl@0
   382
	else {
sl@0
   383
		device_list->selectItem(0);
sl@0
   384
	}
sl@0
   385
sl@0
   386
	return 1;
sl@0
   387
}
sl@0
   388
sl@0
   389
size_t
sl@0
   390
MainWindow::getDataFromTextField(FXTextField *tf, char *buf, size_t len)
sl@0
   391
{
sl@0
   392
	const char *delim = " ,{}\t\r\n";
sl@0
   393
	FXString data = tf->getText();
sl@0
   394
	const FXchar *d = data.text();
sl@0
   395
	size_t i = 0;
sl@3
   396
sl@0
   397
	// Copy the string from the GUI.
sl@0
   398
	size_t sz = strlen(d);
sl@0
   399
	char *str = (char*) malloc(sz+1);
sl@0
   400
	strcpy(str, d);
sl@3
   401
sl@0
   402
	// For each token in the string, parse and store in buf[].
sl@0
   403
	char *token = strtok(str, delim);
sl@0
   404
	while (token) {
sl@0
   405
		char *endptr;
sl@0
   406
		long int val = strtol(token, &endptr, 0);
sl@0
   407
		buf[i++] = val;
sl@0
   408
		token = strtok(NULL, delim);
sl@0
   409
	}
sl@3
   410
sl@0
   411
	free(str);
sl@0
   412
	return i;
sl@0
   413
}
sl@0
   414
sl@0
   415
/* getLengthFromTextField()
sl@0
   416
   Returns length:
sl@0
   417
	 0: empty text field
sl@0
   418
	>0: valid length
sl@0
   419
	-1: invalid length */
sl@0
   420
int
sl@0
   421
MainWindow::getLengthFromTextField(FXTextField *tf)
sl@0
   422
{
sl@0
   423
	long int len;
sl@0
   424
	FXString str = tf->getText();
sl@0
   425
	size_t sz = str.length();
sl@0
   426
sl@0
   427
	if (sz > 0) {
sl@0
   428
		char *endptr;
sl@0
   429
		len = strtol(str.text(), &endptr, 0);
sl@0
   430
		if (endptr != str.text() && *endptr == '\0') {
sl@0
   431
			if (len <= 0) {
sl@0
   432
				FXMessageBox::error(this, MBOX_OK, "Invalid length", "Enter a length greater than zero.");
sl@0
   433
				return -1;
sl@0
   434
			}
sl@0
   435
			return len;
sl@0
   436
		}
sl@0
   437
		else
sl@0
   438
			return -1;
sl@0
   439
	}
sl@0
   440
sl@0
   441
	return 0;
sl@0
   442
}
sl@0
   443
sl@0
   444
long
sl@0
   445
MainWindow::onSendOutputReport(FXObject *sender, FXSelector sel, void *ptr)
sl@0
   446
{
sl@0
   447
	char buf[256];
sl@0
   448
	size_t data_len, len;
sl@0
   449
	int textfield_len;
sl@0
   450
sl@0
   451
	memset(buf, 0x0, sizeof(buf));
sl@0
   452
	textfield_len = getLengthFromTextField(output_len);
sl@0
   453
	data_len = getDataFromTextField(output_text, buf, sizeof(buf));
sl@0
   454
sl@0
   455
	if (textfield_len < 0) {
sl@0
   456
		FXMessageBox::error(this, MBOX_OK, "Invalid length", "Length field is invalid. Please enter a number in hex, octal, or decimal.");
sl@0
   457
		return 1;
sl@0
   458
	}
sl@0
   459
sl@0
   460
	if (textfield_len > sizeof(buf)) {
sl@0
   461
		FXMessageBox::error(this, MBOX_OK, "Invalid length", "Length field is too long.");
sl@0
   462
		return 1;
sl@0
   463
	}
sl@0
   464
sl@0
   465
	len = (textfield_len)? textfield_len: data_len;
sl@0
   466
sl@0
   467
	int res = hid_write(connected_device, (const unsigned char*)buf, len);
sl@0
   468
	if (res < 0) {
sl@0
   469
		FXMessageBox::error(this, MBOX_OK, "Error Writing", "Could not write to device. Error reported was: %ls", hid_error(connected_device));
sl@0
   470
	}
sl@3
   471
sl@0
   472
	return 1;
sl@0
   473
}
sl@0
   474
sl@0
   475
long
sl@0
   476
MainWindow::onSendFeatureReport(FXObject *sender, FXSelector sel, void *ptr)
sl@0
   477
{
sl@0
   478
	char buf[256];
sl@0
   479
	size_t data_len, len;
sl@0
   480
	int textfield_len;
sl@0
   481
sl@0
   482
	memset(buf, 0x0, sizeof(buf));
sl@0
   483
	textfield_len = getLengthFromTextField(feature_len);
sl@0
   484
	data_len = getDataFromTextField(feature_text, buf, sizeof(buf));
sl@0
   485
sl@0
   486
	if (textfield_len < 0) {
sl@0
   487
		FXMessageBox::error(this, MBOX_OK, "Invalid length", "Length field is invalid. Please enter a number in hex, octal, or decimal.");
sl@0
   488
		return 1;
sl@0
   489
	}
sl@0
   490
sl@0
   491
	if (textfield_len > sizeof(buf)) {
sl@0
   492
		FXMessageBox::error(this, MBOX_OK, "Invalid length", "Length field is too long.");
sl@0
   493
		return 1;
sl@0
   494
	}
sl@0
   495
sl@0
   496
	len = (textfield_len)? textfield_len: data_len;
sl@0
   497
sl@3
   498
	int res = hid_send_feature_report(connected_device, (const unsigned char*)buf, len);
sl@0
   499
	if (res < 0) {
sl@0
   500
		FXMessageBox::error(this, MBOX_OK, "Error Writing", "Could not send feature report to device. Error reported was: %ls", hid_error(connected_device));
sl@0
   501
	}
sl@0
   502
sl@0
   503
	return 1;
sl@0
   504
}
sl@0
   505
sl@0
   506
long
sl@0
   507
MainWindow::onGetFeatureReport(FXObject *sender, FXSelector sel, void *ptr)
sl@0
   508
{
sl@0
   509
	char buf[256];
sl@0
   510
	size_t len;
sl@0
   511
sl@0
   512
	memset(buf, 0x0, sizeof(buf));
sl@0
   513
	len = getDataFromTextField(get_feature_text, buf, sizeof(buf));
sl@0
   514
sl@0
   515
	if (len != 1) {
sl@0
   516
		FXMessageBox::error(this, MBOX_OK, "Too many numbers", "Enter only a single report number in the text field");
sl@0
   517
	}
sl@0
   518
sl@0
   519
	int res = hid_get_feature_report(connected_device, (unsigned char*)buf, sizeof(buf));
sl@0
   520
	if (res < 0) {
sl@0
   521
		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
   522
	}
sl@0
   523
sl@0
   524
	if (res > 0) {
sl@0
   525
		FXString s;
sl@0
   526
		s.format("Returned Feature Report. %d bytes:\n", res);
sl@0
   527
		for (int i = 0; i < res; i++) {
sl@0
   528
			FXString t;
sl@0
   529
			t.format("%02hhx ", buf[i]);
sl@0
   530
			s += t;
sl@0
   531
			if ((i+1) % 4 == 0)
sl@0
   532
				s += " ";
sl@0
   533
			if ((i+1) % 16 == 0)
sl@0
   534
				s += "\n";
sl@0
   535
		}
sl@0
   536
		s += "\n";
sl@0
   537
		input_text->appendText(s);
sl@0
   538
		input_text->setBottomLine(INT_MAX);
sl@0
   539
	}
sl@3
   540
sl@0
   541
	return 1;
sl@0
   542
}
sl@0
   543
sl@0
   544
long
sl@0
   545
MainWindow::onClear(FXObject *sender, FXSelector sel, void *ptr)
sl@0
   546
{
sl@0
   547
	input_text->setText("");
sl@0
   548
	return 1;
sl@0
   549
}
sl@0
   550
sl@2
   551
sl@2
   552
long
sl@2
   553
MainWindow::onFutabaClearDisplay(FXObject *sender, FXSelector sel, void *ptr)
sl@2
   554
{
sl@18
   555
    iVfd01.Clear();
sl@23
   556
	iVfd01.SwapBuffers();
sl@2
   557
	return 1;
sl@2
   558
}
sl@2
   559
sl@2
   560
long
sl@2
   561
MainWindow::onFutabaDimming(FXObject *sender, FXSelector sel, void *ptr)
sl@2
   562
{
sl@18
   563
    iBrightness = (iBrightness==iVfd01.MaxBrightness()?iVfd01.MinBrightness():++iBrightness);
sl@18
   564
    iVfd01.SetBrightness(iBrightness);
sl@2
   565
	return 1;
sl@2
   566
}
sl@2
   567
sl@2
   568
long
sl@2
   569
MainWindow::onFutabaDisplayDataInput(FXObject *sender, FXSelector sel, void *ptr)
sl@2
   570
{
sl@2
   571
	//@1B 5B F0 00 00 07 00 01 FF
sl@2
   572
sl@10
   573
	memset(iOutputReportBuffer, 0x0, KFutabaMaxHidReportSize);
sl@2
   574
	iOutputReportBuffer[0]=0x00; //Report ID
sl@2
   575
	iOutputReportBuffer[1]=0x09; //Report length
sl@2
   576
	iOutputReportBuffer[2]=0x1B; //
sl@2
   577
	iOutputReportBuffer[3]=0x5B; //
sl@2
   578
	iOutputReportBuffer[4]=0xF0; //
sl@4
   579
	iOutputReportBuffer[5]=0x00; //X
sl@4
   580
	iOutputReportBuffer[6]=0x00; //Y
sl@2
   581
	iOutputReportBuffer[7]=0x07; //
sl@2
   582
	iOutputReportBuffer[8]=0x00; //
sl@2
   583
	iOutputReportBuffer[9]=0x01; //
sl@2
   584
	iOutputReportBuffer[10]=0xFF; //
sl@10
   585
	int res = hid_write(connected_device, iOutputReportBuffer, KFutabaMaxHidReportSize);
sl@2
   586
sl@3
   587
sl@2
   588
	return 1;
sl@2
   589
}
sl@2
   590
sl@4
   591
/**
sl@4
   592
*/
sl@4
   593
long MainWindow::onFutabaSetPixel(FXObject *sender, FXSelector sel, void *ptr)
sl@4
   594
{
sl@4
   595
	int x=0;
sl@4
   596
	int y=0;
sl@4
   597
	iTextFieldX->getText().scan("%d",&x);
sl@4
   598
	iTextFieldY->getText().scan("%d",&y);
sl@18
   599
    //SetPixel(x,y,0x01);
sl@18
   600
    iVfd01.SetPixel(x,y,true);
sl@23
   601
	iVfd01.SwapBuffers();
sl@4
   602
	return 1;
sl@4
   603
}
sl@4
   604
sl@4
   605
/**
sl@4
   606
*/
sl@4
   607
long MainWindow::onFutabaResetPixel(FXObject *sender, FXSelector sel, void *ptr)
sl@4
   608
{
sl@4
   609
	int x=0;
sl@4
   610
	int y=0;
sl@4
   611
	iTextFieldX->getText().scan("%d",&x);
sl@4
   612
	iTextFieldY->getText().scan("%d",&y);
sl@18
   613
    //SetPixel(x,y,0x00);
sl@18
   614
    iVfd01.SetPixel(x,y,false);
sl@23
   615
	iVfd01.SwapBuffers();
sl@4
   616
    return 1;
sl@4
   617
}
sl@4
   618
sl@4
   619
long MainWindow::onFutabaSetAllPixels(FXObject *sender, FXSelector sel, void *ptr)
sl@4
   620
	{
sl@23
   621
    iVfd01.SetAllPixels(0xFF);
sl@23
   622
	iVfd01.SwapBuffers();
sl@4
   623
    return 1;
sl@4
   624
	}
sl@4
   625
sl@21
   626
sl@21
   627
/**
sl@21
   628
sl@21
   629
*/
sl@21
   630
long MainWindow::onFutabaSetDisplayPosition(FXObject *sender, FXSelector sel, void *ptr)
sl@21
   631
    {
sl@21
   632
    int x=0;
sl@21
   633
    int y=0;
sl@21
   634
    iTextFieldX->getText().scan("%d",&x);
sl@21
   635
    iTextFieldY->getText().scan("%d",&y);
sl@22
   636
    iVfd01.SetDisplayPosition(x,y);
sl@21
   637
    return 1;
sl@21
   638
    }
sl@21
   639
sl@21
   640
sl@23
   641
/**
sl@23
   642
sl@23
   643
*/
sl@23
   644
long MainWindow::onFutabaToggleOffScreenMode(FXObject *sender, FXSelector sel, void *ptr)
sl@23
   645
    {
sl@23
   646
    iVfd01.ToggleOffScreenMode();
sl@23
   647
	if (iVfd01.OffScreenMode())
sl@23
   648
		{
sl@23
   649
		iButtonToggleOffScreenMode->setText("Switch to on-screen");
sl@23
   650
		}
sl@23
   651
	else
sl@23
   652
		{
sl@23
   653
		iButtonToggleOffScreenMode->setText("Switch to off-screen");
sl@23
   654
		}
sl@23
   655
    return 1;
sl@23
   656
    }
sl@23
   657
sl@23
   658
sl@23
   659
sl@23
   660
sl@2
   661
long
sl@2
   662
MainWindow::onFutabaReadId(FXObject *sender, FXSelector sel, void *ptr)
sl@2
   663
{
sl@19
   664
    iVfd01.RequestId();
sl@2
   665
	return 1;
sl@2
   666
}
sl@2
   667
sl@2
   668
long
sl@2
   669
MainWindow::onFutabaReadFirmwareRevision(FXObject *sender, FXSelector sel, void *ptr)
sl@2
   670
{
sl@19
   671
    iVfd01.RequestFirmwareRevision();
sl@2
   672
	return 1;
sl@2
   673
}
sl@2
   674
sl@2
   675
long
sl@2
   676
MainWindow::onFutabaPowerSupplyMonitor(FXObject *sender, FXSelector sel, void *ptr)
sl@2
   677
{
sl@19
   678
    iVfd01.RequestPowerSupplyStatus();
sl@2
   679
	return 1;
sl@2
   680
}
sl@2
   681
sl@2
   682
sl@2
   683
sl@0
   684
long
sl@0
   685
MainWindow::onTimeout(FXObject *sender, FXSelector sel, void *ptr)
sl@0
   686
{
sl@17
   687
    FutabaVfdReport report;
sl@17
   688
	unsigned char buffer[256];
sl@17
   689
    unsigned char* buf=NULL;
sl@17
   690
    int res = 0;
sl@17
   691
sl@17
   692
    //Use either our display or our generic device depending which one is connected
sl@17
   693
    if (connected_device)
sl@17
   694
        {
sl@17
   695
	    res = hid_read(connected_device, buffer, sizeof(buffer));
sl@17
   696
        buf=buffer;
sl@17
   697
        }
sl@17
   698
    else
sl@17
   699
        {
sl@17
   700
        res=iVfd01.Read(report);
sl@17
   701
        buf=report.Buffer();
sl@17
   702
        }
sl@3
   703
sl@0
   704
	if (res > 0) {
sl@0
   705
		FXString s;
sl@0
   706
		s.format("Received %d bytes:\n", res);
sl@0
   707
		for (int i = 0; i < res; i++) {
sl@0
   708
			FXString t;
sl@0
   709
			t.format("%02hhx ", buf[i]);
sl@0
   710
			s += t;
sl@0
   711
			if ((i+1) % 4 == 0)
sl@0
   712
				s += " ";
sl@0
   713
			if ((i+1) % 16 == 0)
sl@0
   714
				s += "\n";
sl@0
   715
		}
sl@0
   716
		s += "\n";
sl@0
   717
		input_text->appendText(s);
sl@0
   718
		input_text->setBottomLine(INT_MAX);
sl@0
   719
	}
sl@0
   720
	if (res < 0) {
sl@0
   721
		input_text->appendText("hid_read() returned error\n");
sl@0
   722
		input_text->setBottomLine(INT_MAX);
sl@0
   723
	}
sl@0
   724
sl@17
   725
    //Keep on reading
sl@0
   726
	getApp()->addTimeout(this, ID_TIMER,
sl@0
   727
		5 * timeout_scalar /*5ms*/);
sl@0
   728
	return 1;
sl@0
   729
}
sl@0
   730
sl@0
   731
long
sl@0
   732
MainWindow::onMacTimeout(FXObject *sender, FXSelector sel, void *ptr)
sl@0
   733
{
sl@0
   734
#ifdef __APPLE__
sl@0
   735
	check_apple_events();
sl@3
   736
sl@0
   737
	getApp()->addTimeout(this, ID_MAC_TIMER,
sl@0
   738
		50 * timeout_scalar /*50ms*/);
sl@0
   739
#endif
sl@0
   740
sl@0
   741
	return 1;
sl@0
   742
}
sl@0
   743
sl@6
   744
/**
sl@6
   745
sl@6
   746
*/
sl@6
   747
long MainWindow::onSelectFont(FXObject *sender, FXSelector sel, void *ptr)
sl@7
   748
    {
sl@6
   749
    FXFontDialog* dlg=new FXFontDialog(this,"Pick a font");
sl@7
   750
    if (dlg->execute())
sl@7
   751
        {
sl@7
   752
        dlg->getFontSelection(iCurrentFontDesc);
sl@7
   753
        delete iCurrentFont;
sl@7
   754
        iCurrentFont = NULL;
sl@7
   755
        iCurrentFont = new FXFont(getApp(),iCurrentFontDesc);
sl@8
   756
        iCurrentFont->create();
sl@8
   757
        //
sl@8
   758
        delete iFontImage;
sl@8
   759
        iFontImage = NULL;
sl@8
   760
        //
sl@24
   761
		delete[] iPixelBuffer;
sl@24
   762
		iPixelBuffer=NULL;
sl@24
   763
sl@24
   764
        //FXString text="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-[]{}();%$£&~#|_";
sl@24
   765
		FXString text="0123456789ABCDEF";
sl@8
   766
        //Create an image the proper size for our text
sl@24
   767
        iFontImage = new FXTGAImage(getApp(),NULL,IMAGE_KEEP/*IMAGE_SHMI|IMAGE_SHMP*/,iCurrentFont->getTextWidth(text),iCurrentFont->getFontHeight());
sl@8
   768
        iFontImage->create();
sl@8
   769
        //Perform our drawing
sl@8
   770
            {
sl@8
   771
            FXDCWindow dc(iFontImage);
sl@8
   772
            //dc.begin(iFontImage);
sl@8
   773
            dc.setFont(iCurrentFont);
sl@8
   774
            dc.setForeground(0xFFFFFFFF);
sl@8
   775
            //dc.setBackground(0xFF000000);
sl@8
   776
            //dc.setFillStyle(FILL_SOLID);
sl@8
   777
            dc.fillRectangle(0,0,iFontImage->getWidth(),iFontImage->getHeight());
sl@8
   778
            dc.setForeground(0xFF000000);
sl@8
   779
            dc.drawText(0,iCurrentFont->getFontAscent(),text);
sl@8
   780
            //dc.end();
sl@8
   781
            }
sl@24
   782
sl@24
   783
		//Save to file
sl@8
   784
        FXFileStream file;
sl@8
   785
        file.open("fonttest.tga",FXStreamSave);
sl@8
   786
        iFontImage->restore();
sl@8
   787
        iFontImage->savePixels(file);
sl@8
   788
        file.close();
sl@8
   789
sl@24
   790
        //Create display pixel buffer from our image pixels
sl@24
   791
		int w=iFontImage->getWidth();
sl@24
   792
		int h=iFontImage->getHeight(); 
sl@25
   793
		int pixelCount=(w*h);
sl@25
   794
		iPixelBuffer = new BitArray(pixelCount);
sl@24
   795
		for (int i=0;i<w;i++)
sl@24
   796
			{
sl@24
   797
			for (int j=0;j<h;j++)
sl@25
   798
				{				
sl@24
   799
				FXColor color=iFontImage->getPixel(i,j);
sl@24
   800
				if (color!=0xffffffff)
sl@24
   801
					{
sl@25
   802
					iPixelBuffer->SetBit(i*h+j);
sl@24
   803
					}
sl@24
   804
				}
sl@24
   805
			}
sl@24
   806
sl@24
   807
		if (iVfd01.IsOpen())
sl@24
   808
			{
sl@25
   809
			iVfd01.BitBlit(*iPixelBuffer,w,h,0,0);
sl@24
   810
			iVfd01.SwapBuffers();
sl@24
   811
			}
sl@7
   812
sl@7
   813
        }
sl@7
   814
sl@6
   815
    delete dlg;
sl@6
   816
    return 1;
sl@7
   817
    }
sl@6
   818
sl@6
   819
sl@6
   820