def test(dummy):
    automationhat.enable_auto_lights(False)

    automationhat.light.on()

    for analog in automationhat.analog:
        analog.light.on()

    for output in automationhat.output:
        output.light.on()

    for input in automationhat.input:
        input.light.on()

    time.sleep(1)

    automationhat.light.off()

    for analog in automationhat.analog:
        analog.light.off()

    for output in automationhat.output:
        output.light.off()

    for input in automationhat.input:
        input.light.off()

    automationhat.enable_auto_lights(True)

    if automationhat.is_automation_hat():
        automationhat.light.power.write(1)

    for i in range(4):
        if automationhat.is_automation_hat():
            automationhat.light.comms.toggle()
            automationhat.light.warn.toggle()

        automationhat.relay.one.toggle()

        if automationhat.is_automation_hat():
            automationhat.relay.two.toggle()
            automationhat.relay.three.toggle()
        automationhat.output.toggle()

        print(automationhat.analog.read())

        time.sleep(0.5)

    for i in range(4):
        print(automationhat.input.read())
        time.sleep(0.5)

    if automationhat.is_automation_hat():
        automationhat.light.power.write(0)

    return 0
def writeOutput(channel, state):
    if automationhat.is_automation_hat():
        automationhat.enable_auto_lights(True)
    if channel == 1:
        automationhat.output.one.write(state)
    elif channel == 2:
        automationhat.output.two.write(state)
    elif channel == 3:
        automationhat.output.three.write(state)
    else:
        print "Specified output channel is out of range."
    return 0
def writeInputLight(channel, state):
    if automationhat.is_automation_hat():
        automationhat.enable_auto_lights(False)
        if channel == 1:
            automationhat.input.one.light.write(state)
        elif channel == 2:
            automationhat.input.two.light.write(state)
        elif channel == 3:
            automationhat.input.three.light.write(state)
        else:
            print "Specified input light channel is out of range."
    return 0
def writeRelay(channel, state):
    if automationhat.is_automation_hat():
        automationhat.enable_auto_lights(True)
    if channel == 1:
        automationhat.relay.one.write(state)
    elif channel == 2:
        if automationhat.is_automation_hat():
            automationhat.relay.two.write(state)
    elif channel == 3:
        if automationhat.is_automation_hat():
            automationhat.relay.three.write(state)
    else:
        print "Specified relay channel is out of range."
    return 0
def readInput(channel):
    if automationhat.is_automation_hat():
        automationhat.enable_auto_lights(True)
    if channel == 1:
        state = automationhat.input.one.read()
    elif channel == 2:
        state = automationhat.input.two.read()
    elif channel == 3:
        state = automationhat.input.three.read()
    else:
        state = False
        print "Specified input channel is out of range."
    print "Input value = ", state
    return int(state)
def readADC(channel):
    if automationhat.is_automation_hat():
        automationhat.enable_auto_lights(True)
    if channel == 1:
        voltage = automationhat.analog.one.read()
    elif channel == 2:
        voltage = automationhat.analog.two.read()
    elif channel == 3:
        voltage = automationhat.analog.three.read()
    elif channel == 4:
        voltage = automationhat.analog.four.read()
    else:
        voltage = 0
        print "Specified analog channel is out of range."
    print "voltage value = ", voltage
    return float(voltage)
Esempio n. 7
0
def handle_command(cmd):
    if cmd is not None:
        cmd = cmd.strip()

        if cmd.startswith("auto_lights") and ":" in cmd:
            # ToDo: Allow control of autolights for each of input, output, relay and analog
            cmd, data = cmd.split(":")
            # channel = cmd.split(".")[1]
            if data in on_values:
                automationhat.enable_auto_lights(True)
            else:
                automationhat.enable_auto_lights(False)
            return

        if cmd.startswith("relay") and ":" in cmd:
            cmd, data = cmd.split(":")
            channel = cmd.split(".")[1]

            if channel in relay_index:
                channel = relay_index.index(channel)
            else:
                channel = int(channel) - 1

            if channel < 0 or channel > 2:
                error("Invalid channel: " + str(channel))
                return

            if data in on_values:
                automationhat.relay[channel].on()
            elif data in off_values:
                automationhat.relay[channel].off()
            elif data in toggle_values:
                automationhat.relay[channel].toggle()
            else:
                error("Unhandled relay value: '" + data + "'")
            return

        if cmd.startswith("output") and ":" in cmd:
            cmd, data = cmd.split(":")
            channel = cmd.split(".")[1]

            if channel in output_index:
                channel = output_index.index(channel)
            else:
                channel = int(channel) - 1

            if channel < 0 or channel > 2:
                error("Invalid channel: " + str(channel))
                return

            if data in on_values:
                automationhat.output[channel].on()
            elif data in off_values:
                automationhat.output[channel].off()
            elif data in toggle_values:
                automationhat.output[channel].toggle()
            else:
                error("Unhandled output value: '" + data + "'")
            return

        if cmd.startswith("light.") and ":" in cmd:
            # ToDo: This doesn't support switching only specific lights associated with the input/output/relays.
            cmd, data = cmd.split(":")
            channel = cmd.split(".")[1]

            if channel in light_index:
                channel = light_index.index(channel)
            else:
                channel = int(channel) - 1

            if channel < 0 or channel > 2:
                error("Invalid channel: " + str(channel))
                return

            if data in on_values:
                automationhat.light[channel].on()
            elif data in off_values:
                automationhat.light[channel].off()
            elif data in toggle_values:
                automationhat.light[channel].toggle()
            else:
                error("Unhandled output value: '" + data + "'")
            return

        if cmd == "stop":
            stdin.stop()
            running = False
Esempio n. 8
0
def error(message):
    emit("ERROR: " + message)

def fatal(message):
    emit("FATAL: " + message)
    sys.exit(1)


try:
    import automationhat
except ImportError:
    fatal("Unable to import automationhat python library")

if automationhat.is_automation_hat() or automationhat.is_automation_phat():
    automationhat.enable_auto_lights(True)
else:
    fatal("automationHAT/automationPHAT not detected")

running = True

stdin = NonBlockingStreamReader(sys.stdin)

# pin_index = {'one':1, 'two':2, 'three':3, 'four':4}

# def handle_input(pin):
#     emit("input.{}:{}".format(pin_index[pin.name],pin.read()))

# automationhat.input.changed(handle_input)

# last_change = [0,0,0,0]
#!/usr/bin/python
import sys
import time
import os
import automationhat

time.sleep(1)

# setup pimoroni
automationhat.enable_auto_lights(False)
automationhat.analog.one.auto_light(False)
automationhat.analog.two.auto_light(False)
automationhat.analog.one.light.off()
automationhat.analog.two.light.off()
automationhat.light.comms.off()
automationhat.light.warn.off()
automationhat.light.power.off()
time.sleep(1)
automationhat.output.one.on()
automationhat.output.two.on()
time.sleep(2)  # let it settle


def map(value, leftMin, leftMax, rightMin, rightMax):
    leftSpan = leftMax - leftMin
    rightSpan = rightMax - rightMin
    valueScaled = float(value - leftMin) / float(leftSpan)
    return rightMin + (valueScaled * rightSpan)


def cut(value, minValue, maxValue):
Esempio n. 10
0
SERVER_ADDRESS = "0.0.0.0"  # LISTENING ADDRESS
SERVER_PORT = 7000  # LISTENING PORT
PRIMARY_ADDRESS = "10.0.0.123"  # SEND TO PRIMARY
BACKUP_ADDRESS = "172.22.22.108"  # SEND TO BACKUP
CLIENT_PORT = 8000  # SEND TO PORT
HBTIMEOUT = 150  # Heartbeat timeout

HOSTNAME = socket.gethostname()
INPUTSTATE = [0, 0, 0]
ADCSTATE = [0, 0, 0, 0]
TIMER = 0

print(HOSTNAME)

if automationhat.is_automation_hat():
    automationhat.enable_auto_lights(
        True)  ##### Set to False for better ADC performance
    automationhat.light.power.write(1)
    automationhat.light.comms.write(0)
    automationhat.light.warn.write(0)
    print("Automation Hat library version: " + automationhat.__version__)

log = logging.getLogger('OSC Automation Hat')
log.addHandler(journal.JournaldLogHandler())
log.setLevel(logging.INFO)


def quit_handler(signal_received, frame):
    # Handle any cleanup here
    print('SIGINT or CTRL-C detected.')
    automationhat.light.power.write(0)
    server.shutdown()
def writeAutoLightMode(state):
    if automationhat.is_automation_hat():
        automationhat.enable_auto_lights(state)
    return 0
def writeWarnLight(state):
    if automationhat.is_automation_hat():
        automationhat.enable_auto_lights(False)
        automationhat.light.warn.write(state)
    return 0
def writeCommsLight(state):
    if automationhat.is_automation_hat():
        automationhat.enable_auto_lights(False)
        automationhat.light.comms.write(state)
    return 0
def writePowerLight(state):
    if automationhat.is_automation_hat():
        automationhat.enable_auto_lights(False)
        automationhat.light.power.write(state)
    return 0