Python machine 模块,PWM 实例源码

我们从Python开源项目中,提取了以下10个代码示例,用于说明如何使用machine.PWM

项目:micropython-share    作者:pacmac    | 项目源码 | 文件源码
def _config(self, duty_cycle=0):
        if self._function == 'dig':
            _mode = machine.Pin.OUT if self._mode == 'out' else machine.Pin.IN
            if self._pull == 'pu':
                _pull = machine.Pin.PULL_UP
            elif self._pull == 'pd':
                _pull = machine.Pin.PULL_DOWN
            else:
                _pull = None
            self._pin = machine.Pin(self._name, mode=_mode, pull=_pull, drive=machine.Pin.MED_POWER)
        elif self._function == 'ana':
            adc = machine.ADC(bits=12)
            self._apin = adc.channel(pin=self._name)
        else:
            self._pwm = machine.PWM(machine.Pin(self.pin_num),duty=duty_cycle * 100,freq=1000)
            #timer = machine.Timer(HwPin._TimerMap[self._name][0], mode=machine.Timer.PWM)
            #self._pwm = timer.channel(HwPin._TimerMap[self._name][1], freq=20000, duty_cycle=(duty_cycle * 100))
项目:ulnoiot    作者:ulno    | 项目源码 | 文件源码
def __init__(self, name, pin,
                 ignore_case=True, on_change=None,
                 report_change=False,
                 turn_time_ms=700,
                 freq=50, min_us=600, max_us=2400, angle=180):
        self.min_us = min_us
        self.max_us = max_us
        self.us = 0
        self.freq = freq
        self.angle = angle
        self.angle_list=None
        self.turn_time_ms=turn_time_ms
        self.turn_start=None
        Device.__init__(self, name, PWM(pin, freq=self.freq, duty=0),
                        setters={"set":self.turn}, ignore_case=ignore_case,
                        on_change = on_change,report_change=report_change)
        self._init()
项目:led_strip    作者:mattytrentini    | 项目源码 | 文件源码
def fader_loop(self):

        FADER_MAX_STEP = 5
        FADER_DELAY = 0.005

        fader = PWM(Pin(15))
        fader_cur_val = 0

        while True:

            if self.fader_target_val > fader_cur_val:
                step = min(FADER_MAX_STEP, self.fader_target_val - fader_cur_val)
            elif self.fader_target_val < fader_cur_val:
                step = -min(FADER_MAX_STEP, fader_cur_val - self.fader_target_val)
            else:
                step = 0

            fader_cur_val += step
            fader.duty(fader_cur_val)

            await asyncio.sleep(FADER_DELAY)
项目:ulnoiot    作者:ulno    | 项目源码 | 文件源码
def __init__(self, name, pinr, ping, pinb,
                 ignore_case=True, on_change=None,
                 report_change=False):
        pin = (PWM(pinr),PWM(ping),PWM(pinb))
        RGB_Base.__init__(self, name, pin,
                          ignore_case=ignore_case,
                          on_change = on_change,
                          report_change=report_change)
项目:ulnoiot    作者:ulno    | 项目源码 | 文件源码
def __init__(self, name, pin, freq=50, duty=0,
                 ignore_case=True, on_change=None,
                 report_change=False):
        self._duty = 0
        self._freq = freq
        Device.__init__(self, name, PWM(pin, freq=freq, duty=duty),
                        setters={"freq/set":self.set_freq,"duty/set":self.set_duty},
                        getters={"freq":self.get_freq,"duty":self.get_duty},
                        ignore_case=ignore_case,
                        on_change=on_change, report_change=report_change)
项目:ulnoiot    作者:ulno    | 项目源码 | 文件源码
def set_duty(self, d):
        try:
            d=int(d)
        except:
            print("PWM: received invalid duty value:",d)
        else:
            self._duty = d
            self.pin.duty(d)
项目:ulnoiot    作者:ulno    | 项目源码 | 文件源码
def set_freq(self, f):
        try:
            f=int(f)
        except:
            print("PWM: received invalid frequency value:",f)
        else:
            self._freq = f
            self.pin.freq(f)
项目:micropython-dev-kit    作者:db4linq    | 项目源码 | 文件源码
def __init__(self, pin, freq=50, min_us=600, max_us=2400, angle=180):
        self.min_us = min_us
        self.max_us = max_us
        self.us = 0
        self.freq = freq
        self.angle = angle
        self.pwm = PWM(pin, freq=freq, duty=0)
项目:py-mpu6050    作者:larsks    | 项目源码 | 文件源码
def init_pins(self):
        print('* initializing pins')
        self.pin_sda = Pin(self.sda)
        self.pin_scl = Pin(self.scl)
        self.pin_intr = Pin(self.intr, mode=Pin.IN)
        self.pin_led = PWM(Pin(self.led, mode=Pin.OUT))
项目:esp    作者:mhoffma    | 项目源码 | 文件源码
def __init__(self,pinno,pos=77):
        self.pinno = pinno
        self.pin=machine.Pin(pinno)
        self.pin.init(self.pin.OUT)
        self.p = machine.PWM(self.pin,freq=50)
        self.rot(pos)