Ejemplo n.º 1
0
 def __init__(self):
     self.__dict__ = self._shared_state
     self.parport = ParPort()
     if not hasattr(self, 'last_send'):
         self.last_send = datetime.now()
     if hasattr(self, 'inited'):
         if self.inited:
             log.warning('LCD connection has already been established')
         else:
             log.warning('re-establishing LCD connection')
             self._cominit()
     else:
         self.inited = False
         self.comlock = Lock()
         self._cominit()
Ejemplo n.º 2
0
class LCD(object):
    _shared_state = {}

    def _cominit(self):
        with self.comlock:
            self.comconn = serial.Serial(
                port=settings.LCD_PORT,
                baudrate=9600,
                parity=serial.PARITY_NONE,
                stopbits=serial.STOPBITS_ONE,
                bytesize=serial.EIGHTBITS,
            )
            self.comconn.open()
            assert self.comconn.isOpen()
            self.inited = True
            log.info('LCD connection established')

    def __init__(self):
        self.__dict__ = self._shared_state
        self.parport = ParPort()
        if not hasattr(self, 'last_send'):
            self.last_send = datetime.now()
        if hasattr(self, 'inited'):
            if self.inited:
                log.warning('LCD connection has already been established')
            else:
                log.warning('re-establishing LCD connection')
                self._cominit()
        else:
            self.inited = False
            self.comlock = Lock()
            self._cominit()

    def blank(self):
        entered = datetime.now()
        with self.comlock:
            delta = entered - self.last_send 
            if delta.total_seconds() < settings.LCD_BLANK_SECONDS:
                return
            log.info('blanking %r' % self)
            self.comconn.write(CMD_CLEAR)
            self.parport.blank()

    def send(self, msgs, ok=True):
        """Send to terminal. An `Output` object is created from `msgs`
        internally."""
        log.info('sending to %r' % self)
        self.last_send = datetime.now()
        output = Output(msgs)
        to_send = CMD_CLEAR

        for precmd, line in zip(CMD_ROWS, output.lines):
            to_send += precmd + line.msg

        with self.comlock:
            # FIXME: should not be here
            if ok:
                self.parport.order_ok()
            else:
                self.parport.order_bad()
            self.comconn.write(to_send)
            self.last_send = datetime.now()
            log.info('sent: %r' % to_send)
            log.info('preview: %s' % output.preview())

        sleep(settings.LCD_BLANK_SECONDS)
        self.blank()

    def close(self):
        with self.comlock:
            if self.comconn.isOpen():
                self.comconn.close()
            else:
                log.warning('close() called but serial connection already closed')

            self.inited = False
            log.info('LCD connection closed')

    def __del__(self):
        self.close()