Basically, I have this 8x8 array grid displaying temperature values in Celsius, as shown in the grid above. I am reading these values from an infrared sensor. As of now, I have accomplished my goal of being able to read the sensor values pick up on higher temperature values too, and display rectangles correlating to pixel size on an OLED screen. About the same size, you see on the image above. I have placed my code below:
def Correlate(Num):
while True:
Update_Temperature_Data = Read_raw_temp()
print(Temperature_Data)
##########################First-Row########################
if Update_Temperature_Data[0] > 27:
draw.rectangle((80,1,85,8), fill=1, outline=0)
disp.image(image)
disp.display()
else:
draw.rectangle((80,1,85,8), outline=0, fill=0)
if Update_Temperature_Data[1] > 27:
draw.rectangle((85,1,91,8), fill=1, outline=0)
disp.image(image)
disp.display()
else:
draw.rectangle((85,1,91,8), outline=0, fill=0)
if Update_Temperature_Data[2] > 27:
draw.rectangle((91,1,97,8), fill=1, outline=0)
disp.image(image)
disp.display()
else:
draw.rectangle((91,1,97,8), outline=0, fill=0)
if Update_Temperature_Data[3] > 27:
draw.rectangle((97,1,103,8), fill=1, outline=0)
disp.image(image)
disp.display()
else:
draw.rectangle((97,1,103,8), outline=0, fill=0)
if Update_Temperature_Data[4] > 27:
draw.rectangle((103,1,109,8), fill=1, outline=0)
disp.image(image)
disp.display()
else:
draw.rectangle((103,1,109,8), outline=0, fill=0)
if Update_Temperature_Data[5] > 27:
draw.rectangle((109,1,115,8), fill=1, outline=0)
disp.image(image)
disp.display()
else:
draw.rectangle((109,1,115,8), outline=0, fill=0)
if Update_Temperature_Data[6] > 27:
draw.rectangle((115,1,121,8), fill=1, outline=0)
disp.image(image)
disp.display()
else:
draw.rectangle((115,1,121,8), outline=0, fill=0)
if Update_Temperature_Data[7] > 27:
draw.rectangle((121,1,126,8), fill=1, outline=0)
disp.image(image)
disp.display()
else:
draw.rectangle((121,1,126,8), outline=0, fill=0)
######################Second-Row#############################
if Update_Temperature_Data[8] > 27:
draw.rectangle((80,8,85,16), fill=1, outline=0)
disp.image(image)
disp.display()
else:
draw.rectangle((80,8,85,16), outline=0, fill=0)
if Update_Temperature_Data[9] > 27:
draw.rectangle((85,8,91,16), fill=1, outline=0)
disp.image(image)
disp.display()
else:
draw.rectangle((85,8,91,16), outline=0, fill=0)
if Update_Temperature_Data[10] > 27:
draw.rectangle((91,8,97,16), fill=1, outline=0)
disp.image(image)
disp.display()
else:
draw.rectangle((91,8,97,16), outline=0, fill=0)
if Update_Temperature_Data[11] > 27:
draw.rectangle((97,8,103,16), fill=1, outline=0)
disp.image(image)
disp.display()
else:
draw.rectangle((97,8,103,16), outline=0, fill=0)
if Update_Temperature_Data[12] > 27:
draw.rectangle((103,8,109,16), fill=1, outline=0)
disp.image(image)
disp.display()
else:
draw.rectangle((103,1,109,16), outline=0, fill=0)
if Update_Temperature_Data[13] > 27:
draw.rectangle((109,8,115,16), fill=1, outline=0)
disp.image(image)
disp.display()
else:
draw.rectangle((109,8,115,16), outline=0, fill=0)
if Update_Temperature_Data[14] > 27:
draw.rectangle((115,8,121,16), fill=1, outline=0)
disp.image(image)
disp.display()
else:
draw.rectangle((115,8,121,16), outline=0, fill=0)
if Update_Temperature_Data[15] > 27:
draw.rectangle((121,8,126,16), fill=1, outline=0)
disp.image(image)
disp.display()
else:
draw.rectangle((121,8,126,16), outline=0, fill=0)
As you can see I have a bunch of if-else statements for the first two rows, and it would be the same procedure for the following rows as well. But this definitely seems too tedious and has a lot of code to cover. I need help in finding a way to shorten the code and make this much simpler because it is repetitive. Any help would be great?
CodePudding user response:
This is what I would do. It's not a completed version of your code you will have to modify it to fit your needs.
def Correlate(Num):
Row1recs = {0:"""80,1,85,8""",1:"""85,1,91,8""",2:"""91,1,97,8""",3:"""97,1,103,8""",4:"""103,1,109,8""",}
Row2recs = {0:"""80,1,85,8"""}
elserecsrow1 = {0:"""103,1,109,8"""}
elserecsrow2 = {0:"""103,1,109,8"""}
while True:
Update_Temperature_Data = Read_raw_temp()
print(Temperature_Data)
for i in Update_Temperature_Data:
if Update_Temperature_Data[i] > 27:
draw.rectangle((eval(Row1recs[i])), fill=1, outline=0)
disp.image(image)
disp.display()
else:
draw.rectangle((eval(Row2recs[i])), outline=0, fill=0)
if Update_Temperature_Data[i] > 27:
draw.rectangle((eval(elserecsrow1[i])), fill=1, outline=0)
disp.image(image)
disp.display()
else:
draw.rectangle((eval(elserecsrow2[i])), outline=0, fill=0)