Browse Source

Process the raw values and handle percentages

master
Elis Hirwing 3 years ago
parent
commit
cbfd064552
Signed by: etu GPG Key ID: D57EFA625C9A925F
  1. 50
      src/csms12.py

50
src/csms12.py

@ -16,26 +16,38 @@ class CSMS12:
self.Interval = (self.AirValue - self.WaterValue) / 3
# Just a wrapper to read the value
def getValue(self):
def getRawValue(self):
return self.Adc.read()
# Do the varying scale of the moisture for Capacinance readers
def getScaledValue(self):
scaledValue = 0
rawValue = self.getRawValue()
# Calculate percentage within the calibrated range
scaledValue = (
((self.WaterValue - rawValue) * 100) / (self.WaterValue - self.AirValue)
)
# Make sure that we don't go out of range
if scaledValue < 0:
scaledValue = 0
if scaledValue > 100:
scaledValue = 100
# Convert so 0% is completely dry and 100% is completely wet.
return 100 - scaledValue
# Get current interval of how wet the sensor is
def getInterval(self):
value = self.getValue()
if self.WaterValue <= value <= (self.WaterValue + self.Interval):
return("Very Wet (Value: %s, Water: %s, Air: %s)" % (
value, self.WaterValue, self.AirValue
))
elif (self.WaterValue + self.Interval) <= value <= (self.AirValue - self.Interval):
return("Wet (Value: %s, Water: %s, Air: %s)" % (
value, self.WaterValue, self.AirValue
))
elif (self.AirValue - self.Interval) <= value <= self.AirValue:
return("Dry (Value: %s, Water: %s, Air: %s)" % (
value, self.WaterValue, self.AirValue
))
else:
return("Out of range (Value: %s, Water: %s, Air: %s)" % (
value, self.WaterValue, self.AirValue
))
value = self.getScaledValue()
if 75 <= value <= 100:
return("Very Moist (Value: %s, Water: 100, Air: 0)" % value)
elif 50 <= value <= 75:
return("Quite Moist (Value: %s, Water: 100, Air: 0)" % value)
elif 25 <= value <= 50:
return("Kinda Dry (Value: %s, Water: 100, Air: 0)" % value)
elif 0 <= value <= 25:
return("Very Dry (Value: %s, Water: 100, Air: 0)" % value)

Loading…
Cancel
Save