示例#1
0
def Interrupt(channel): 
    global DI
    print("Interrupt!")
    #The list of for the new values
    DI_new = [0,0,0,0,0,0,0,0]
    
    #read all inputs sequentially
    for i in range (1,8):
      DI_new[i-1]=i2ciotools.readDigitalInputPort(I2C_PORT, DI_MODULE_1_ADDR, i)
    
    #Now check if the new value equals the old value
    #if it does... nothing changes
    for i in range (0,7):
      if (DI[i] != DI_new[i]):
        #If old doesn't match new value... that PIN changed
        print("PIN "+str(i)+" changed")
    #print out a list of the current state of all inputs
    for i in range(0,7):    
      sys.stdout.write(str(DI[i]))
      sys.stdout.write(" ")
    print("")
    print("")
    #Don't forget that step... new=old
    DI=DI_new
示例#2
0
    print ("Input is NOT active")
except:
  print ("ERROR! The DI module isn't there. Check the wires and the address")


#OK, enough with the exception catching for each statement. If you have just DI or DO
#the following code block won't execute correctly as the first time a non-existing module
#is used the code will fail and go to the except statement

try: 
  #Set the output PIN/Port 1 to 0 (inactive)
  #Use the slightly different writeDigitalOutputPort to do that...
  i2ciotools.writeDigitalOutputPort(I2C_PORT, DO_MODULE_1_ADDR, 1, 0)

  #Read using the readDigitalInputPort 
  if (i2ciotools.readDigitalInputPort(I2C_PORT, DI_MODULE_1_ADDR,1)==1):
    print ("Input is active")
  else:
    print ("Input is NOT active")

  #Let's go hardcore and send the byte directly (note the negated byte)  
  #Set the output PIN/Port 1 to 1 (active)   
  i2ciotools.writeDigitalOutput(I2C_PORT, DO_MODULE_1_ADDR, 0xFE)

  if (i2ciotools.readDigitalInput(I2C_PORT, DI_MODULE_1_ADDR)==0xFE):
    print ("Input is active")
  else:
    print ("Input is NOT active")
  
  #Set the output PIN/Port 1 to 0 (inactive)
  #Back to the shortest notation...