Skip to content

Commit 7bc15ff

Browse files
committed
Handle adjacent failed pixels
1 parent 12e92e9 commit 7bc15ff

File tree

1 file changed

+44
-17
lines changed

1 file changed

+44
-17
lines changed

adafruit_mlx90640.py

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ class MLX90640: # pylint: disable=too-many-instance-attributes
9696
cpAlpha = [0] * 2
9797
cpOffset = [0] * 2
9898
ilChessC = [0] * 3
99-
brokenPixels = [0xFFFF] * 5
100-
outlierPixels = [0xFFFF] * 5
99+
brokenPixels = []
100+
outlierPixels = []
101101
cpKta = 0
102102
cpKv = 0
103103

@@ -744,30 +744,57 @@ def _ExtractCILCParameters(self):
744744
self.ilChessC = ilChessC
745745

746746
def _ExtractDeviatingPixels(self):
747-
self.brokenPixels = [0xFFFF] * 5
748-
self.outlierPixels = [0xFFFF] * 5
749-
750747
pixCnt = 0
751-
brokenPixCnt = 0
752-
outlierPixCnt = 0
753748

754-
while (pixCnt < 768) and (brokenPixCnt < 5) and (outlierPixCnt < 5):
749+
while (
750+
(pixCnt < 768)
751+
and (len(self.brokenPixels) < 5)
752+
and (len(self.outlierPixels) < 5)
753+
):
755754
if eeData[pixCnt + 64] == 0:
756-
self.brokenPixels[brokenPixCnt] = pixCnt
757-
brokenPixCnt += 1
755+
self.brokenPixels.append(pixCnt)
758756
elif (eeData[pixCnt + 64] & 0x0001) != 0:
759-
self.outlierPixels[outlierPixCnt] = pixCnt
760-
outlierPixCnt += 1
757+
self.outlierPixels.append(pixCnt)
761758
pixCnt += 1
762759

763-
if brokenPixCnt > 4:
760+
if len(self.brokenPixels) > 4:
764761
raise RuntimeError("More than 4 broken pixels")
765-
if outlierPixCnt > 4:
762+
if len(self.outlierPixels) > 4:
766763
raise RuntimeError("More than 4 outlier pixels")
767-
if (brokenPixCnt + outlierPixCnt) > 4:
764+
if (len(self.brokenPixels) + len(self.outlierPixels)) > 4:
768765
raise RuntimeError("More than 4 faulty pixels")
769-
# print("Found %d broken pixels, %d outliers" % (brokenPixCnt, outlierPixCnt))
770-
# TODO INCOMPLETE
766+
# print("Found %d broken pixels, %d outliers"
767+
# % (len(self.brokenPixels), len(self.outlierPixels)))
768+
769+
for brokenPixel1, brokenPixel2 in self._UniqueListPairs(self.brokenPixels):
770+
if self._ArePixelsAdjacent(brokenPixel1, brokenPixel2):
771+
raise RuntimeError("Adjacent broken pixels")
772+
773+
for outlierPixel1, outlierPixel2 in self._UniqueListPairs(self.outlierPixels):
774+
if self._ArePixelsAdjacent(outlierPixel1, outlierPixel2):
775+
raise RuntimeError("Adjacent outlier pixels")
776+
777+
for brokenPixel in self.brokenPixels:
778+
for outlierPixel in self.outlierPixels:
779+
if self._ArePixelsAdjacent(brokenPixel, outlierPixel):
780+
raise RuntimeError("Adjacent broken and outlier pixels")
781+
782+
def _UniqueListPairs(self, inputList):
783+
for i, listValue1 in enumerate(inputList):
784+
for listValue2 in inputList[i + 1 :]:
785+
yield (listValue1, listValue2)
786+
787+
def _ArePixelsAdjacent(self, pix1, pix2):
788+
pixPosDif = pix1 - pix2
789+
790+
if pixPosDif > -34 and pixPosDif < -30:
791+
return True
792+
if pixPosDif > -2 and pixPosDif < 2:
793+
return True
794+
if pixPosDif > 30 and pixPosDif < 34:
795+
return True
796+
797+
return False
771798

772799
def _I2CWriteWord(self, writeAddress, data):
773800
cmd = bytearray(4)

0 commit comments

Comments
 (0)