/*
             LUFA Library
     Copyright (C) Dean Camera, 2015.

  dean [at] fourwalledcubicle [dot] com
           www.lufa-lib.org
*/

/*
  Copyright 2015  Dean Camera (dean [at] fourwalledcubicle [dot] com)

  Permission to use, copy, modify, distribute, and sell this
  software and its documentation for any purpose is hereby granted
  without fee, provided that the above copyright notice appear in
  all copies and that both that the copyright notice and this
  permission notice and warranty disclaimer appear in supporting
  documentation, and that the name of the author not be used in
  advertising or publicity pertaining to distribution of the
  software without specific, written prior permission.

  The author disclaims all warranties with regard to this
  software, including all implied warranties of merchantability
  and fitness.  In no event shall the author be liable for any
  special, indirect or consequential damages or any damages
  whatsoever resulting from loss of use, data or profits, whether
  in an action of contract, negligence or other tortious action,
  arising out of or in connection with the use or performance of
  this software.
*/

#include "Mouse.h"

/** Buffer to hold the previously generated Mouse HID report, for comparison purposes inside the HID class driver. */
static uint8_t PrevMouseHIDReportBuffer[sizeof(USB_MouseReport_Data_t)];

/** LUFA HID Class driver interface configuration and state information. This structure is
 *  passed to all HID Class driver functions, so that multiple instances of the same class
 *  within a device can be differentiated from one another.
 */
USB_ClassInfo_HID_Device_t Mouse_HID_Interface =
	{
		.Config =
			{
				.InterfaceNumber              = INTERFACE_ID_Mouse,
				.ReportINEndpoint             =
					{
						.Address              = MOUSE_EPADDR,
						.Size                 = MOUSE_EPSIZE,
						.Banks                = 1,
					},
				.PrevReportINBuffer           = PrevMouseHIDReportBuffer,
				.PrevReportINBufferSize       = sizeof(PrevMouseHIDReportBuffer),
			},
	};


int main() {
	/* Disable watchdog if enabled by bootloader/fuses */
	MCUSR &= ~(1 << WDRF);
	wdt_disable();

	USB_Init();
    LEDs_Init();

	GlobalInterruptEnable();

    PORTC |= _BV(PC6); // weak pullup on PC6

    while(1) {
		HID_Device_USBTask(&Mouse_HID_Interface);
		USB_USBTask();
	}
}

void EVENT_USB_Device_Connect() {
    LEDs_TurnOnLEDs(LEDS_LED1);
}

void EVENT_USB_Device_Disconnect() {
    LEDs_TurnOffLEDs(LEDS_LED1);
}

void EVENT_USB_Device_ConfigurationChanged() {
	bool ConfigSuccess = true;
	ConfigSuccess &= HID_Device_ConfigureEndpoints(&Mouse_HID_Interface);
	USB_Device_EnableSOFEvents();
}

void EVENT_USB_Device_ControlRequest() {
	HID_Device_ProcessControlRequest(&Mouse_HID_Interface);
}

void EVENT_USB_Device_StartOfFrame() {
	HID_Device_MillisecondElapsed(&Mouse_HID_Interface);
}

bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
                                         uint8_t* const ReportID,
                                         const uint8_t ReportType,
                                         void* ReportData,
                                         uint16_t* const ReportSize) {
    static char counter = 0;
	USB_MouseReport_Data_t* MouseReport = (USB_MouseReport_Data_t*)ReportData;

    // If button pin pulled up, no dx
    if(PINC & _BV(PC6)) {
        LEDs_TurnOffLEDs(LEDS_LED2);
        MouseReport->X = 0;
    } else {
        LEDs_TurnOnLEDs(LEDS_LED2);
        MouseReport->X = 1;
    }

    MouseReport->Y = 0;
    MouseReport->Button = 0;
	*ReportSize = sizeof(USB_MouseReport_Data_t);

	return true;
}

void CALLBACK_HID_Device_ProcessHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
                                          const uint8_t ReportID,
                                          const uint8_t ReportType,
                                          const void* ReportData,
                                          const uint16_t ReportSize) {
	// Unused (but mandatory for the HID class driver) in this demo, since there are no Host->Device reports
}

