-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfan.c
89 lines (79 loc) · 3.58 KB
/
fan.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
* Author: Dino Ciuffetti - dam2000 at gmail dot com
* Home: https://github.com/dam2k/fanChat
* Date: 2019-10-27
* Whatis: Melopero's FanHat Raspberry Pi FAN controller daemon rethinked from scratch and written in C
* Work with: Melopero FAN HAT for Raspberry Pi 4
* https://www.melopero.com/shop/melopero-engineering/melopero-fan-hat-for-raspberry-pi-4/
* Requires: PiGpio library: http://abyz.me.uk/rpi/pigpio/ - apt update && apt install libpigpio-dev
* Replaces: official fan driver written in python: https://www.melopero.com/fan-hat/
* License: MIT License - https://opensource.org/licenses/MIT
*
* Notes:
* The Melopero FAN HAT for Raspberry Pi 4 is a cool (freddo!! :-)) fan driver for Raspberry Pi 4. It works using GPIO PIN 18 that
* has hardware driven PWM capabilities. This way they can handle fan speed changing the PIN's duty cycle.
* We continuously read the CPU temperature thanks to the /sys/class/thermal/thermal_zone0/temp file and we take care of cooling
* the CPU by setting up the right fan speed modulating the pin's PWM attached to the fan hat.
* Since I really hate when the fan is always on at low speed, I thinked of using a high and low watermarks.
* If the cpu's temperature is below the LW the fan will be shut down, and when the cpu's temperature raises above the HW the fan
* will come into play at the right speed. Also, there is a trigger timeout that will fire if the fan is down for more then a few
* minutes after the LW event. In this way we can cool down the RPI's temperature a little bit without reaching the HW or
* stressing us too much. Enjoy it!!
*/
/**
* Copyright 2019 Dino Ciuffetti - dam2000 at gmail dot com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
* IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "common.h"
#include "fan.h"
#include <pigpio.h>
/**
* Setup GPIO pin of the fan
*/
int fan_setup(void) {
int ret;
// Disable pigpio support of the fifo and socket interfaces.
ret=gpioCfgInterfaces(PI_DISABLE_FIFO_IF | PI_DISABLE_SOCK_IF);
if(ret<0) {
fprintf(stderr, "Fatal error on disabling GPIO library interfaces, not required to handle fan\n");
return -1;
}
ret=gpioInitialise();
if(ret<0) {
fprintf(stderr, "Fatal error on initializing GPIO library, required to handle fan\n");
return -1;
}
// GPIO 18 is setted to OUTPUT
gpioSetMode(18, PI_OUTPUT);
// shutting down fan
gpioPWM(18, 0);
return 0;
}
/**
* Stop fan and shut down GPIO
*/
void fan_shutdown(void) {
gpioPWM(18, 0);
// Stop DMA, release resources
gpioTerminate();
}
/**
* Set fan to p%
*/
void fan_set(unsigned short p) {
if(p<0) p=0;
if(p>100) p=100;
// 255 is 100%
gpioPWM(18, 255*p/100);
}