def getTemper(self): """ 获取温度 :return: 温度,单位摄氏度 """ _pin = MPythonPin(self.pin, PinMode.ANALOG) val = _pin.read_analog() return val / 4 * (3 / 10.24)
def getTemper(self): """ 获取温度 :return: 温度,单位摄氏度 """ _pin = MPythonPin(self.pin, PinMode.ANALOG) val = _pin.read_analog() return 1 / (math.log(4095 / val - 1) / 3935 + 1 / 298) - 273
def getVal(self): """ 获取按键 :return: 返回按键,值可以是'A','B','C','D',无按键按下'None'. """ _pin = MPythonPin(self.pin, PinMode.ANALOG) val = _pin.read_analog() btn = None if val < 100: btn = 'A' elif val > 600 and val < 1023: btn = 'B' elif val > 1700 and val < 2048: btn = 'C' elif val > 2700 and val < 3200: btn = 'D' return btn
class Servo: def __init__(self, pin, min_us=750, max_us=2250, actuation_range=180): self.min_us = min_us self.max_us = max_us self.actuation_range = actuation_range self.servoPin = MPythonPin(pin, PinMode.PWM) def write_us(self, us): if us < self.min_us or us > self.max_us: raise ValueError("us out of range") duty = round(us / 20000 * 1023) self.servoPin.write_analog(duty, 50) def write_angle(self, angle): if angle < 0 or angle > self.actuation_range: raise ValueError("Angle out of range") us_range = self.max_us - self.min_us us = self.min_us + round(angle * us_range / self.actuation_range) self.write_us(us)
class MicroBitPin: IN = 1 OUT = 2 PWM = 3 ANALOG = 4 def __init__(self, id): self.id = id self.current_mode = None self.mpython_pin = None def __change_pin_mode(self, pinmode): if self.current_mode is not pinmode: self.mpython_pin = MPythonPin(self.id, mode=pinmode) if pinmode == MicroBitPin.ANALOG: self.mpython_pin.adc.width(ADC.WIDTH_10BIT) self.current_mode = pinmode def read_digital(self): self.__change_pin_mode(MicroBitPin.IN) return self.mpython_pin.read_digital() def write_digital(self, value): self.__change_pin_mode(MicroBitPin.OUT) self.mpython_pin.write_digital(value) def read_analog(self): self.__change_pin_mode(MicroBitPin.ANALOG) return self.mpython_pin.read_analog() def write_analog(self, value): self.__change_pin_mode(MicroBitPin.PWM) self.mpython_pin.write_analog(value) def set_analog_period(period): #TODO pass def set_analog_period_microseconds(period): #TODO pass
def __change_pin_mode(self, pinmode): if self.current_mode is not pinmode: self.mpython_pin = MPythonPin(self.id, mode=pinmode) if pinmode == MicroBitPin.ANALOG: self.mpython_pin.adc.width(ADC.WIDTH_10BIT) self.current_mode = pinmode
def __init__(self, pin, min_us=750, max_us=2250, actuation_range=180): self.min_us = min_us self.max_us = max_us self.actuation_range = actuation_range self.servoPin = MPythonPin(pin, PinMode.PWM)