<commit_before>/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.ociweb.iot.grove.motor_driver;

import com.ociweb.gl.api.StartupListener;
import static com.ociweb.iot.grove.motor_driver.MotorDriver_Constants.*;
import com.ociweb.iot.maker.FogCommandChannel;
import com.ociweb.iot.maker.IODeviceFacade;
import com.ociweb.pronghorn.iot.schema.I2CCommandSchema;
import com.ociweb.pronghorn.pipe.DataOutputBlobWriter;

/**
 *
 * @author huydo
 */
public class MotorDriver_Facade implements IODeviceFacade,StartupListener{
    private final FogCommandChannel target;
    public int DRIVER_I2C_ADD = 0x0f; //default address of the driver
    private int motor1Vel = 0;
    private int motor2Vel = 0;

    public enum Port {
        A,
        B
    }
    
    public MotorDriver_Facade(FogCommandChannel ch){
        this.target = ch;
    }
    
    public MotorDriver_Facade(FogCommandChannel ch,int i2cAddress){
        this.target = ch;
        this.DRIVER_I2C_ADD = i2cAddress;
    }
    
    private void direction(int _direction){
        
        DataOutputBlobWriter<I2CCommandSchema> i2cPayloadWriter = target.i2cCommandOpen(DRIVER_I2C_ADD);
        
        i2cPayloadWriter.writeByte(DIR_REG);
        i2cPayloadWriter.writeByte(_direction);
        i2cPayloadWriter.writeByte(DUMMY_BYTE);
        
        target.i2cCommandClose();
        target.i2cFlushBatch();
        target.i2cDelay(DRIVER_I2C_ADD, 4000000);
        
    }

    /**
     * Set the velocity of the motor on specified channel
     * @param port either A or B
     * @param velocity integer between -255 and 255
     */
    public void setVelocity(Port port,int velocity) {
        switch (port) {
            case A:
                setVelocity(velocity, this.motor2Vel);
                break;
            case B:
                setVelocity(this.motor1Vel, velocity);
                break;
        }
    }

    /**
     * Set the velocity of the motor on specified channel
     * @param motorAVel integer between -255 and 255
     * @param motorBVel integer between -255 and 255
     */
    public void setVelocity(int motorAVel,int motorBVel){
        this.motor1Vel = motorAVel;
        this.motor2Vel = motorBVel;
        
        if(motor1Vel >= 0 && motor2Vel >= 0){
            direction(M1CW_M2CW);
        }else if(motor1Vel < 0 && motor2Vel<0){
            direction(M1ACW_M2ACW);
        }else if(motor1Vel >= 0 && motor2Vel < 0){
            direction(M1CW_M2ACW);
        }else if(motor1Vel < 0 && motor2Vel >= 0){
            direction(M1ACW_M2CW);
        }
        
        motor1Vel = (Math.abs(motor1Vel)>255)?255:Math.abs(motor1Vel);
        motor2Vel = (Math.abs(motor2Vel)>255)?255:Math.abs(motor2Vel);
        
        DataOutputBlobWriter<I2CCommandSchema> i2cPayloadWriter = target.i2cCommandOpen(DRIVER_I2C_ADD);
        
        i2cPayloadWriter.writeByte(SPEED_REG);
        i2cPayloadWriter.writeByte(motor1Vel);
        i2cPayloadWriter.writeByte(motor2Vel);
        
        target.i2cCommandClose();
        target.i2cFlushBatch();
        target.i2cDelay(DRIVER_I2C_ADD, 4000000);
    }
    <commit_msg>Update MotorDriver_Facade.java<commit_after>/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.ociweb.iot.grove.motor_driver;

import com.ociweb.gl.api.StartupListener;
import static com.ociweb.iot.grove.motor_driver.MotorDriver_Constants.*;
import com.ociweb.iot.maker.FogCommandChannel;
import com.ociweb.iot.maker.IODeviceFacade;
import com.ociweb.pronghorn.iot.schema.I2CCommandSchema;
import com.ociweb.pronghorn.pipe.DataOutputBlobWriter;

/**
 *
 * @author huydo
 */
public class MotorDriver_Facade implements IODeviceFacade,StartupListener{
    private final FogCommandChannel target;
    public int DRIVER_I2C_ADD = 0x0f; //default address of the driver
    private int motor1Vel = 0;
    private int motor2Vel = 0;

    public enum Channel {
        CH1,
        CH2
    }
    
    public MotorDriver_Facade(FogCommandChannel ch){
        this.target = ch;
    }
    
    public MotorDriver_Facade(FogCommandChannel ch,int i2cAddress){
        this.target = ch;
        this.DRIVER_I2C_ADD = i2cAddress;
    }
    
    private void direction(int _direction){
        
        DataOutputBlobWriter<I2CCommandSchema> i2cPayloadWriter = target.i2cCommandOpen(DRIVER_I2C_ADD);
        
        i2cPayloadWriter.writeByte(DIR_REG);
        i2cPayloadWriter.writeByte(_direction);
        i2cPayloadWriter.writeByte(DUMMY_BYTE);
        
        target.i2cCommandClose();
        target.i2cFlushBatch();
        target.i2cDelay(DRIVER_I2C_ADD, 4000000);
        
    }

    /**
     * Set the velocity of the motor on specified channel
     * @param Channel either CH1 or CH2
     * @param velocity integer between -255 and 255
     */
    public void setVelocity(Channel chan,int velocity) {
        switch (chan) {
            case CH1:
                setVelocity(velocity, this.motor2Vel);
                break;
            case CH2:
                setVelocity(this.motor1Vel, velocity);
                break;
        }
    }

    /**
     * Set the velocity of the motor on both channel
     * @param motorAVel integer between -255 and 255
     * @param motorBVel integer between -255 and 255
     */
    public void setVelocity(int motorAVel,int motorBVel){
        this.motor1Vel = motorAVel;
        this.motor2Vel = motorBVel;
        
        if(motor1Vel >= 0 && motor2Vel >= 0){
            direction(M1CW_M2CW);
        }else if(motor1Vel < 0 && motor2Vel<0){
            direction(M1ACW_M2ACW);
        }else if(motor1Vel >= 0 && motor2Vel < 0){
            direction(M1CW_M2ACW);
        }else if(motor1Vel < 0 && motor2Vel >= 0){
            direction(M1ACW_M2CW);
        }
        
        motor1Vel = (Math.abs(motor1Vel)>255)?255:Math.abs(motor1Vel);
        motor2Vel = (Math.abs(motor2Vel)>255)?255:Math.abs(motor2Vel);
        
        DataOutputBlobWriter<I2CCommandSchema> i2cPayloadWriter = target.i2cCommandOpen(DRIVER_I2C_ADD);
        
        i2cPayloadWriter.writeByte(SPEED_REG);
        i2cPayloadWriter.writeByte(motor1Vel);
        i2cPayloadWriter.writeByte(motor2Vel);
        
        target.i2cCommandClose();
        target.i2cFlushBatch();
        target.i2cDelay(DRIVER_I2C_ADD, 4000000);
    }
    
{'fraction_non_alphanumeric': 0.08965938597831068, 'fraction_numerical': 0.03543607759279059, 'mean_word_length': 2.5185384202041914, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5013703', 'n_tokens_mistral': 2221, 'n_tokens_neox': 2069, 'n_words': 419}