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