Example #1
0
class WindowDriver(object):
    def __init__(self, full_open, step_interval=DEF_INTERVAL):
        FORMAT = '%(asctime)s %(funcName)s[%(levelname)s] %(message)s'
        logging.basicConfig(format=FORMAT, level=logging.INFO)
        self.full_open = full_open
        self.step_interval = step_interval
        self.increment_size = self.full_open / NUM_INCREMENTS
        cur_pos = self._getWindowPosition()
        self.commanded_pos = cur_pos
        self.exit = False
        logging.debug('Starting driver thread')
        self.driver_thread = DriverThread(self, self.step_interval, cur_pos)
        self.driver_thread.daemon = True
        self.driver_thread.start()
        # Wait for driver thread to setup GPIO
        sleep(0.5)

    def _getWindowPosition(self):
        f = open(windowStateFile, 'r')
        steps = int(f.readline().strip())
        f.close()
        logging.info('position {0}'.format(steps))
        return steps

    def _writeWindowPosition(self, position):
        f = open(windowStateFile, 'w')
        f.write(str(int(position)))
        f.flush()
        f.close()

    def _resolve_to_increment(self, exact_pos):
        increment_pos = exact_pos - (exact_pos % self.increment_size)
        return increment_pos

    def set_position(self, percent_open):
        '''
        Set new window position as a percent of open. 100 is full open
        '''
        if (percent_open > 100 or percent_open < 0):
            raise ValueError('Percent open {0} must be between 0 and 100'.format(percent_open))
        exact_pos = self.full_open * percent_open / 100
        self.commanded_pos = self._resolve_to_increment(exact_pos)
        self._writeWindowPosition(self.commanded_pos)

        msg = 'Percent open: {0} '.format(percent_open)
        msg += 'Exact pos: {0} '.format(exact_pos)
        logging.info(msg)
        msg = 'Commanded pos: {0} '.format(self.commanded_pos)
        msg += 'Actual pos: {0} '.format(self.get_actual_position())
        logging.info(msg)

    def get_commanded_position(self):
        return self.commanded_pos

    def get_actual_position(self):
        return self.driver_thread.actual_position
Example #2
0
 def __init__(self, full_open, step_interval=DEF_INTERVAL):
     FORMAT = '%(asctime)s %(funcName)s[%(levelname)s] %(message)s'
     logging.basicConfig(format=FORMAT, level=logging.INFO)
     self.full_open = full_open
     self.step_interval = step_interval
     self.increment_size = self.full_open / NUM_INCREMENTS
     cur_pos = self._getWindowPosition()
     self.commanded_pos = cur_pos
     self.exit = False
     logging.debug('Starting driver thread')
     self.driver_thread = DriverThread(self, self.step_interval, cur_pos)
     self.driver_thread.daemon = True
     self.driver_thread.start()
     # Wait for driver thread to setup GPIO
     sleep(0.5)