temp-fan-control/scripts/pwm.py

97 lines
2.3 KiB
Python
Executable File

#! /usr/bin/env python3
#import RPIO
import RPi.GPIO as GPIO
import time
from datetime import datetime
from datetime import timedelta
switch = 17
pwm = 18
fan_speed =18
#RPIO.setup(switch, RPIO.OUT)
#RPIO.output(switch, True)
# RPIO.PWM.setup()
#fan = RPIO.PWM.Servo(0, 40, 1)
GPIO.setmode(GPIO.BCM)
GPIO.setup(switch, GPIO.OUT)
GPIO.setup(pwm, GPIO.OUT)
fan = GPIO.PWM(pwm, 25000)
uswitch = 22
upwm = 23
GPIO.setup(uswitch, GPIO.OUT)
GPIO.setup(upwm, GPIO.OUT)
ufan = GPIO.PWM(pwm,25000)
# GPIO.setup(fan_speed, GPIO.IN, pull_up_down=GPIO.PUD_UP)
global last_ts
global current_ts
global diff
diff = timedelta()
last_ts = datetime.now()
current_ts = 0
global counter
counter = 0
def speed_callback(channel):
global last_ts
global current_ts
global diff
global counter
current_ts = datetime.now()
if current_ts != 0 and last_ts != 0:
diff = current_ts - last_ts
if diff.seconds < 1:
counter+=1
else:
rpm = (counter/2)*60
print("{} ||| {} ".format(counter, rpm), end="\r")
last_ts = current_ts
counter = 0
# print("{} ".format(rpm), end="\r")
# print("{:d} | \t{:.2f} | \t{} | \t{} | \t{}".format(diff.microseconds, rpm, diff, current_ts, last_ts), end="\r")
# GPIO.add_event_detect(fan_speed, GPIO.FALLING, callback=speed_callback)
import sys
try:
GPIO.output(switch, True)
GPIO.output(uswitch, True)
step = 10
c = 0
direction = 1
diff=timedelta()
fan.start(c)
ufan.start(c)
while True:
l = input().strip().split(" ")
if len(l) == 2:
if int(l[0]) < 0:
GPIO.output(switch, False)
else:
GPIO.output(switch, True)
fan.ChangeDutyCycle(100 - int(l[0]))
if int(l[1]) < 0:
GPIO.output(uswitch, False)
else:
GPIO.output(uswitch, True)
ufan.ChangeDutyCycle(100 - int(l[1]))
# if c == 100:
# direction = -1
# elif c == 0:
# direction = 1
# c += step*direction
# fan.ChangeDutyCycle(c)
# print("{} ".format(c))
# while True:
time.sleep(0.1)
except KeyboardInterrupt:
pass
finally:
#fan.stop()
#GPIO.output(switch, False)
GPIO.cleanup()