thermostat  2.0.0.0
thermostat.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 THERMOSTAT_H
36 #define THERMOSTAT_H
37 
42 #ifdef PREINIT_SUPPORTED
43 #include "preinit.h"
44 #endif
45 
46 #ifdef MikroCCoreVersion
47  #if MikroCCoreVersion >= 1
48  #include "delays.h"
49  #endif
50 #endif
51 
52 #include "drv_digital_out.h"
53 #include "drv_digital_in.h"
54 #include "drv_i2c_master.h"
55 
56 
57 // -------------------------------------------------------------- PUBLIC MACROS
67 #define THERMOSTAT_MAP_MIKROBUS( cfg, mikrobus ) \
68  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
69  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
70  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
71  cfg.te = MIKROBUS( mikrobus, MIKROBUS_CS )
72 
78 #define THERMOSTAT_RETVAL uint8_t
79 
80 #define THERMOSTAT_OK 0x00
81 #define THERMOSTAT_INIT_ERROR 0xFF
82 
88 #define THERMOSTAT_I2C_ADDRESS_0 0x48
89 #define THERMOSTAT_I2C_ADDRESS_1 0x49
90 #define THERMOSTAT_I2C_ADDRESS_2 0x4A
91 #define THERMOSTAT_I2C_ADDRESS_3 0x4B
92 
98 #define THERMOSTAT_REG_TEMP_OUT 0x00
99 #define THERMOSTAT_REG_CONFIG 0x01
100 #define THERMOSTAT_REG_TEMP_HYST 0x02
101 #define THERMOSTAT_REG_TEMP_REF 0x03
102 
103 #define THERMOSTAT_CHECK_SIGN 0x80
104 #define THERMOSTAT_DEFAULT_CONFIG 0x0000
105 #define THERMOSTAT_MSB_BITS 0xFF00
106 #define THERMOSTAT_LSB_BITS 0x00FF
107 #define THERMOSTAT_START_RESULT 0x0000
108 
109 #define THERMOSTAT_UPPER_LIMITS_TEMP 126.00
110 #define THERMOSTAT_LOWER_LIMITS_TEMP -55.00
111 #define THERMOSTAT_CALCULATE_COEF_MULT 1.8000
112 #define THERMOSTAT_CALCULATE_COEF_PLUS 32.000
113  // End group macro
116 // --------------------------------------------------------------- PUBLIC TYPES
125 typedef struct
126 {
127  // Output pins
128 
129  digital_out_t rst;
130  digital_out_t te;
131 
132  // Modules
133 
134  i2c_master_t i2c;
135 
136  // ctx variable
137 
138  uint8_t slave_address;
139 
140 } thermostat_t;
141 
145 typedef struct
146 {
147  // Communication gpio pins
148 
149  pin_name_t scl;
150  pin_name_t sda;
151 
152  // Additional gpio pins
153 
154  pin_name_t rst;
155  pin_name_t te;
156 
157  // static variable
158 
159  uint32_t i2c_speed;
160  uint8_t i2c_address;
161 
163  // End types group
165 
166 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
167 
173 #ifdef __cplusplus
174 extern "C"{
175 #endif
176 
186 
196 
207 void thermostat_generic_write ( thermostat_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
208 
219 void thermostat_generic_read ( thermostat_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
220 
230 void thermostat_write_data( thermostat_t *ctx, uint8_t reg_address, uint16_t write_data );
231 
240  uint16_t thermostat_read_data( thermostat_t *ctx, uint8_t reg_address );
241 
250 
259 float thermostat_convert_fahrenheit( thermostat_t *ctx, float temp_celsius );
260 
269  void thermostat_set_configuration( thermostat_t *ctx, uint16_t config_value );
270 
279  void thermostat_set_temp_hyst( thermostat_t *ctx, int8_t temp_hyst );
280 
289 void thermostat_set_temp_ref( thermostat_t *ctx, int8_t temp_ref );
290 
299 
308 
317 
326 
335 
336 
337 #ifdef __cplusplus
338 }
339 #endif
340 #endif // _THERMOSTAT_H_
341  // End public_function group
344 
345 // ------------------------------------------------------------------------- END
thermostat_cfg_t::scl
pin_name_t scl
Definition: thermostat.h:149
thermostat_convert_fahrenheit
float thermostat_convert_fahrenheit(thermostat_t *ctx, float temp_celsius)
Convert temperature in degrees Celsius to Fahrenheit.
thermostat_cfg_t::te
pin_name_t te
Definition: thermostat.h:155
thermostat_set_configuration
void thermostat_set_configuration(thermostat_t *ctx, uint16_t config_value)
Set configuration function.
thermostat_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: thermostat.h:159
THERMOSTAT_RETVAL
#define THERMOSTAT_RETVAL
Definition: thermostat.h:78
thermostat_cfg_t::i2c_address
uint8_t i2c_address
Definition: thermostat.h:160
thermostat_enable
void thermostat_enable(thermostat_t *ctx)
Thermostat enabled function.
thermostat_cfg_setup
void thermostat_cfg_setup(thermostat_cfg_t *cfg)
Config Object Initialization function.
thermostat_init
THERMOSTAT_RETVAL thermostat_init(thermostat_t *ctx, thermostat_cfg_t *cfg)
Initialization function.
thermostat_get_temperature
float thermostat_get_temperature(thermostat_t *ctx)
Get temperature in degrees Celsius.
thermostat_t
Click ctx object definition.
Definition: thermostat.h:126
thermostat_t::slave_address
uint8_t slave_address
Definition: thermostat.h:138
thermostat_switch_off
void thermostat_switch_off(thermostat_t *ctx)
Set off thermal switch function.
thermostat_soft_reset
void thermostat_soft_reset(thermostat_t *ctx)
Soft reset function.
thermostat_write_data
void thermostat_write_data(thermostat_t *ctx, uint8_t reg_address, uint16_t write_data)
Generic write 16-bit data function.
thermostat_t::te
digital_out_t te
Definition: thermostat.h:130
thermostat_t::rst
digital_out_t rst
Definition: thermostat.h:129
thermostat_set_temp_ref
void thermostat_set_temp_ref(thermostat_t *ctx, int8_t temp_ref)
Set referent temperature function.
thermostat_generic_write
void thermostat_generic_write(thermostat_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
thermostat_cfg_t::sda
pin_name_t sda
Definition: thermostat.h:150
thermostat_read_data
uint16_t thermostat_read_data(thermostat_t *ctx, uint8_t reg_address)
Generic read 16-bit data function.
thermostat_switch_on
void thermostat_switch_on(thermostat_t *ctx)
Set on thermal switch function.
thermostat_set_temp_hyst
void thermostat_set_temp_hyst(thermostat_t *ctx, int8_t temp_hyst)
Set temperature hysterezis function.
thermostat_disable
void thermostat_disable(thermostat_t *ctx)
Thermostat disabled function.
thermostat_cfg_t::rst
pin_name_t rst
Definition: thermostat.h:154
thermostat_t::i2c
i2c_master_t i2c
Definition: thermostat.h:134
thermostat_generic_read
void thermostat_generic_read(thermostat_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.
thermostat_cfg_t
Click configuration structure definition.
Definition: thermostat.h:146