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 "drv_digital_out.h"
36 #include "drv_digital_in.h"
37 #include "drv_spi_master.h"
38 #include "spi_specifics.h"
39 
60 #define JOYSTICK3_ADC_START_BIT 0x04
61 #define JOYSTICK3_ADC_SINGLE_ENDED 0x02
62 #define JOYSTICK3_ADC_CHANNEL_X 0x00
63 #define JOYSTICK3_ADC_CHANNEL_Y 0x40
64 #define JOYSTICK3_ADC_DATA_RESOLUTION 0x0FFF
65 #define JOYSTICK3_ADC_DATA_RESOLUTION_HALF ( ( float ) JOYSTICK3_ADC_DATA_RESOLUTION / 2 )
66 
71 #define JOYSTICK3_RADIANS_TO_DEGREES 57.295779513
72 #define JOYSTICK3_ANGLE_ZERO_OFFSET 180.0
73 #define JOYSTICK3_PI_RAD_OFFSET 1.0
74 #define JOYSTICK3_NEUTRAL_POSITION_OFFSET ( ( float ) JOYSTICK3_ADC_DATA_RESOLUTION / 8 )
75 #define JOYSTICK3_FULL_ANGLE 360.0f
76 #define JOYSTICK3_ZERO_ANGLE 0.0f
77 #define JOYSTICK3_ANGLE_POSITION_STEP 45.0f
78 
83 #define JOYSTICK3_POSITION_NEUTRAL 0
84 #define JOYSTICK3_POSITION_UP 1
85 #define JOYSTICK3_POSITION_UPPER_RIGHT 2
86 #define JOYSTICK3_POSITION_RIGHT 3
87 #define JOYSTICK3_POSITION_LOWER_RIGHT 4
88 #define JOYSTICK3_POSITION_DOWN 5
89 #define JOYSTICK3_POSITION_LOWER_LEFT 6
90 #define JOYSTICK3_POSITION_LEFT 7
91 #define JOYSTICK3_POSITION_UPPER_LEFT 8
92 
101 #define JOYSTICK3_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
102 #define JOYSTICK3_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
103  // joystick3_set
105 
120 #define JOYSTICK3_MAP_MIKROBUS( cfg, mikrobus ) \
121  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
122  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
123  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
124  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS );
125  // joystick3_map // joystick3
128 
133 typedef struct
134 {
135  // Modules
136  spi_master_t spi;
138  pin_name_t chip_select;
140 } joystick3_t;
141 
146 typedef struct
147 {
148  // Communication gpio pins
149  pin_name_t miso;
150  pin_name_t mosi;
151  pin_name_t sck;
152  pin_name_t cs;
154  // static variable
155  uint32_t spi_speed;
156  spi_master_mode_t spi_mode;
157  spi_master_chip_select_polarity_t cs_polarity;
160 
165 typedef enum
166 {
168  JOYSTICK3_ERROR = -1
169 
171 
188 
203 
216 err_t joystick3_read_raw_adc ( joystick3_t *ctx, uint16_t *raw_x, uint16_t *raw_y );
217 
226 float joystick3_get_angle ( uint16_t raw_x, uint16_t raw_y );
227 
236 uint8_t joystick3_get_position ( uint16_t raw_x, uint16_t raw_y );
237 
238 #ifdef __cplusplus
239 }
240 #endif
241 #endif // JOYSTICK3_H
242  // joystick3
244 
245 // ------------------------------------------------------------------------ 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:138
joystick3_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: joystick3.h:157
JOYSTICK3_ERROR
@ JOYSTICK3_ERROR
Definition: joystick3.h:168
joystick3_cfg_t::cs
pin_name_t cs
Definition: joystick3.h:152
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:166
joystick3_cfg_t
Joystick 3 Click configuration object.
Definition: joystick3.h:147
joystick3_cfg_t::sck
pin_name_t sck
Definition: joystick3.h:151
joystick3_cfg_t::mosi
pin_name_t mosi
Definition: joystick3.h:150
joystick3_t::spi
spi_master_t spi
Definition: joystick3.h:136
joystick3_cfg_t::miso
pin_name_t miso
Definition: joystick3.h:149
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:156
joystick3_init
err_t joystick3_init(joystick3_t *ctx, joystick3_cfg_t *cfg)
Joystick 3 initialization function.
JOYSTICK3_OK
@ JOYSTICK3_OK
Definition: joystick3.h:167
joystick3_t
Joystick 3 Click context object.
Definition: joystick3.h:134
joystick3_cfg_t::spi_speed
uint32_t spi_speed
Definition: joystick3.h:155