コード例 #1
0
class irrecv:
    '''irrecv receive IR signal on pin into circular buffer'''
    def __init__(self, pno=4, bsz=1024, dbgport=None):
        self.BUFSZ = int(pow(2, math.ceil(math.log(bsz) / math.log(2))))
        self.MASK = self.BUFSZ - 1
        self.pin = pno
        self.ir = Pin(pno, Pin.IN)
        self.buf = array.array('i', 0 for i in range(self.BUFSZ))
        self.ledge = 0
        self.wptr = 0
        self.rptr = 0
        self.dbgport = dbgport
        if dbgport is None:
            self.dbgport = DummyPort()
        self.dbgport.low()
        # cache ticks functions for native call
        self.ticks_diff = time.ticks_diff
        self.ticks_us = time.ticks_us
        self.start()

    def __repr__(self):
        return "<irrecv: {}, wptr={} rptr={}>".format(self.pin, self.wptr,
                                                      self.rptr)

    @micropython.native
    def timeseq_isr(self, p):
        '''compute edge to edge transition time, ignore polarity'''
        e = self.ticks_us()
        self.dbgport.high()
        s = 2 * p.value() - 1
        d = self.ticks_diff(e, self.ledge)
        self.ledge = e
        self.buf[self.wptr] = d * s
        self.wptr = (self.wptr + 1) & self.MASK
        self.dbgport.low()

    def stop(self):
        '''disable isr'''
        self.ir.deinit()

    def start(self):
        self.ir.irq(trigger=Pin.IRQ_FALLING | Pin.IRQ_RISING,
                    handler=self.timeseq_isr)

    def reset(self):
        state = disable_irq()
        self.wptr = 0
        self.rptr = 0
        enable_irq(state)

    def read(self):
        w = self.wptr
        r = self.rptr
        n = w - r
        if n >= 0:
            x = self.buf[r:w]
        elif n < 0:
            x = self.buf[r:] + self.buf[:w]
        self.rptr = w
        return x
コード例 #2
0
ファイル: _ir.py プロジェクト: saimaza123/UIFlow-Code
class Ir:
    portMethod = unit.PORT0 | unit.PORT1

    def __init__(self, port):
        self.tx = PWM(port[0], freq=38000, duty=0, timer=1)
        self.rx = Pin(port[1], Pin.IN)
        self.rx.init(Pin.IN)
        self.rx.irq(handler=self._irq_cb, trigger=Pin.IRQ_FALLING)
        self.rx_value = 0
        self.times = 0
        self.status = 0
        self.tx_en = 0
        self.duty = 0
        self.time_num = peripheral.get_timer()
        if self.time_num == None:
            raise unit.Unit('ir application time fail')
        self.timer = Timer(self.time_num)
        self.timer.init(period=50, mode=self.timer.PERIODIC, callback=self._update)

    def _irq_cb(self, pin):
        self.times = 0
    
    def _update(self, arg):
        if self.tx_en:
            self.duty = 0 if self.duty else 10
        else:
            self.duty = 0
        self.tx.duty(self.duty)
        self.times += 1

    def rxStatus(self):
        return 1 if self.times < 5 else 0

    def txOn(self):
        self.tx_en = 1
    
    def txOff(self):
        self.tx_en = 0

    def deinit(self):
        self.timer.deinit()
        if self.time_num is not None:
            peripheral.free_timer(self.time_num)
        self.rx.deinit()