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 "drv_digital_in.h"
39 #include "drv_spi_master.h"
40 
41 // -------------------------------------------------------------- PUBLIC MACROS
52 #define THUMBSTICK_MAP_MIKROBUS( cfg, mikrobus ) \
53  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
54  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
55  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
56  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
57  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT );
58 
64 #define THUMBSTICK_RETVAL uint8_t
65 
66 #define THUMBSTICK_OK 0x00
67 #define THUMBSTICK_INIT_ERROR 0xFF
68 #define THUMBSTICK_FUNCTION_ERROR 0xEE
69 
75 #define THUMBSTICK_START_SINGLE_CONV 0x06
76 #define THUMBSTICK_START_DIFF_CONV 0x04
77 
83 #define THUMBSTICK_VERTICAL 0x00
84 #define THUMBSTICK_HORIZONTAL 0x60
85 
91 #define THUMBSTICK_POSITION_DEFAULT 0x01
92 #define THUMBSTICK_POSITION_TOP 0x02
93 #define THUMBSTICK_POSITION_RIGHT 0x03
94 #define THUMBSTICK_POSITION_BOTTOM 0x04
95 #define THUMBSTICK_POSITION_LEFT 0x05
96 
102 #define THUMBSTICK_BUTTON_INACTIVE 0x00
103 #define THUMBSTICK_BUTTON_ACTIVE 0x01
104 
110 #define POSTION_SENS_DEFAULT 500
111 
117 #define THUMBSTICK_PRESS_BUTTON 0x00
118  // End group macro
121 // --------------------------------------------------------------- PUBLIC TYPES
130 typedef struct
131 {
132  digital_out_t cs;
133 
134  // Input pins
135 
136  digital_in_t int_pin;
137 
138  // Modules
139 
140  spi_master_t spi;
141  pin_name_t chip_select;
142 
143 } thumbstick_t;
144 
148 typedef struct
149 {
150  // Communication gpio pins
151 
152  pin_name_t miso;
153  pin_name_t mosi;
154  pin_name_t sck;
155  pin_name_t cs;
156 
157  // Additional gpio pins
158 
159  pin_name_t int_pin;
160 
161  // static variable
162 
163  uint32_t spi_speed;
164  spi_master_mode_t spi_mode;
165  spi_master_chip_select_polarity_t cs_polarity;
166 
168 
172 typedef struct
173 {
174  uint8_t vertical;
175  uint8_t horizontal;
176 
178  // End types group
180 
181 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
182 
187 #ifdef __cplusplus
188 extern "C"{
189 #endif
190 
200 
209 
222 (
223  thumbstick_t *ctx,
224  uint8_t *wr_buf,
225  uint16_t wr_len,
226  uint8_t *rd_buf,
227  uint16_t rd_len
228 );
229 
251 uint16_t thumbstick_read_rawadc ( thumbstick_t *ctx, uint8_t type, uint8_t channel );
252 
277 uint8_t thumbstick_get_single_axis_postion ( thumbstick_t *ctx, uint8_t axis );
278 
287 
301 
302 
316 uint8_t thumbstick_set_sensitivity ( uint16_t sensitivity );
317 
318 
319 #ifdef __cplusplus
320 }
321 #endif
322 #endif // _THUMBSTICK_H_
323  // End public_function group
326 
327 // ------------------------------------------------------------------------- END
thumbstick_position_t::vertical
uint8_t vertical
Definition: thumbstick.h:174
thumbstick_t::chip_select
pin_name_t chip_select
Definition: thumbstick.h:141
thumbstick_cfg_t::cs
pin_name_t cs
Definition: thumbstick.h:155
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:175
THUMBSTICK_RETVAL
#define THUMBSTICK_RETVAL
Definition: thumbstick.h:64
thumbstick_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: thumbstick.h:164
thumbstick_cfg_t::int_pin
pin_name_t int_pin
Definition: thumbstick.h:159
thumbstick_t::int_pin
digital_in_t int_pin
Definition: thumbstick.h:136
thumbstick_t::cs
digital_out_t cs
Definition: thumbstick.h:132
thumbstick_cfg_t
Click configuration structure definition.
Definition: thumbstick.h:149
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:173
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:152
thumbstick_t::spi
spi_master_t spi
Definition: thumbstick.h:140
thumbstick_cfg_setup
void thumbstick_cfg_setup(thumbstick_cfg_t *cfg)
Config Object Initialization function.
thumbstick_t
Click ctx object definition.
Definition: thumbstick.h:131
thumbstick_cfg_t::sck
pin_name_t sck
Definition: thumbstick.h:154
thumbstick_cfg_t::spi_speed
uint32_t spi_speed
Definition: thumbstick.h:163
thumbstick_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: thumbstick.h:165
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:153