#include <stdint.h>
#include "usbdrv/usbdrv.h"

#include "main.h"


#include "spi_proto.h"
#include "usb.h"

usbMsgLen_t usbFunctionSetup(uchar data[8]) {
  usbRequest_t    *rq = (void *)data;
  static uchar    dataBuffer[5];
  uint16_t recv;

  if(rq->bRequest == 100) {
    dataBuffer[0] = (thermoData [0] & 0xff00)>>8;
    dataBuffer[1] = thermoData [0] & 0x00ff;
    dataBuffer[2] = (thermoData [1] & 0xff00)>>8;
    dataBuffer[3] = thermoData [1] & 0x00ff;
    usbMsgPtr = dataBuffer;
    return 4;
  } else if(rq->bRequest == 101) {
    dataBuffer[0] = (thermoData [2] & 0xff00)>>8;
    dataBuffer[1] = thermoData [2] & 0x00ff;
    dataBuffer[2] = (thermoData [3] & 0xff00)>>8;
    dataBuffer[3] = thermoData [3] & 0x00ff;
    usbMsgPtr = dataBuffer;
    return 4;
  } else {
    uint8_t opcode = rq->bRequest;
    uint8_t addr =   rq->wValue.bytes[0];
    uint8_t valH =   rq->wIndex.bytes[0];
    uint8_t valL =   rq->wIndex.bytes[1];

    uint16_t send = (valH << 8) | valL;
    recv = spi_master_transceive(opcode, addr, send);
    
	dataBuffer[0] = (recv & 0xff00) >> 8;
	dataBuffer[1] = (recv & 0xff);
    usbMsgPtr = dataBuffer;
    return 2;
    
  }

  return 0;
}

