vsergeev / python-periphery

A pure Python 2/3 library for peripheral I/O (GPIO, LED, PWM, SPI, I2C, MMIO, Serial) in Linux.
MIT License
519 stars 139 forks source link

Improve clarity of errors when writing to pin opened as input #44

Closed CrazyIvan359 closed 3 years ago

CrazyIvan359 commented 3 years ago

The cdev and sysfs interfaces will throw errors (1: Operation not permitted or 22: Invalid argument for me) when attempting to write to a pin opened as an input. This PR improves clarity by catching attempts to write to a pin opened for input and raises a GPIOError with a clearer message. Similar checks also added to the poll and read_event methods.

Closes #43

vsergeev commented 3 years ago

Thanks, merged in 69bd36e. In addition to a few minor modifications, I decided to drop the checks in the sysfs implementation. I think it's a bit too much to open and read the direction file on every call to write() or poll() to check that it's the right direction. Ideally, that state would be cached, like it is in the character device implementation. But the sysfs implementation has avoided caching to be robust to external modifications (e.g. if another process modifies the GPIO in /sys).

CrazyIvan359 commented 3 years ago

I decided to drop the checks in the sysfs implementation. I think it's a bit too much to open and read the direction file on every call

Yeah that's quite the performance hit if you are doing a lot of writes in a short time, I did not see that the sysfs class was doing that.