def value(self, value): self._value = constrain(value, 0, 100) if self._mode == LED.Mode.ANALOG: analog_write_percent(self._pin, self._value) else: self._pin.value(amap(self._value, 0, 100, 0, 1))
def _distance(self, min_value, max_value, divider): self._send_pulse() duration = time_pulse_us(self._pin, 1, 20_000) if duration != -1: return constrain(duration / divider, min_value, max_value) else: return -1
def color(self): measured_color = [0, 0, 0] for i in range(3): self._colorPins[i].high() delay(10) measured_color[i] = self._cds_multiple_read(5) measured_color[i] = int(amap(constrain(measured_color[i], self._black_sample[i], self._white_sample[i]), self._black_sample[i], self._white_sample[i], 0, 100)) self._colorPins[i].low() delay(10) # WHITE if measured_color[0] >= self._upper_threshold and \ measured_color[1] >= self._upper_threshold and \ measured_color[2] >= self._upper_threshold and \ abs(measured_color[0] - measured_color[1]) <= self._color_near and \ abs(measured_color[0] - measured_color[2]) <= self._color_near and \ abs(measured_color[1] - measured_color[2]) <= self._color_near: return self.Color.WHITE # BLACK elif measured_color[0] <= self._lower_threshold and \ measured_color[1] <= self._lower_threshold and \ measured_color[2] <= self._lower_threshold and \ abs(measured_color[0] - measured_color[1]) <= self._delta_black and \ abs(measured_color[0] - measured_color[2]) <= self._delta_black and \ abs(measured_color[1] - measured_color[2]) <= self._delta_black: return self.Color.BLACK # RED elif measured_color[0] - measured_color[1] >= self._delta_color and \ measured_color[0] - measured_color[2] >= self._delta_color: return self.Color.RED # GREEN elif measured_color[1] - measured_color[0] >= self._delta_color and \ measured_color[1] - measured_color[2] >= self._delta_color: return self.Color.GREEN # BLUE elif measured_color[2] - measured_color[0] >= self._delta_color and \ measured_color[2] - measured_color[1] >= self._delta_color: return self.Color.BLUE # YELLOW elif measured_color[0] - measured_color[2] >= self._color_near and \ measured_color[1] - measured_color[2] >= self._color_near and \ abs(measured_color[0] - measured_color[1]) <= self._delta_color: return self.Color.YELLOW # PURPLE elif measured_color[0] - measured_color[1] >= self._color_near and \ measured_color[2] - measured_color[1] >= self._color_near and \ abs(measured_color[0] - measured_color[2]) <= self._delta_color: return self.Color.PURPLE # CYAN elif measured_color[1] - measured_color[0] >= self._color_near and \ measured_color[2] - measured_color[0] >= self._color_near and \ abs(measured_color[1] - measured_color[2]) <= self._delta_color: return self.Color.CYAN return self.Color.UNKNOWN
def flame(self, threshold=900): return self.read() >= constrain(threshold, 0, 1023)
def light(self, threshold=500): return self.read() > constrain(threshold, 0, 1023)
def dark(self, threshold=500): return self.read() <= constrain(threshold, 0, 1023)
def shock(self, threshold=55): return self.read() <= constrain(threshold, 0, 1023)
def angle(self, angle): angle = constrain(angle, 0, 180) width = amap(angle, 0, 180, self._min_duty, self._max_duty) self._timer_channel.pulse_width(int(width))
def volume(self, volume): self._volume = amap(constrain(volume, 0, 100), 0, 100, 0, 8) if self._is_on: self.on()
def sound(self, threshold=100): return analog_read(self._adc, bit=10) <= constrain(threshold, 0, 1023)
def white(self, threshold=700): return self.read() >= constrain(threshold, 0, 1023)
def power(self, power): self._power = constrain(power, 0, 100) analog_write_percent(self._ENC, self._power)