diff --git a/coincellhell.c b/coincellhell.c index 5417b23dcf32dffaf637ac03a2b9b43e9a2b41cf..42ed9d1bcc38e0f0d2213d4e0041502a7379a288 100644 --- a/coincellhell.c +++ b/coincellhell.c @@ -235,6 +235,18 @@ const uint8_t* coincellhell_get_fw_git_revision(struct coincellhell* hell) return gitrev; } +int coincellhell_reset(struct coincellhell* hell) +{ + usbshm_reopen(hell->priv); + for(int i = 0; i < 4; ++i) + { + int ret = coincellhell_set_enabled(hell, 0, false); + if(ret != 0) + return ret; + } + return 0; +} + void coincellhell_disconnect(struct coincellhell* hell) { usbshm_distroy(hell->priv); diff --git a/coincellhell/coincellhell.h b/coincellhell/coincellhell.h index 328249313d8ccacac94c0e10bd7b2fc12efd2698..3d483321814171032265b3ef8191e39061ff8a5d 100644 --- a/coincellhell/coincellhell.h +++ b/coincellhell/coincellhell.h @@ -117,7 +117,7 @@ int coincellhell_get_state(struct coincellhell* hell, uint8_t heater, struct hea /** * @brief Sets the enabled state for a give heater - * @param heater heater for which to set the temperature + * @param heater heater for which to set the enabled state * @param state A struct where the state will be stored * @return 0 on sucess and < 0 on failure */ @@ -153,6 +153,12 @@ int coincellhell_cancle_ramp(struct coincellhell* hell, uint8_t heater); */ int coincellhell_set_led(struct coincellhell* hell, bool on); +/** + * @brief resets the device + * @param hell pointer to a coincellhell struct + */ +int coincellhell_reset(struct coincellhell* hell); + /** * @brief Disconnects from the coincellhell */ diff --git a/usbshm.c b/usbshm.c index 42ed24866c1c31b0eded03384bb58ac5a5428ba0..78620727cdb3ffaa72e07e54166d5a541ca145d6 100644 --- a/usbshm.c +++ b/usbshm.c @@ -60,6 +60,8 @@ void usbshm_distroy(struct usbshm* instance) libusb_close(instance->priv->handle); free(instance->priv->buffer); free(instance->priv); + if(instance->serial) + free(instance->serial); if(--objectCounter == 0) { threadStop = true; @@ -92,6 +94,7 @@ int usbshm_init(struct usbshm* instance, void (*dataCallback)(uint8_t request, u instance->priv->buffer = NULL; instance->vendorID = 0; instance->productID = 0; + instance->serial = NULL; instance->dataCallback = dataCallback; instance->user_data = user_data; if(objectCounter == 0) @@ -156,6 +159,11 @@ int usbshm_open(struct usbshm* instance, int vendorID, int productID, const unsi { instance->vendorID = vendorID; instance->productID = productID; + if(serial) + { + instance->serial = calloc(strlen((char*)serial), 1); + memcpy(instance->serial, serial, strlen((char*)serial)); + } libusb_set_auto_detach_kernel_driver(instance->priv->handle, true); } else @@ -176,14 +184,14 @@ bool usbshm_usbshm_isOpen(struct usbshm* instance) void usbshm_reset(struct usbshm* instance) { - printf("Usb transfer failed with %u\n", instance->priv->transfer->status); libusb_reset_device(instance->priv->handle); } void usbshm_reopen(struct usbshm* instance) { + usbshm_reset(instance); libusb_close(instance->priv->handle); - usbshm_open(instance, instance->vendorID, instance->productID, NULL); + usbshm_open(instance, instance->vendorID, instance->productID, instance->serial); } int usbshm_writeControlTransfer(struct usbshm* instance, const uint8_t request, diff --git a/usbshm.h b/usbshm.h index d03ad2decb928da656653b8042ed295623c9534d..0f44eb2b9a302a3898f58fb18130bc1441de8a7c 100644 --- a/usbshm.h +++ b/usbshm.h @@ -45,6 +45,7 @@ struct usbshm { struct usbshm_priv* priv; int vendorID; int productID; + unsigned char* serial; void* user_data; void (*dataCallback)(uint8_t request, unsigned char* data, size_t length, void* user_data); };