def adjust_clock(): direction = request.query.direction minutes = int(request.query.minutes) print "Adjusting clock %s minutes %s" % (minutes, direction) offset_file = open('offset.txt', 'r+', 0) offset_file.seek(0) offset_string = offset_file.read() offset = int(offset_string) if 'back' in direction: offset -= minutes dir = True else: offset += minutes dir = False offset_string = str(offset) offset_file.seek(0) offset_file.truncate() offset_file.write(offset_string) offset_file.close() klok_lib.init() klok_lib.turn(minutes * klok_lib.quarter_turns_per_minute / float(4), brake=2, dir=dir) # return the HTML we want the user to see on screen return "<b>Adjustment done</b>"
def calibrate_clock(): offset_file = open('offset.txt', 'r+', 0) offset_file.seek(0) offset_string = offset_file.read() offset = int(offset_string) new_offset_string = str(0) offset_file.seek(0) offset_file.truncate() offset_file.write(new_offset_string) offset_file.close() calibration_file = open('calibration.txt', 'r+', 0) calibration_file.seek(0) calibration_string = calibration_file.read() calibration = float(calibration_string) new_calibration = calendar.timegm(time.gmtime()) / 60.0 new_calibration_string = str(new_calibration) calibration_file.seek(0) calibration_file.truncate() calibration_file.write(new_calibration_string) calibration_file.close() minutes_since_calibration = new_calibration - calibration correction_file = open('correction.txt', 'r+', 0) correction_file.seek(0) correction_string = correction_file.read() correction = float(correction_string) new_correction = correction * (minutes_since_calibration + offset) / minutes_since_calibration new_correction_string = str(new_correction) correction_file.seek(0) correction_file.truncate() correction_file.write(new_correction_string) correction_file.close() print >> klok_lib.log, "Calibration offset: %s -> %s; calibration: %s -> %s; correction: %s -> %s" % ( offset_string, new_offset_string, calibration_string, new_calibration_string, correction_string, new_correction_string) klok_lib.init() klok_lib.turn(1, brake=2, step=klok_lib.bells_step)
import klok_lib import calendar import time def calibrate(): # read and reset offset offset = int(klok_lib.read_string_from_file('offset.txt')) # [int min] new_offset = 0 klok_lib.write_string_to_file('offset.txt', str(new_offset)) # read time of last calibration, reset it and calculate minutes passed calibration = float(klok_lib.read_string_from_file('calibration.txt')) # [int min last calibration since epoch] new_calibration = calendar.timegm(time.gmtime()) / 60.0 # [int min now since epoch] klok_lib.write_string_to_file('calibration.txt', str(new_calibration)) minutes_since_calibration = new_calibration - calibration # [int min] # read quarter_turns_per_minute_correction from file, calculate new correction and store it correction = float(klok_lib.read_string_from_file('correction.txt')) # [float factor] new_correction = correction * (minutes_since_calibration + offset) / minutes_since_calibration # [float factor] klok_lib.write_string_to_file('correction.txt', str(new_correction)) print >> klok_lib.log, "Calibration offset: %s -> %s; calibration: %s -> %s; correction: %s -> %s" % (str(offset), str(new_offset), str(calibration), str(new_calibration), str(correction), str(new_correction)) klok_lib.init() calibrate() klok_lib.turn(1, brake=2, step=klok_lib.bells_step)
direction = False if difference > 0 else True # [boolean] when hands are behind, False, meaning to move forward difference = abs(difference) if difference > 0: logging.info("* * * " + str(now) + " * * *") logging.info("assumed hands: %s" % assumed_clock_hands_string) if adjustment: logging.info("spoke confirmed hands; %s" % clock_hands_string) logging.info("going to move the hands %d minutes %s" % (difference, "backward" if direction else "forward")) logging.info("new hands: %s" % hands_string) # calculate number of turns with correction applied count = difference * klok_lib.quarter_turns_per_minute * klok_lib.quarter_turns_per_minute_correction / float(4) # [turns float] logging.info("quarter_turns_per_minute = %f" % klok_lib.quarter_turns_per_minute) logging.info("quarter_turns_per_minute_correction = %f" % klok_lib.quarter_turns_per_minute_correction) logging.info("count = %f" % count) # drive the motor for the hands klok_lib.turn(count, dir=direction) logging.info("moved hands") # compose new assumed hands position string and write it to file klok_lib.write_string_to_file('hands.txt', hands_string) # allow sounds chime_done = False bells_done = False if minute in [0, 15, 30, 45] and not chime_done and not os.path.isfile(klok_silence_file): chimes_count = minute / 15 if minute > 1 else 4 logging.info("going to sound %d chimes" % chimes_count) for i in range(0, chimes_count): klok_lib.turn(0.5, brake=6, step=klok_lib.chime_step) chime_done = True if minute == 0 and not bells_done and not os.path.isfile(klok_silence_file): bells_count = hour if hour > 0 else 12 logging.info("going to sound the bells %d times" % bells_count)
import klok_lib import RPi.GPIO as GPIO klok_lib.init() try: while True: # half a sound klok_lib.turn(0.05, brake=2, step=klok_lib.chime_step) except KeyboardInterrupt: GPIO.output(klok_lib.sleep, False) print 'Interrupted!'
import klok_lib offset = int(klok_lib.read_string_from_file('offset.txt')) offset += 10 klok_lib.write_string_to_file('offset.txt', str(offset)) klok_lib.init() klok_lib.turn(10 * klok_lib.quarter_turns_per_minute / float(4))
#!/usr/bin/python import klok_lib # initialize the GPIO klok_lib.init() # calculate turns for 1 minute count = klok_lib.quarter_turns_per_minute * klok_lib.quarter_turns_per_minute_correction / float( 4) # [turns float] # drive the motor for the hands klok_lib.turn(count)
import klok_lib offset = int(klok_lib.read_string_from_file('offset.txt')) offset -= 1 klok_lib.write_string_to_file('offset.txt', str(offset)) klok_lib.init() klok_lib.turn(klok_lib.quarter_turns_per_minute / float(4), dir=True)
import klok_lib import RPi.GPIO as GPIO klok_lib.init() # 5 tones (actually 4 + silence) klok_lib.turn(0.5, brake=8, step=klok_lib.chime_step) GPIO.output(klok_lib.sleep, False)