-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrtleds.py
More file actions
205 lines (179 loc) · 7.04 KB
/
Copy pathrtleds.py
File metadata and controls
205 lines (179 loc) · 7.04 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
"""
rtleds 1.0
David Vilchis
"""
# Importing required modules
from sys import exit
import os
#Print available user LEDs in current NI Linux RT device ***************
def PrintTargetLEDs():
#Search LEDs brightness files
LEDs = os.popen('find /sys/devices -type f -name brightness | grep -i leds/nilrt:').read()
LEDs = LEDs.splitlines()
#Get LEDs common path
common_path = os.path.commonprefix(LEDs)
#Filter RT target user LEDs
userLEDs = {"user0":['OFF'], "user1":['OFF'], "user2":['OFF']}
colors = ["red","green","yellow"]
#Search user match in path
for key in userLEDs.keys():
#Search color match in path
for path in LEDs:
if key in path:
for color in colors:
if color in path:
userLEDs[key].append(color)
break
#Print Results
c = 0
for key in userLEDs.keys():
if len(userLEDs[key]) > 1:
print("{} >> {}".format(key,userLEDs[key]))
else:
c += 1
if c == 3:
print("This target does not support User LEDs")
# USER LEDs CLASS ******************************************************
class RT_LED:
def __init__(self):
"""
Initialize LED class attributes
"""
self.values = {0:"off",1:"green",2:"yellow"}
self.value = 0
self.led = ""
self.leds_path = self._GetLEDsPath()
def __call__(self, value):
"""
Change RT LED status on each isntance call
"""
if value == 0:
self._TurnOFF()
else:
self._ChangeStatus(value)
#PRIVATE --------------------------------------------------------
def _GetLEDsPath(self):
"""
Search and saves RT LEDs files location in the RT OS
"""
#Look for brightness files related to RT target LEDs
BrightnessPath = os.popen('find /sys/devices -type f -name brightness').read()
BrightnessPath = BrightnessPath.splitlines()
#Return common directory of user LEDs
LEDsPath = []
for pad in BrightnessPath:
if pad.find("leds/nilrt:") != -1:
LEDsPath.append(pad)
return os.path.commonprefix(LEDsPath)
def _ValidateValue(self, value):
"""
Validates input value is supported on RT target
"""
val = self.values.get(value, "0")
try:
if isinstance(self, PXIe_user1) or isinstance(self, PXIe_user2):
#Build path for PXIe targets
path = "{}{}:{}/brightness".format(self.leds_path,val,self.led)
else:
#Build path for RIO targets
path = "{}{}:{}/brightness".format(self.leds_path,self.led,val)
#Read file to validate correct LED color
file = open(path, "r")
file.close()
#Error Handling xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
except:
if value < 0 or value > 2:
#print(f'Selected value is out of defined colors <{value}>, please select a valid color: 0->OFF 1->GREEN 2->YELLOW')
print("Selected value is out of defined colors <{}>, please select a valid color: 0->OFF 1->GREEN 2->YELLOW".format(value))
else:
#print(f'The RT target does not support <{self.values[value]}> color in LED <{self.led}>')
print("The RT target does not support <{}> color in LED <{}>".format(self.values[value],self.led))
exit()
#xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
def _ValidateLED(self):
try:
"""
Verify whether class is for PXIe or RIO device
"""
if isinstance(self, PXIe_user1) or isinstance(self, PXIe_user2):
#Build path file for PXIe targets
led_path = "{}green:{}/brightness".format(self.leds_path,self.led)
else:
#Build path for RIO targets
led_path = "{}{}:green/brightness".format(self.leds_path,self.led)
#Read file to validate correct LED color
file = open(led_path, "r")
file.read()
file.close()
#Error Handling xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
except:
print("This RT target does not support LED <{}>".format(self.led))
exit()
#xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
def _TurnOFF(self):
"""
Turns off both GREEN and YELLOW values of selected LED
"""
if isinstance(self, PXIe_user1) or isinstance(self, PXIe_user2):
#Build path for PXIe targets
g_path = "{}green:{}/brightness".format(self.leds_path,self.led)
y_path = "{}yellow:{}/brightness".format(self.leds_path,self.led)
else:
#Build path for RIO targets
g_path = "{}{}:green/brightness".format(self.leds_path,self.led)
y_path = "{}{}:yellow/brightness".format(self.leds_path,self.led)
#Turn OFF green and yellow colors of selected LED
leds = [g_path, y_path]
for led_path in leds:
try:
#print(led_path)
file = open(led_path, "w")
file.write("0")
file.close()
except:
pass
def _ChangeStatus(self, value):
"""
Updates selected LED status: OFF GREEN YELLOW
"""
#Validate input LED color value
self._ValidateValue(value)
if isinstance(self, PXIe_user1) or isinstance(self, PXIe_user2):
#Build path for PXIe targets
led_path = "{}{}:{}/brightness".format(self.leds_path,self.values[value],self.led)
else:
#Build path for RIO targets
led_path = "{}{}:{}/brightness".format(self.leds_path,self.led,self.values[value])
#print(led_path)
file = open(led_path, "w")
file.write("1")
file.close()
#INHERITED CLASSES *******************************************************
class RIO_user1(RT_LED):
def __init__(self):
super().__init__()
self.led = "user1"
self._ValidateLED()
def __call__(self, value):
return super().__call__(value)
class RIO_user2(RT_LED):
def __init__(self):
super().__init__()
self.led = "user2"
self._ValidateLED()
def __call__(self, value):
return super().__call__(value)
class PXIe_user1(RT_LED):
def __init__(self):
super().__init__()
self.led = "user1"
self._ValidateLED()
def __call__(self, value):
return super().__call__(value)
class PXIe_user2(RT_LED):
def __init__(self):
super().__init__()
self.led = "user2"
self._ValidateLED()
def __call__(self, value):
return super().__call__(value)