test.cpp
author sl
Wed, 21 May 2014 08:54:35 +0200
changeset 6 7d41682fce1a
parent 5 4cb67272815c
child 7 e8c05ae7f418
permissions -rw-r--r--
Adding font selection dialog. Did a bit of cleaning too.
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@0
    32
	FXMAPFUNC(SEL_COMMAND, MainWindow::ID_RESCAN, MainWindow::onRescan ),
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@6
    46
    FXMAPFUNC(SEL_COMMAND, MainWindow::ID_SELECT_FONT, MainWindow::onSelectFont ),
sl@0
    47
	FXMAPFUNC(SEL_TIMEOUT, MainWindow::ID_TIMER, MainWindow::onTimeout ),
sl@0
    48
	FXMAPFUNC(SEL_TIMEOUT, MainWindow::ID_MAC_TIMER, MainWindow::onMacTimeout ),
sl@0
    49
};
sl@0
    50
sl@0
    51
FXIMPLEMENT(MainWindow, FXMainWindow, MainWindowMap, ARRAYNUMBER(MainWindowMap));
sl@0
    52
sl@0
    53
MainWindow::MainWindow(FXApp *app)
sl@3
    54
	: FXMainWindow(app, "HIDAPI Test Application", NULL, NULL, DECOR_ALL, 200,100, 600,900)
sl@0
    55
{
sl@2
    56
	iDimming=0x35;
sl@0
    57
	devices = NULL;
sl@0
    58
	connected_device = NULL;
sl@0
    59
sl@0
    60
	FXVerticalFrame *vf = new FXVerticalFrame(this, LAYOUT_FILL_Y|LAYOUT_FILL_X);
sl@0
    61
sl@0
    62
	FXLabel *label = new FXLabel(vf, "HIDAPI Test Tool");
sl@0
    63
	title_font = new FXFont(getApp(), "Arial", 14, FXFont::Bold);
sl@0
    64
	label->setFont(title_font);
sl@3
    65
sl@0
    66
	new FXLabel(vf,
sl@0
    67
		"Select a device and press Connect.", NULL, JUSTIFY_LEFT);
sl@0
    68
	new FXLabel(vf,
sl@0
    69
		"Output data bytes can be entered in the Output section, \n"
sl@0
    70
		"separated by space, comma or brackets. Data starting with 0x\n"
sl@0
    71
		"is treated as hex. Data beginning with a 0 is treated as \n"
sl@0
    72
		"octal. All other data is treated as decimal.", NULL, JUSTIFY_LEFT);
sl@0
    73
	new FXLabel(vf,
sl@0
    74
		"Data received from the device appears in the Input section.",
sl@0
    75
		NULL, JUSTIFY_LEFT);
sl@0
    76
	new FXLabel(vf,
sl@0
    77
		"Optionally, a report length may be specified. Extra bytes are\n"
sl@0
    78
		"padded with zeros. If no length is specified, the length is \n"
sl@0
    79
		"inferred from the data.",
sl@0
    80
		NULL, JUSTIFY_LEFT);
sl@0
    81
	new FXLabel(vf, "");
sl@0
    82
sl@0
    83
	// Device List and Connect/Disconnect buttons
sl@0
    84
	FXHorizontalFrame *hf = new FXHorizontalFrame(vf, LAYOUT_FILL_X);
sl@0
    85
	//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
    86
	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
    87
	FXVerticalFrame *buttonVF = new FXVerticalFrame(hf);
sl@0
    88
	connect_button = new FXButton(buttonVF, "Connect", NULL, this, ID_CONNECT, BUTTON_NORMAL|LAYOUT_FILL_X);
sl@0
    89
	disconnect_button = new FXButton(buttonVF, "Disconnect", NULL, this, ID_DISCONNECT, BUTTON_NORMAL|LAYOUT_FILL_X);
sl@0
    90
	disconnect_button->disable();
sl@0
    91
	rescan_button = new FXButton(buttonVF, "Re-Scan devices", NULL, this, ID_RESCAN, BUTTON_NORMAL|LAYOUT_FILL_X);
sl@0
    92
	new FXHorizontalFrame(buttonVF, 0, 0,0,0,0, 0,0,50,0);
sl@0
    93
sl@0
    94
	connected_label = new FXLabel(vf, "Disconnected");
sl@3
    95
sl@6
    96
    //Font group
sl@6
    97
    new FXHorizontalFrame(vf);
sl@6
    98
    FXGroupBox *gb = new FXGroupBox(vf, "Fonts", FRAME_GROOVE|LAYOUT_FILL_X);
sl@6
    99
    FXMatrix *matrix = new FXMatrix(gb, 3, MATRIX_BY_COLUMNS|LAYOUT_FILL_X);
sl@6
   100
    iButtonSelectFont = new FXButton(matrix, "Select Font", NULL, this, ID_SELECT_FONT, BUTTON_NORMAL|LAYOUT_FILL_X);
sl@6
   101
sl@2
   102
	//Futaba VFD commands
sl@0
   103
	new FXHorizontalFrame(vf);
sl@6
   104
	gb = new FXGroupBox(vf, "Futaba GP1212A01A", FRAME_GROOVE|LAYOUT_FILL_X);
sl@6
   105
	matrix = new FXMatrix(gb, 3, MATRIX_BY_COLUMNS|LAYOUT_FILL_X);
sl@2
   106
	iButtonClearDisplay = new FXButton(matrix, "Clear Display", NULL, this, ID_FUTABA_CLEAR_DISPLAY, BUTTON_NORMAL|LAYOUT_FILL_X);
sl@2
   107
	iButtonDimming = new FXButton(matrix, "Dimming", NULL, this, ID_FUTABA_DIMMING, BUTTON_NORMAL|LAYOUT_FILL_X);
sl@2
   108
	iButtonDisplayDataInput = new FXButton(matrix, "Display Data Input", NULL, this, ID_FUTABA_DISPLAY_DATA_INPUT, BUTTON_NORMAL|LAYOUT_FILL_X);
sl@2
   109
	iButtonReadId = new FXButton(matrix, "Read Id", NULL, this, ID_FUTABA_READ_ID, BUTTON_NORMAL|LAYOUT_FILL_X);
sl@2
   110
	iButtonReadFirmwareRevision =  new FXButton(matrix, "Read Firmware Revision", NULL, this, ID_FUTABA_READ_FIRMWARE_REVISION, BUTTON_NORMAL|LAYOUT_FILL_X);
sl@2
   111
	iButtonPowerSupplyMonitor = new FXButton(matrix, "Power Supply Monitor", NULL, this, ID_FUTABA_POWER_SUPPLY_MONITOR, BUTTON_NORMAL|LAYOUT_FILL_X);
sl@3
   112
    new FXLabel(matrix, "X",NULL,LABEL_NORMAL|LAYOUT_FILL_X);
sl@3
   113
    new FXLabel(matrix, "Y",NULL,LABEL_NORMAL|LAYOUT_FILL_X);
sl@3
   114
    new FXLabel(matrix, "",NULL,LABEL_NORMAL|LAYOUT_FILL_X);
sl@3
   115
    iTextFieldX = new FXTextField(matrix, 3, NULL, 0, TEXTFIELD_NORMAL|TEXTFIELD_INTEGER|LAYOUT_FILL_X);
sl@3
   116
    iTextFieldY = new FXTextField(matrix, 3, NULL, 0, TEXTFIELD_NORMAL|TEXTFIELD_INTEGER|LAYOUT_FILL_X);
sl@4
   117
    iButtonSetPixel = new FXButton(matrix, "Set Pixel", NULL, this, ID_FUTABA_SET_PIXEL, BUTTON_NORMAL|LAYOUT_FILL_X);
sl@4
   118
	iButtonResetPixel = new FXButton(matrix, "Reset Pixel", NULL, this, ID_FUTABA_RESET_PIXEL, BUTTON_NORMAL|LAYOUT_FILL_X);
sl@3
   119
    //
sl@3
   120
    iButtonSetAllPixels = new FXButton(matrix, "Set All Pixels", NULL, this, ID_FUTABA_SET_ALL_PIXELS, BUTTON_NORMAL|LAYOUT_FILL_X);
sl@6
   121
    //
sl@6
   122
sl@2
   123
	//
sl@2
   124
	iButtonClearDisplay->disable();
sl@2
   125
	iButtonDimming->disable();
sl@2
   126
	iButtonDisplayDataInput->disable();
sl@2
   127
	iButtonReadId->disable();
sl@2
   128
	iButtonReadFirmwareRevision->disable();
sl@2
   129
	iButtonPowerSupplyMonitor->disable();
sl@3
   130
    iTextFieldX->disable();
sl@3
   131
    iTextFieldY->disable();
sl@3
   132
    iButtonSetPixel->disable();
sl@4
   133
	iButtonResetPixel->disable();
sl@3
   134
    iButtonSetAllPixels->disable();
sl@2
   135
	//
sl@2
   136
sl@0
   137
	// Output Group Box
sl@3
   138
	new FXHorizontalFrame(vf);
sl@2
   139
	gb = new FXGroupBox(vf, "Output", FRAME_GROOVE|LAYOUT_FILL_X);
sl@2
   140
	matrix = new FXMatrix(gb, 3, MATRIX_BY_COLUMNS|LAYOUT_FILL_X);
sl@0
   141
	new FXLabel(matrix, "Data");
sl@0
   142
	new FXLabel(matrix, "Length");
sl@0
   143
	new FXLabel(matrix, "");
sl@0
   144
sl@0
   145
	//hf = new FXHorizontalFrame(gb, LAYOUT_FILL_X);
sl@0
   146
	output_text = new FXTextField(matrix, 30, NULL, 0, TEXTFIELD_NORMAL|LAYOUT_FILL_X|LAYOUT_FILL_COLUMN);
sl@0
   147
	output_text->setText("1 0x81 0");
sl@0
   148
	output_len = new FXTextField(matrix, 5, NULL, 0, TEXTFIELD_NORMAL|LAYOUT_FILL_X|LAYOUT_FILL_COLUMN);
sl@0
   149
	output_button = new FXButton(matrix, "Send Output Report", NULL, this, ID_SEND_OUTPUT_REPORT, BUTTON_NORMAL|LAYOUT_FILL_X);
sl@0
   150
	output_button->disable();
sl@0
   151
	//new FXHorizontalFrame(matrix, LAYOUT_FILL_X);
sl@0
   152
sl@0
   153
	//hf = new FXHorizontalFrame(gb, LAYOUT_FILL_X);
sl@0
   154
	feature_text = new FXTextField(matrix, 30, NULL, 0, TEXTFIELD_NORMAL|LAYOUT_FILL_X|LAYOUT_FILL_COLUMN);
sl@0
   155
	feature_len = new FXTextField(matrix, 5, NULL, 0, TEXTFIELD_NORMAL|LAYOUT_FILL_X|LAYOUT_FILL_COLUMN);
sl@0
   156
	feature_button = new FXButton(matrix, "Send Feature Report", NULL, this, ID_SEND_FEATURE_REPORT, BUTTON_NORMAL|LAYOUT_FILL_X);
sl@0
   157
	feature_button->disable();
sl@0
   158
sl@0
   159
	get_feature_text = new FXTextField(matrix, 30, NULL, 0, TEXTFIELD_NORMAL|LAYOUT_FILL_X|LAYOUT_FILL_COLUMN);
sl@0
   160
	new FXWindow(matrix);
sl@0
   161
	get_feature_button = new FXButton(matrix, "Get Feature Report", NULL, this, ID_GET_FEATURE_REPORT, BUTTON_NORMAL|LAYOUT_FILL_X);
sl@0
   162
	get_feature_button->disable();
sl@0
   163
sl@0
   164
sl@0
   165
	// Input Group Box
sl@0
   166
	gb = new FXGroupBox(vf, "Input", FRAME_GROOVE|LAYOUT_FILL_X|LAYOUT_FILL_Y);
sl@0
   167
	FXVerticalFrame *innerVF = new FXVerticalFrame(gb, LAYOUT_FILL_X|LAYOUT_FILL_Y);
sl@0
   168
	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
   169
	input_text->setEditable(false);
sl@0
   170
	new FXButton(innerVF, "Clear", NULL, this, ID_CLEAR, BUTTON_NORMAL|LAYOUT_RIGHT);
sl@3
   171
sl@0
   172
sl@0
   173
}
sl@0
   174
sl@0
   175
MainWindow::~MainWindow()
sl@0
   176
{
sl@0
   177
	if (connected_device)
sl@0
   178
		hid_close(connected_device);
sl@0
   179
	hid_exit();
sl@0
   180
	delete title_font;
sl@0
   181
}
sl@0
   182
sl@0
   183
void
sl@0
   184
MainWindow::create()
sl@0
   185
{
sl@0
   186
	FXMainWindow::create();
sl@0
   187
	show();
sl@0
   188
sl@0
   189
	onRescan(NULL, 0, NULL);
sl@3
   190
sl@0
   191
sl@0
   192
#ifdef __APPLE__
sl@0
   193
	init_apple_message_system();
sl@0
   194
#endif
sl@3
   195
sl@0
   196
	getApp()->addTimeout(this, ID_MAC_TIMER,
sl@0
   197
		50 * timeout_scalar /*50ms*/);
sl@0
   198
}
sl@0
   199
sl@0
   200
long
sl@0
   201
MainWindow::onConnect(FXObject *sender, FXSelector sel, void *ptr)
sl@0
   202
{
sl@0
   203
	if (connected_device != NULL)
sl@0
   204
		return 1;
sl@3
   205
sl@0
   206
	FXint cur_item = device_list->getCurrentItem();
sl@0
   207
	if (cur_item < 0)
sl@0
   208
		return -1;
sl@0
   209
	FXListItem *item = device_list->getItem(cur_item);
sl@0
   210
	if (!item)
sl@0
   211
		return -1;
sl@0
   212
	struct hid_device_info *device_info = (struct hid_device_info*) item->getData();
sl@0
   213
	if (!device_info)
sl@0
   214
		return -1;
sl@3
   215
sl@0
   216
	connected_device =  hid_open_path(device_info->path);
sl@3
   217
sl@0
   218
	if (!connected_device) {
sl@0
   219
		FXMessageBox::error(this, MBOX_OK, "Device Error", "Unable To Connect to Device");
sl@0
   220
		return -1;
sl@0
   221
	}
sl@3
   222
sl@0
   223
	hid_set_nonblocking(connected_device, 1);
sl@0
   224
sl@0
   225
	getApp()->addTimeout(this, ID_TIMER,
sl@0
   226
		5 * timeout_scalar /*5ms*/);
sl@3
   227
sl@0
   228
	FXString s;
sl@0
   229
	s.format("Connected to: %04hx:%04hx -", device_info->vendor_id, device_info->product_id);
sl@0
   230
	s += FXString(" ") + device_info->manufacturer_string;
sl@0
   231
	s += FXString(" ") + device_info->product_string;
sl@0
   232
	connected_label->setText(s);
sl@0
   233
	output_button->enable();
sl@0
   234
	feature_button->enable();
sl@0
   235
	get_feature_button->enable();
sl@0
   236
	connect_button->disable();
sl@0
   237
	disconnect_button->enable();
sl@0
   238
	input_text->setText("");
sl@2
   239
	//
sl@2
   240
	iButtonClearDisplay->enable();
sl@2
   241
	iButtonDimming->enable();
sl@2
   242
	iButtonDisplayDataInput->enable();
sl@2
   243
	iButtonReadId->enable();
sl@2
   244
	iButtonReadFirmwareRevision->enable();
sl@2
   245
	iButtonPowerSupplyMonitor->enable();
sl@3
   246
    iTextFieldX->enable();
sl@3
   247
    iTextFieldY->enable();
sl@3
   248
    iButtonSetPixel->enable();
sl@4
   249
	iButtonResetPixel->enable();
sl@3
   250
    iButtonSetAllPixels->enable();
sl@3
   251
sl@2
   252
	//
sl@2
   253
	iOutputReportBuffer=new unsigned char[KFutabaOutputReportLength]; //TODO: use connected_device->output_report_length
sl@0
   254
sl@0
   255
	return 1;
sl@0
   256
}
sl@0
   257
sl@0
   258
long
sl@0
   259
MainWindow::onDisconnect(FXObject *sender, FXSelector sel, void *ptr)
sl@0
   260
{
sl@0
   261
	hid_close(connected_device);
sl@0
   262
	connected_device = NULL;
sl@0
   263
	connected_label->setText("Disconnected");
sl@0
   264
	output_button->disable();
sl@0
   265
	feature_button->disable();
sl@0
   266
	get_feature_button->disable();
sl@0
   267
	connect_button->enable();
sl@0
   268
	disconnect_button->disable();
sl@0
   269
sl@0
   270
	getApp()->removeTimeout(this, ID_TIMER);
sl@2
   271
sl@2
   272
	//
sl@2
   273
	iButtonClearDisplay->disable();
sl@2
   274
	iButtonDimming->disable();
sl@2
   275
	iButtonDisplayDataInput->disable();
sl@2
   276
	iButtonReadId->disable();
sl@2
   277
	iButtonReadFirmwareRevision->disable();
sl@2
   278
	iButtonPowerSupplyMonitor->disable();
sl@3
   279
    iTextFieldX->disable();
sl@3
   280
    iTextFieldY->disable();
sl@3
   281
    iButtonSetPixel->disable();
sl@4
   282
	iButtonResetPixel->disable();
sl@3
   283
    iButtonSetAllPixels->disable();
sl@2
   284
	//
sl@2
   285
sl@2
   286
sl@2
   287
	delete iOutputReportBuffer;
sl@2
   288
	iOutputReportBuffer=NULL;
sl@3
   289
sl@0
   290
	return 1;
sl@0
   291
}
sl@0
   292
sl@0
   293
long
sl@0
   294
MainWindow::onRescan(FXObject *sender, FXSelector sel, void *ptr)
sl@0
   295
{
sl@0
   296
	struct hid_device_info *cur_dev;
sl@0
   297
sl@0
   298
	device_list->clearItems();
sl@3
   299
sl@2
   300
	//Define Futaba vendor ID to filter our list of device
sl@2
   301
	const unsigned short KFutabaVendorId = 0x1008;
sl@2
   302
sl@0
   303
	// List the Devices
sl@0
   304
	hid_free_enumeration(devices);
sl@0
   305
	devices = hid_enumerate(0x0, 0x0);
sl@3
   306
	cur_dev = devices;
sl@0
   307
	while (cur_dev) {
sl@2
   308
		// Add it to the List Box only if it is a Futaba device
sl@3
   309
		if (cur_dev->vendor_id == KFutabaVendorId)
sl@2
   310
			{
sl@2
   311
			FXString s;
sl@2
   312
			FXString usage_str;
sl@2
   313
			s.format("%04hx:%04hx -", cur_dev->vendor_id, cur_dev->product_id);
sl@2
   314
			s += FXString(" ") + cur_dev->manufacturer_string;
sl@2
   315
			s += FXString(" ") + cur_dev->product_string;
sl@2
   316
			usage_str.format(" (usage: %04hx:%04hx) ", cur_dev->usage_page, cur_dev->usage);
sl@2
   317
			s += usage_str;
sl@2
   318
			FXListItem *li = new FXListItem(s, NULL, cur_dev);
sl@2
   319
			device_list->appendItem(li);
sl@2
   320
			}
sl@3
   321
sl@0
   322
		cur_dev = cur_dev->next;
sl@0
   323
	}
sl@0
   324
sl@0
   325
	if (device_list->getNumItems() == 0)
sl@0
   326
		device_list->appendItem("*** No Devices Connected ***");
sl@0
   327
	else {
sl@0
   328
		device_list->selectItem(0);
sl@0
   329
	}
sl@0
   330
sl@0
   331
	return 1;
sl@0
   332
}
sl@0
   333
sl@0
   334
size_t
sl@0
   335
MainWindow::getDataFromTextField(FXTextField *tf, char *buf, size_t len)
sl@0
   336
{
sl@0
   337
	const char *delim = " ,{}\t\r\n";
sl@0
   338
	FXString data = tf->getText();
sl@0
   339
	const FXchar *d = data.text();
sl@0
   340
	size_t i = 0;
sl@3
   341
sl@0
   342
	// Copy the string from the GUI.
sl@0
   343
	size_t sz = strlen(d);
sl@0
   344
	char *str = (char*) malloc(sz+1);
sl@0
   345
	strcpy(str, d);
sl@3
   346
sl@0
   347
	// For each token in the string, parse and store in buf[].
sl@0
   348
	char *token = strtok(str, delim);
sl@0
   349
	while (token) {
sl@0
   350
		char *endptr;
sl@0
   351
		long int val = strtol(token, &endptr, 0);
sl@0
   352
		buf[i++] = val;
sl@0
   353
		token = strtok(NULL, delim);
sl@0
   354
	}
sl@3
   355
sl@0
   356
	free(str);
sl@0
   357
	return i;
sl@0
   358
}
sl@0
   359
sl@0
   360
/* getLengthFromTextField()
sl@0
   361
   Returns length:
sl@0
   362
	 0: empty text field
sl@0
   363
	>0: valid length
sl@0
   364
	-1: invalid length */
sl@0
   365
int
sl@0
   366
MainWindow::getLengthFromTextField(FXTextField *tf)
sl@0
   367
{
sl@0
   368
	long int len;
sl@0
   369
	FXString str = tf->getText();
sl@0
   370
	size_t sz = str.length();
sl@0
   371
sl@0
   372
	if (sz > 0) {
sl@0
   373
		char *endptr;
sl@0
   374
		len = strtol(str.text(), &endptr, 0);
sl@0
   375
		if (endptr != str.text() && *endptr == '\0') {
sl@0
   376
			if (len <= 0) {
sl@0
   377
				FXMessageBox::error(this, MBOX_OK, "Invalid length", "Enter a length greater than zero.");
sl@0
   378
				return -1;
sl@0
   379
			}
sl@0
   380
			return len;
sl@0
   381
		}
sl@0
   382
		else
sl@0
   383
			return -1;
sl@0
   384
	}
sl@0
   385
sl@0
   386
	return 0;
sl@0
   387
}
sl@0
   388
sl@0
   389
long
sl@0
   390
MainWindow::onSendOutputReport(FXObject *sender, FXSelector sel, void *ptr)
sl@0
   391
{
sl@0
   392
	char buf[256];
sl@0
   393
	size_t data_len, len;
sl@0
   394
	int textfield_len;
sl@0
   395
sl@0
   396
	memset(buf, 0x0, sizeof(buf));
sl@0
   397
	textfield_len = getLengthFromTextField(output_len);
sl@0
   398
	data_len = getDataFromTextField(output_text, buf, sizeof(buf));
sl@0
   399
sl@0
   400
	if (textfield_len < 0) {
sl@0
   401
		FXMessageBox::error(this, MBOX_OK, "Invalid length", "Length field is invalid. Please enter a number in hex, octal, or decimal.");
sl@0
   402
		return 1;
sl@0
   403
	}
sl@0
   404
sl@0
   405
	if (textfield_len > sizeof(buf)) {
sl@0
   406
		FXMessageBox::error(this, MBOX_OK, "Invalid length", "Length field is too long.");
sl@0
   407
		return 1;
sl@0
   408
	}
sl@0
   409
sl@0
   410
	len = (textfield_len)? textfield_len: data_len;
sl@0
   411
sl@0
   412
	int res = hid_write(connected_device, (const unsigned char*)buf, len);
sl@0
   413
	if (res < 0) {
sl@0
   414
		FXMessageBox::error(this, MBOX_OK, "Error Writing", "Could not write to device. Error reported was: %ls", hid_error(connected_device));
sl@0
   415
	}
sl@3
   416
sl@0
   417
	return 1;
sl@0
   418
}
sl@0
   419
sl@0
   420
long
sl@0
   421
MainWindow::onSendFeatureReport(FXObject *sender, FXSelector sel, void *ptr)
sl@0
   422
{
sl@0
   423
	char buf[256];
sl@0
   424
	size_t data_len, len;
sl@0
   425
	int textfield_len;
sl@0
   426
sl@0
   427
	memset(buf, 0x0, sizeof(buf));
sl@0
   428
	textfield_len = getLengthFromTextField(feature_len);
sl@0
   429
	data_len = getDataFromTextField(feature_text, buf, sizeof(buf));
sl@0
   430
sl@0
   431
	if (textfield_len < 0) {
sl@0
   432
		FXMessageBox::error(this, MBOX_OK, "Invalid length", "Length field is invalid. Please enter a number in hex, octal, or decimal.");
sl@0
   433
		return 1;
sl@0
   434
	}
sl@0
   435
sl@0
   436
	if (textfield_len > sizeof(buf)) {
sl@0
   437
		FXMessageBox::error(this, MBOX_OK, "Invalid length", "Length field is too long.");
sl@0
   438
		return 1;
sl@0
   439
	}
sl@0
   440
sl@0
   441
	len = (textfield_len)? textfield_len: data_len;
sl@0
   442
sl@3
   443
	int res = hid_send_feature_report(connected_device, (const unsigned char*)buf, len);
sl@0
   444
	if (res < 0) {
sl@0
   445
		FXMessageBox::error(this, MBOX_OK, "Error Writing", "Could not send feature report to device. Error reported was: %ls", hid_error(connected_device));
sl@0
   446
	}
sl@0
   447
sl@0
   448
	return 1;
sl@0
   449
}
sl@0
   450
sl@0
   451
long
sl@0
   452
MainWindow::onGetFeatureReport(FXObject *sender, FXSelector sel, void *ptr)
sl@0
   453
{
sl@0
   454
	char buf[256];
sl@0
   455
	size_t len;
sl@0
   456
sl@0
   457
	memset(buf, 0x0, sizeof(buf));
sl@0
   458
	len = getDataFromTextField(get_feature_text, buf, sizeof(buf));
sl@0
   459
sl@0
   460
	if (len != 1) {
sl@0
   461
		FXMessageBox::error(this, MBOX_OK, "Too many numbers", "Enter only a single report number in the text field");
sl@0
   462
	}
sl@0
   463
sl@0
   464
	int res = hid_get_feature_report(connected_device, (unsigned char*)buf, sizeof(buf));
sl@0
   465
	if (res < 0) {
sl@0
   466
		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
   467
	}
sl@0
   468
sl@0
   469
	if (res > 0) {
sl@0
   470
		FXString s;
sl@0
   471
		s.format("Returned Feature Report. %d bytes:\n", res);
sl@0
   472
		for (int i = 0; i < res; i++) {
sl@0
   473
			FXString t;
sl@0
   474
			t.format("%02hhx ", buf[i]);
sl@0
   475
			s += t;
sl@0
   476
			if ((i+1) % 4 == 0)
sl@0
   477
				s += " ";
sl@0
   478
			if ((i+1) % 16 == 0)
sl@0
   479
				s += "\n";
sl@0
   480
		}
sl@0
   481
		s += "\n";
sl@0
   482
		input_text->appendText(s);
sl@0
   483
		input_text->setBottomLine(INT_MAX);
sl@0
   484
	}
sl@3
   485
sl@0
   486
	return 1;
sl@0
   487
}
sl@0
   488
sl@0
   489
long
sl@0
   490
MainWindow::onClear(FXObject *sender, FXSelector sel, void *ptr)
sl@0
   491
{
sl@0
   492
	input_text->setText("");
sl@0
   493
	return 1;
sl@0
   494
}
sl@0
   495
sl@2
   496
sl@2
   497
long
sl@2
   498
MainWindow::onFutabaClearDisplay(FXObject *sender, FXSelector sel, void *ptr)
sl@2
   499
{
sl@2
   500
	memset(iOutputReportBuffer, 0x0, KFutabaOutputReportLength);
sl@2
   501
	iOutputReportBuffer[0]=0x00; //Report ID
sl@2
   502
	iOutputReportBuffer[1]=0x04; //Report length
sl@2
   503
	iOutputReportBuffer[2]=0x1B; //
sl@2
   504
	iOutputReportBuffer[3]=0x5B; //
sl@2
   505
	iOutputReportBuffer[4]=0x32; //
sl@2
   506
	iOutputReportBuffer[5]=0x4A; //
sl@2
   507
	int res = hid_write(connected_device, iOutputReportBuffer, KFutabaOutputReportLength);
sl@2
   508
sl@2
   509
	return 1;
sl@2
   510
}
sl@2
   511
sl@2
   512
long
sl@2
   513
MainWindow::onFutabaDimming(FXObject *sender, FXSelector sel, void *ptr)
sl@2
   514
{
sl@2
   515
	memset(iOutputReportBuffer, 0x0, KFutabaOutputReportLength);
sl@2
   516
	iOutputReportBuffer[0]=0x00; //Report ID
sl@2
   517
	iOutputReportBuffer[1]=0x06; //Report length
sl@2
   518
	iOutputReportBuffer[2]=0x1B; //
sl@2
   519
	iOutputReportBuffer[3]=0x5C; //
sl@2
   520
	iOutputReportBuffer[4]=0x3F; //
sl@2
   521
	iOutputReportBuffer[5]=0x4C; //
sl@2
   522
	iOutputReportBuffer[6]=0x44; //
sl@2
   523
	iDimming = (iDimming==0x35?0x30:++iDimming);
sl@2
   524
	iOutputReportBuffer[7]=iDimming;
sl@2
   525
	int res = hid_write(connected_device, iOutputReportBuffer, KFutabaOutputReportLength);
sl@2
   526
sl@2
   527
	return 1;
sl@2
   528
}
sl@2
   529
sl@2
   530
long
sl@2
   531
MainWindow::onFutabaDisplayDataInput(FXObject *sender, FXSelector sel, void *ptr)
sl@2
   532
{
sl@2
   533
	//@1B 5B F0 00 00 07 00 01 FF
sl@2
   534
sl@2
   535
	memset(iOutputReportBuffer, 0x0, KFutabaOutputReportLength);
sl@2
   536
	iOutputReportBuffer[0]=0x00; //Report ID
sl@2
   537
	iOutputReportBuffer[1]=0x09; //Report length
sl@2
   538
	iOutputReportBuffer[2]=0x1B; //
sl@2
   539
	iOutputReportBuffer[3]=0x5B; //
sl@2
   540
	iOutputReportBuffer[4]=0xF0; //
sl@4
   541
	iOutputReportBuffer[5]=0x00; //X
sl@4
   542
	iOutputReportBuffer[6]=0x00; //Y
sl@2
   543
	iOutputReportBuffer[7]=0x07; //
sl@2
   544
	iOutputReportBuffer[8]=0x00; //
sl@2
   545
	iOutputReportBuffer[9]=0x01; //
sl@2
   546
	iOutputReportBuffer[10]=0xFF; //
sl@2
   547
	int res = hid_write(connected_device, iOutputReportBuffer, KFutabaOutputReportLength);
sl@2
   548
sl@3
   549
sl@2
   550
	return 1;
sl@2
   551
}
sl@2
   552
sl@5
   553
sl@5
   554
sl@5
   555
sl@4
   556
/**
sl@5
   557
Set a single pixel to the specified value.
sl@5
   558
@param X coordinate of our pixel.
sl@5
   559
@param Y coordinate of our pixel.
sl@5
   560
@param The LSB defines our pixel value.
sl@4
   561
*/
sl@4
   562
void MainWindow::SetPixel(int aX, int aY, unsigned char aValue)
sl@4
   563
	{
sl@5
   564
	//Just specify a one pixel block
sl@5
   565
	SetPixelBlock(aX,aY,0x00,0x01,aValue);
sl@5
   566
	}
sl@5
   567
sl@5
   568
/**
sl@5
   569
*/
sl@5
   570
void MainWindow::SetPixelBlock(int aX, int aY, int aHeight, int aSize, unsigned char aValue)
sl@5
   571
	{
sl@5
   572
	//Size must be 63 or below
sl@4
   573
	memset(iOutputReportBuffer, 0x0, KFutabaOutputReportLength);
sl@4
   574
	iOutputReportBuffer[0]=0x00; //Report ID
sl@5
   575
	iOutputReportBuffer[1]=0x08+aSize; //Report length
sl@4
   576
	iOutputReportBuffer[2]=0x1B; //
sl@4
   577
	iOutputReportBuffer[3]=0x5B; //
sl@4
   578
	iOutputReportBuffer[4]=0xF0; //
sl@4
   579
	iOutputReportBuffer[5]=aX; //X
sl@4
   580
	iOutputReportBuffer[6]=aY; //Y
sl@6
   581
	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
   582
	iOutputReportBuffer[8]=0x00; //Size of pixel data in bytes (MSB)
sl@5
   583
	iOutputReportBuffer[9]=aSize; //Size of pixel data in bytes (LSB)
sl@5
   584
	memset(iOutputReportBuffer+10, aValue, KFutabaOutputReportLength);
sl@5
   585
	//iOutputReportBuffer[10]=aValue; //Pixel data
sl@4
   586
	int res = hid_write(connected_device, iOutputReportBuffer, KFutabaOutputReportLength);
sl@4
   587
	}
sl@4
   588
sl@5
   589
sl@4
   590
/**
sl@4
   591
*/
sl@4
   592
long MainWindow::onFutabaSetPixel(FXObject *sender, FXSelector sel, void *ptr)
sl@4
   593
{
sl@4
   594
	int x=0;
sl@4
   595
	int y=0;
sl@4
   596
	iTextFieldX->getText().scan("%d",&x);
sl@4
   597
	iTextFieldY->getText().scan("%d",&y);
sl@4
   598
    SetPixel(x,y,0x01);
sl@4
   599
	return 1;
sl@4
   600
}
sl@4
   601
sl@4
   602
/**
sl@4
   603
*/
sl@4
   604
long MainWindow::onFutabaResetPixel(FXObject *sender, FXSelector sel, void *ptr)
sl@4
   605
{
sl@4
   606
	int x=0;
sl@4
   607
	int y=0;
sl@4
   608
	iTextFieldX->getText().scan("%d",&x);
sl@4
   609
	iTextFieldY->getText().scan("%d",&y);
sl@4
   610
    SetPixel(x,y,0x00);
sl@4
   611
    return 1;
sl@4
   612
}
sl@4
   613
sl@4
   614
long MainWindow::onFutabaSetAllPixels(FXObject *sender, FXSelector sel, void *ptr)
sl@4
   615
	{
sl@5
   616
	//One pixel at a time
sl@5
   617
	/*
sl@4
   618
	for (int i=0;i<256;i++)
sl@4
   619
		{
sl@4
   620
		for (int j=0;j<64;j++)
sl@4
   621
			{
sl@4
   622
			SetPixel(i,j,0x01);
sl@4
   623
			}
sl@5
   624
		}
sl@5
   625
	*/
sl@5
   626
	//16x16=256 pixels at a time goes much faster
sl@5
   627
	for (int i=0;i<256;i+=16)
sl@5
   628
		{
sl@5
   629
		for (int j=0;j<64;j+=16)
sl@5
   630
			{
sl@5
   631
			SetPixelBlock(i,j,15,32,0xFF);
sl@5
   632
			//FXThread::sleep(1000000000);
sl@5
   633
			}
sl@4
   634
		}
sl@4
   635
sl@4
   636
    return 1;
sl@4
   637
	}
sl@4
   638
sl@2
   639
long
sl@2
   640
MainWindow::onFutabaReadId(FXObject *sender, FXSelector sel, void *ptr)
sl@2
   641
{
sl@2
   642
	//1BH,5BH,63H,49H,44H
sl@2
   643
	memset(iOutputReportBuffer, 0x0, KFutabaOutputReportLength);
sl@2
   644
	iOutputReportBuffer[0]=0x00; //Report ID
sl@2
   645
	iOutputReportBuffer[1]=0x05; //Report length
sl@2
   646
	iOutputReportBuffer[2]=0x1B; //
sl@2
   647
	iOutputReportBuffer[3]=0x5B; //
sl@2
   648
	iOutputReportBuffer[4]=0x63; //
sl@2
   649
	iOutputReportBuffer[5]=0x49; //
sl@2
   650
	iOutputReportBuffer[6]=0x44; //
sl@2
   651
	int res = hid_write(connected_device, iOutputReportBuffer, KFutabaOutputReportLength);
sl@2
   652
sl@2
   653
	return 1;
sl@2
   654
}
sl@2
   655
sl@2
   656
long
sl@2
   657
MainWindow::onFutabaReadFirmwareRevision(FXObject *sender, FXSelector sel, void *ptr)
sl@2
   658
{
sl@2
   659
	//1BH,5BH,63H,46H,52H
sl@2
   660
	memset(iOutputReportBuffer, 0x0, KFutabaOutputReportLength);
sl@2
   661
	iOutputReportBuffer[0]=0x00; //Report ID
sl@2
   662
	iOutputReportBuffer[1]=0x05; //Report length
sl@2
   663
	iOutputReportBuffer[2]=0x1B; //
sl@2
   664
	iOutputReportBuffer[3]=0x5B; //
sl@2
   665
	iOutputReportBuffer[4]=0x63; //
sl@2
   666
	iOutputReportBuffer[5]=0x46; //
sl@2
   667
	iOutputReportBuffer[6]=0x52; //
sl@2
   668
	int res = hid_write(connected_device, iOutputReportBuffer, KFutabaOutputReportLength);
sl@2
   669
sl@2
   670
	return 1;
sl@2
   671
}
sl@2
   672
sl@2
   673
long
sl@2
   674
MainWindow::onFutabaPowerSupplyMonitor(FXObject *sender, FXSelector sel, void *ptr)
sl@2
   675
{
sl@2
   676
	//1BH,5BH,63H,50H,4DH
sl@2
   677
	memset(iOutputReportBuffer, 0x0, KFutabaOutputReportLength);
sl@2
   678
	iOutputReportBuffer[0]=0x00; //Report ID
sl@2
   679
	iOutputReportBuffer[1]=0x05; //Report length
sl@2
   680
	iOutputReportBuffer[2]=0x1B; //
sl@2
   681
	iOutputReportBuffer[3]=0x5B; //
sl@2
   682
	iOutputReportBuffer[4]=0x63; //
sl@2
   683
	iOutputReportBuffer[5]=0x50; //
sl@2
   684
	iOutputReportBuffer[6]=0x4D; //
sl@2
   685
	int res = hid_write(connected_device, iOutputReportBuffer, KFutabaOutputReportLength);
sl@2
   686
sl@2
   687
	return 1;
sl@2
   688
}
sl@2
   689
sl@2
   690
sl@2
   691
sl@0
   692
long
sl@0
   693
MainWindow::onTimeout(FXObject *sender, FXSelector sel, void *ptr)
sl@0
   694
{
sl@0
   695
	unsigned char buf[256];
sl@0
   696
	int res = hid_read(connected_device, buf, sizeof(buf));
sl@3
   697
sl@0
   698
	if (res > 0) {
sl@0
   699
		FXString s;
sl@0
   700
		s.format("Received %d bytes:\n", res);
sl@0
   701
		for (int i = 0; i < res; i++) {
sl@0
   702
			FXString t;
sl@0
   703
			t.format("%02hhx ", buf[i]);
sl@0
   704
			s += t;
sl@0
   705
			if ((i+1) % 4 == 0)
sl@0
   706
				s += " ";
sl@0
   707
			if ((i+1) % 16 == 0)
sl@0
   708
				s += "\n";
sl@0
   709
		}
sl@0
   710
		s += "\n";
sl@0
   711
		input_text->appendText(s);
sl@0
   712
		input_text->setBottomLine(INT_MAX);
sl@0
   713
	}
sl@0
   714
	if (res < 0) {
sl@0
   715
		input_text->appendText("hid_read() returned error\n");
sl@0
   716
		input_text->setBottomLine(INT_MAX);
sl@0
   717
	}
sl@0
   718
sl@0
   719
	getApp()->addTimeout(this, ID_TIMER,
sl@0
   720
		5 * timeout_scalar /*5ms*/);
sl@0
   721
	return 1;
sl@0
   722
}
sl@0
   723
sl@0
   724
long
sl@0
   725
MainWindow::onMacTimeout(FXObject *sender, FXSelector sel, void *ptr)
sl@0
   726
{
sl@0
   727
#ifdef __APPLE__
sl@0
   728
	check_apple_events();
sl@3
   729
sl@0
   730
	getApp()->addTimeout(this, ID_MAC_TIMER,
sl@0
   731
		50 * timeout_scalar /*50ms*/);
sl@0
   732
#endif
sl@0
   733
sl@0
   734
	return 1;
sl@0
   735
}
sl@0
   736
sl@6
   737
/**
sl@6
   738
sl@6
   739
*/
sl@6
   740
long MainWindow::onSelectFont(FXObject *sender, FXSelector sel, void *ptr)
sl@0
   741
{
sl@6
   742
    FXFontDialog* dlg=new FXFontDialog(this,"Pick a font");
sl@6
   743
    dlg->execute();
sl@6
   744
    dlg->getFontSelection(iCurrentFontDesc);
sl@6
   745
    delete dlg;
sl@6
   746
    return 1;
sl@0
   747
}
sl@6
   748
sl@6
   749
sl@6
   750