From 9c802777392db7aa668dfef3a686bbb3d7fdf701 Mon Sep 17 00:00:00 2001
From: Erik Strand <erik.strand@cba.mit.edu>
Date: Sat, 8 Dec 2018 15:23:37 -0500
Subject: [PATCH] Echo my board's address over virtual serial

---
 .../ClassDriver/VirtualSerial/VirtualSerial.c | 46 ++++++++++++++-----
 .../ClassDriver/VirtualSerial/VirtualSerial.h |  4 +-
 .../Device/ClassDriver/VirtualSerial/makefile |  8 ++--
 3 files changed, 41 insertions(+), 17 deletions(-)

diff --git a/Demos/Device/ClassDriver/VirtualSerial/VirtualSerial.c b/Demos/Device/ClassDriver/VirtualSerial/VirtualSerial.c
index 2c9261554..9b66db454 100644
--- a/Demos/Device/ClassDriver/VirtualSerial/VirtualSerial.c
+++ b/Demos/Device/ClassDriver/VirtualSerial/VirtualSerial.c
@@ -79,10 +79,13 @@ int main(void)
 {
 	SetupHardware();
 
+    PORTC.DIRSET = (1 << 5) | (1u << 6) | (1u << 7);
+    PORTC.OUTSET = (1 << 5) | (1u << 6) | (1u << 7);
+
 	/* Create a regular character stream for the interface so that it can be used with the stdio.h functions */
 	CDC_Device_CreateStream(&VirtualSerial_CDC_Interface, &USBSerialStream);
 
-	LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
+	//LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
 	GlobalInterruptEnable();
 
 	for (;;)
@@ -120,18 +123,30 @@ void SetupHardware(void)
 #endif
 
 	/* Hardware Initialization */
-	Joystick_Init();
-	LEDs_Init();
+	//Joystick_Init();
+	//LEDs_Init();
 	USB_Init();
 }
 
 /** Checks for changes in the position of the board joystick, sending strings to the host upon each change. */
 void CheckJoystickMovement(void)
 {
-	uint8_t     JoyStatus_LCL = Joystick_GetStatus();
+	//uint8_t     JoyStatus_LCL = Joystick_GetStatus();
 	char*       ReportString  = NULL;
-	static bool ActionSent    = false;
-
+	//static bool ActionSent    = false;
+
+    ReportString = "00000000";
+    unsigned address = USB.ADDR;
+    if (address & (1u < 0)) { ReportString[0] = '1'; }
+    if (address & (1u < 1)) { ReportString[1] = '1'; }
+    if (address & (1u < 2)) { ReportString[2] = '1'; }
+    if (address & (1u < 3)) { ReportString[3] = '1'; }
+    if (address & (1u < 4)) { ReportString[4] = '1'; }
+    if (address & (1u < 5)) { ReportString[5] = '1'; }
+    if (address & (1u < 6)) { ReportString[6] = '1'; }
+    if (address & (1u < 7)) { ReportString[7] = '1'; }
+
+    /*
 	if (JoyStatus_LCL & JOY_UP)
 	  ReportString = "Joystick Up\r\n";
 	else if (JoyStatus_LCL & JOY_DOWN)
@@ -144,29 +159,36 @@ void CheckJoystickMovement(void)
 	  ReportString = "Joystick Pressed\r\n";
 	else
 	  ActionSent = false;
+    */
+
+    fputs(ReportString, &USBSerialStream);
 
+    /*
 	if ((ReportString != NULL) && (ActionSent == false))
 	{
 		ActionSent = true;
 
-		/* Write the string to the virtual COM port via the created character stream */
+		// Write the string to the virtual COM port via the created character stream
 		fputs(ReportString, &USBSerialStream);
 
-		/* Alternatively, without the stream: */
+		// Alternatively, without the stream:
 		// CDC_Device_SendString(&VirtualSerial_CDC_Interface, ReportString);
 	}
+    */
 }
 
 /** Event handler for the library USB Connection event. */
 void EVENT_USB_Device_Connect(void)
 {
-	LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
+	//LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
+    PORTC.OUTCLR = (1 << 5);
 }
 
 /** Event handler for the library USB Disconnection event. */
 void EVENT_USB_Device_Disconnect(void)
 {
-	LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
+	//LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
+    PORTC.OUTSET = (1 << 5);
 }
 
 /** Event handler for the library USB Configuration Changed event. */
@@ -175,14 +197,16 @@ void EVENT_USB_Device_ConfigurationChanged(void)
 	bool ConfigSuccess = true;
 
 	ConfigSuccess &= CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface);
+    PORTC.OUTTGL = (1 << 6);
 
-	LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
+	//LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
 }
 
 /** Event handler for the library USB Control Request reception event. */
 void EVENT_USB_Device_ControlRequest(void)
 {
 	CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface);
+    PORTC.OUTTGL = (1 << 7);
 }
 
 /** CDC class driver callback function the processing of changes to the virtual
diff --git a/Demos/Device/ClassDriver/VirtualSerial/VirtualSerial.h b/Demos/Device/ClassDriver/VirtualSerial/VirtualSerial.h
index 8a42414b2..95d425666 100644
--- a/Demos/Device/ClassDriver/VirtualSerial/VirtualSerial.h
+++ b/Demos/Device/ClassDriver/VirtualSerial/VirtualSerial.h
@@ -46,8 +46,8 @@
 
 		#include "Descriptors.h"
 
-		#include <LUFA/Drivers/Board/LEDs.h>
-		#include <LUFA/Drivers/Board/Joystick.h>
+		//#include <LUFA/Drivers/Board/LEDs.h>
+		//#include <LUFA/Drivers/Board/Joystick.h>
 		#include <LUFA/Drivers/USB/USB.h>
 		#include <LUFA/Platform/Platform.h>
 
diff --git a/Demos/Device/ClassDriver/VirtualSerial/makefile b/Demos/Device/ClassDriver/VirtualSerial/makefile
index cf14580b4..fdbbee8c3 100644
--- a/Demos/Device/ClassDriver/VirtualSerial/makefile
+++ b/Demos/Device/ClassDriver/VirtualSerial/makefile
@@ -11,10 +11,10 @@
 
 # Run "make help" for target help.
 
-MCU          = at90usb1287
-ARCH         = AVR8
-BOARD        = USBKEY
-F_CPU        = 8000000
+MCU          = atxmega128a4u
+ARCH         = XMEGA
+BOARD        = BOARD_USER
+F_CPU        = 48000000
 F_USB        = $(F_CPU)
 OPTIMIZATION = s
 TARGET       = VirtualSerial
-- 
GitLab