# 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: print('Exception') try: pin = Pin(pin_map[0], mode=Pin.LOW_POWER, pull=Pin.PULL_UP) # incorrect mode value except Exception: print('Exception') try:
# 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: print('Exception') try: pin = Pin(pin_map[0], mode=Pin.LOW_POWER, pull=Pin.PULL_UP) # incorrect mode value except Exception: print('Exception') try: pin = Pin(pin_map[0], mode=Pin.IN, pull=Pin.HIGH_POWER) # incorrect pull value except Exception:
pin = Pin(magic_pin, Pin.OUT, alt=-1) print("Constructor pass") # test all getters and setters pin = Pin(magic_pin, 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) # id print(pin.id() == magic_pin) # all the next ones MUST raise try: pin = Pin(magic_pin, mode=Pin.LOW_POWER, pull=Pin.PULL_UP) # incorrect mode value except Exception: print('Exception') try: pin = Pin(magic_pin, mode=Pin.IN, pull=Pin.HIGH_POWER) # incorrect pull value except Exception: print('Exception') try:
ntp_synced = False wlan = WLAN(mode=WLAN.STA) connect_wireless(wlan) if wlan.isconnected(): rtc.ntp_sync(NTP_SERVER) if rtc.synced(): ntp_synced = True if HAVE_EXTERNAL_SENSORS: # initialise Ultrasonic Sensor pins us_trigger_pin = Pin("P4", mode=Pin.OUT) us_echo_pin = Pin("P10", mode=Pin.IN, pull=Pin.PULL_DOWN) # Initialise flow sensors rate_pin = Pin("P11", mode=Pin.IN, pull=Pin.PULL_UP) # Lopy4 specific: Pin('P20', mode=Pin.IN) rate_pin_id = rate_pin.id() # Pin seems to occasionally get 'stuck' on low so we just measure a # transition and log that, as it doens't matter if it's going 1->0 or 0->1 rate_pin.callback(Pin.IRQ_FALLING, rate_pin_cb) # Setup SD card try: sd = SD() HAVE_SD = True except OSError: print("No disk available") if HAVE_SD: uos.mount(sd, MOUNTPOINT) readings = take_readings(3, have_external_sensors=HAVE_EXTERNAL_SENSORS) print("Setting up logfile")
# 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() == magic_pin) # all the next ones MUST raise ''' try: pin = Pin(magic_pin, mode=Pin.OUT, pull=Pin.PULL_UP, drive=pin.IN) # incorrect drive value except Exception: print('Exception') ''' try: pin = Pin(magic_pin, mode=Pin.LOW_POWER, pull=Pin.PULL_UP) # incorrect mode value except Exception: print('Exception')
from senddata import Data p4 = Pin('P4', mode=Pin.IN, pull=Pin.PULL_UP) # rot: Lichtschranke p8 = Pin('P8', mode=Pin.IN, pull=Pin.PULL_UP) # orange: case p9 = Pin('P9', mode=Pin.IN, pull=Pin.PULL_UP) # gelb: Buzzer p10 = Pin('P10', mode=Pin.IN, pull=Pin.PULL_UP) # grün Präsenztaste Licht p11 = Pin('P11', mode=Pin.IN, pull=Pin.PULL_UP) # blau: Präsenztaste switch1 = 0 switch2 = 0 while True: if (p4.value() == 0): time.sleep(0.1) if (p4.value() == 0 and switch1 == 0): print(p4.id() + "Schalter1 an entprellt") Data.sendoutputbasket(1) switch1 = 1 if (p4.value() == 1 and switch1 == 1): time.sleep(0.1) if (p4.value() == 1 and switch1 == 1): print(p4.id() + "Schalter1 aus entprellt") Data.sendoutputbasket(2) switch1 = 0 if (p8.value() == 0): time.sleep(0.1) if (p8.value() == 0 and switch2 == 0): print(p4.id() + "Schalter2 an entprellt") Data.sendcase(3)
# 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: print("Exception") try: pin = Pin(pin_map[0], mode=Pin.LOW_POWER, pull=Pin.PULL_UP) # incorrect mode value except Exception: print("Exception") try:
pin = Pin(pin_map[0], Pin.ALT_OPEN_DRAIN, Pin.PULL_UP, alt=-1) # incorrect af except Exception: print('Exception') try: pin = Pin(pin_map[0], Pin.ALT_OPEN_DRAIN, Pin.PULL_UP, alt=16) # incorrect af except Exception: print('Exception') try: pin.mode(Pin.PULL_UP) # incorrect pin mode except Exception: print('Exception') try: pin.pull(Pin.OUT) # incorrect pull except Exception: print('Exception') try: pin.drive(Pin.IN) # incorrect drive strength except Exception: print('Exception') try: pin.id('ABC') # id cannot be set except Exception: print('Exception')
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