def manual_move_end_setter(): import RPi.GPIO as GPIO from time import sleep from DBFunctions import curtain_length, current_position, set_curtain_length while True: cnx = DBFunctions.start_connection() if not cnx: continue cursor = cnx.cursor(buffered=True) if GPIO.input( ENABLE_PIN ): # motor is currently activated; wait before next check sleep(INTERVAL_BETWEEN_MANUAL_MOVEMENT_CHECKS) length = curtain_length(cursor) position = current_position(cursor) # if curtain open and position not marked at open end if GPIO.input(OPEN_STOP_PIN) and length != position: set_curtain_length(cnx, cursor, length) # if curtain closed and position not marked at closed end elif GPIO.input(CLOSED_STOP_PIN) and position: set_curtain_length(cnx, cursor, 0) sleep(INTERVAL_BETWEEN_MANUAL_MOVEMENT_CHECKS) cnx.close()
def activate_curtain(cnx, cursor, new_position_steps): from DBFunctions import curtain_length, direction, new_position total_steps = curtain_length(cursor) if not total_steps: CalibrateCurtain.calibrate( cnx, cursor) # check if curtain length not set up if total_steps - 5 <= new_position_steps: open_curtain(False ^ direction(cursor)) return new_position(cnx, cursor, total_steps) elif new_position_steps <= 5: close_curtain(True ^ direction(cursor)) return new_position(cnx, cursor, 0) # calculate steps needed to take from current position to desired steps_to_move = needed_steps(cursor, new_position_steps, total_steps) # move curtain to position: direction based on DB switch move_direction = (steps_to_move < 0) ^ direction(cursor) # pin of stop switch, into which curtain can run # position is approaching 0 (closed) if negative steps; else increasing position (open) stop_pin = CLOSED_STOP_PIN if steps_to_move < 0 else OPEN_STOP_PIN remaining_steps = move_curtain(move_direction, steps_to_move, stop_pin) final_position = curtain_final_position(new_position_steps, remaining_steps, stop_pin, total_steps) new_position(cnx, cursor, final_position) return remaining_steps # returned to see whether it failed
def manual_move_loop(): import RPi.GPIO as GPIO from time import sleep from GPIOUtility import curtain_is_closed, curtain_is_fully_open, motor_is_engaged from DBFunctions import connect_to_DB, curtain_length, \ current_position, set_current_position while True: cnx, cursor = connect_to_DB() if motor_is_engaged( ): # motor is currently activated; wait before next check sleep(INTERVAL_BETWEEN_MANUAL_MOVEMENT_CHECKS) continue length = curtain_length(cursor) position = current_position(cursor) # if curtain open and position not marked at open end if curtain_is_fully_open() and length != position: DBFunctions.set_current_position(cnx, cursor, length) # if curtain closed and position not marked at closed end elif curtain_is_closed() and position: DBFunctions.set_current_position(cnx, cursor, 0) cnx.close() sleep(INTERVAL_BETWEEN_MANUAL_MOVEMENT_CHECKS)
def calibrate_if_necessary(cnx, cursor, remaining_steps): from DBFunctions import curtain_length total_steps = curtain_length(cursor) # check if motor stopped prematurely leniency = STOPPED_PERCENT_LENIENCY * total_steps / 100 if leniency < remaining_steps: calibrate(cnx, cursor)
def sunrise_loop(): from DBFunctions import add_event, connect_to_DB, curtain_length, \ event_set_at_approximate_time, sunrise_open while True: cnx, cursor = connect_to_DB() if sunrise_open(cursor): sunrise = sunrise_time() if is_null_sleep_then(sunrise): continue open_position = curtain_length(cursor) if not event_set_at_approximate_time(cursor, open_position, sunrise): add_event(cnx, cursor, open_position, sunrise) sleep(SUNRISE_LOOP_WAIT) cnx.close()