-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcableControl.py
66 lines (59 loc) · 2.42 KB
/
cableControl.py
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
#libraries and warnings
import time
import random
import logging
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
#logger Config, for log msg format: logRec attributes pyth
logger= logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
file_handler = logging.FileHandler('The cable control logger just started')
file_handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s:%(levelname)s:%(funcName)s:%(message)s')
stream_handler=logging.StreamHandler()
logger.addHandler(stream_handler)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
#GPIO PINS Pinout for cables, list with the cable name
cable_1 = [2, 3, 4, 17]
cable_2 = [19,26,20,21]
class cable_control:
def turnOn(self, cable):
#turns ON a cable, input is a list with the cable pins
t1='turning ON Cable'
logger.debug(t1)
for i in cable:
GPIO.setup(i, GPIO.OUT)
GPIO.output(i, GPIO.HIGH)
GPIO.output(i, GPIO.LOW)
time.sleep(10)
def turnOff(offcable=ALL):
#turns off a cable, input is a list with the cable pins
for i in offcable:
GPIO.setup(i, GPIO.OUT)
GPIO.output(i, GPIO.HIGH)
def switchTime(self, sleep, cables, testing_time):
#turns on several defined cables interchangeably, input is a list of the cables, switching time in seconds and run time in minutes
logger.debug('starting the switching function')
t_end= time.time() + testing_time*60
while time.time() < t_end:
for cable in cables:
GPIO.setup(cable,GPIO.OUT)
self.turnOn(cable)
time.sleep(sleep)
self.turnOff(cable)
logger.debug('Intercable test finished')
def switchRandom(self, cables,max_limit, testing_time):
#turns on several defined cables interchangeably with random times, input is the cables, runtime in minutes and max time of switching in seconds
logger.debug('Random test starting')
t_end= time.time() + testing_time*60
while time.time() < t_end:
rand=random.randint(1,max_limit) #random pin
for cable in cables:
GPIO.setup(cable,GPIO.OUT)
self.turnOn(cable)
time.sleep(rand)
self.turnOff(cable)
logger.debug('Random test finished')
# NEVER FORGET TO GPIO.cleanup() AT THE END FOR AN Exit cleanout