1 /**************************************************************************/
4 @author K. Townsend (microBuilder.eu)
8 Driver for a simple four-way analog joystick.
17 Software License Agreement (BSD License)
19 Copyright (c) 2010, microBuilder SARL
22 Redistribution and use in source and binary forms, with or without
23 modification, are permitted provided that the following conditions are met:
24 1. Redistributions of source code must retain the above copyright
25 notice, this list of conditions and the following disclaimer.
26 2. Redistributions in binary form must reproduce the above copyright
27 notice, this list of conditions and the following disclaimer in the
28 documentation and/or other materials provided with the distribution.
29 3. Neither the name of the copyright holders nor the
30 names of its contributors may be used to endorse or promote products
31 derived from this software without specific prior written permission.
33 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
34 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
35 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
37 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
39 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
40 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
41 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
42 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 /**************************************************************************/
45 #include "analogjoystick.h"
46 #include "core/adc/adc.h"
47 #include "core/gpio/gpio.h"
49 static bool _joystickInitialised
= false;
51 /**************************************************************************/
53 @brief Initialises the analog joystick, setting the appropriate
54 pins to analog and digital input.
56 /**************************************************************************/
57 void joystickInit(void)
59 // Make sure SEL is set to GPIO and input
60 JOYSTICK_SEL_FUNC_GPIO
;
61 gpioSetDir (JOYSTICK_SEL_PORT
, JOYSTICK_SEL_PIN
, 0);
63 // Make sure that ADC is initialised
66 // Set HORIZ/VERT pins to ADC
67 JOYSTICK_VERT_FUNC_ADC
;
68 JOYSTICK_HORIZ_FUNC_ADC
;
70 _joystickInitialised
= true;
73 /**************************************************************************/
75 @brief Gets the raw ADC values for the horizontal and vertical
76 axis, as well as the current state of the select button.
78 @param[out] horizontal
79 pointer to the uint32_t variable that will hold the
80 results of the horizontal analog conversion
82 pointer to the uint32_t variable that will hold the
83 results of the vertical analog conversion
85 pointer to the bool variable that will hold the
86 current state of the digital select button
88 /**************************************************************************/
89 void joystickGetValues(uint32_t *horizontal
, uint32_t *vertical
, bool *select
)
91 if (!_joystickInitialised
) joystickInit();
93 // Get current ADC values
94 *horizontal
= adcRead(JOYSTICK_HORIZ_ADCPORT
);
95 *vertical
= adcRead(JOYSTICK_VERT_ADCPORT
);
96 *select
= gpioGetValue(JOYSTICK_SEL_PORT
, JOYSTICK_SEL_PIN
);
99 /**************************************************************************/
101 @brief Gets the rough direction that the joystick is currently
104 @param[out] direction
105 The joystick_direction_t (enum) that will be used to
106 identify the joysticks approximate current direction.
108 /**************************************************************************/
109 void joystickGetDirection(joystick_direction_t
*direction
)
111 if (!_joystickInitialised
) joystickInit();
116 // Get current joystick position
117 joystickGetValues(&hor
, &ver
, &sel
);
119 // ToDo: Process values
120 direction
= JOYSTICK_DIR_NONE
;
123 /**************************************************************************/
125 @brief Get the approximate rotation of the joystick from 0-359° in
126 a clock-wise direction.
129 Pointer to the uint32_t variable that will hold the
132 /**************************************************************************/
133 void joystickGetRotation(uint32_t *rotation
)
135 if (!_joystickInitialised
) joystickInit();
This page took 0.05498 seconds and 5 git commands to generate.