thumbstick  2.0.0.0
thumbstick.h
Go to the documentation of this file.
1 /*
2  * MikroSDK - MikroE Software Development Kit
3  * Copyright (c) 2019, MikroElektronika - www.mikroe.com
4  * All rights reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * 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  // Input pins
106 
107  digital_in_t int_pin;
108 
109  // Modules
110 
111  spi_master_t spi;
112 
113 } thumbstick_t;
114 
118 typedef struct
119 {
120  // Communication gpio pins
121 
122  pin_name_t miso;
123  pin_name_t mosi;
124  pin_name_t sck;
125  pin_name_t cs;
126 
127  // Additional gpio pins
128 
129  pin_name_t int_pin;
130 
131  // static variable
132 
133  hal_spi_speed_t spi_speed;
134  hal_spi_mode_t spi_mode;
135 
137  // End types group
139 
140 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
141 
146 #ifdef __cplusplus
147 extern "C"{
148 #endif
149 
159 
168 
177 void thumbstick_generic_transfer ( thumbstick_t *ctx, spi_master_transfer_data_t *block );
178 
194 uint16_t thumbstick_read_data( thumbstick_t *ctx, uint8_t channel );
195 
212 uint8_t thumbstick_get_position( thumbstick_t *ctx );
213 
226 uint8_t thumbstick_button_state( thumbstick_t *ctx );
227 
228 #ifdef __cplusplus
229 }
230 #endif
231 #endif // _THUMBSTICK_H_
232  // End public_function group
235 
236 // ------------------------------------------------------------------------- END
hal_spi_speed_t spi_speed
Definition: thumbstick.h:133
pin_name_t miso
Definition: thumbstick.h:122
uint8_t thumbstick_button_state(thumbstick_t *ctx)
Get state of thumbstick button function.
void thumbstick_generic_transfer(thumbstick_t *ctx, spi_master_transfer_data_t *block)
Generic transfer function.
THUMBSTICK_RETVAL thumbstick_init(thumbstick_t *ctx, thumbstick_cfg_t *cfg)
Initialization function.
hal_spi_mode_t spi_mode
Definition: thumbstick.h:134
spi_master_t spi
Definition: thumbstick.h:111
pin_name_t sck
Definition: thumbstick.h:124
pin_name_t int_pin
Definition: thumbstick.h:129
void thumbstick_cfg_setup(thumbstick_cfg_t *cfg)
Config Object Initialization function.
#define THUMBSTICK_RETVAL
Definition: thumbstick.h:64
Click ctx object definition.
Definition: thumbstick.h:103
pin_name_t cs
Definition: thumbstick.h:125
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:107
pin_name_t mosi
Definition: thumbstick.h:123
Click configuration structure definition.
Definition: thumbstick.h:118