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