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 
39 #ifdef PREINIT_SUPPORTED
40 #include "preinit.h"
41 #endif
42 
43 #ifdef MikroCCoreVersion
44  #if MikroCCoreVersion >= 1
45  #include "delays.h"
46  #endif
47 #endif
48 
49 #include "drv_digital_out.h"
50 #include "drv_digital_in.h"
51 #include "drv_spi_master.h"
52 #include "spi_specifics.h"
53 
74 #define JOYSTICK3_ADC_START_BIT 0x04
75 #define JOYSTICK3_ADC_SINGLE_ENDED 0x02
76 #define JOYSTICK3_ADC_CHANNEL_X 0x00
77 #define JOYSTICK3_ADC_CHANNEL_Y 0x40
78 #define JOYSTICK3_ADC_DATA_RESOLUTION 0x0FFF
79 #define JOYSTICK3_ADC_DATA_RESOLUTION_HALF ( ( float ) JOYSTICK3_ADC_DATA_RESOLUTION / 2 )
80 
85 #define JOYSTICK3_RADIANS_TO_DEGREES 57.295779513
86 #define JOYSTICK3_ANGLE_ZERO_OFFSET 180.0
87 #define JOYSTICK3_PI_RAD_OFFSET 1.0
88 #define JOYSTICK3_NEUTRAL_POSITION_OFFSET ( ( float ) JOYSTICK3_ADC_DATA_RESOLUTION / 8 )
89 #define JOYSTICK3_FULL_ANGLE 360.0f
90 #define JOYSTICK3_ZERO_ANGLE 0.0f
91 #define JOYSTICK3_ANGLE_POSITION_STEP 45.0f
92 
97 #define JOYSTICK3_POSITION_NEUTRAL 0
98 #define JOYSTICK3_POSITION_UP 1
99 #define JOYSTICK3_POSITION_UPPER_RIGHT 2
100 #define JOYSTICK3_POSITION_RIGHT 3
101 #define JOYSTICK3_POSITION_LOWER_RIGHT 4
102 #define JOYSTICK3_POSITION_DOWN 5
103 #define JOYSTICK3_POSITION_LOWER_LEFT 6
104 #define JOYSTICK3_POSITION_LEFT 7
105 #define JOYSTICK3_POSITION_UPPER_LEFT 8
106 
115 #define JOYSTICK3_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
116 #define JOYSTICK3_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
117  // joystick3_set
119 
134 #define JOYSTICK3_MAP_MIKROBUS( cfg, mikrobus ) \
135  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
136  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
137  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
138  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS );
139  // joystick3_map // joystick3
142 
147 typedef struct
148 {
149  // Modules
150  spi_master_t spi;
152  pin_name_t chip_select;
154 } joystick3_t;
155 
160 typedef struct
161 {
162  // Communication gpio pins
163  pin_name_t miso;
164  pin_name_t mosi;
165  pin_name_t sck;
166  pin_name_t cs;
168  // static variable
169  uint32_t spi_speed;
170  spi_master_mode_t spi_mode;
171  spi_master_chip_select_polarity_t cs_polarity;
174 
179 typedef enum
180 {
182  JOYSTICK3_ERROR = -1
183 
185 
202 
217 
230 err_t joystick3_read_raw_adc ( joystick3_t *ctx, uint16_t *raw_x, uint16_t *raw_y );
231 
240 float joystick3_get_angle ( uint16_t raw_x, uint16_t raw_y );
241 
250 uint8_t joystick3_get_position ( uint16_t raw_x, uint16_t raw_y );
251 
252 #ifdef __cplusplus
253 }
254 #endif
255 #endif // JOYSTICK3_H
256  // joystick3
258 
259 // ------------------------------------------------------------------------ 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:152
joystick3_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: joystick3.h:171
JOYSTICK3_ERROR
@ JOYSTICK3_ERROR
Definition: joystick3.h:182
joystick3_cfg_t::cs
pin_name_t cs
Definition: joystick3.h:166
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:180
joystick3_cfg_t
Joystick 3 Click configuration object.
Definition: joystick3.h:161
joystick3_cfg_t::sck
pin_name_t sck
Definition: joystick3.h:165
joystick3_cfg_t::mosi
pin_name_t mosi
Definition: joystick3.h:164
joystick3_t::spi
spi_master_t spi
Definition: joystick3.h:150
joystick3_cfg_t::miso
pin_name_t miso
Definition: joystick3.h:163
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:170
joystick3_init
err_t joystick3_init(joystick3_t *ctx, joystick3_cfg_t *cfg)
Joystick 3 initialization function.
JOYSTICK3_OK
@ JOYSTICK3_OK
Definition: joystick3.h:181
joystick3_t
Joystick 3 Click context object.
Definition: joystick3.h:148
joystick3_cfg_t::spi_speed
uint32_t spi_speed
Definition: joystick3.h:169