class DisplayDriver(object): """ This class represents the display driver that links the raspberry pi to the display. This driver will have high- level control over the data being sent to the display, but will do this mostly through the help of smaller utility classes. """ logger = logging.getLogger("display") def __init__(self): """ Ctor - Initialises the display driver, setting up the database helper and display controller. """ self._db_helper = DatabaseHelper(db_name) self._display_controller = DisplayController(serial_name, serial_baudrate) def start(self): """ Starts the application: begins checking the current time and working out what information needs to be sent to the display at that time. """ mode = ScreensaverMode(self._db_helper.get_random_screensaver()) last_pattern = None last_check_time = datetime.datetime.now() - datetime.timedelta(minutes=10) while True: # Check the current time and make it accurate to the second current_time = datetime.datetime.now() min_current_time = minutify(current_time) if (current_time.minute % 5) == 0 and (min_current_time > last_check_time): # If the current time is on one of the five min. intervals, attempt to pull a pattern from the # internal database. last_check_time = min_current_time t_format = "%Y-%m-%d %H:%M:00.000000" pattern = self._db_helper.get_run_for_time(current_time.strftime(t_format)) if pattern: # If a pattern is successfully pulled, run the display's 'run mode' on it. mode = RunMode(pattern) else: # Otherwise pull a random screensaver from the internal database and run that in 'screensaver # mode' mode = ScreensaverMode(self._db_helper.get_random_screensaver(), last_pattern) if not mode.is_active(): # If the current pattern finishes before the five minute timer has stopped, switch to # screensaver mode mode = ScreensaverMode(self._db_helper.get_random_screensaver(), last_pattern) # Retrieve the latest pattern to be sent to the display last_pattern = mode.get_display_pattern() # Send the pattern to the display controller for printing. self._display_controller.output_pattern(last_pattern) # While not working, sleep sleep_until_time = current_time + datetime.timedelta(seconds=sleep_time) time.sleep((sleep_until_time - datetime.datetime.now()).microseconds / 1000000) self.logger.warn("Outputted pattern at: %s" % current_time)
def test__draw(self, serial_mock): """ Test the private set function of DisplayController """ serial = MagicMock() serial.write = MagicMock() dc = DisplayController(0, 9600) dc._connection = serial dc._draw() serial.write.assert_called_once_with('3')
def test__set(self, serial_mock): """ Test the private set function of DisplayController """ serial = MagicMock() serial.write = MagicMock() dc = DisplayController(0, 9600) dc._connection = serial dc._set(0, 0) serial.write.assert_has_calls([ call('1'), call('0'), call('0') ])
def test_output_pattern(self, serial_mock): """ This method ensures the prototype display controller can correctly output a pattern to the DisplayController. """ dc = DisplayController(0, 9600) dc._connection = MagicMock() dc._clear = MagicMock() dc._set = MagicMock() dc._draw = MagicMock() dc.output_pattern("-*-\n-*-\n-*-") # Ensure clear and draw were called once dc._clear.assert_called_once_with() dc._draw.assert_called_once_with() # Test the set calls dc._set.assert_has_calls([ call(1, 0), call(1, 1), call(1, 2) ])
def __init__(self): """ Ctor - Initialises the display driver, setting up the database helper and display controller. """ self._db_helper = DatabaseHelper(db_name) self._display_controller = DisplayController(serial_name, serial_baudrate)