Levelreihenfolge, -fixes.
[hackover2013-badge-firmware.git] / drivers / sensors / analogjoystick / analogjoystick.c
1 /**************************************************************************/
2 /*!
3 @file analogjoystick.c
4 @author K. Townsend (microBuilder.eu)
5
6 @section DESCRIPTION
7
8 Driver for a simple four-way analog joystick.
9
10 @section Example
11
12 @code
13 @endcode
14
15 @section LICENSE
16
17 Software License Agreement (BSD License)
18
19 Copyright (c) 2010, microBuilder SARL
20 All rights reserved.
21
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.
32
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.
43 */
44 /**************************************************************************/
45 #include "analogjoystick.h"
46 #include "core/adc/adc.h"
47 #include "core/gpio/gpio.h"
48
49 static bool _joystickInitialised = false;
50
51 /**************************************************************************/
52 /*!
53 @brief Initialises the analog joystick, setting the appropriate
54 pins to analog and digital input.
55 */
56 /**************************************************************************/
57 void joystickInit(void)
58 {
59 // Make sure SEL is set to GPIO and input
60 JOYSTICK_SEL_FUNC_GPIO;
61 gpioSetDir (JOYSTICK_SEL_PORT, JOYSTICK_SEL_PIN, 0);
62
63 // Make sure that ADC is initialised
64 adcInit();
65
66 // Set HORIZ/VERT pins to ADC
67 JOYSTICK_VERT_FUNC_ADC;
68 JOYSTICK_HORIZ_FUNC_ADC;
69
70 _joystickInitialised = true;
71 }
72
73 /**************************************************************************/
74 /*!
75 @brief Gets the raw ADC values for the horizontal and vertical
76 axis, as well as the current state of the select button.
77
78 @param[out] horizontal
79 pointer to the uint32_t variable that will hold the
80 results of the horizontal analog conversion
81 @param[out] vertical
82 pointer to the uint32_t variable that will hold the
83 results of the vertical analog conversion
84 @param[out] select
85 pointer to the bool variable that will hold the
86 current state of the digital select button
87 */
88 /**************************************************************************/
89 void joystickGetValues(uint32_t *horizontal, uint32_t *vertical, bool *select)
90 {
91 if (!_joystickInitialised) joystickInit();
92
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);
97 }
98
99 /**************************************************************************/
100 /*!
101 @brief Gets the rough direction that the joystick is currently
102 positioned at
103
104 @param[out] direction
105 The joystick_direction_t (enum) that will be used to
106 identify the joysticks approximate current direction.
107 */
108 /**************************************************************************/
109 void joystickGetDirection(joystick_direction_t *direction)
110 {
111 if (!_joystickInitialised) joystickInit();
112
113 uint32_t hor, ver;
114 bool sel;
115
116 // Get current joystick position
117 joystickGetValues(&hor, &ver, &sel);
118
119 // ToDo: Process values
120 direction = JOYSTICK_DIR_NONE;
121 }
122
123 /**************************************************************************/
124 /*!
125 @brief Get the approximate rotation of the joystick from 0-359° in
126 a clock-wise direction.
127
128 @param[out] rotation
129 Pointer to the uint32_t variable that will hold the
130 rotation value.
131 */
132 /**************************************************************************/
133 void joystickGetRotation(uint32_t *rotation)
134 {
135 if (!_joystickInitialised) joystickInit();
136
137 // ToDo
138 }
This page took 0.053785 seconds and 5 git commands to generate.