Advertisement
Guest User

Untitled

a guest
Jul 12th, 2015
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.96 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import time, os, sys, signal, setproctitle, traceback
  5.  
  6. MIN_TEMP = 50 # fan starts to cool
  7. MAX_TEMP = 80 # fan cooling full power
  8.  
  9. def call_acpi(command):
  10.     acpi_call = open('/proc/acpi/call', 'w')
  11.     acpi_call.write(command)
  12.     acpi_call.close()
  13.    
  14.     # Response
  15.     acpi_call = open('/proc/acpi/call')
  16.     response = acpi_call.read()
  17.     acpi_call.close()
  18.    
  19.     return response
  20.  
  21. def set_fan(speed, fan=1):
  22.     if speed < 0:
  23.         speed = 0
  24.     elif speed > 100:
  25.         speed = 100
  26.     raw_speed = round(speed * 2.55)
  27.     call_acpi('\_SB.PCI0.LPCB.EC0.SFNV %d %s' % (fan, raw_speed))
  28.    
  29.     print('Set fan speed to %d %%' % speed)
  30.    
  31. def get_cpu_temp():
  32.     return int(call_acpi('\_SB.PCI0.LPCB.EC0.ECPU')[:4], 16)
  33.  
  34. # you can adjust the fan behavior with any mathematic function
  35. # for example you can use a square function "return temp**2* or cubic "return temp**3"
  36. def scale_temp(temp):
  37.     return temp
  38.  
  39. def handle_fan(current_temp):
  40.     temp_range = MAX_TEMP - MIN_TEMP
  41.     norm_temp = current_temp - MIN_TEMP if (current_temp - MIN_TEMP) > 0 else 0
  42.            
  43.     scaled_temp = scale_temp(norm_temp)
  44.     scaled_border = scale_temp(temp_range)
  45.    
  46.     fan_speed = 100/scaled_border * scaled_temp # in percent
  47.        
  48.     print("Current CPU temperature %d°" % current_temp)
  49.    
  50.     set_fan(fan_speed, 1)
  51.     set_fan(fan_speed, 2)
  52.    
  53. def cleanup(signal=None, frame=None):
  54.     call_acpi('\_SB.PCI0.LPCB.EC0.SFNV 0 0')
  55.     call_acpi('\_SB.ATKD.QMOD 0x02')
  56.    
  57.     print('\nReset fan control to automatic. Exiting...')
  58.     sys.exit(0)
  59.  
  60. def main():
  61.     signal.signal(signal.SIGTERM, cleanup)
  62.     try:
  63.         while True:
  64.             handle_fan(get_cpu_temp())
  65.             time.sleep(2)
  66.     except:
  67.         traceback.print_exc()
  68.     finally:
  69.         cleanup()
  70.  
  71. if __name__=='__main__':
  72.     # TODO: only one instance
  73.     setproctitle.setproctitle('fancontrol')
  74.     main(
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement