/*
ECE649 Spring 2010
Group 7
Justin Ray/justinr2
(other names would go here)
 */

package simulator.elevatorcontrol;

import java.util.BitSet;
import simulator.framework.Direction;
import simulator.payloads.CanMailbox;
import simulator.payloads.translators.CanPayloadTranslator;


/************************************
CLASS:
VendMotorCanPayloadTranslator
AUTHOR:
Justin Ray
REVISION DATE:
CONSTRUCTOR PARAMETERS:
CanMailbox payload
DESCRIPTION:
CanPayloadTranslators convert between bit-level message representations used in
the CAN network simulation and semantically valid typed representations (e.g.
boolean, int, enum types, etc) used by the Controller classes.

Translators are a convenience feature, but they are also required because they
are used by the MessageInjector to inject network messages into unit and
integration tests.

This translator is used by the CAN network simulation framework for the
mVendMotor message
 *************************************/
public class VendMotorCanPayloadTranslator extends CanPayloadTranslator {

    public VendMotorCanPayloadTranslator(CanMailbox payload) {
        super(payload, 1, MessageDictionary.VEND_MOTOR_CAN_ID);
    }

    //this method is required for reflection
    public void set(Direction direction) {
        setDirection(direction);
    }
    
    public void setDirection(Direction direction) {
        BitSet b = mailbox.getMessagePayload();
        addUnsignedIntToBitset(b, direction.ordinal(), 0, 2);
        mailbox.setMessagePayload(b, byteSize);
    }

    public Direction getDirection() {
        int val = getUnsignedIntFromBitset(mailbox.getMessagePayload(), 0, 2);
        for (Direction d : Direction.values()) {
            if (val == d.ordinal()) {
                return d;
            }
        }
        throw new RuntimeException("Unrecognized Direction value " + val);
    }

    @Override
    public String payloadToString() {
        return "VendMotor=" + getDirection();
    }
}
