Spell checked non-source documentation pages.
[pub/USBasp.git] / Demos / OTG / TestApp / TestApp.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
20
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
28 this software.
29 */
30
31 /** \file
32 *
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.
35 */
36
37 #include "TestApp.h"
38
39 /* Scheduler Task List */
40 TASK_LIST
41 {
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 },
46 };
47
48 /** Main program entry point. This routine configures the hardware required by the application, then
49 * starts the scheduler to run the application tasks.
50 */
51 int main(void)
52 {
53 /* Disable watchdog if enabled by bootloader/fuses */
54 MCUSR &= ~(1 << WDRF);
55 wdt_disable();
56
57 /* Disable clock division */
58 clock_prescale_set(clock_div_1);
59
60 /* Hardware initialization */
61 SerialStream_Init(9600, false);
62 ADC_Init(ADC_SINGLE_CONVERSION | ADC_PRESCALE_64);
63 Temperature_Init();
64 Joystick_Init();
65 LEDs_Init();
66 HWB_Init();
67
68 /* Millisecond timer initialization, with output compare interrupt enabled */
69 OCR0A = 0x7D;
70 TCCR0A = (1 << WGM01);
71 TCCR0B = ((1 << CS01) | (1 << CS00));
72 TIMSK0 = (1 << OCIE0A);
73
74 /* Turn on interrupts */
75 sei();
76
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));
80
81 /* Scheduling - routine never returns, so put this last in the main function */
82 Scheduler_Start();
83 }
84
85 /** ISR for the timer 0 compare vector. This ISR fires once each millisecond, and increments the
86 * scheduler tick counter.
87 */
88 ISR(TIMER0_COMPA_vect, ISR_BLOCK)
89 {
90 /* Scheduler test - increment scheduler tick counter once each millisecond */
91 Scheduler_TickCounter++;
92 }
93
94 /** Task responsible for checking the joystick position, and displaying the joystick position onto the
95 * board LEDs.
96 */
97 TASK(TestApp_CheckJoystick)
98 {
99 uint8_t JoyStatus_LCL = Joystick_GetStatus();
100 uint8_t LEDMask = 0;
101
102 /* Test of the Joystick - change a mask in response to joystick */
103 if (JoyStatus_LCL & JOY_UP)
104 LEDMask |= LEDS_LED1;
105
106 if (JoyStatus_LCL & JOY_DOWN)
107 LEDMask |= LEDS_LED2;
108
109 if (JoyStatus_LCL & JOY_LEFT)
110 LEDMask |= LEDS_LED3;
111
112 if (JoyStatus_LCL & JOY_RIGHT)
113 LEDMask |= LEDS_LED4;
114
115 if (JoyStatus_LCL & JOY_PRESS)
116 LEDMask = LEDS_ALL_LEDS;
117
118 /* Test of LEDs - light up in response to joystick */
119 LEDs_SetAllLEDs(LEDMask);
120 }
121
122 /** Task responsible for checking the current temperature via the temperature sensor mounted on the
123 * board, and displaying it through the serial USART.
124 */
125 TASK(TestApp_CheckTemp)
126 {
127 static SchedulerDelayCounter_t DelayCounter = 10000; // Force immediate run on start-up
128
129 /* Task runs every 10000 ticks, 10 seconds for this demo */
130 if (Scheduler_HasDelayElapsed(10000, &DelayCounter))
131 {
132 printf_P(PSTR("Current temperature: %d Degrees Celcius\r\n\r\n"),
133 (int8_t)Temperature_GetTemperature());
134
135 /* Reset the delay counter, ready to count another 10000 tick interval */
136 Scheduler_ResetDelay(&DelayCounter);
137 }
138 }
139
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.
142 */
143 TASK(TestApp_CheckHWB)
144 {
145 static SchedulerDelayCounter_t DelayCounter = 0;
146 static bool IsPressed;
147 static bool BlockingJoystickTask;
148
149 /* Check if HWB pressed (start USB) */
150 if (HWB_GetStatus() == true)
151 {
152 /* Debounce - check 100 ticks later to see if button is still being pressed */
153 if ((IsPressed == false) && (Scheduler_HasDelayElapsed(100, &DelayCounter)))
154 {
155 /* Set flag, indicating that current pressed state has been handled */
156 IsPressed = true;
157
158 /* First start of the USB interface permanently blocks the joystick task */
159 if (BlockingJoystickTask == false)
160 {
161 Scheduler_SetTaskMode(TestApp_CheckJoystick, TASK_STOP);
162 BlockingJoystickTask = true;
163 }
164
165 /* Toggle USB interface */
166 if (USB_IsInitialized == true)
167 {
168 USB_ShutDown();
169
170 LEDs_SetAllLEDs(LEDS_LED1);
171 puts_P(PSTR(ESC_BG_WHITE "USB Power Off.\r\n"));
172
173 Scheduler_SetTaskMode(TestApp_CheckTemp, TASK_RUN);
174 }
175 else
176 {
177 Scheduler_SetTaskMode(TestApp_CheckTemp, TASK_STOP);
178
179 LEDs_SetAllLEDs(LEDS_LED2 | LEDS_LED3);
180 puts_P(PSTR(ESC_BG_YELLOW "USB Power On.\r\n"));
181
182 USB_Init(USB_MODE_UID, USB_DEVICE_OPT_FULLSPEED | USB_OPT_REG_ENABLED | USB_OPT_AUTO_PLL);
183 }
184 }
185 }
186 else
187 {
188 /* HWB not pressed - reset debounce interval counter and press handled flag */
189 Scheduler_ResetDelay(&DelayCounter);
190 IsPressed = false;
191 }
192 }