#!/usr/bin/python3

import sys

from dtnsense import atsci, util

def on(sensor):
    sensor.write("L,1")

def off(sensor):
    sensor.write("L,0")

def toggle(sensor):
    sensor.write("L,?")

    if sensor.read()[3] == ord('1'):
        off(sensor)
    else:
        on(sensor)

FNS = {"on": on, "off": off}

try:
    fn = FNS[sys.argv[1]]
except IndexError:
    fn = toggle

with util.GPIOGuard():
    sensor = atsci.AtSciSensor()

    sensor.switch(atsci.COND)
    fn(sensor)

    sensor.switch(atsci.PH)
    fn(sensor)
