8 x 8 阵列与 python 中的 OLED 屏幕打印相关
8 by 8 array correlate to printing on OLED screen in python
基本上,我有这个 8x8 阵列网格,以摄氏度显示温度值,如上图所示。我正在从红外传感器读取这些值。截至目前,我已经实现了我的目标,即能够读取在较高温度值下拾取的传感器值,并在 OLED 屏幕上显示与像素大小相关的矩形。您在上图中看到的大小大致相同。我把我的代码放在下面:
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)
如您所见,前两行有一堆 if-else 语句,后面几行的过程也是相同的。但这绝对看起来太乏味了,而且有很多代码要覆盖。我需要帮助找到一种方法来缩短代码并使它变得更简单,因为它是重复的。任何帮助都会很棒?
这就是我会做的。这不是您的代码的完整版本,您必须对其进行修改以满足您的需要。
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)
基本上,我有这个 8x8 阵列网格,以摄氏度显示温度值,如上图所示。我正在从红外传感器读取这些值。截至目前,我已经实现了我的目标,即能够读取在较高温度值下拾取的传感器值,并在 OLED 屏幕上显示与像素大小相关的矩形。您在上图中看到的大小大致相同。我把我的代码放在下面:
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)
如您所见,前两行有一堆 if-else 语句,后面几行的过程也是相同的。但这绝对看起来太乏味了,而且有很多代码要覆盖。我需要帮助找到一种方法来缩短代码并使它变得更简单,因为它是重复的。任何帮助都会很棒?
这就是我会做的。这不是您的代码的完整版本,您必须对其进行修改以满足您的需要。
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)