joystick4  2.1.0.0
joystick4.h
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (C) 2020 MikroElektronika d.o.o.
3 ** Contact: https://www.mikroe.com/contact
4 **
5 ** Permission is hereby granted, free of charge, to any person obtaining a copy
6 ** of this software and associated documentation files (the "Software"), to deal
7 ** in the Software without restriction, including without limitation the rights
8 ** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 ** copies of the Software, and to permit persons to whom the Software is
10 ** furnished to do so, subject to the following conditions:
11 ** The above copyright notice and this permission notice shall be
12 ** included in all copies or substantial portions of the Software.
13 **
14 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16 ** OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 ** DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
19 ** OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 ** USE OR OTHER DEALINGS IN THE SOFTWARE.
21 ****************************************************************************/
22 
28 #ifndef JOYSTICK4_H
29 #define JOYSTICK4_H
30 
31 #ifdef __cplusplus
32 extern "C"{
33 #endif
34 
39 #ifdef PREINIT_SUPPORTED
40 #include "preinit.h"
41 #endif
42 
43 #ifdef MikroCCoreVersion
44  #if MikroCCoreVersion >= 1
45  #include "delays.h"
46  #endif
47 #endif
48 
49 #include "drv_digital_out.h"
50 #include "drv_digital_in.h"
51 #include "drv_i2c_master.h"
52 
73 #define JOYSTICK4_REG_INPUT 0x00
74 #define JOYSTICK4_REG_OUTPUT 0x01
75 #define JOYSTICK4_REG_POLARITY 0x02
76 #define JOYSTICK4_REG_CONFIG 0x03
77  // joystick4_reg
79 
94 #define JOYSTICK4_PIN_A 0x20
95 #define JOYSTICK4_PIN_B 0x08
96 #define JOYSTICK4_PIN_C 0x80
97 #define JOYSTICK4_PIN_D 0x10
98 #define JOYSTICK4_PIN_CE 0x40
99 
104 #define JOYSTICK4_DEFAULT_PIN_CONFIG 0xFF
105 
110 #define JOYSTICK4_PIN_STATE_LOW 0
111 #define JOYSTICK4_PIN_STATE_HIGH 1
112 
117 #define JOYSTICK4_POSITION_IDLE 0
118 #define JOYSTICK4_POSITION_CENTER 1
119 #define JOYSTICK4_POSITION_CENTER_UP 2
120 #define JOYSTICK4_POSITION_CENTER_RIGHT 3
121 #define JOYSTICK4_POSITION_CENTER_DOWN 4
122 #define JOYSTICK4_POSITION_CENTER_LEFT 5
123 #define JOYSTICK4_POSITION_UP 6
124 #define JOYSTICK4_POSITION_UPPER_RIGHT 7
125 #define JOYSTICK4_POSITION_RIGHT 8
126 #define JOYSTICK4_POSITION_LOWER_RIGHT 9
127 #define JOYSTICK4_POSITION_DOWN 10
128 #define JOYSTICK4_POSITION_LOWER_LEFT 11
129 #define JOYSTICK4_POSITION_LEFT 12
130 #define JOYSTICK4_POSITION_UPPER_LEFT 13
131 
137 #define JOYSTICK4_DEVICE_ADDRESS_0 0x20
138 #define JOYSTICK4_DEVICE_ADDRESS_1 0x21
139  // joystick4_set
141 
156 #define JOYSTICK4_MAP_MIKROBUS( cfg, mikrobus ) \
157  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
158  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
159  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
160  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
161  // joystick4_map // joystick4
164 
169 typedef struct
170 {
171  // Output pins
172  digital_out_t rst;
174  // Input pins
175  digital_in_t int_pin;
177  // Modules
178  i2c_master_t i2c;
180  // I2C slave address
181  uint8_t slave_address;
183 } joystick4_t;
184 
189 typedef struct
190 {
191  pin_name_t scl;
192  pin_name_t sda;
194  pin_name_t rst;
195  pin_name_t int_pin;
197  uint32_t i2c_speed;
198  uint8_t i2c_address;
201 
206 typedef enum
207 {
209  JOYSTICK4_ERROR = -1
210 
212 
229 
244 
258 
272 err_t joystick4_write_reg ( joystick4_t *ctx, uint8_t reg, uint8_t data_in );
273 
286 err_t joystick4_read_reg ( joystick4_t *ctx, uint8_t reg, uint8_t *data_out );
287 
297 
308 void joystick4_set_rst_pin ( joystick4_t *ctx, uint8_t state );
309 
319 
331 err_t joystick4_get_pins ( joystick4_t *ctx, uint8_t *pin_mask );
332 
340 uint8_t joystick4_get_position ( uint8_t pin_mask );
341 
342 #ifdef __cplusplus
343 }
344 #endif
345 #endif // JOYSTICK4_H
346  // joystick4
348 
349 // ------------------------------------------------------------------------ END
joystick4_t
Joystick 4 Click context object.
Definition: joystick4.h:170
joystick4_cfg_t::scl
pin_name_t scl
Definition: joystick4.h:191
joystick4_get_int_pin
uint8_t joystick4_get_int_pin(joystick4_t *ctx)
Joystick 4 get INT pin function.
joystick4_cfg_setup
void joystick4_cfg_setup(joystick4_cfg_t *cfg)
Joystick 4 configuration object setup function.
joystick4_get_position
uint8_t joystick4_get_position(uint8_t pin_mask)
Joystick 4 get position function.
joystick4_cfg_t
Joystick 4 Click configuration object.
Definition: joystick4.h:190
joystick4_t::i2c
i2c_master_t i2c
Definition: joystick4.h:178
joystick4_reset_device
void joystick4_reset_device(joystick4_t *ctx)
Joystick 4 reset device function.
JOYSTICK4_OK
@ JOYSTICK4_OK
Definition: joystick4.h:208
JOYSTICK4_ERROR
@ JOYSTICK4_ERROR
Definition: joystick4.h:209
joystick4_get_pins
err_t joystick4_get_pins(joystick4_t *ctx, uint8_t *pin_mask)
Joystick 4 get pins function.
joystick4_return_value_t
joystick4_return_value_t
Joystick 4 Click return value data.
Definition: joystick4.h:207
joystick4_cfg_t::sda
pin_name_t sda
Definition: joystick4.h:192
joystick4_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: joystick4.h:197
joystick4_write_reg
err_t joystick4_write_reg(joystick4_t *ctx, uint8_t reg, uint8_t data_in)
Joystick 4 write reg function.
joystick4_t::slave_address
uint8_t slave_address
Definition: joystick4.h:181
joystick4_set_rst_pin
void joystick4_set_rst_pin(joystick4_t *ctx, uint8_t state)
Joystick 4 set RST pin function.
joystick4_read_reg
err_t joystick4_read_reg(joystick4_t *ctx, uint8_t reg, uint8_t *data_out)
Joystick 4 read reg function.
joystick4_cfg_t::i2c_address
uint8_t i2c_address
Definition: joystick4.h:198
joystick4_t::rst
digital_out_t rst
Definition: joystick4.h:172
joystick4_cfg_t::int_pin
pin_name_t int_pin
Definition: joystick4.h:195
joystick4_default_cfg
err_t joystick4_default_cfg(joystick4_t *ctx)
Joystick 4 default configuration function.
joystick4_t::int_pin
digital_in_t int_pin
Definition: joystick4.h:175
joystick4_init
err_t joystick4_init(joystick4_t *ctx, joystick4_cfg_t *cfg)
Joystick 4 initialization function.
joystick4_cfg_t::rst
pin_name_t rst
Definition: joystick4.h:194