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 
42 #ifdef PREINIT_SUPPORTED
43 #include "preinit.h"
44 #endif
45 
46 #ifdef MikroCCoreVersion
47  #if MikroCCoreVersion >= 1
48  #include "delays.h"
49  #endif
50 #endif
51 
52 #include "drv_digital_in.h"
53 #include "drv_spi_master.h"
54 
55 // -------------------------------------------------------------- PUBLIC MACROS
66 #define THUMBSTICK_MAP_MIKROBUS( cfg, mikrobus ) \
67  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
68  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
69  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
70  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
71  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT );
72 
78 #define THUMBSTICK_RETVAL uint8_t
79 
80 #define THUMBSTICK_OK 0x00
81 #define THUMBSTICK_INIT_ERROR 0xFF
82 #define THUMBSTICK_FUNCTION_ERROR 0xEE
83 
89 #define THUMBSTICK_START_SINGLE_CONV 0x06
90 #define THUMBSTICK_START_DIFF_CONV 0x04
91 
97 #define THUMBSTICK_VERTICAL 0x00
98 #define THUMBSTICK_HORIZONTAL 0x60
99 
105 #define THUMBSTICK_POSITION_DEFAULT 0x01
106 #define THUMBSTICK_POSITION_TOP 0x02
107 #define THUMBSTICK_POSITION_RIGHT 0x03
108 #define THUMBSTICK_POSITION_BOTTOM 0x04
109 #define THUMBSTICK_POSITION_LEFT 0x05
110 
116 #define THUMBSTICK_BUTTON_INACTIVE 0x00
117 #define THUMBSTICK_BUTTON_ACTIVE 0x01
118 
124 #define POSTION_SENS_DEFAULT 500
125 
131 #define THUMBSTICK_PRESS_BUTTON 0x00
132  // End group macro
135 // --------------------------------------------------------------- PUBLIC TYPES
144 typedef struct
145 {
146  digital_out_t cs;
147 
148  // Input pins
149 
150  digital_in_t int_pin;
151 
152  // Modules
153 
154  spi_master_t spi;
155  pin_name_t chip_select;
156 
157 } thumbstick_t;
158 
162 typedef struct
163 {
164  // Communication gpio pins
165 
166  pin_name_t miso;
167  pin_name_t mosi;
168  pin_name_t sck;
169  pin_name_t cs;
170 
171  // Additional gpio pins
172 
173  pin_name_t int_pin;
174 
175  // static variable
176 
177  uint32_t spi_speed;
178  spi_master_mode_t spi_mode;
179  spi_master_chip_select_polarity_t cs_polarity;
180 
182 
186 typedef struct
187 {
188  uint8_t vertical;
189  uint8_t horizontal;
190 
192  // End types group
194 
195 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
196 
201 #ifdef __cplusplus
202 extern "C"{
203 #endif
204 
214 
223 
236 (
237  thumbstick_t *ctx,
238  uint8_t *wr_buf,
239  uint16_t wr_len,
240  uint8_t *rd_buf,
241  uint16_t rd_len
242 );
243 
265 uint16_t thumbstick_read_rawadc ( thumbstick_t *ctx, uint8_t type, uint8_t channel );
266 
291 uint8_t thumbstick_get_single_axis_postion ( thumbstick_t *ctx, uint8_t axis );
292 
301 
315 
316 
330 uint8_t thumbstick_set_sensitivity ( uint16_t sensitivity );
331 
332 
333 #ifdef __cplusplus
334 }
335 #endif
336 #endif // _THUMBSTICK_H_
337  // End public_function group
340 
341 // ------------------------------------------------------------------------- END
thumbstick_position_t::vertical
uint8_t vertical
Definition: thumbstick.h:188
thumbstick_t::chip_select
pin_name_t chip_select
Definition: thumbstick.h:155
thumbstick_cfg_t::cs
pin_name_t cs
Definition: thumbstick.h:169
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:189
THUMBSTICK_RETVAL
#define THUMBSTICK_RETVAL
Definition: thumbstick.h:78
thumbstick_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: thumbstick.h:178
thumbstick_cfg_t::int_pin
pin_name_t int_pin
Definition: thumbstick.h:173
thumbstick_t::int_pin
digital_in_t int_pin
Definition: thumbstick.h:150
thumbstick_t::cs
digital_out_t cs
Definition: thumbstick.h:146
thumbstick_cfg_t
Click configuration structure definition.
Definition: thumbstick.h:163
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:187
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:166
thumbstick_t::spi
spi_master_t spi
Definition: thumbstick.h:154
thumbstick_cfg_setup
void thumbstick_cfg_setup(thumbstick_cfg_t *cfg)
Config Object Initialization function.
thumbstick_t
Click ctx object definition.
Definition: thumbstick.h:145
thumbstick_cfg_t::sck
pin_name_t sck
Definition: thumbstick.h:168
thumbstick_cfg_t::spi_speed
uint32_t spi_speed
Definition: thumbstick.h:177
thumbstick_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: thumbstick.h:179
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:167