joystick3  2.1.0.0
joystick3.h
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (C) 2020 MikroElektronika d.o.o.
3 ** Contact: https://www.mikroe.com/contact
4 **
5 ** Permission is hereby granted, free of charge, to any person obtaining a copy
6 ** of this software and associated documentation files (the "Software"), to deal
7 ** in the Software without restriction, including without limitation the rights
8 ** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 ** copies of the Software, and to permit persons to whom the Software is
10 ** furnished to do so, subject to the following conditions:
11 ** The above copyright notice and this permission notice shall be
12 ** included in all copies or substantial portions of the Software.
13 **
14 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16 ** OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 ** DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
19 ** OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 ** USE OR OTHER DEALINGS IN THE SOFTWARE.
21 ****************************************************************************/
22 
28 #ifndef JOYSTICK3_H
29 #define JOYSTICK3_H
30 
31 #ifdef __cplusplus
32 extern "C"{
33 #endif
34 
35 #include "mikrosdk_version.h"
36 
37 #ifdef __GNUC__
38 #if mikroSDK_GET_VERSION < 20800ul
39 #include "rcu_delays.h"
40 #else
41 #include "delays.h"
42 #endif
43 #endif
44 
45 #include "drv_digital_out.h"
46 #include "drv_digital_in.h"
47 #include "drv_spi_master.h"
48 #include "spi_specifics.h"
49 
70 #define JOYSTICK3_ADC_START_BIT 0x04
71 #define JOYSTICK3_ADC_SINGLE_ENDED 0x02
72 #define JOYSTICK3_ADC_CHANNEL_X 0x00
73 #define JOYSTICK3_ADC_CHANNEL_Y 0x40
74 #define JOYSTICK3_ADC_DATA_RESOLUTION 0x0FFF
75 #define JOYSTICK3_ADC_DATA_RESOLUTION_HALF ( ( float ) JOYSTICK3_ADC_DATA_RESOLUTION / 2 )
76 
81 #define JOYSTICK3_RADIANS_TO_DEGREES 57.295779513
82 #define JOYSTICK3_ANGLE_ZERO_OFFSET 180.0
83 #define JOYSTICK3_PI_RAD_OFFSET 1.0
84 #define JOYSTICK3_NEUTRAL_POSITION_OFFSET ( ( float ) JOYSTICK3_ADC_DATA_RESOLUTION / 8 )
85 #define JOYSTICK3_FULL_ANGLE 360.0f
86 #define JOYSTICK3_ZERO_ANGLE 0.0f
87 #define JOYSTICK3_ANGLE_POSITION_STEP 45.0f
88 
93 #define JOYSTICK3_POSITION_NEUTRAL 0
94 #define JOYSTICK3_POSITION_UP 1
95 #define JOYSTICK3_POSITION_UPPER_RIGHT 2
96 #define JOYSTICK3_POSITION_RIGHT 3
97 #define JOYSTICK3_POSITION_LOWER_RIGHT 4
98 #define JOYSTICK3_POSITION_DOWN 5
99 #define JOYSTICK3_POSITION_LOWER_LEFT 6
100 #define JOYSTICK3_POSITION_LEFT 7
101 #define JOYSTICK3_POSITION_UPPER_LEFT 8
102 
111 #define JOYSTICK3_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
112 #define JOYSTICK3_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
113  // joystick3_set
115 
130 #define JOYSTICK3_MAP_MIKROBUS( cfg, mikrobus ) \
131  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
132  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
133  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
134  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS );
135  // joystick3_map // joystick3
138 
143 typedef struct
144 {
145  // Modules
146  spi_master_t spi;
148  pin_name_t chip_select;
150 } joystick3_t;
151 
156 typedef struct
157 {
158  // Communication gpio pins
159  pin_name_t miso;
160  pin_name_t mosi;
161  pin_name_t sck;
162  pin_name_t cs;
164  // static variable
165  uint32_t spi_speed;
166  spi_master_mode_t spi_mode;
167  spi_master_chip_select_polarity_t cs_polarity;
170 
175 typedef enum
176 {
178  JOYSTICK3_ERROR = -1
179 
181 
198 
213 
226 err_t joystick3_read_raw_adc ( joystick3_t *ctx, uint16_t *raw_x, uint16_t *raw_y );
227 
236 float joystick3_get_angle ( uint16_t raw_x, uint16_t raw_y );
237 
246 uint8_t joystick3_get_position ( uint16_t raw_x, uint16_t raw_y );
247 
248 #ifdef __cplusplus
249 }
250 #endif
251 #endif // JOYSTICK3_H
252  // joystick3
254 
255 // ------------------------------------------------------------------------ END
joystick3_get_angle
float joystick3_get_angle(uint16_t raw_x, uint16_t raw_y)
Joystick 3 get angle function.
joystick3_read_raw_adc
err_t joystick3_read_raw_adc(joystick3_t *ctx, uint16_t *raw_x, uint16_t *raw_y)
Joystick 3 read raw adc function.
joystick3_cfg_setup
void joystick3_cfg_setup(joystick3_cfg_t *cfg)
Joystick 3 configuration object setup function.
joystick3_t::chip_select
pin_name_t chip_select
Definition: joystick3.h:148
joystick3_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: joystick3.h:167
JOYSTICK3_ERROR
@ JOYSTICK3_ERROR
Definition: joystick3.h:178
joystick3_cfg_t::cs
pin_name_t cs
Definition: joystick3.h:162
spi_specifics.h
This file contains SPI specific macros, functions, etc.
joystick3_return_value_t
joystick3_return_value_t
Joystick 3 Click return value data.
Definition: joystick3.h:176
joystick3_cfg_t
Joystick 3 Click configuration object.
Definition: joystick3.h:157
joystick3_cfg_t::sck
pin_name_t sck
Definition: joystick3.h:161
joystick3_cfg_t::mosi
pin_name_t mosi
Definition: joystick3.h:160
joystick3_t::spi
spi_master_t spi
Definition: joystick3.h:146
joystick3_cfg_t::miso
pin_name_t miso
Definition: joystick3.h:159
joystick3_get_position
uint8_t joystick3_get_position(uint16_t raw_x, uint16_t raw_y)
Joystick 3 get position function.
joystick3_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: joystick3.h:166
joystick3_init
err_t joystick3_init(joystick3_t *ctx, joystick3_cfg_t *cfg)
Joystick 3 initialization function.
JOYSTICK3_OK
@ JOYSTICK3_OK
Definition: joystick3.h:177
joystick3_t
Joystick 3 Click context object.
Definition: joystick3.h:144
joystick3_cfg_t::spi_speed
uint32_t spi_speed
Definition: joystick3.h:165