3      Copyright (C) Dean Camera, 2009. 
   5   dean [at] fourwalledcubicle [dot] com 
   6       www.fourwalledcubicle.com 
  10   Copyright 2009  Dean Camera (dean [at] fourwalledcubicle [dot] com) 
  12   Permission to use, copy, modify, and distribute this software 
  13   and its documentation for any purpose and without fee is hereby 
  14   granted, provided that the above copyright notice appear in all 
  15   copies and that both that the copyright notice and this 
  16   permission notice and warranty disclaimer appear in supporting 
  17   documentation, and that the name of the author not be used in 
  18   advertising or publicity pertaining to distribution of the 
  19   software without specific, written prior permission. 
  21   The author disclaim all warranties with regard to this 
  22   software, including all implied warranties of merchantability 
  23   and fitness.  In no event shall the author be liable for any 
  24   special, indirect or consequential damages or any damages 
  25   whatsoever resulting from loss of use, data or profits, whether 
  26   in an action of contract, negligence or other tortious action, 
  27   arising out of or in connection with the use or performance of 
  33  *  Main source file for the TestApp demo. This file contains the main tasks of the demo and 
  34  *  is responsible for the initial application hardware configuration. 
  39 /* Project Tags, for reading out using the ButtLoad project */ 
  40 BUTTLOADTAG(ProjName
,    "LUFA Test App"); 
  41 BUTTLOADTAG(BuildTime
,   __TIME__
); 
  42 BUTTLOADTAG(BuildDate
,   __DATE__
); 
  43 BUTTLOADTAG(LUFAVersion
, "LUFA V" LUFA_VERSION_STRING
); 
  45 /* Scheduler Task List */ 
  48         { Task
: TestApp_CheckJoystick
, TaskStatus
: TASK_RUN  
}, 
  49         { Task
: TestApp_CheckHWB     
, TaskStatus
: TASK_RUN  
}, 
  50         { Task
: TestApp_CheckTemp    
, TaskStatus
: TASK_RUN  
}, 
  51         { Task
: USB_USBTask          
, TaskStatus
: TASK_RUN  
}, 
  54 /** Main program entry point. This routine configures the hardware required by the application, then 
  55  *  starts the scheduler to run the application tasks. 
  59         /* Disable watchdog if enabled by bootloader/fuses */ 
  60         MCUSR 
&= ~(1 << WDRF
); 
  63         /* Disable clock division */ 
  64         clock_prescale_set(clock_div_1
); 
  66         /* Hardware initialization */ 
  67         SerialStream_Init(9600, false); 
  68         ADC_Init(ADC_SINGLE_CONVERSION 
| ADC_PRESCALE_64
); 
  74         /* Millisecond timer initialization, with output compare interrupt enabled */ 
  76         TCCR0A 
= (1 << WGM01
); 
  77         TCCR0B 
= ((1 << CS01
) | (1 << CS00
)); 
  78         TIMSK0 
= (1 << OCIE0A
); 
  80         /* Turn on interrupts */ 
  83     /* Start-up message via USART */ 
  84         puts_P(PSTR(ESC_RESET ESC_BG_WHITE ESC_INVERSE_ON ESC_ERASE_DISPLAY
 
  85                "LUFA Demo running.\r\n" ESC_INVERSE_OFF
)); 
  87         /* Scheduling - routine never returns, so put this last in the main function */ 
  91 /** ISR for the timer 0 compare vector. This ISR fires once each millisecond, and increments the 
  92  *  scheduler tick counter. 
  94 ISR(TIMER0_COMPA_vect
, ISR_BLOCK
) 
  96         /* Scheduler test - increment scheduler tick counter once each millisecond */ 
  97         Scheduler_TickCounter
++; 
 100 /** Task responsible for checking the joystick position, and displaying the joystick position onto the 
 103 TASK(TestApp_CheckJoystick
) 
 105         uint8_t JoyStatus_LCL 
= Joystick_GetStatus(); 
 108         /* Test of the Joystick - change a mask in response to joystick */ 
 109         if (JoyStatus_LCL 
& JOY_UP
) 
 110           LEDMask 
|= LEDS_LED1
; 
 112         if (JoyStatus_LCL 
& JOY_DOWN
) 
 113           LEDMask 
|= LEDS_LED2
; 
 115         if (JoyStatus_LCL 
& JOY_LEFT
) 
 116           LEDMask 
|= LEDS_LED3
; 
 118         if (JoyStatus_LCL 
& JOY_RIGHT
) 
 119           LEDMask 
|= LEDS_LED4
; 
 121         if (JoyStatus_LCL 
& JOY_PRESS
) 
 122           LEDMask  
= LEDS_ALL_LEDS
; 
 124         /* Test of LEDs - light up in response to joystick */ 
 125         LEDs_SetAllLEDs(LEDMask
); 
 128 /** Task responsible for checking the current temperature via the temperature sensor mounted on the 
 129  *  board, and displaying it through the serial USART. 
 131 TASK(TestApp_CheckTemp
) 
 133         static SchedulerDelayCounter_t DelayCounter 
= 10000; // Force immediate run on start-up 
 135         /* Task runs every 10000 ticks, 10 seconds for this demo */ 
 136         if (Scheduler_HasDelayElapsed(10000, &DelayCounter
)) 
 138                 printf_P(PSTR("Current temperature: %d Degrees Celcius\r\n\r\n"), 
 139                          (int)Temperature_GetTemperature()); 
 141                 /* Reset the delay counter, ready to count another 10000 tick interval */ 
 142                 Scheduler_ResetDelay(&DelayCounter
); 
 146 /** Task responsible for checking the HWB button position, and start-stopping other tasks and the USB 
 147  *  interface in response to user joystick movements. 
 149 TASK(TestApp_CheckHWB
) 
 151         static SchedulerDelayCounter_t DelayCounter 
= 0; 
 152         static bool                    IsPressed
; 
 153         static bool                    BlockingJoystickTask
; 
 155         /* Check if HWB pressed (start USB) */ 
 156         if (HWB_GetStatus() == true) 
 158                 /* Debounce - check 100 ticks later to see if button is still being pressed */ 
 159                 if ((IsPressed 
== false) && (Scheduler_HasDelayElapsed(100, &DelayCounter
))) 
 161                         /* Set flag, indicating that current pressed state has been handled */ 
 164                         /* First start of the USB interface permanently blocks the joystick task */ 
 165                         if (BlockingJoystickTask 
== false) 
 167                                 Scheduler_SetTaskMode(TestApp_CheckJoystick
, TASK_STOP
); 
 168                                 BlockingJoystickTask 
= true; 
 171                         /* Toggle USB interface */ 
 172                         if (USB_IsInitialized 
== true) 
 176                                 LEDs_SetAllLEDs(LEDS_LED1
); 
 177                                 puts_P(PSTR(ESC_BG_WHITE 
"USB Power Off.\r\n")); 
 179                                 Scheduler_SetTaskMode(TestApp_CheckTemp
, TASK_RUN
); 
 183                                 Scheduler_SetTaskMode(TestApp_CheckTemp
, TASK_STOP
); 
 185                                 LEDs_SetAllLEDs(LEDS_LED2 
| LEDS_LED3
); 
 186                                 puts_P(PSTR(ESC_BG_YELLOW 
"USB Power On.\r\n")); 
 188                                 USB_Init(USB_MODE_UID
, USB_DEVICE_OPT_FULLSPEED 
| USB_OPT_REG_ENABLED 
| USB_OPT_AUTO_PLL
); 
 194                 /* HWB not pressed - reset debounce interval counter and press handled flag */ 
 195                 Scheduler_ResetDelay(&DelayCounter
);