-/*\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
-/** 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
- SetupHardware();\r
- \r
- puts_P(PSTR(ESC_FG_CYAN "LUFA Demo running.\r\n" ESC_FG_WHITE));\r
-\r
- for (;;)\r
- {\r
- CheckJoystick();\r
- CheckButton();\r
- CheckTemperature();\r
-\r
- /* Clear millisecond timer's Output Compare flag (logic 1 clears the flag) */\r
- TIFR0 |= (1 << OCF0A);\r
- \r
- USB_USBTask();\r
- }\r
-}\r
-\r
-void SetupHardware(void)\r
-{\r
- /* Disable watchdog if enabled by bootloader/fuses */\r
- MCUSR &= ~(1 << WDRF);\r
- wdt_disable();\r
-\r
- /* Disable clock division */\r
- clock_prescale_set(clock_div_1);\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
- Buttons_Init();\r
- \r
- /* Millisecond timer initialization */\r
- OCR0A = 0x7D;\r
- TCCR0A = (1 << WGM01);\r
- TCCR0B = ((1 << CS01) | (1 << CS00));\r
-}\r
-\r
-/** Task responsible for checking the joystick position, and displaying the joystick position onto the\r
- * board LEDs.\r
- */\r
-void CheckJoystick(void)\r
-{\r
- uint8_t JoyStatus_LCL = Joystick_GetStatus();\r
- uint8_t LEDMask = LEDS_NO_LEDS;\r
- \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
- 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
-void CheckTemperature(void)\r
-{\r
- static uint16_t MSElapsed = 0;\r
-\r
- /* Timer 0's compare flag is set every millisecond */\r
- if (TIFR0 & (1 << OCF0A))\r
- MSElapsed++;\r
-\r
- /* Task runs every 10000 ticks, 10 seconds for this demo */\r
- if (MSElapsed == 10000)\r
- {\r
- printf_P(PSTR("Current temperature: %d Degrees Celcius\r\n\r\n"),\r
- (int8_t)Temperature_GetTemperature());\r
-\r
- MSElapsed = 0;\r
- } \r
-}\r
-\r
-/** Task responsible for checking the board's first button' position, and start-stopping other tasks and the USB\r
- * interface in response to user joystick movements.\r
- */\r
-void CheckButton(void)\r
-{\r
- static uint16_t DebounceMSElapsed = 0;\r
- static bool IsPressed;\r
- \r
- /* Timer 0's compare flag is set every millisecond */\r
- if (TIFR0 & (1 << OCF0A))\r
- DebounceMSElapsed++;\r
-\r
- if (Buttons_GetStatus() & BUTTONS_BUTTON1)\r
- {\r
- if (!(IsPressed) && (DebounceMSElapsed == 100))\r
- {\r
- IsPressed = true;\r
-\r
- if (USB_IsInitialized == true)\r
- {\r
- USB_ShutDown();\r
- puts_P(PSTR(ESC_FG_YELLOW "USB Power Off.\r\n" ESC_FG_WHITE));\r
- }\r
- else\r
- {\r
- puts_P(PSTR(ESC_FG_YELLOW "USB Power On.\r\n" ESC_FG_WHITE)); \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
- DebounceMSElapsed = 0;\r
- IsPressed = false;\r
- }\r
-}\r