|
| 1 | +# Fan Shim for Raspberry Pi |
| 2 | + |
| 3 | +[](https://travis-ci.com/pimoroni/fanshim-python) |
| 4 | +[](https://coveralls.io/github/pimoroni/fanshim-python?branch=master) |
| 5 | +[](https://pypi.python.org/pypi/fanshim) |
| 6 | +[](https://pypi.python.org/pypi/fanshim) |
| 7 | + |
| 8 | +# Installing |
| 9 | + |
| 10 | +Stable library from PyPi: |
| 11 | + |
| 12 | +* Just run `sudo pip install fanshim` |
| 13 | + |
| 14 | +Latest/development library from GitHub: |
| 15 | + |
| 16 | +* `git clone https://github.com/pimoroni/fanshim-python` |
| 17 | +* `cd fanshim-python` |
| 18 | +* `sudo ./install.sh` |
| 19 | + |
| 20 | +# Reference |
| 21 | + |
| 22 | +You should first set up an instance of the `FANShim` class, eg: |
| 23 | + |
| 24 | +```python |
| 25 | +from fanshim import FanShim |
| 26 | +fanshim = FanShim() |
| 27 | +``` |
| 28 | + |
| 29 | +## Fan |
| 30 | + |
| 31 | +Turn the fan on with: |
| 32 | + |
| 33 | +```python |
| 34 | +fanshim.set_fan(True) |
| 35 | +``` |
| 36 | + |
| 37 | +Turn it off with: |
| 38 | + |
| 39 | +```python |
| 40 | +fanshim.set_fan(False) |
| 41 | +``` |
| 42 | + |
| 43 | +You can also toggle the fan with: |
| 44 | + |
| 45 | +```python |
| 46 | +fanshim.toggle_fan() |
| 47 | +``` |
| 48 | + |
| 49 | +You can check the status of the fan with: |
| 50 | + |
| 51 | +```python |
| 52 | +fanshim.get_fan() # returns 1 for 'on', 0 for 'off' |
| 53 | +``` |
| 54 | + |
| 55 | +## LED |
| 56 | + |
| 57 | +Fan Shim includes one RGB APA-102 LED. |
| 58 | + |
| 59 | +Set it to any colour with: |
| 60 | + |
| 61 | +```python |
| 62 | +fanshim.set_light(r, g, b) |
| 63 | +``` |
| 64 | + |
| 65 | +Arguments r, g and b should be numbers between 0 and 255 that describe the colour you want. |
| 66 | + |
| 67 | +For example, full red: |
| 68 | + |
| 69 | +``` |
| 70 | +fanshim.set_light(255, 0, 0) |
| 71 | +``` |
| 72 | + |
| 73 | +## Button |
| 74 | + |
| 75 | +Fan Shim includes a button, you can bind actions to press, release and hold events. |
| 76 | + |
| 77 | +Do something when the button is pressed: |
| 78 | + |
| 79 | +```python |
| 80 | +@fanshim.on_press() |
| 81 | +def button_pressed(): |
| 82 | + print("The button has been pressed!") |
| 83 | +``` |
| 84 | + |
| 85 | +Or when it has been released: |
| 86 | + |
| 87 | +```python |
| 88 | +@fanshim.on_release() |
| 89 | +def button_released(was_held): |
| 90 | + print("The button has been pressed!") |
| 91 | +``` |
| 92 | + |
| 93 | +Or when it's been pressed long enough to trigger a hold: |
| 94 | + |
| 95 | +```python |
| 96 | +fanshim.set_hold_time(2.0) |
| 97 | + |
| 98 | +@fanshim.on_hold() |
| 99 | +def button_held(): |
| 100 | + print("The button was held for 2 seconds") |
| 101 | +``` |
| 102 | + |
| 103 | +The function you bind to `on_release()` is passed a `was_held` parameter, |
| 104 | +this lets you know if the button was held down for longer than the configured |
| 105 | +hold time. If you want to bind an action to "press" and another to "hold" you |
| 106 | +should check this flag and perform your action in the `on_release()` handler: |
| 107 | + |
| 108 | +```python |
| 109 | +@fanshim.on_release() |
| 110 | +def button_released(was_held): |
| 111 | + if was_held: |
| 112 | + print("Long press!") |
| 113 | + else: |
| 114 | + print("Short press!") |
| 115 | +``` |
| 116 | + |
| 117 | +To configure the amount of time the button should be held (in seconds), use: |
| 118 | + |
| 119 | +```python |
| 120 | +fanshim.set_hold_time(number_of_seconds) |
| 121 | +``` |
| 122 | + |
| 123 | +If you need to stop Fan Shim from polling the button, use: |
| 124 | + |
| 125 | +```python |
| 126 | +fanshim.stop_polling() |
| 127 | +``` |
| 128 | + |
| 129 | +You can start it again with: |
| 130 | + |
| 131 | +```python |
| 132 | +fanshim.start_polling() |
| 133 | +``` |
| 134 | + |
| 135 | + |
| 136 | +0.0.3 |
| 137 | +----- |
| 138 | + |
| 139 | +* Fix: lower polling frequency and make customisable, for PR #6 |
| 140 | + |
| 141 | +0.0.2 |
| 142 | +----- |
| 143 | + |
| 144 | +* Fix: Fix error on exit |
| 145 | + |
| 146 | +0.0.1 |
| 147 | +----- |
| 148 | + |
| 149 | +* Initial Release |
0 commit comments