rotaryswitch  2.1.0.0
rotaryswitch.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 ROTARYSWITCH_H
29 #define ROTARYSWITCH_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 ROTARYSWITCH_REG_INPUT_PORT 0x00
74 #define ROTARYSWITCH_REG_OUTPUT_PORT 0x01
75 #define ROTARYSWITCH_REG_POLARITY_INV 0x02
76 #define ROTARYSWITCH_REG_CONFIG 0x03
77 #define ROTARYSWITCH_REG_SPECIAL_FUNC 0x50
78  // rotaryswitch_reg
80 
95 #define ROTARYSWITCH_CONFIG_DEFAULT 0xFF
96 #define ROTARYSWITCH_POLARITY_INV_DEFAULT 0x0F
97 #define ROTARYSWITCH_SPECIAL_FUNC_DEFAULT 0x00
98 
103 #define ROTARYSWITCH_NO_PIN_MASK 0x00
104 #define ROTARYSWITCH_PIN_0_MASK 0x01
105 #define ROTARYSWITCH_PIN_1_MASK 0x02
106 #define ROTARYSWITCH_PIN_2_MASK 0x04
107 #define ROTARYSWITCH_PIN_3_MASK 0x08
108 #define ROTARYSWITCH_ALL_PINS_MASK 0x0F
109 
115 #define ROTARYSWITCH_DEVICE_ADDRESS 0x41
116  // rotaryswitch_set
118 
133 #define ROTARYSWITCH_MAP_MIKROBUS( cfg, mikrobus ) \
134  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
135  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA );
136  // rotaryswitch_map // rotaryswitch
139 
144 typedef struct
145 {
146  // Modules
147  i2c_master_t i2c;
149  // I2C slave address
150  uint8_t slave_address;
153 
158 typedef struct
159 {
160  pin_name_t scl;
161  pin_name_t sda;
163  uint32_t i2c_speed;
164  uint8_t i2c_address;
167 
172 typedef enum
173 {
175  ROTARYSWITCH_ERROR = -1
176 
178 
195 
210 
224 
237 err_t rotaryswitch_write_reg ( rotaryswitch_t *ctx, uint8_t reg, uint8_t data_in );
238 
251 err_t rotaryswitch_read_reg ( rotaryswitch_t *ctx, uint8_t reg, uint8_t *data_out );
252 
264 err_t rotaryswitch_get_position ( rotaryswitch_t *ctx, uint8_t *position );
265 
266 #ifdef __cplusplus
267 }
268 #endif
269 #endif // ROTARYSWITCH_H
270  // rotaryswitch
272 
273 // ------------------------------------------------------------------------ END
rotaryswitch_cfg_t::sda
pin_name_t sda
Definition: rotaryswitch.h:161
rotaryswitch_init
err_t rotaryswitch_init(rotaryswitch_t *ctx, rotaryswitch_cfg_t *cfg)
Rotary Switch initialization function.
rotaryswitch_t::slave_address
uint8_t slave_address
Definition: rotaryswitch.h:150
rotaryswitch_cfg_t
Rotary Switch Click configuration object.
Definition: rotaryswitch.h:159
rotaryswitch_t
Rotary Switch Click context object.
Definition: rotaryswitch.h:145
rotaryswitch_read_reg
err_t rotaryswitch_read_reg(rotaryswitch_t *ctx, uint8_t reg, uint8_t *data_out)
Rotary Switch read reg function.
rotaryswitch_cfg_t::scl
pin_name_t scl
Definition: rotaryswitch.h:160
rotaryswitch_write_reg
err_t rotaryswitch_write_reg(rotaryswitch_t *ctx, uint8_t reg, uint8_t data_in)
Rotary Switch write reg function.
rotaryswitch_get_position
err_t rotaryswitch_get_position(rotaryswitch_t *ctx, uint8_t *position)
Rotary Switch get position function.
rotaryswitch_t::i2c
i2c_master_t i2c
Definition: rotaryswitch.h:147
rotaryswitch_default_cfg
err_t rotaryswitch_default_cfg(rotaryswitch_t *ctx)
Rotary Switch default configuration function.
rotaryswitch_cfg_t::i2c_address
uint8_t i2c_address
Definition: rotaryswitch.h:164
ROTARYSWITCH_ERROR
@ ROTARYSWITCH_ERROR
Definition: rotaryswitch.h:175
rotaryswitch_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: rotaryswitch.h:163
rotaryswitch_return_value_t
rotaryswitch_return_value_t
Rotary Switch Click return value data.
Definition: rotaryswitch.h:173
rotaryswitch_cfg_setup
void rotaryswitch_cfg_setup(rotaryswitch_cfg_t *cfg)
Rotary Switch configuration object setup function.
ROTARYSWITCH_OK
@ ROTARYSWITCH_OK
Definition: rotaryswitch.h:174