/*
 * MotorController — 直流电机控制器实现
 */
#include "MotorController.h"

MotorController::MotorController(int pwmchip, int pwmnum, int gpioNum, unsigned int period_ns)
    : pwmController(pwmchip, pwmnum), directionGPIO(gpioNum)
{
    pwmController.setPeriod(period_ns);
    directionGPIO.setDirection("out");
    pwmController.enable();
}

MotorController::~MotorController(void)
{
    pwmController.disable();
}

void MotorController::updateduty(double dutyCycle)
{
    int newduty = pwmController.readPeriod() * std::abs(dutyCycle) / 100.0;
    if (newduty != pwmController.readDutyCycle())
        pwmController.setDutyCycle(newduty);

    directionGPIO.setValue(dutyCycle > 0);
}
