# Path: pym2149/dac.py
# class PWMEffect(FixedLevelEffect):
#     'Interrupt setup/routine alternates fixed level between fixedreg and zero.'
# 
#     level4toshape = level4totone5shape
# 
# Path: pym2149/mfp.py
# class MFPTimer:
# 
#     def __init__(self):
#         self.control = Reg()
#         self.data = Reg()
#         # TODO LATER: Verify that TDR 0 indeed behaves like 0x100.
#         self.effectivedata = Reg().link(lambda tdr: tdr if tdr else 0x100, self.data)
#         self.control.value = 0
#         self.data.value = 0
#         self.effect = Reg(NullEffect)
#         self.control_data = Reg()
#         self.control.link(lambda cd: cd[0], self.control_data)
#         self.data.link(lambda cd: cd[1], self.control_data)
#         self.freq = Reg()
#         # XXX: Should change of wavelength trigger this link?
#         self.control_data.link(self._findtcrtdr, self.freq)
#         self.prescalerornone = Reg().link(lambda tcr: prescalers.get(tcr), self.control)
#         self.repeat = Reg().link(lambda _: None, self.effect)
# 
#     def update(self, tcr, tdr, effect):
#         self.control_data.value = tcr, tdr
#         self.effect.value = effect
# 
#     def _findtcrtdr(self, freq):
#         if not freq:
#             return 0, self.data.value # Stop timer.
#         diff = float('inf')
#         for tcr, prescaler in prescalers.items():
#             prescaler *= self.effect.value.wavelength # Avoid having to multiply twice.
#             etdr = int(round(mfpclock / (freq * prescaler)))
#             if 1 <= etdr and etdr <= 0x100:
#                 d = abs(mfpclock / (etdr * prescaler) - freq)
#                 if d < diff:
#                     tcrtdr = tcr, etdr & 0xff
#                     diff = d
#         return tcrtdr
# 
#     def _getnormperiod(self):
#         return prescalers[self.control.value] * self.effectivedata.value * self.effect.value.wavelength
# 
#     def getfreq(self): # Currently only called when effect is not None.
#         return mfpclock / self._getnormperiod()


from .dac import PWMEffect
from .mfp import MFPTimer
from unittest import TestCase
# Copyright 2014, 2018, 2019, 2020 Andrzej Cichocki

# This file is part of pym2149.
#
# pym2149 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 3 of the License, or
# (at your option) any later version.
#
# pym2149 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 pym2149.  If not, see <http://www.gnu.org/licenses/>.


class TestMFPTimer(TestCase):

    def test_setfreq(self):
        timer = MFPTimer()

# Path: pym2149/dac.py
# class PWMEffect(FixedLevelEffect):
#     'Interrupt setup/routine alternates fixed level between fixedreg and zero.'
# 
#     level4toshape = level4totone5shape
# 
# Path: pym2149/mfp.py
# class MFPTimer:
# 
#     def __init__(self):
#         self.control = Reg()
#         self.data = Reg()
#         # TODO LATER: Verify that TDR 0 indeed behaves like 0x100.
#         self.effectivedata = Reg().link(lambda tdr: tdr if tdr else 0x100, self.data)
#         self.control.value = 0
#         self.data.value = 0
#         self.effect = Reg(NullEffect)
#         self.control_data = Reg()
#         self.control.link(lambda cd: cd[0], self.control_data)
#         self.data.link(lambda cd: cd[1], self.control_data)
#         self.freq = Reg()
#         # XXX: Should change of wavelength trigger this link?
#         self.control_data.link(self._findtcrtdr, self.freq)
#         self.prescalerornone = Reg().link(lambda tcr: prescalers.get(tcr), self.control)
#         self.repeat = Reg().link(lambda _: None, self.effect)
# 
#     def update(self, tcr, tdr, effect):
#         self.control_data.value = tcr, tdr
#         self.effect.value = effect
# 
#     def _findtcrtdr(self, freq):
#         if not freq:
#             return 0, self.data.value # Stop timer.
#         diff = float('inf')
#         for tcr, prescaler in prescalers.items():
#             prescaler *= self.effect.value.wavelength # Avoid having to multiply twice.
#             etdr = int(round(mfpclock / (freq * prescaler)))
#             if 1 <= etdr and etdr <= 0x100:
#                 d = abs(mfpclock / (etdr * prescaler) - freq)
#                 if d < diff:
#                     tcrtdr = tcr, etdr & 0xff
#                     diff = d
#         return tcrtdr
# 
#     def _getnormperiod(self):
#         return prescalers[self.control.value] * self.effectivedata.value * self.effect.value.wavelength
# 
#     def getfreq(self): # Currently only called when effect is not None.
#         return mfpclock / self._getnormperiod()


# Path: pym2149/test_mfp.py
from .dac import PWMEffect
from .mfp import MFPTimer
from unittest import TestCase

# Copyright 2014, 2018, 2019, 2020 Andrzej Cichocki

# This file is part of pym2149.
#
# pym2149 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 3 of the License, or
# (at your option) any later version.
#
# pym2149 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 pym2149.  If not, see <http://www.gnu.org/licenses/>.


class TestMFPTimer(TestCase):

    def test_setfreq(self):
        timer = MFPTimer()

        timer.effect.value = PWMEffect