Ejemplo n.º 1
0
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))
Ejemplo n.º 2
0
                        # find the reference point nearest to the assumed hands position
                        # this is where (most probably) the hands actually are
                        adjustment = 15  # just largest, since we're looking for the minimum
                        for ref_hour in range(12):
                                for ref_minute in [12, 27, 42, 57]:
                                        ref_hands = hands_from_hour_minute(ref_hour, ref_minute)
                                        ref_adjustment = klok_lib.path(ref_hands, assumed_clock_hands, 12*60-1)
                                        if abs(ref_adjustment) < abs(adjustment):
                                                adjustment = ref_adjustment
                                                clock_hands = ref_hands
                                                clock_hands_string = string_from_hour_minute(ref_hour, ref_minute)
                        if adjustment:
                            # add the adjustment to offset.txt
                            offset = int(klok_lib.read_string_from_file('offset.txt'))
                            offset += adjustment
                            klok_lib.write_string_to_file('offset.txt', str(offset))
                            logging.info("passing spoke and adding %s minutes to offset" % str(adjustment))
                            if abs(offset) > 5:
                                    logging.info("offset |%s| > 5, so recalibrating the speed" % str(offset))
                                    klok_calibrate.calibrate()
        previous_IR = current_IR
        # calculate the shortest path to move the hands to actual time
        difference = klok_lib.path(clock_hands, hands, 12*60-1)  # [minutes int]
	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"))
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))