Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixes #7

  • Loading branch information...
commit 9d6e5bf6b5c0d23dd96975e19e83945a8b3bbb62 1 parent b593179
@ejholmes authored
Showing with 40 additions and 28 deletions.
  1. +1 −1  Makefile
  2. +2 −2 cdevice.cpp
  3. +27 −15 device.cpp
  4. +10 −10 openfocus.h
View
2  Makefile
@@ -58,4 +58,4 @@ $(OUTPUT): $(OBJECTS)
$(CPP) -shared $(LDFLAGS) $(OBJECTS) $(USBLIBS)
clean:
- rm -f *.o $(OUTPUT) *.exe *.a *.dll *.lib *.so
+ rm -f *.o $(OUTPUT) *.exe *.a *.dll *.lib *.so *.so*
View
4 cdevice.cpp
@@ -29,8 +29,8 @@ extern "C" {
int device_halt(Device *device) { return device->Halt(); }
void device_reboot_to_bootloader(Device *device) { device->RebootToBootloader(); }
int device_set_position(Device *device, unsigned short position) { return device->SetPosition(position); }
- int device_get_temperature(Device *device, double *temperature) { return device->GetTemperature(temperature); }
- int device_get_position(Device *device, unsigned short *position) { return device->GetPosition(position); }
+ double device_get_temperature(Device *device) { return device->GetTemperature(); }
+ unsigned short device_get_position(Device *device) { return device->GetPosition(); }
int device_reverse_direction(Device *device, int reverse) { return device->ReverseDirection(reverse); }
int device_is_moving(Device *device) { return device->IsMoving(); }
int device_is_connected(Device *device) { return device->IsConnected(); }
View
42 device.cpp
@@ -29,6 +29,7 @@ OpenFocus::Device::Device()
TemperatureCoefficient = 0.0;
TempCompEnabled = false;
LastTemperature = 0.0;
+ have_error = false;
}
bool OpenFocus::Device::Connect(const char *serial)
@@ -100,24 +101,30 @@ int OpenFocus::Device::ReverseDirection(bool reverse)
return usb_control_msg(device, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, USB_RQ_REVERSE, (int)((reverse)?1:0), 0, NULL, 0, 5000);
}
-int OpenFocus::Device::GetTemperature(double *temperature)
+double OpenFocus::Device::GetTemperature()
{
- int retval;
unsigned short adc;
- retval = usb_control_msg(device, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, USB_RQ_GET_TEMPERATURE, 0, 0, (char *)&adc, sizeof(unsigned short), 5000);
+ double temperature;
- if (retval < 0)
- return retval;
+ int retval = usb_control_msg(device, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, USB_RQ_GET_TEMPERATURE, 0, 0, (char *)&adc, sizeof(temperature), 5000);
- *temperature = (5.00 * (double)adc * 100.00) / 1024.00; /* Convert ADC value to absolute temperature */
- *temperature = round(*temperature * 100.00) / 100.00; /* Round to two decimal places */
+ if (retval != sizeof(temperature))
+ have_error = true;
- return retval;
+ temperature = (5.00 * (double)adc * 100.00) / 1024.00; /* Convert ADC value to absolute temperature */
+ temperature = round(temperature * 100.00) / 100.00; /* Round to two decimal places */
+
+ return temperature;
}
-int OpenFocus::Device::GetPosition(unsigned short *position)
+unsigned short OpenFocus::Device::GetPosition()
{
- return usb_control_msg(device, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, USB_RQ_GET_POSITION, 0, 0, (char *)position, sizeof(unsigned short), 5000);
+ unsigned short position;
+ int retval = usb_control_msg(device, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, USB_RQ_GET_POSITION, 0, 0, (char *)&position, sizeof(position), 5000);
+ if (retval != sizeof(position))
+ have_error = true;
+
+ return position;
}
int OpenFocus::Device::GetCapabilities(unsigned char *capabilities)
@@ -128,11 +135,9 @@ int OpenFocus::Device::GetCapabilities(unsigned char *capabilities)
void OpenFocus::Device::DoTempComp()
{
if (TempCompEnabled && !IsMoving() && (LastTemperature != 0.0)) {
- double CurrentTemperature;
- GetTemperature(&CurrentTemperature);
+ double CurrentTemperature = GetTemperature();
- unsigned short position;
- GetPosition(&position);
+ unsigned short position = GetPosition();
double delta = CurrentTemperature - LastTemperature;
MoveTo((unsigned short)(position + (TemperatureCoefficient * delta)));
@@ -144,7 +149,7 @@ void OpenFocus::Device::DoTempComp()
void OpenFocus::Device::EnableTemperatureCompensation()
{
TempCompEnabled = true;
- GetTemperature(&LastTemperature);
+ LastTemperature = GetTemperature();
}
void OpenFocus::Device::DisableTemperatureCompensation()
@@ -156,3 +161,10 @@ bool OpenFocus::Device::TemperatureCompensationEnabled()
{
return TempCompEnabled;
}
+
+bool OpenFocus::Device::HaveError()
+{
+ bool retval = have_error;
+ have_error = false;
+ return retval;
+}
View
20 openfocus.h
@@ -71,6 +71,8 @@ namespace OpenFocus
bool TempCompEnabled;
double LastTemperature;
+
+ bool have_error;
public:
Device();
/* Connect to the device */
@@ -89,9 +91,9 @@ namespace OpenFocus
/* Set the current position on the device */
int SetPosition(unsigned short position);
/* Gets the current temperature read by the LM335 on the device in degrees kelvin */
- int GetTemperature(double *temperature);
+ double GetTemperature();
/* Gets the current position from device */
- int GetPosition(unsigned short *position);
+ unsigned short GetPosition();
/* Reverses the direction that the focuser rotates */
int ReverseDirection(bool reverse);
@@ -125,6 +127,12 @@ namespace OpenFocus
/* Current temperature coefficient to use during temperature compensation */
double TemperatureCoefficient;
+
+ /* Call this to check the return value of the last Get* function.
+ *
+ * ex. GetTemperature(); if (!HaveError()) { printf("Success!"); }
+ * */
+ bool HaveError();
};
/* Functions for communicating with the bootloader. DON'T USE THIS IF YOU DON'T KNOW WHAT YOU'RE DOING */
@@ -193,14 +201,6 @@ namespace OpenFocus
#ifdef __cplusplus
extern "C" {
#endif
- int device_connect(Device **device);
- int device_connect_serial(Device **device, const char *serial);
- void device_disconnect(Device *device);
- int device_move_to(Device *device, unsigned short position);
- int device_halt(Device *device);
- void device_reboot_to_bootloader(Device *device);
- int device_set_position(Device *device, unsigned short position);
- int device_get_temperature(Device *device, double *temperature);
int device_get_position(Device *device, unsigned short *position);
int device_reverse_direction(Device *device, int reverse);
int device_is_moving(Device *device);
Please sign in to comment.
Something went wrong with that request. Please try again.