Exemplo n.º 1
0
def loop():
    global timestamp
    if current_status:
        if pwm_pin.duty() and millis_passed(timestamp) > 100:
            timestamp = get_millis()
            pwm_pin.duty(0)
        elif not pwm_pin.duty() and millis_passed(timestamp) >= 900:
            timestamp = get_millis()
            pwm_pin.duty(DUTY)
    else:
        if pwm_pin.duty():
            pwm_pin.duty(0)
Exemplo n.º 2
0
def loop():
    global timestamp
    if current_status:
        if led_pin.value() and millis_passed(timestamp) > 100:
            timestamp = get_millis()
            led_pin.off()
        elif not led_pin.value() and millis_passed(timestamp) >= sleep_timeout:
            timestamp = get_millis()
            led_pin.on()
    else:
        if led_pin.value():
            led_pin.off()
async def environment_sensors_action():
    print("[SENSORS]: environment_sensors_action")
    while True:
        for sensor in environment_sensors:
            if sensor.timestamp is None or common.millis_passed(
                    sensor.timestamp) >= sensor.timeout:
                sensor.timestamp = common.get_millis()
                await sensor.action()
                if sensor.dirty:
                    sensor.dirty = False
                    if "_ENV" in sensor.alias:
                        for key in sensor.data:
                            if on_state_change_cb is not None:
                                on_state_change_cb(f"{sensor.alias}_{key}",
                                                   sensor.data[key])
                    else:
                        if on_state_change_cb is not None:
                            on_state_change_cb(sensor.alias, sensor.data)

                if sensor.error_msg is not None:
                    if on_state_change_cb is not None:
                        on_state_change_cb(f"{sensor.alias}_ERROR",
                                           sensor.error_msg)
                    sensor.error_msg = None
            await asyncio.sleep_ms(0)
def loop():
    global beep_off_if_no_location_timestamp
    location = get_location()
    if location:
        timestamp = get_millis()
        lines = []
        lines.append("%s:%d" % (get_status(location), location.hacc))
        lines.append("TIME: %d" % (location.time))
        lines.append("LAT: %f" % (location.lat))
        lines.append("LON: %f" % (location.lon))
        if (location.hacc <= 100):
            beep_off_if_no_location_timestamp = get_millis()
            nearest_station = get_nearest_station(location, stations)
            if nearest_station:
                distance = get_distance(location, nearest_station)
                lines.append("%.1fkm in %s" % (distance, nearest_station.name))
                lines.append("%.1fkm/h" % (location.speed))
                if (distance <= 1.0 and not leds.is_on()):
                    leds.set(True)
                if (distance > 1.0 and leds.is_on()):
                    leds.set(False)
                if (distance <= 0.3 and not pwm.is_beep()):
                    pwm.set_beep(True)
                    leds.set_sleep_timeout(400)
                if (distance > 0.3 and pwm.is_beep()):
                    pwm.set_beep(False)
                    leds.set_sleep_timeout()
            else:
                print("ERROR: no nearest station?")
            print("duration %d" % (millis_passed(timestamp)))
        else:
            if pwm.is_beep():
                pwm.set_beep(False)
            if leds.is_on():
                leds.set(False)
        oled.display_lines(lines)

    if beep_off_if_no_location_timestamp != 0 and millis_passed(
            beep_off_if_no_location_timestamp) > 10000:
        beep_off_if_no_location_timestamp = 0
        print("No location in 10 seconds, turning off")
        if pwm.is_beep():
            pwm.set_beep(False)
        if leds.is_on():
            leds.set(False)
def action():
    global timestamp, sent_impulses
    if timestamp is None or common.millis_passed(timestamp) >= timeout:
        sent_impulses = impulses
        value = sent_impulses / IMPULSES_PER_KWH
        timestamp = common.get_millis()
        print("[POWER_COUNTER]: %f kwh" % (value))
        if on_state_change_cb != None:
            on_state_change_cb("POWER_COUNTER", value)
Exemplo n.º 6
0
def check_button():
    global button_timestamp
    if millis_passed(button_timestamp) < 50:
        return
    button_timestamp = get_millis()
    global button_state
    state = not button.value()
    if state != button_state:
        button_state = state
        on_button_callback(button_state)
Exemplo n.º 7
0
def check_rollos_timeout():
    for key in rollos:
        rollo = rollos[key]
        if rollo.up.active or rollo.down.active:
            if common.millis_passed(rollo.timestamp) >= rollo.timeout:
                set_rollos(rollo, "STOP")
                if rollo.first_time_position is not None:
                    if rollo.current_position is None:
                        rollo.first_time_position = None
                    else:
                        set_rollos(rollo, rollo.first_time_position)
                        rollo.first_time_position = None
async def process_time_measure(timeout=20):
    print("[RUNNER]: start process_time_measure")
    timestamp = common.get_millis()
    bigest = 0
    while True:
        await asyncio.sleep(0)
        timepassed = common.millis_passed(timestamp)
        if timepassed >= timeout:
            if timepassed > bigest:
                bigest = timepassed
            print("[RUNNER]: timeout warning %d ms with bigest %d" %
                  (timepassed, bigest))
        timestamp = common.get_millis()
Exemplo n.º 9
0
def set_rollos(rollo, data):
    print("[PHY]: set_rollos[%s], data[%s]" % (rollo.path, data))
    if data == "STOP":
        if rollo.alarm.active == False:
            leds.set_state_by_name(rollo.up.pressed_light, 0)
            leds.set_state_by_name(rollo.up.idle_light, 1)
            leds.set_state_by_name(rollo.down.pressed_light, 0)
            leds.set_state_by_name(rollo.down.idle_light, 1)
        leds.set_state_by_name(rollo.up.relay, 0)
        if rollo.up.active:
            if common.millis_passed(rollo.timestamp) >= rollo.max_timeout:
                rollo.current_position = 100
            else:
                if rollo.current_position is not None:
                    rollo.current_position = rollo.current_position + int(
                        common.millis_passed(rollo.timestamp) /
                        rollo.max_timeout * 100)
                    if rollo.current_position > 100:
                        rollo.current_position = 100
        elif rollo.down.active:
            if common.millis_passed(rollo.timestamp) >= rollo.max_timeout:
                rollo.current_position = 0
            else:
                if rollo.current_position is not None:
                    rollo.current_position = rollo.current_position - int(
                        common.millis_passed(rollo.timestamp) /
                        rollo.max_timeout * 100)
                    if rollo.current_position < 0:
                        rollo.current_position = 0
        print("[PHY]: rollo[%s], position[%s]" %
              (rollo.path, str(rollo.current_position)))
        if on_state_change_cb is not None:
            on_state_change_cb(rollo.path, str(rollo.current_position))
        rollo.up.active = False
        leds.set_state_by_name(rollo.down.relay, 0)
        rollo.down.active = False
        rollo.timeout = None
        rollo.timestamp = None
    else:
        percent = get_percent_from_data(data)
        if percent is not None:
            direction_up = False
            direction_down = False
            timeout = None
            if percent == 100:
                direction_up = True
                timeout = rollo.max_timeout
            elif percent == 0:
                direction_down = True
                timeout = rollo.max_timeout
            else:
                if rollo.current_position is not None:
                    move_percent = percent - rollo.current_position
                    timeout = abs(int(move_percent / 100 * rollo.max_timeout))
                    if move_percent > 0:
                        direction_up = True
                    elif move_percent < 0:
                        direction_down = True
                else:
                    rollo.first_time_position = percent
                    direction_up = True
                    timeout = rollo.max_timeout

            if direction_up:
                if rollo.alarm.active == False:
                    leds.set_state_by_name(rollo.up.pressed_light, 0)
                    leds.set_state_by_name(rollo.up.idle_light, 1)
                if rollo.down.active:
                    leds.set_state_by_name(rollo.down.relay, 0)
                    rollo.down.active = False
                leds.set_state_by_name(rollo.up.relay, 1)
                rollo.up.active = True
                rollo.timeout = timeout
                rollo.timestamp = common.get_millis()
            elif direction_down:
                if rollo.alarm.active == False:
                    leds.set_state_by_name(rollo.down.pressed_light, 0)
                    leds.set_state_by_name(rollo.down.idle_light, 1)
                if rollo.up.active:
                    leds.set_state_by_name(rollo.up.relay, 0)
                    rollo.up.active = False
                leds.set_state_by_name(rollo.down.relay, 1)
                rollo.down.active = True
                rollo.timeout = timeout
                rollo.timestamp = common.get_millis()
def check_signal_led_activity():
    global activity_timestamp
    if activity_timestamp is not None and common.millis_passed(activity_timestamp) >= activity_timeout:
        activity_timestamp = None
        leds.set_state_by_name(ACTIVITY_LED, 0)
def send_mqtt_uptime():
    global timestamp
    if (millis_passed(timestamp) > 30000):
        timestamp = get_millis()
        print("uptime %d" % (timestamp))
        client.publish(TOPIC, "uptime %d".encode() % (timestamp))