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 /* Scheduler Task List */
42 { Task
: TestApp_CheckJoystick
, TaskStatus
: TASK_RUN
},
43 { Task
: TestApp_CheckHWB
, TaskStatus
: TASK_RUN
},
44 { Task
: TestApp_CheckTemp
, TaskStatus
: TASK_RUN
},
45 { Task
: USB_USBTask
, TaskStatus
: TASK_RUN
},
48 /** Main program entry point. This routine configures the hardware required by the application, then
49 * starts the scheduler to run the application tasks.
53 /* Disable watchdog if enabled by bootloader/fuses */
54 MCUSR
&= ~(1 << WDRF
);
57 /* Disable clock division */
58 clock_prescale_set(clock_div_1
);
60 /* Hardware initialization */
61 SerialStream_Init(9600, false);
62 ADC_Init(ADC_SINGLE_CONVERSION
| ADC_PRESCALE_64
);
68 /* Millisecond timer initialization, with output compare interrupt enabled */
70 TCCR0A
= (1 << WGM01
);
71 TCCR0B
= ((1 << CS01
) | (1 << CS00
));
72 TIMSK0
= (1 << OCIE0A
);
74 /* Turn on interrupts */
77 /* Start-up message via USART */
78 puts_P(PSTR(ESC_RESET ESC_BG_WHITE ESC_INVERSE_ON ESC_ERASE_DISPLAY
79 "LUFA Demo running.\r\n" ESC_INVERSE_OFF
));
81 /* Scheduling - routine never returns, so put this last in the main function */
85 /** ISR for the timer 0 compare vector. This ISR fires once each millisecond, and increments the
86 * scheduler tick counter.
88 ISR(TIMER0_COMPA_vect
, ISR_BLOCK
)
90 /* Scheduler test - increment scheduler tick counter once each millisecond */
91 Scheduler_TickCounter
++;
94 /** Task responsible for checking the joystick position, and displaying the joystick position onto the
97 TASK(TestApp_CheckJoystick
)
99 uint8_t JoyStatus_LCL
= Joystick_GetStatus();
102 /* Test of the Joystick - change a mask in response to joystick */
103 if (JoyStatus_LCL
& JOY_UP
)
104 LEDMask
|= LEDS_LED1
;
106 if (JoyStatus_LCL
& JOY_DOWN
)
107 LEDMask
|= LEDS_LED2
;
109 if (JoyStatus_LCL
& JOY_LEFT
)
110 LEDMask
|= LEDS_LED3
;
112 if (JoyStatus_LCL
& JOY_RIGHT
)
113 LEDMask
|= LEDS_LED4
;
115 if (JoyStatus_LCL
& JOY_PRESS
)
116 LEDMask
= LEDS_ALL_LEDS
;
118 /* Test of LEDs - light up in response to joystick */
119 LEDs_SetAllLEDs(LEDMask
);
122 /** Task responsible for checking the current temperature via the temperature sensor mounted on the
123 * board, and displaying it through the serial USART.
125 TASK(TestApp_CheckTemp
)
127 static SchedulerDelayCounter_t DelayCounter
= 10000; // Force immediate run on start-up
129 /* Task runs every 10000 ticks, 10 seconds for this demo */
130 if (Scheduler_HasDelayElapsed(10000, &DelayCounter
))
132 printf_P(PSTR("Current temperature: %d Degrees Celcius\r\n\r\n"),
133 (int8_t)Temperature_GetTemperature());
135 /* Reset the delay counter, ready to count another 10000 tick interval */
136 Scheduler_ResetDelay(&DelayCounter
);
140 /** Task responsible for checking the HWB button position, and start-stopping other tasks and the USB
141 * interface in response to user joystick movements.
143 TASK(TestApp_CheckHWB
)
145 static SchedulerDelayCounter_t DelayCounter
= 0;
146 static bool IsPressed
;
147 static bool BlockingJoystickTask
;
149 /* Check if HWB pressed (start USB) */
150 if (HWB_GetStatus() == true)
152 /* Debounce - check 100 ticks later to see if button is still being pressed */
153 if ((IsPressed
== false) && (Scheduler_HasDelayElapsed(100, &DelayCounter
)))
155 /* Set flag, indicating that current pressed state has been handled */
158 /* First start of the USB interface permanently blocks the joystick task */
159 if (BlockingJoystickTask
== false)
161 Scheduler_SetTaskMode(TestApp_CheckJoystick
, TASK_STOP
);
162 BlockingJoystickTask
= true;
165 /* Toggle USB interface */
166 if (USB_IsInitialized
== true)
170 LEDs_SetAllLEDs(LEDS_LED1
);
171 puts_P(PSTR(ESC_BG_WHITE
"USB Power Off.\r\n"));
173 Scheduler_SetTaskMode(TestApp_CheckTemp
, TASK_RUN
);
177 Scheduler_SetTaskMode(TestApp_CheckTemp
, TASK_STOP
);
179 LEDs_SetAllLEDs(LEDS_LED2
| LEDS_LED3
);
180 puts_P(PSTR(ESC_BG_YELLOW
"USB Power On.\r\n"));
182 USB_Init(USB_MODE_UID
, USB_DEVICE_OPT_FULLSPEED
| USB_OPT_REG_ENABLED
| USB_OPT_AUTO_PLL
);
188 /* HWB not pressed - reset debounce interval counter and press handled flag */
189 Scheduler_ResetDelay(&DelayCounter
);