thumbstick  2.0.0.0
thumbstick.h
Go to the documentation of this file.
1 /*
2  * MikroSDK - MikroE Software Development Kit
3  * Copyright© 2020 MikroElektronika d.o.o.
4  *
5  * Permission is hereby granted, free of charge, to any person
6  * obtaining a copy of this software and associated documentation
7  * files (the "Software"), to deal in the Software without restriction,
8  * including without limitation the rights to use, copy, modify, merge,
9  * publish, distribute, sublicense, and/or sell copies of the Software,
10  * and to permit persons to whom the Software is furnished to do so,
11  * subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22  * OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
33 // ----------------------------------------------------------------------------
34 
35 #ifndef THUMBSTICK_H
36 #define THUMBSTICK_H
37 
38 #include "mikrosdk_version.h"
39 
40 #ifdef __GNUC__
41 #if mikroSDK_GET_VERSION < 20800ul
42 #include "rcu_delays.h"
43 #else
44 #include "delays.h"
45 #endif
46 #endif
47 
48 #include "drv_digital_in.h"
49 #include "drv_spi_master.h"
50 
51 // -------------------------------------------------------------- PUBLIC MACROS
62 #define THUMBSTICK_MAP_MIKROBUS( cfg, mikrobus ) \
63  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
64  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
65  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
66  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
67  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT );
68 
74 #define THUMBSTICK_RETVAL uint8_t
75 
76 #define THUMBSTICK_OK 0x00
77 #define THUMBSTICK_INIT_ERROR 0xFF
78 #define THUMBSTICK_FUNCTION_ERROR 0xEE
79 
85 #define THUMBSTICK_START_SINGLE_CONV 0x06
86 #define THUMBSTICK_START_DIFF_CONV 0x04
87 
93 #define THUMBSTICK_VERTICAL 0x00
94 #define THUMBSTICK_HORIZONTAL 0x60
95 
101 #define THUMBSTICK_POSITION_DEFAULT 0x01
102 #define THUMBSTICK_POSITION_TOP 0x02
103 #define THUMBSTICK_POSITION_RIGHT 0x03
104 #define THUMBSTICK_POSITION_BOTTOM 0x04
105 #define THUMBSTICK_POSITION_LEFT 0x05
106 
112 #define THUMBSTICK_BUTTON_INACTIVE 0x00
113 #define THUMBSTICK_BUTTON_ACTIVE 0x01
114 
120 #define POSTION_SENS_DEFAULT 500
121 
127 #define THUMBSTICK_PRESS_BUTTON 0x00
128  // End group macro
131 // --------------------------------------------------------------- PUBLIC TYPES
140 typedef struct
141 {
142  digital_out_t cs;
143 
144  // Input pins
145 
146  digital_in_t int_pin;
147 
148  // Modules
149 
150  spi_master_t spi;
151  pin_name_t chip_select;
152 
153 } thumbstick_t;
154 
158 typedef struct
159 {
160  // Communication gpio pins
161 
162  pin_name_t miso;
163  pin_name_t mosi;
164  pin_name_t sck;
165  pin_name_t cs;
166 
167  // Additional gpio pins
168 
169  pin_name_t int_pin;
170 
171  // static variable
172 
173  uint32_t spi_speed;
174  spi_master_mode_t spi_mode;
175  spi_master_chip_select_polarity_t cs_polarity;
176 
178 
182 typedef struct
183 {
184  uint8_t vertical;
185  uint8_t horizontal;
186 
188  // End types group
190 
191 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
192 
197 #ifdef __cplusplus
198 extern "C"{
199 #endif
200 
210 
219 
232 (
233  thumbstick_t *ctx,
234  uint8_t *wr_buf,
235  uint16_t wr_len,
236  uint8_t *rd_buf,
237  uint16_t rd_len
238 );
239 
261 uint16_t thumbstick_read_rawadc ( thumbstick_t *ctx, uint8_t type, uint8_t channel );
262 
287 uint8_t thumbstick_get_single_axis_postion ( thumbstick_t *ctx, uint8_t axis );
288 
297 
311 
312 
326 uint8_t thumbstick_set_sensitivity ( uint16_t sensitivity );
327 
328 
329 #ifdef __cplusplus
330 }
331 #endif
332 #endif // _THUMBSTICK_H_
333  // End public_function group
336 
337 // ------------------------------------------------------------------------- END
thumbstick_position_t::vertical
uint8_t vertical
Definition: thumbstick.h:184
thumbstick_t::chip_select
pin_name_t chip_select
Definition: thumbstick.h:151
thumbstick_cfg_t::cs
pin_name_t cs
Definition: thumbstick.h:165
thumbstick_get_position
void thumbstick_get_position(thumbstick_t *ctx, thumbstick_position_t *position)
Get thumbstick position by axis function.
thumbstick_set_sensitivity
uint8_t thumbstick_set_sensitivity(uint16_t sensitivity)
Set thumbstick sensitivity.
thumbstick_position_t::horizontal
uint8_t horizontal
Definition: thumbstick.h:185
THUMBSTICK_RETVAL
#define THUMBSTICK_RETVAL
Definition: thumbstick.h:74
thumbstick_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: thumbstick.h:174
thumbstick_cfg_t::int_pin
pin_name_t int_pin
Definition: thumbstick.h:169
thumbstick_t::int_pin
digital_in_t int_pin
Definition: thumbstick.h:146
thumbstick_t::cs
digital_out_t cs
Definition: thumbstick.h:142
thumbstick_cfg_t
Click configuration structure definition.
Definition: thumbstick.h:159
thumbstick_get_single_axis_postion
uint8_t thumbstick_get_single_axis_postion(thumbstick_t *ctx, uint8_t axis)
Get thumbstick position by axis function.
thumbstick_position_t
Click configuration structure definition.
Definition: thumbstick.h:183
thumbstick_generic_transfer
void thumbstick_generic_transfer(thumbstick_t *ctx, uint8_t *wr_buf, uint16_t wr_len, uint8_t *rd_buf, uint16_t rd_len)
Generic transfer function.
thumbstick_cfg_t::miso
pin_name_t miso
Definition: thumbstick.h:162
thumbstick_t::spi
spi_master_t spi
Definition: thumbstick.h:150
thumbstick_cfg_setup
void thumbstick_cfg_setup(thumbstick_cfg_t *cfg)
Config Object Initialization function.
thumbstick_t
Click ctx object definition.
Definition: thumbstick.h:141
thumbstick_cfg_t::sck
pin_name_t sck
Definition: thumbstick.h:164
thumbstick_cfg_t::spi_speed
uint32_t spi_speed
Definition: thumbstick.h:173
thumbstick_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: thumbstick.h:175
thumbstick_read_rawadc
uint16_t thumbstick_read_rawadc(thumbstick_t *ctx, uint8_t type, uint8_t channel)
Generic read 2 byte of data function.
thumbstick_button_state
uint8_t thumbstick_button_state(thumbstick_t *ctx)
Get state of thumbstick button function.
thumbstick_init
THUMBSTICK_RETVAL thumbstick_init(thumbstick_t *ctx, thumbstick_cfg_t *cfg)
Initialization function.
thumbstick_cfg_t::mosi
pin_name_t mosi
Definition: thumbstick.h:163