- close(g_pwm[pin].fd_polarity);
- close(g_pwm[pin].fd_run);
- fclose(g_pwm[pin].file_period);
- fclose(g_pwm[pin].file_duty);
-
- //Unexport the pin
- sprintf(g_buffer, "%s/unexport", PWM_DEVICE_PATH);
- FILE * export = fopen(g_buffer, "w");
- if (export == NULL)
- {
- Abort("Couldn't open %s to unexport PWM pin %d - %s", g_buffer, pin, strerror(errno));
- }
-
- fprintf(export, "%d", pin);
- fclose(export);
-
-
+ if (pwm->fd_polarity != -1)
+ close(pwm->fd_polarity);
+ if (pwm->fd_run != -1)
+ close(pwm->fd_run);
+ if (pwm->file_period != NULL)
+ fclose(pwm->file_period);
+ if (pwm->file_duty != NULL)
+ fclose(pwm->file_duty);
+
+ //So that another call to PWM_Unexport is OK.
+ pwm->fd_polarity = pwm->fd_run = -1;
+ pwm->file_period = pwm->file_duty = NULL;