Skip to content

Instantly share code, notes, and snippets.

@Dan-in-CA
Last active August 18, 2021 20:03
Show Gist options
  • Save Dan-in-CA/98440df0975c5a8883aecc0c7723a8d4 to your computer and use it in GitHub Desktop.
Save Dan-in-CA/98440df0975c5a8883aecc0c7723a8d4 to your computer and use it in GitHub Desktop.
Test plugin for I2C SIP interfase to relay boards
#!/usr/bin/env python
# Python 2/3 compatibility imports
from __future__ import print_function
# standard library imports
import json
import smbus
import subprocess
import time
# local module imports
from blinker import signal
import gv # Get access to SIP's settings, gv = global variables
from sip import template_render
from urls import urls # Get access to SIP's URLs
import web
from webpages import ProtectedPage
# Add a new url to open the data entry page.
# fmt: off
urls.extend(
[
u"/i2c", u"plugins.i2c_plugin.docs", #### Need to create this ####
]
)
# fmt: on
# Add this plugin to the plugins menu
gv.plugin_menu.append([u"I2C_plugin", u"/i2c"])
bus = smbus.SMBus(1)
addr = 0x20 # starting I2C chip address
#### Not sure if this is needed ####
# if commands["gpio"]:
# gv.use_gpio_pins = False
# else:
# gv.use_gpio_pins = True
#### Control relay when signal received ####
def on_zone_change(name, **kw):
""" Switch relays when core program signals a change in station state."""
global addr
for b in range(gv.sd[u"nbrd"]): # for each group of 8 stations (boards/I2C chips)
addr += b # b is 0 based, so first iteration b = 0, next iteration b = 1 etc
byte = 0xFF # start with full byte (all 1's).
for s in range(8): # for each station per board/I2C chip
sid = b * 8 + s # station index in gv.srvals
if gv.srvals[sid]: # station is on
byte = byte ^ (1 << s) # use exclusive or to set station bit to 0
print("writing byte: ", byte)
bus.write_byte(addr, byte)
return
zones = signal(u"zone_change")
zones.connect(on_zone_change)
################################################################################
# Web pages: #
################################################################################
#### Need to create documentation page ####
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment