Reading an android display mode monitor which gives the value of the plugged in resolution. I want the loop to break if it reads "null" multiple times in a row:
Display Mode: 720p60hz
Display Mode: 720p60hz
Display Mode: null
Display Mode: null
Display Mode: null
Display Mode: null
BREAK!
CODE
import time
import subprocess
while True:
z = subprocess.getoutput("adb shell cat /sys/class/display/mode")
time.sleep(.1)
print(f'Display Mode: {z}')
t1 = time.time()
t2 = time.time()
if z == 'null':
print(f't1 is :{t1}')
else:
continue
if z == 'null'
print(f't2 is :{t2}')
print('i am null')
if t2-t1 > .1:
break
CodePudding user response:
Your code is a bit hard to read because the indentation was left out, and indentation is a part of Python syntax. But here's my best guess for what you're looking for:
import time
import subprocess
THRESHOLD_BAD_DISPLAY_READS = 10 # Set this to the number of bad reads in a row that you can tolerate
num_bad_display_reads = 0
while num_bad_display_reads < THRESHOLD_BAD_DISPLAY_READS:
display_mode = subprocess.getoutput("adb shell cat /sys/class/display/mode")
time.sleep(.1)
if display_mode == 'null' or display_mode is None:
num_bad_display_reads = 1 # Start counting up
else:
display_mode = num_bad_display_reads = 0 # Reset the count
#print(f'Display Mode: {display_mode}') # Uncomment this during debugging
# You have now left the display loop. Feel free to do something else.
My guess is that you would like to re-enter the loop if the display recovers. If that's true, then I recommend that you post some specifics about that in another SO question.
Note: I renamed z
to display_mode
.
CodePudding user response:
Your example above does not show valid Python code.
For your desired result, you can initialize a counter to 0, increment it every time you get null and reset it back to 0 if any other value appears:
counter = 0
while True:
if value == 'null':
counter = 1
else:
counter = 0
if counter < 5:
# Code runs as usual here
else:
print("Too many null values in a row, halting")
break
CodePudding user response:
If I understood your code well You're trying to break the loop when z receives null value. In python we use None to represent null values.
Try to replace this line:
if z == 'null'
For this line:
if z == None