- //So that another call to PWM_Unexport is OK.
- pwm->fd_polarity = pwm->fd_run = -1;
- pwm->file_period = pwm->file_duty = NULL;
+ // Close the file descriptors
+ close(pwm->fd_polarity);
+ //Stop it, if it's still running
+ pwrite(pwm->fd_run, "0", 1, 0);
+ close(pwm->fd_run);
+ fclose(pwm->file_period);
+ fclose(pwm->file_duty);
+
+ pwm->initialised = false;
+
+ // Try unexport the pin, doesn't matter if it's already unexported.
+ sprintf(g_buffer, "%s/unexport", PWM_DEVICE_PATH);
+ FILE * file_unexport = fopen(g_buffer, "w");
+ if (file_unexport == NULL)
+ {
+ Abort("Couldn't open %s to unexport PWM pin %d - %s", g_buffer, pin, strerror(errno));
+ }
+ fprintf(file_unexport, "%d\n", pin);
+ fclose(file_unexport);