Example #1
0
class MonitorOption(object):
    STATUS = enum("INIT", "SAMPLING")

    def __init__(self, quantity, **kwargs):
        if quantity <= 0:
            raise ValueError("'quantity' must be a positive integer")

        self.quantity = quantity
        self.fake_samples = kwargs["fake_samples"]
        self.debug = kwargs["debug"]
        self.output = kwargs["output_fd"]
        self.config = Config(kwargs["calibration_filename"], False)

        self.status = self.STATUS.INIT

    def init(self):
        """
        Do a handshake:
        1. Open a serial connection with Arduino.
        2. Send a STOP REQUEST.
        3. Wait for an OK RESPONSE.
        """
        self.arduino = Arduino(RESPONSE_SIZE, debug=self.debug)
        self.arduino.start()

        self.arduino.send_message(enc_stop_request())
        message = self.arduino.read_message()
        opcode, data = dec_message(message)

        if opcode != RESPONSE.OK:
            raise IOError("Expecting OK, got %s"
                % RESPONSE.reverse[opcode])

    def run(self):
        """Run the function with same name as current status."""
        getattr(self, "status_"
                + self.STATUS.reverse[self.status].lower())()

    def sigint_handler(self):
        self.arduino.send_message(enc_stop_request())
        self.arduino.close()

    def send_monitor_request(self, mode):
        self.arduino.send_message(enc_monitor_request(mode,
            self.fake_samples, self.quantity,
            self.config.calibration("phase"),
            self.config.calibration("voltage_offset"),
            self.config.calibration("current_offset")))

    def print_data(self, data):
        data = self.unpack_data(data)
        out = ' '.join("%f" % d for d in data) + '\n'
        # sys.stderr.write('MONITOR: ' + out)
        self.output.write(out)
        self.output.flush()
Example #2
0
    def __init__(self, quantity, **kwargs):
        if quantity <= 0:
            raise ValueError("'quantity' must be a positive integer")

        self.quantity = quantity
        self.fake_samples = kwargs["fake_samples"]
        self.debug = kwargs["debug"]
        self.output = kwargs["output_fd"]
        self.config = Config(kwargs["calibration_filename"], False)

        self.status = self.STATUS.INIT