/**
 * /MotorBroker.cpp
 * @version 1.0
 * @desc Body for the motor broker
 * @author Fándly Gergő Zoltán (fandlygergo@gmail.hu, systemtest.tk)
 * @copy 2018 Fándly Gergő Zoltán
 * License:
    Firmware for the Profile F1 fertilizer machine.
    Copyright (C) 2018  Fándly Gergő Zoltán

    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 3 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, see <https://www.gnu.org/licenses/>.
 **/

#incldue "modules.h"

MotorBroker::MotorBroker(){
    //init motors
    initMotors();
    return;
}

void MotorBroker::addExtrude(float pump0, float pump1, float pump2, float pump3, float pump4){
    extrude[0]+=pump0*STEPS_PER_ML;
    extrude[1]+=pump1*STEPS_PER_ML;
    extrude[2]+=pump2*STEPS_PER_ML;
    extrude[3]+=pump3*STEPS_PER_ML;
    extrude[4]+=pump4*STEPS_PER_ML;
}

void MotorBroker::run(){
    //run the motors if it is needed
    for(int i=0; i<5; i++){
        if(extrude[i]){
            int toMove=extrude[i]>=STEPS_PER_ROUND?STEPS_PER_ROUND:extrude[i];
            motors[i].move(true, toMove);
            extrude[i]-=toMove;
        }
    }
}

void MotorBroker::initMotors(){
    //init first motor which is wired directly to the AtMega
    motors[0]=CheapMCPStepper(NULL, 10, 11, 12, 13);

    //init other motors if needed.
    int mcpPin=0;
    for(int i=1; i<memory.getMotorCount(); i++){
        motors[i]=CheapMCPStepper(&mcp, mcpPin, mcpPin+1, mcpPin+2, mcpPin+3);
        mcpPin+=4;
    }
}
