Advertisement
rveach

read_pins

May 20th, 2013
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.04 KB | None | 0 0
  1. def read_pins():
  2.     pins = {
  3.         0 : pfio.digital_read(0),
  4.         1 : pfio.digital_read(1),
  5.         2 : pfio.digital_read(2),
  6.         3 : pfio.digital_read(3),
  7.         4 : pfio.digital_read(4),
  8.         5 : pfio.digital_read(5),
  9.         6 : pfio.digital_read(6),
  10.         7 : pfio.digital_read(7)
  11.         }
  12.     return pins
  13.  
  14. def check_input_changes(prev, curr):
  15.     i = 0
  16.     changes = []
  17.     while i < 8:
  18.         if prev[i] == curr[i]:
  19.             pass
  20.             # no change, do nothing
  21.         elif (prev[i] > curr[i]):
  22.             chg = {
  23.                 "pin" : i,
  24.                 "change" : "off"
  25.                 }
  26.             changes.append(chg)
  27.         elif (prev[i] < curr[i]):
  28.             chg = {
  29.                 "pin" : i,
  30.                 "change" : "on"
  31.                 }
  32.             changes.append(chg)
  33.         i = i + 1
  34.        
  35.     change_report = {
  36.         "time" : time.time(),
  37.         "changes" : changes,
  38.         "diff" : len(changes)
  39.         }
  40.        
  41.     return change_report
  42.  
  43.  
  44. if __name__ == '__main__':
  45.     pfio.init()
  46.    
  47.     pins = {
  48.         0 : 0,
  49.         1 : 0,
  50.         2 : 0,
  51.         3 : 0,
  52.         4 : 0,
  53.         5 : 0,
  54.         6 : 0,
  55.         7 : 0
  56.         }
  57.    
  58.     while True:
  59.         prev_pins = pins
  60.         pins = read_pins()
  61.         change_report = check_input_changes(prev_pins, pins)
  62.         time.sleep(.05)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement