# *****************************************************************************
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# Module authors:
#   Markus Zolliker <markus.zolliker@psi.ch>
#   Leon Zimmermann <leon.zimmermann@psi.ch>
# *****************************************************************************
"""Powersupply TDK-Lambda GEN8-400-3P400"""

from frappy.core import StringIO, Readable, Parameter, Writable, HasIO
from frappy.datatypes import BoolType, EnumType, FloatRange


class IO(StringIO):
    end_of_line = '\r'
    default_settings = {'baudrate': 9600}
    identification = [('ADR 0', 'OK'), ('IDN?', r'LAMBDA,GEN8-400')]


class Power(HasIO, Readable):
    ioClass = IO
    value = Parameter(datatype=FloatRange(0,3300,unit='W'))
    voltage = Parameter('voltage', FloatRange(0,8, unit='V'))
    current = Parameter('current', FloatRange(0,400, unit='A'))

    def read_value(self):
        self.voltage = float(self.communicate('MV?'))
        self.current = float(self.communicate('MC?'))
        return self.voltage * self.current

    
class Output(HasIO, Writable):
    ioClass = IO
    value = Parameter(datatype=FloatRange(0,100,unit='%'), default=0)
    target = Parameter(datatype=FloatRange(0,100,unit='%'))
    mode = Parameter('regulation mode', EnumType(voltage=1, current=2, both=3),
                     default='voltage', readonly=False)
    maxvolt = Parameter('voltage at 100%',
                        datatype=FloatRange(0,8,unit='V'), readonly=False)
    maxcurrent = Parameter('current at 100%',
                           datatype=FloatRange(0,400,unit='A'), readonly=False)
    output_enable = Parameter('control on/off', BoolType(), readonly=False)
    
    def initModule(self):
        super().initModule()
        self.write_output_enable(False)
        
    def write_target(self, target):
        # take care of proper order
        if target == 0:
            self.write_output_enable(False)
        prev_curr = float(self.communicate(f'PC?'))
        volt = self.maxvolt if self.mode == 'current' else self.maxvolt * 0.01 * target
        curr = self.maxcurrent if self.mode == 'voltage' else self.maxcurrent * 0.01 * target
        if curr < prev_curr:
            self.communicate(f'PC {curr:.6g}')
            self.communicate(f'PV {volt:.6g}')
        else:
            self.communicate(f'PV {volt:.6g}')
            self.communicate(f'PC {curr:.6g}')
        if target:
            self.write_output_enable(True)
        self.value = target

    def write_output_enable(self, value):
        self.communicate(f'OUT {int(value)}')
        
    def shutdown(self):
        self.write_target(0)
    
