Skip to content

Commit 7fc4fce

Browse files
committed
added business logic
1 parent 43575a1 commit 7fc4fce

File tree

6 files changed

+137
-35
lines changed

6 files changed

+137
-35
lines changed

Core/Inc/bl.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* aht20_driver_stm32f446ret
3+
* driver for aht20 temperature and humidity sensor
4+
* Copyright (C) 2025 Andrew Kushyk
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Affero General Public License as published
8+
* by the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Affero General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Affero General Public License
17+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
20+
#include "main.h"
21+
22+
/*
23+
* return statuses for business logic
24+
*/
25+
typedef enum {
26+
BL_STATUS_OK = 1,
27+
BL_STATUS_RUN_FAILED,
28+
} bl_status_t;
29+
30+
/*
31+
* runs calibration check. if wasn't calibrated, calibrates the sensor
32+
*/
33+
bl_status_t bl_run_sensor(I2C_HandleTypeDef *hi2c, UART_HandleTypeDef *huart);
34+
35+
/*
36+
* processes and calculates sensor data
37+
*/
38+
bl_status_t bl_process_sensor_data(I2C_HandleTypeDef *hi2c, UART_HandleTypeDef *huart);
39+
40+
/*
41+
* transmits formatted data to UART
42+
*/
43+
void bl_uart_transmit_sensor_data(UART_HandleTypeDef *huart);

Core/Inc/main.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ extern "C" {
4747
/* Exported macro ------------------------------------------------------------*/
4848
/* USER CODE BEGIN EM */
4949

50+
/* remove the comment to enable debugging messages via UART */
51+
//#define DEBUGGING
52+
5053
/* USER CODE END EM */
5154

5255
/* Exported functions prototypes ---------------------------------------------*/

Core/Inc/utils.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@
2121
#include <aht20.h>
2222
#include "main.h"
2323

24+
#ifdef DEBUGGING
2425
/*
2526
* prints error message via UART
2627
*/
2728
void print_error(UART_HandleTypeDef *huart, aht20_status_t status);
29+
#endif
30+
31+
/* transmits data to UART */
32+
void transmit_data(UART_HandleTypeDef *huart, float humidity, float temperature_c, float temperature_f);

Core/Src/bl.c

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* aht20_driver_stm32f446ret
3+
* driver for aht20 temperature and humidity sensor
4+
* Copyright (C) 2025 Andrew Kushyk
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Affero General Public License as published
8+
* by the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Affero General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Affero General Public License
17+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
#include "bl.h"
20+
#include "aht20.h"
21+
#include "utils.h"
22+
23+
/*
24+
* holds sensor data
25+
*/
26+
static aht20_data_t sensor_data = {0};
27+
28+
/*
29+
* runs calibration check. if wasn't calibrated, calibrates the sensor
30+
*/
31+
bl_status_t bl_run_sensor(I2C_HandleTypeDef *hi2c, UART_HandleTypeDef *huart) {
32+
aht20_status_t status = AHT20_STATUS_OK;
33+
34+
status = aht20_api.aht20_validate_calibration(hi2c);
35+
if (status != AHT20_STATUS_OK) {
36+
#ifdef DEBUGGING
37+
print_error(huart, status);
38+
#endif
39+
return BL_STATUS_RUN_FAILED;
40+
}
41+
42+
return BL_STATUS_OK;
43+
}
44+
45+
/*
46+
* processes and calculates sensor data
47+
*/
48+
bl_status_t bl_process_sensor_data(I2C_HandleTypeDef *hi2c, UART_HandleTypeDef *huart) {
49+
aht20_status_t status = AHT20_STATUS_OK;
50+
51+
status = aht20_measure(hi2c, sensor_data.measured_data, (uint16_t)sizeof(sensor_data.measured_data));
52+
if (status != AHT20_STATUS_OK) {
53+
#ifdef DEBUGGING
54+
print_error(huart, status);
55+
#endif
56+
status = aht20_soft_reset(hi2c);
57+
if (status != AHT20_STATUS_OK) {
58+
return BL_STATUS_RUN_FAILED;
59+
}
60+
}
61+
62+
aht20_calculate_measurments(sensor_data.measured_data, &sensor_data.humidity, &sensor_data.temperature_c, &sensor_data.temperature_f);
63+
64+
return BL_STATUS_OK;
65+
}
66+
67+
/*
68+
* transmits formatted data to UART
69+
*/
70+
void bl_uart_transmit_sensor_data(UART_HandleTypeDef *huart) {
71+
transmit_data(huart, sensor_data.humidity, sensor_data.temperature_c, sensor_data.temperature_f);
72+
}

Core/Src/main.c

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
/* Private includes ----------------------------------------------------------*/
2727
/* USER CODE BEGIN Includes */
28-
#include "aht20.h"
28+
#include "bl.h"
2929
#include "utils.h"
3030
#include <stdio.h>
3131
#include <string.h>
@@ -64,18 +64,7 @@ static void MX_I2C1_Init(void);
6464

6565
/* Private user code ---------------------------------------------------------*/
6666
/* USER CODE BEGIN 0 */
67-
/* just a wrapper for HAL_UART_Transmit*/
68-
void UART_Send_String(const char* str)
69-
{
70-
HAL_UART_Transmit(&huart2, (uint8_t*)str, strlen(str), HAL_MAX_DELAY);
71-
}
7267

73-
/* transmits data to UART */
74-
void transmit_data(float humidity, float temperature_c, float temperature_f) {
75-
char data_to_display[60] = "\0";
76-
sprintf(data_to_display, "Humidity: %.2f%%, Temperature: %.2fC, %.2fF\r\n", humidity, temperature_c, temperature_f);
77-
HAL_UART_Transmit(&huart2, (uint8_t *)data_to_display, strlen(data_to_display), HAL_MAX_DELAY);
78-
}
7968
/* USER CODE END 0 */
8069

8170
/**
@@ -109,39 +98,20 @@ int main(void)
10998
MX_USART2_UART_Init();
11099
MX_I2C1_Init();
111100
/* USER CODE BEGIN 2 */
112-
113-
/*
114-
* Datasheet: AHT20 Product manuals
115-
* 5.4 Sensor reading process, paragraph 1
116-
*/
117-
aht20_status_t status = AHT20_STATUS_OK;
118-
119-
/* getting info about sensor calibration */
120-
status = aht20_validate_calibration(&hi2c1);
121-
if (status != AHT20_STATUS_OK) {
122-
print_error(&huart2, status);
101+
if(BL_STATUS_OK != bl_run_sensor(&hi2c1, &huart2)) {
123102
return 1;
124103
}
125-
126104
/* USER CODE END 2 */
127105

128106
/* Infinite loop */
129107
/* USER CODE BEGIN WHILE */
130-
aht20_data_t sensor_data = {0};
131-
132108
while (1)
133109
{
134-
/* triggering measuring */
135-
status = aht20_measure(&hi2c1, sensor_data.measured_data, (uint16_t)sizeof(sensor_data.measured_data));
136-
if (status != AHT20_STATUS_OK) {
137-
print_error(&huart2, status);
138-
aht20_soft_reset(&hi2c1);
139-
continue;
110+
if (BL_STATUS_OK != bl_process_sensor_data(&hi2c1, &huart2)) {
111+
return 2;
140112
}
141113

142-
aht20_calculate_measurments(sensor_data.measured_data, &sensor_data.humidity, &sensor_data.temperature_c, &sensor_data.temperature_f);
143-
transmit_data(sensor_data.humidity, sensor_data.temperature_c, sensor_data.temperature_f);
144-
114+
bl_uart_transmit_sensor_data(&huart2);
145115
HAL_Delay(1000);
146116
/* USER CODE END WHILE */
147117

Core/Src/utils.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <stdio.h>
2222
#include <string.h>
2323

24+
#ifdef DEBUGGING
2425
/*
2526
* prints error message via UART
2627
*/
@@ -39,3 +40,11 @@ void print_error(UART_HandleTypeDef *huart, aht20_status_t status) {
3940
}
4041
HAL_UART_Transmit(huart, (uint8_t*)debug_msg, strlen(debug_msg), HAL_MAX_DELAY);
4142
}
43+
#endif
44+
45+
/* transmits data to UART */
46+
void transmit_data(UART_HandleTypeDef *huart, float humidity, float temperature_c, float temperature_f) {
47+
char data_to_display[60] = "\0";
48+
sprintf(data_to_display, "Humidity: %.2f%%, Temperature: %.2fC, %.2fF\r\n", humidity, temperature_c, temperature_f);
49+
HAL_UART_Transmit(huart, (uint8_t *)data_to_display, strlen(data_to_display), HAL_MAX_DELAY);
50+
}

0 commit comments

Comments
 (0)