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 
74 #define THUMBSTICK_START_SINGLE_ENDED_BIT 0x04
75 #define THUMBSTICK_CHANNEL_V 0x00
76 #define THUMBSTICK_CHANNEL_H 0x40
77 #define THUMBSTICK_POSITION_START 0x00
78 #define THUMBSTICK_POSITION_TOP 0x01
79 #define THUMBSTICK_POSITION_RIGHT 0x02
80 #define THUMBSTICK_CHANNEL_BOTTOM 0x03
81 #define THUMBSTICK_CHANNEL_LEFT 0x04
82 #define THUMBSTICK_BUTTON_INACTIVE 0x00
83 #define THUMBSTICK_BUTTON_ACTIVE 0x01
84 
90 #define THUMBSTICK_PRESS_BUTTON 0x00
91  // End group macro
94 // --------------------------------------------------------------- PUBLIC TYPES
103 typedef struct
104 {
105  digital_out_t cs;
106 
107  // Input pins
108 
109  digital_in_t int_pin;
110 
111  // Modules
112 
113  spi_master_t spi;
114  pin_name_t chip_select;
115 
116 } thumbstick_t;
117 
121 typedef struct
122 {
123  // Communication gpio pins
124 
125  pin_name_t miso;
126  pin_name_t mosi;
127  pin_name_t sck;
128  pin_name_t cs;
129 
130  // Additional gpio pins
131 
132  pin_name_t int_pin;
133 
134  // static variable
135 
136  uint32_t spi_speed;
137  uint8_t spi_mode;
138  spi_master_chip_select_polarity_t cs_polarity;
139 
141  // End types group
143 
144 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
145 
150 #ifdef __cplusplus
151 extern "C"{
152 #endif
153 
163 
172 
185 (
186  thumbstick_t *ctx,
187  uint8_t *wr_buf,
188  uint16_t wr_len,
189  uint8_t *rd_buf,
190  uint16_t rd_len
191 );
192 
208 uint16_t thumbstick_read_data( thumbstick_t *ctx, uint8_t channel );
209 
226 uint8_t thumbstick_get_position( thumbstick_t *ctx );
227 
240 uint8_t thumbstick_button_state( thumbstick_t *ctx );
241 
242 #ifdef __cplusplus
243 }
244 #endif
245 #endif // _THUMBSTICK_H_
246  // End public_function group
249 
250 // ------------------------------------------------------------------------- END
spi_master_chip_select_polarity_t cs_polarity
Definition: thumbstick.h:138
pin_name_t miso
Definition: thumbstick.h:125
digital_out_t cs
Definition: thumbstick.h:105
uint8_t thumbstick_button_state(thumbstick_t *ctx)
Get state of thumbstick button function.
pin_name_t chip_select
Definition: thumbstick.h:114
THUMBSTICK_RETVAL thumbstick_init(thumbstick_t *ctx, thumbstick_cfg_t *cfg)
Initialization function.
spi_master_t spi
Definition: thumbstick.h:113
pin_name_t sck
Definition: thumbstick.h:127
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.
pin_name_t int_pin
Definition: thumbstick.h:132
void thumbstick_cfg_setup(thumbstick_cfg_t *cfg)
Config Object Initialization function.
uint8_t spi_mode
Definition: thumbstick.h:137
uint32_t spi_speed
Definition: thumbstick.h:136
#define THUMBSTICK_RETVAL
Definition: thumbstick.h:64
Click ctx object definition.
Definition: thumbstick.h:103
pin_name_t cs
Definition: thumbstick.h:128
uint16_t thumbstick_read_data(thumbstick_t *ctx, uint8_t channel)
Generic read 2 byte of data function.
uint8_t thumbstick_get_position(thumbstick_t *ctx)
Get thumbstick position function.
digital_in_t int_pin
Definition: thumbstick.h:109
pin_name_t mosi
Definition: thumbstick.h:126
Click configuration structure definition.
Definition: thumbstick.h:121