[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

printing to stdout

On Sun, 19 Aug 2018 19:53:04 +1000
Cameron Simpson <cs at cskk.id.au> wrote:

[Oops, apparently you set the Reply-To to python-list at python.org,
normally that's no problem, but I did something wrong somewhere]

> There are always unPythonic bits. Even after you've cleaned them all
> up, since people will disagree about the finer points of Pythonicism
> there will be bits both over and under cleaned.

Although I do not understand exactly what zip is doing here (I presume
I switch to use pointers instead of the values), I like the
"enumerate" function, very handy :-) The code is now as

#!/usr/bin/env python3

import sys
from smbus import SMBus
from time import sleep
import RPi.GPIO as GPIO

list_pcf = [0x38, 0x39, 0x3a, 0x3b]
list_pcf_value = []
bus = SMBus(1)

# initialisation of the input devices:
print ("[INFO] initialisation input devices")
for pcf in list_pcf:
    bus.write_byte(pcf, 0xff) # set device to 0xff
  except IOError as e:
    print ("[ALERT] I/O problem device 0x%x (init): %s" % (pcf, e))
  output = bus.read_byte(pcf)
  list_pcf_value.append(output) # append value to list
  print ("found pcf8574 at 0x%x, input value: 0x%x" % (pcf, output))

# GPIO 23 set up as input. It is pulled up to stop false signals
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
loopcntr = 0 # detects if INT is kept low

while True:
  if GPIO.input(23) == 1: # if still 0, another event has occurred
    GPIO.wait_for_edge(23, GPIO.FALLING)

  while GPIO.input(23) == 0:
    for device_nr, (pcf, pcf_value) in enumerate(zip(list_pcf, list_pcf_value)):
        output = bus.read_byte(pcf)
      except IOError as e:
        print ("[ALERT] I/O problem device 0x%x: %s" % (pcf, e))

      if output != pcf_value:
        xor = pcf_value ^ output
        for bit_pos in range(8):
          if xor & 0x1:
            up_down = (output >> bit_pos) & 0x1
            print ("pcf8574 0x%x bit %d: to %d" % (pcf,bit_pos,up_down))
            # send decimal values to event script:
            with open('/mnt/ramdisk/var/lib/ha/events.fifo', 'w') as fifo:
              print("%d %d %d" % (pcf,bit_pos,up_down), file=fifo)
          xor >>= 1
        list_pcf_value[device_nr] = output

      if GPIO.input(23) == 1:
        loopcntr = 0
      loopcntr += 1
      if loopcntr >=20:
        print ('[ALERT] possible INT loop, disabling 10 seconds')
        sleep (10)


And it still works :-)



richard lucassen