Sorting out connected state management with our new VFD object.
authorsl
Thu, 22 May 2014 08:17:14 +0200
changeset 15e5b84f315be7
parent 14 4a5538e0ccbf
child 16 21033ded8f86
Sorting out connected state management with our new VFD object.
inc/FutabaVfd.h
inc/HidDevice.h
inc/MainWindow.h
src/HidDevice.cpp
src/test.cpp
     1.1 --- a/inc/FutabaVfd.h	Thu May 22 07:50:02 2014 +0200
     1.2 +++ b/inc/FutabaVfd.h	Thu May 22 08:17:14 2014 +0200
     1.3 @@ -79,7 +79,6 @@
     1.4  	virtual int HeightInPixels()=0;
     1.5  	virtual void SetPixel(int aX, int aY, bool aOn)=0;
     1.6  	virtual void SetAllPixels(bool aOn)=0;
     1.7 -
     1.8  	};
     1.9  
    1.10  /**
     2.1 --- a/inc/HidDevice.h	Thu May 22 07:50:02 2014 +0200
     2.2 +++ b/inc/HidDevice.h	Thu May 22 08:17:14 2014 +0200
     2.3 @@ -15,6 +15,7 @@
     2.4  class HidDevice
     2.5      {
     2.6  public:
     2.7 +    HidDevice();
     2.8      int Open(const char* aPath);
     2.9      int Open(unsigned short aVendorId, unsigned short aProductId, const wchar_t* aSerialNumber);
    2.10      void Close();
     3.1 --- a/inc/MainWindow.h	Thu May 22 07:50:02 2014 +0200
     3.2 +++ b/inc/MainWindow.h	Thu May 22 08:17:14 2014 +0200
     3.3 @@ -56,7 +56,7 @@
     3.4  
     3.5      long onConnect(FXObject *sender, FXSelector sel, void *ptr);
     3.6      long onDisconnect(FXObject *sender, FXSelector sel, void *ptr);
     3.7 -    long onRescan(FXObject *sender, FXSelector sel, void *ptr);
     3.8 +    long onScan(FXObject *sender, FXSelector sel, void *ptr);
     3.9      long onSendOutputReport(FXObject *sender, FXSelector sel, void *ptr);
    3.10      long onSendFeatureReport(FXObject *sender, FXSelector sel, void *ptr);
    3.11      long onGetFeatureReport(FXObject *sender, FXSelector sel, void *ptr);
    3.12 @@ -81,6 +81,10 @@
    3.13      void SetPixelBlock(int aX, int aY, int aHeight, int aSize, unsigned char aValue);
    3.14  
    3.15  private:
    3.16 +    void SetConnectedStates();
    3.17 +    bool IsConnected();
    3.18 +
    3.19 +private:
    3.20      FXList *device_list;
    3.21      FXButton *connect_button;
    3.22      FXButton *disconnect_button;
     4.1 --- a/src/HidDevice.cpp	Thu May 22 07:50:02 2014 +0200
     4.2 +++ b/src/HidDevice.cpp	Thu May 22 08:17:14 2014 +0200
     4.3 @@ -10,6 +10,11 @@
     4.4  // class HidDevice
     4.5  //
     4.6  
     4.7 +HidDevice::HidDevice():iHidDevice(NULL)
     4.8 +    {
     4.9 +
    4.10 +    }
    4.11 +
    4.12  /**
    4.13  */
    4.14  int HidDevice::Open(const char* aPath)
     5.1 --- a/src/test.cpp	Thu May 22 07:50:02 2014 +0200
     5.2 +++ b/src/test.cpp	Thu May 22 08:17:14 2014 +0200
     5.3 @@ -29,7 +29,7 @@
     5.4  FXDEFMAP(MainWindow) MainWindowMap [] = {
     5.5  	FXMAPFUNC(SEL_COMMAND, MainWindow::ID_CONNECT, MainWindow::onConnect ),
     5.6  	FXMAPFUNC(SEL_COMMAND, MainWindow::ID_DISCONNECT, MainWindow::onDisconnect ),
     5.7 -	FXMAPFUNC(SEL_COMMAND, MainWindow::ID_RESCAN, MainWindow::onRescan ),
     5.8 +	FXMAPFUNC(SEL_COMMAND, MainWindow::ID_RESCAN, MainWindow::onScan ),
     5.9  	FXMAPFUNC(SEL_COMMAND, MainWindow::ID_SEND_OUTPUT_REPORT, MainWindow::onSendOutputReport ),
    5.10  	FXMAPFUNC(SEL_COMMAND, MainWindow::ID_SEND_FEATURE_REPORT, MainWindow::onSendFeatureReport ),
    5.11  	FXMAPFUNC(SEL_COMMAND, MainWindow::ID_GET_FEATURE_REPORT, MainWindow::onGetFeatureReport ),
    5.12 @@ -122,20 +122,6 @@
    5.13      iButtonSetAllPixels = new FXButton(matrix, "Set All Pixels", NULL, this, ID_FUTABA_SET_ALL_PIXELS, BUTTON_NORMAL|LAYOUT_FILL_X);
    5.14      //
    5.15  
    5.16 -	//
    5.17 -	iButtonClearDisplay->disable();
    5.18 -	iButtonDimming->disable();
    5.19 -	iButtonDisplayDataInput->disable();
    5.20 -	iButtonReadId->disable();
    5.21 -	iButtonReadFirmwareRevision->disable();
    5.22 -	iButtonPowerSupplyMonitor->disable();
    5.23 -    iTextFieldX->disable();
    5.24 -    iTextFieldY->disable();
    5.25 -    iButtonSetPixel->disable();
    5.26 -	iButtonResetPixel->disable();
    5.27 -    iButtonSetAllPixels->disable();
    5.28 -	//
    5.29 -
    5.30  	// Output Group Box
    5.31  	new FXHorizontalFrame(vf);
    5.32  	gb = new FXGroupBox(vf, "Output", FRAME_GROOVE|LAYOUT_FILL_X);
    5.33 @@ -171,6 +157,7 @@
    5.34  	input_text->setEditable(false);
    5.35  	new FXButton(innerVF, "Clear", NULL, this, ID_CLEAR, BUTTON_NORMAL|LAYOUT_RIGHT);
    5.36  
    5.37 +    SetConnectedStates();
    5.38  
    5.39  }
    5.40  
    5.41 @@ -194,13 +181,14 @@
    5.42  	FXMainWindow::create();
    5.43  	show();
    5.44  
    5.45 -	onRescan(NULL, 0, NULL);
    5.46 +	onScan(NULL, 0, NULL);
    5.47  
    5.48 -	//Just testing our new VFD class
    5.49 +	//Try to connect to our VFD from start-up
    5.50  	if (iVfd01.Open())
    5.51          {
    5.52  	    iVfd01.SetAllPixels(true);
    5.53  	    iVfd01.Close();
    5.54 +        SetConnectedStates();
    5.55          }
    5.56  
    5.57  #ifdef __APPLE__
    5.58 @@ -236,76 +224,110 @@
    5.59  
    5.60  	hid_set_nonblocking(connected_device, 1);
    5.61  
    5.62 -	getApp()->addTimeout(this, ID_TIMER,
    5.63 -		5 * timeout_scalar /*5ms*/);
    5.64 -
    5.65 -	FXString s;
    5.66 -	s.format("Connected to: %04hx:%04hx -", device_info->vendor_id, device_info->product_id);
    5.67 -	s += FXString(" ") + device_info->manufacturer_string;
    5.68 -	s += FXString(" ") + device_info->product_string;
    5.69 -	connected_label->setText(s);
    5.70 -	output_button->enable();
    5.71 -	feature_button->enable();
    5.72 -	get_feature_button->enable();
    5.73 -	connect_button->disable();
    5.74 -	disconnect_button->enable();
    5.75 -	input_text->setText("");
    5.76  	//
    5.77 -	iButtonClearDisplay->enable();
    5.78 -	iButtonDimming->enable();
    5.79 -	iButtonDisplayDataInput->enable();
    5.80 -	iButtonReadId->enable();
    5.81 -	iButtonReadFirmwareRevision->enable();
    5.82 -	iButtonPowerSupplyMonitor->enable();
    5.83 -    iTextFieldX->enable();
    5.84 -    iTextFieldY->enable();
    5.85 -    iButtonSetPixel->enable();
    5.86 -	iButtonResetPixel->enable();
    5.87 -    iButtonSetAllPixels->enable();
    5.88 -
    5.89 -	//
    5.90 -	iOutputReportBuffer=new unsigned char[KFutabaMaxHidReportSize]; //TODO: use connected_device->output_report_length
    5.91 +	iOutputReportBuffer=new unsigned char[KFutabaMaxHidReportSize];
    5.92  
    5.93  	return 1;
    5.94  }
    5.95  
    5.96 +/**
    5.97 +Tell whether or not we are currently connected to an HID device.
    5.98 +*/
    5.99 +bool MainWindow::IsConnected()
   5.100 +    {
   5.101 +    return (connected_device||iVfd01.IsOpen());
   5.102 +    }
   5.103 +
   5.104 +/**
   5.105 +Update our UI states depending on whether or not we are connected to a device
   5.106 +*/
   5.107 +void MainWindow::SetConnectedStates()
   5.108 +    {
   5.109 +    if (IsConnected())
   5.110 +        {
   5.111 +        //Start pulling for input report
   5.112 +        getApp()->addTimeout(this, ID_TIMER, 5 * timeout_scalar /*5ms*/);
   5.113 +
   5.114 +        FXString s;
   5.115 +        //s.format("Connected to: %04hx:%04hx -", device_info->vendor_id, device_info->product_id);
   5.116 +        s.format("Connected to: ");
   5.117 +        if (connected_device)
   5.118 +            {
   5.119 +            wchar_t string[256];
   5.120 +            hid_get_manufacturer_string(connected_device,string,sizeof(string));
   5.121 +            s += FXString(" ") + string;
   5.122 +            hid_get_product_string(connected_device,string,sizeof(string));
   5.123 +            s += FXString(" ") + string;
   5.124 +            }
   5.125 +        else
   5.126 +            {
   5.127 +            //TODO: Missing function on our HidDevice class
   5.128 +            }
   5.129 +        connected_label->setText(s);
   5.130 +        output_button->enable();
   5.131 +        feature_button->enable();
   5.132 +        get_feature_button->enable();
   5.133 +        connect_button->disable();
   5.134 +        disconnect_button->enable();
   5.135 +        input_text->setText("");
   5.136 +        //
   5.137 +        iButtonClearDisplay->enable();
   5.138 +        iButtonDimming->enable();
   5.139 +        iButtonDisplayDataInput->enable();
   5.140 +        iButtonReadId->enable();
   5.141 +        iButtonReadFirmwareRevision->enable();
   5.142 +        iButtonPowerSupplyMonitor->enable();
   5.143 +        iTextFieldX->enable();
   5.144 +        iTextFieldY->enable();
   5.145 +        iButtonSetPixel->enable();
   5.146 +        iButtonResetPixel->enable();
   5.147 +        iButtonSetAllPixels->enable();
   5.148 +        }
   5.149 +    else
   5.150 +        {
   5.151 +        //Stop pulling for input report
   5.152 +        getApp()->removeTimeout(this, ID_TIMER);
   5.153 +        //
   5.154 +        connected_label->setText("Disconnected");
   5.155 +        output_button->disable();
   5.156 +        feature_button->disable();
   5.157 +        get_feature_button->disable();
   5.158 +        connect_button->enable();
   5.159 +        disconnect_button->disable();
   5.160 +        //
   5.161 +        iButtonClearDisplay->disable();
   5.162 +        iButtonDimming->disable();
   5.163 +        iButtonDisplayDataInput->disable();
   5.164 +        iButtonReadId->disable();
   5.165 +        iButtonReadFirmwareRevision->disable();
   5.166 +        iButtonPowerSupplyMonitor->disable();
   5.167 +        iTextFieldX->disable();
   5.168 +        iTextFieldY->disable();
   5.169 +        iButtonSetPixel->disable();
   5.170 +        iButtonResetPixel->disable();
   5.171 +        iButtonSetAllPixels->disable();
   5.172 +        //
   5.173 +        }
   5.174 +
   5.175 +    }
   5.176 +
   5.177  long
   5.178  MainWindow::onDisconnect(FXObject *sender, FXSelector sel, void *ptr)
   5.179  {
   5.180  	hid_close(connected_device);
   5.181  	connected_device = NULL;
   5.182 -	connected_label->setText("Disconnected");
   5.183 -	output_button->disable();
   5.184 -	feature_button->disable();
   5.185 -	get_feature_button->disable();
   5.186 -	connect_button->enable();
   5.187 -	disconnect_button->disable();
   5.188 -
   5.189 -	getApp()->removeTimeout(this, ID_TIMER);
   5.190 -
   5.191 -	//
   5.192 -	iButtonClearDisplay->disable();
   5.193 -	iButtonDimming->disable();
   5.194 -	iButtonDisplayDataInput->disable();
   5.195 -	iButtonReadId->disable();
   5.196 -	iButtonReadFirmwareRevision->disable();
   5.197 -	iButtonPowerSupplyMonitor->disable();
   5.198 -    iTextFieldX->disable();
   5.199 -    iTextFieldY->disable();
   5.200 -    iButtonSetPixel->disable();
   5.201 -	iButtonResetPixel->disable();
   5.202 -    iButtonSetAllPixels->disable();
   5.203 -	//
   5.204 -
   5.205 +    iVfd01.Close();
   5.206  
   5.207  	delete iOutputReportBuffer;
   5.208  	iOutputReportBuffer=NULL;
   5.209  
   5.210 +    SetConnectedStates();
   5.211 +
   5.212  	return 1;
   5.213  }
   5.214  
   5.215  long
   5.216 -MainWindow::onRescan(FXObject *sender, FXSelector sel, void *ptr)
   5.217 +MainWindow::onScan(FXObject *sender, FXSelector sel, void *ptr)
   5.218  {
   5.219  	struct hid_device_info *cur_dev;
   5.220