--- /dev/null
+/*\r
+ LUFA Library\r
+ Copyright (C) Dean Camera, 2009.\r
+ \r
+ dean [at] fourwalledcubicle [dot] com\r
+ www.fourwalledcubicle.com\r
+*/\r
+\r
+/*\r
+ Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
+\r
+ Permission to use, copy, modify, and distribute this software\r
+ and its documentation for any purpose and without fee is hereby\r
+ granted, provided that the above copyright notice appear in all\r
+ copies and that both that the copyright notice and this\r
+ permission notice and warranty disclaimer appear in supporting\r
+ documentation, and that the name of the author not be used in\r
+ advertising or publicity pertaining to distribution of the\r
+ software without specific, written prior permission.\r
+\r
+ The author disclaim all warranties with regard to this\r
+ software, including all implied warranties of merchantability\r
+ and fitness. In no event shall the author be liable for any\r
+ special, indirect or consequential damages or any damages\r
+ whatsoever resulting from loss of use, data or profits, whether\r
+ in an action of contract, negligence or other tortious action,\r
+ arising out of or in connection with the use or performance of\r
+ this software.\r
+*/\r
+\r
+/** \file\r
+ *\r
+ * Main source file for the TestApp demo. This file contains the main tasks of the demo and\r
+ * is responsible for the initial application hardware configuration.\r
+ */\r
+\r
+#include "TestApp.h"\r
+\r
+/* Project Tags, for reading out using the ButtLoad project */\r
+BUTTLOADTAG(ProjName, "LUFA Test App");\r
+BUTTLOADTAG(BuildTime, __TIME__);\r
+BUTTLOADTAG(BuildDate, __DATE__);\r
+BUTTLOADTAG(LUFAVersion, "LUFA V" LUFA_VERSION_STRING);\r
+\r
+/* Scheduler Task List */\r
+TASK_LIST\r
+{\r
+ { Task: TestApp_CheckJoystick, TaskStatus: TASK_RUN },\r
+ { Task: TestApp_CheckHWB , TaskStatus: TASK_RUN },\r
+ { Task: TestApp_CheckTemp , TaskStatus: TASK_RUN },\r
+ { Task: USB_USBTask , TaskStatus: TASK_RUN },\r
+};\r
+\r
+/** Main program entry point. This routine configures the hardware required by the application, then\r
+ * starts the scheduler to run the application tasks.\r
+ */\r
+int main(void)\r
+{\r
+ /* Disable watchdog if enabled by bootloader/fuses */\r
+ MCUSR &= ~(1 << WDRF);\r
+ wdt_disable();\r
+\r
+ /* Disable clock division */\r
+ SetSystemClockPrescaler(0);\r
+ \r
+ /* Hardware initialization */\r
+ SerialStream_Init(9600, false);\r
+ ADC_Init(ADC_SINGLE_CONVERSION | ADC_PRESCALE_64);\r
+ Temperature_Init();\r
+ Joystick_Init();\r
+ LEDs_Init();\r
+ HWB_Init();\r
+ \r
+ /* Millisecond timer initialization, with output compare interrupt enabled */\r
+ OCR0A = 0x7D;\r
+ TCCR0A = (1 << WGM01);\r
+ TCCR0B = ((1 << CS01) | (1 << CS00));\r
+ TIMSK0 = (1 << OCIE0A);\r
+ \r
+ /* Turn on interrupts */\r
+ sei();\r
+\r
+ /* Startup message via USART */\r
+ puts_P(PSTR(ESC_RESET ESC_BG_WHITE ESC_INVERSE_ON ESC_ERASE_DISPLAY\r
+ "LUFA Demo running.\r\n" ESC_INVERSE_OFF));\r
+\r
+ /* Scheduling - routine never returns, so put this last in the main function */\r
+ Scheduler_Start();\r
+}\r
+\r
+/** ISR for the timer 0 compare vector. This ISR fires once each millisecond, and increments the\r
+ * scheduler tick counter.\r
+ */\r
+ISR(TIMER0_COMPA_vect, ISR_BLOCK)\r
+{\r
+ /* Scheduler test - increment scheduler tick counter once each millisecond */\r
+ Scheduler_TickCounter++;\r
+}\r
+\r
+/** Task responsible for checking the joystick position, and displaying the joystick position onto the\r
+ * board LEDs.\r
+ */\r
+TASK(TestApp_CheckJoystick)\r
+{\r
+ uint8_t JoyStatus_LCL = Joystick_GetStatus();\r
+ uint8_t LEDMask = 0;\r
+ \r
+ /* Test of the Joystick - change a mask in response to joystick */\r
+ if (JoyStatus_LCL & JOY_UP)\r
+ LEDMask |= LEDS_LED1;\r
+ \r
+ if (JoyStatus_LCL & JOY_DOWN)\r
+ LEDMask |= LEDS_LED2;\r
+\r
+ if (JoyStatus_LCL & JOY_LEFT)\r
+ LEDMask |= LEDS_LED3;\r
+\r
+ if (JoyStatus_LCL & JOY_RIGHT)\r
+ LEDMask |= LEDS_LED4;\r
+ \r
+ if (JoyStatus_LCL & JOY_PRESS)\r
+ LEDMask = LEDS_ALL_LEDS;\r
+\r
+ /* Test of LEDs - light up in response to joystick */\r
+ LEDs_SetAllLEDs(LEDMask);\r
+}\r
+\r
+/** Task responsible for checking the current temperature via the temperature sensor mounted on the\r
+ * board, and displaying it through the serial USART.\r
+ */\r
+TASK(TestApp_CheckTemp)\r
+{\r
+ static SchedulerDelayCounter_t DelayCounter = 10000; // Force immediate run on startup\r
+\r
+ /* Task runs every 10000 ticks, 10 seconds for this demo */\r
+ if (Scheduler_HasDelayElapsed(10000, &DelayCounter))\r
+ {\r
+ printf_P(PSTR("Current temperature: %d Degrees Celcius\r\n\r\n"),\r
+ (int)Temperature_GetTemperature());\r
+\r
+ /* Reset the delay counter, ready to count another 10000 tick interval */\r
+ Scheduler_ResetDelay(&DelayCounter);\r
+ } \r
+}\r
+\r
+/** Task responsible for checking the HWB button position, and start-stopping other tasks and the USB\r
+ * interface in response to user joystick movements.\r
+ */\r
+TASK(TestApp_CheckHWB)\r
+{\r
+ static SchedulerDelayCounter_t DelayCounter = 0;\r
+ static bool IsPressed;\r
+ static bool BlockingJoystickTask;\r
+ \r
+ /* Check if HWB pressed (start USB) */\r
+ if (HWB_GetStatus() == true)\r
+ {\r
+ /* Debounce - check 100 ticks later to see if button is still being pressed */\r
+ if ((IsPressed == false) && (Scheduler_HasDelayElapsed(100, &DelayCounter)))\r
+ {\r
+ /* Set flag, indicating that current pressed state has been handled */\r
+ IsPressed = true;\r
+ \r
+ /* First start of the USB interface permenantly blocks the joystick task */\r
+ if (BlockingJoystickTask == false)\r
+ {\r
+ Scheduler_SetTaskMode(TestApp_CheckJoystick, TASK_STOP);\r
+ BlockingJoystickTask = true;\r
+ }\r
+\r
+ /* Toggle USB interface */\r
+ if (USB_IsInitialized == true)\r
+ {\r
+ USB_ShutDown();\r
+\r
+ LEDs_SetAllLEDs(LEDS_LED1);\r
+ puts_P(PSTR(ESC_BG_WHITE "USB Power Off.\r\n"));\r
+ \r
+ Scheduler_SetTaskMode(TestApp_CheckTemp, TASK_RUN);\r
+ }\r
+ else\r
+ {\r
+ Scheduler_SetTaskMode(TestApp_CheckTemp, TASK_STOP);\r
+\r
+ LEDs_SetAllLEDs(LEDS_LED2 | LEDS_LED3);\r
+ puts_P(PSTR(ESC_BG_YELLOW "USB Power On.\r\n"));\r
+ \r
+ USB_Init(USB_MODE_UID, USB_DEVICE_OPT_FULLSPEED | USB_OPT_REG_ENABLED | USB_OPT_AUTO_PLL);\r
+ }\r
+ }\r
+ }\r
+ else\r
+ {\r
+ /* HWB not pressed - reset debounce interval counter and press handled flag */\r
+ Scheduler_ResetDelay(&DelayCounter);\r
+ IsPressed = false;\r
+ }\r
+}\r