Beispiel #1
0
print(pin.value())
pin(0)
print(pin.value())
pin.value(1)
print(pin())
pin.value(0)
print(pin())

# test all getters and setters
pin = Pin(pin_map[0], mode=Pin.OUT)
# mode
print(pin.mode() == Pin.OUT)
pin.mode(Pin.IN)
print(pin.mode() == Pin.IN)
# pull
pin.pull(None)
print(pin.pull() == None)
pin.pull(Pin.PULL_DOWN)
print(pin.pull() == Pin.PULL_DOWN)
# drive
pin.drive(Pin.MED_POWER)
print(pin.drive() == Pin.MED_POWER)
pin.drive(Pin.HIGH_POWER)
print(pin.drive() == Pin.HIGH_POWER)
# id
print(pin.id() == pin_map[0])

# all the next ones MUST raise
try:
    pin = Pin(pin_map[0], mode=Pin.OUT, pull=Pin.PULL_UP, drive=pin.IN) # incorrect drive value
except Exception:
Beispiel #2
0
    try_pin = "P000"
    try_s = "Pin(Pin.cpu.P000, mode=Pin.IN, pull=Pin.PULL_NONE, drive=Pin.LOW_POWER)"

p = Pin(try_pin, Pin.IN)
if str(p) == try_s:
    print("OK")
else:
    print("NG")
    print("exp: " + try_s)
    print("out: " + str(p))

p = Pin("SW1", Pin.IN, Pin.PULL_UP)
if p.mode() != 1:
    print("mode: NG")
p = Pin("SW1", Pin.IN, pull=Pin.PULL_UP)
if p.pull() != 14:
    print("pull: NG")
p = Pin("SW1", mode=Pin.IN, pull=Pin.PULL_UP)

p.init(p.IN, p.PULL_UP)
p.init(p.IN, pull=p.PULL_UP)
p.init(mode=p.IN, pull=p.PULL_UP)
print(p.value())

p.init(p.OUT)
p.init(p.OPEN_DRAIN)
p.low()
print(p.value())
p.high()
print(p.value())
p.value(0)
Beispiel #3
0
print(pin.value())
pin(0)
print(pin.value())
pin.value(1)
print(pin())
pin.value(0)
print(pin())

# test all getters and setters
pin = Pin(pin_map[0], mode=Pin.OUT)
# mode
print(pin.mode() == Pin.OUT)
pin.mode(Pin.IN)
print(pin.mode() == Pin.IN)
# pull
pin.pull(None)
print(pin.pull() == None)
pin.pull(Pin.PULL_DOWN)
print(pin.pull() == Pin.PULL_DOWN)
# drive
pin.drive(Pin.MED_POWER)
print(pin.drive() == Pin.MED_POWER)
pin.drive(Pin.HIGH_POWER)
print(pin.drive() == Pin.HIGH_POWER)
# id
print(pin.id() == pin_map[0])

# all the next ones MUST raise
try:
    pin = Pin(pin_map[0], mode=Pin.OUT, pull=Pin.PULL_UP,
              drive=pin.IN)  # incorrect drive value
Beispiel #4
0
class PinButton:

    # ============================================================================
    # ===( Constructor )==========================================================
    # ============================================================================

    def __init__(self,
                 pinNum,
                 strPull,
                 btnChangeCallback,
                 btnReversed=False,
                 btnThreaded=False,
                 Id=0):

        pinId = self._getPinIdFromPinNum(pinNum)
        pinPull = self._getPinPullFromStrPull(strPull)
        if not pinId or not pinPull:
            raise Exception('Incorrects arguments in PinButton constructor.')

        self._Id = Id
        self._btnChangeCB = btnChangeCallback
        self._btnReversed = btnReversed
        self._btnThreaded = btnThreaded
        self._lockProcess = allocate_lock()
        self._process = False
        self._pin = Pin(pinId, mode=Pin.IN, pull=pinPull)
        self._btnIsOn = self._isLogicalBtnOnFromPin()
        self._saveTicksMS = ticks_ms() if self._btnIsOn else 0

        print("BUTTON ON PIN [%s] INITIALIZED FROM [%s]" %
              (self._pin.id(), "ON" if self._btnIsOn else "OFF"))

        self._pin.callback(Pin.IRQ_FALLING | Pin.IRQ_RISING,
                           self._pinInterrupt)

    # ============================================================================
    # ===( Functions )============================================================
    # ============================================================================

    def GetId(self):
        return self._Id

    # ----------------------------------------------------------------------------

    def IsOn(self):
        return self._btnIsOn

    # ----------------------------------------------------------------------------

    def IsOff(self):
        return not self._btnIsOn

    # ============================================================================
    # ===( Utils )================================================================
    # ============================================================================

    def _getPinIdFromPinNum(self, pinNum):
        return ("P" + str(pinNum)) if isinstance(pinNum, int) else None

    # ----------------------------------------------------------------------------

    def _getPinPullFromStrPull(self, strPull):
        if hasattr(strPull, 'upper'):
            strPull = strPull.upper()
            if strPull == "UP":
                return Pin.PULL_UP
            if strPull == "DOWN":
                return Pin.PULL_DOWN
        return None

    # ----------------------------------------------------------------------------

    def _isLogicalBtnOnFromPin(self):
        if self._pin.pull() == Pin.PULL_DOWN:
            isOn = self._pin.value()
        else:
            isOn = not self._pin.value()
        return (isOn if not self._btnReversed else not isOn)

    # ============================================================================
    # ===( Events )===============================================================
    # ============================================================================

    def _pinInterrupt(self, pin):
        isOn = self._isLogicalBtnOnFromPin()
        if (isOn != self._btnIsOn):
            if (isOn and not self._process) or not isOn:
                self._btnIsOn = isOn
                self._process = True
                if isOn:
                    msPressed = 0
                    self._saveTicksMS = ticks_ms()
                else:
                    msPressed = ticks_ms() - self._saveTicksMS
                if self._btnThreaded:
                    start_new_thread(self._processBtnChange, (isOn, msPressed))
                else:
                    self._processBtnChange(isOn, msPressed)

    # ----------------------------------------------------------------------------

    def _processBtnChange(self, isOn, msPressed):
        with self._lockProcess:
            print("BUTTON ON PIN [%s] CHANGED TO [%s]" %
                  (self._pin.id(), "ON" if isOn else "OFF"))
            if self._btnChangeCB:
                try:
                    self._btnChangeCB(self, isOn, msPressed)
                except:
                    print(
                        'Error on callback process of PinButton change (%s).' %
                        exc_info()[1])
            if not isOn:
                self._process = False