[DNI] refresh ancient code that could hang the system

- remove the usage of ancient PANIC macro
- remove the unused fahrenheit conversion code
- add proper detection and printouts of HAL errors
This commit is contained in:
ІО-23 Шмуляр Олег 2025-04-20 11:33:30 +03:00
parent d145aa3661
commit 4f8fe7ba39
1 changed files with 48 additions and 35 deletions

View File

@ -3,25 +3,23 @@
#include "lcd.h"
#include "DNI.h"
static uint32_t DNI_read(void)
static int DNI_read(void)
{
HAL_ADC_Start(&hadc1);
if (HAL_ADC_PollForConversion(&hadc1, 100) != HAL_OK)
PANIC(0x4000);
HAL_StatusTypeDef s = HAL_ADC_PollForConversion(&hadc1, 100);
if (HAL_OK != s)
return -s;
else
return HAL_ADC_GetValue(&hadc1);
}
static int DNI_convert_to_celsius(uint32_t value)
static int DNI_convert_to_celsius(int value)
{
return (2512 - value) << 2;
}
static int DNI_convert_to_fahrenheit(uint32_t value)
{
return (2953 - value) * 50 / 7;
}
static void DNI_print(int temperature)
{
int add_sign = temperature < 0;
@ -50,27 +48,48 @@ static void DNI_print(int temperature)
}
if (add_sign) {
DISPLAY_SET_CURSOR(1, 4);
DISPLAY_SET_CURSOR(1, 9);
display_write_data_byte('-');
}
}
static void DNI_print_celsius(int temperature)
{
DISPLAY_SET_CURSOR(1, 10);
DISPLAY_SET_CURSOR(1, 15);
DISPLAY_SET_DECREMENT;
display_write_data_seq("C ");
DNI_print(temperature);
}
static void DNI_print_fahrenheit(int temperature)
static void print_ok(void)
{
DISPLAY_SET_CURSOR(1, 10);
DISPLAY_SET_DECREMENT;
display_write_data_seq("F ");
DISPLAY_SET_CURSOR(1, 4);
DISPLAY_SET_INCREMENT;
display_write_data_seq("OK");
}
DNI_print(temperature);
static void print_error(int err_code)
{
DISPLAY_SET_CURSOR(1, 4);
DISPLAY_SET_INCREMENT;
switch (err_code) {
case HAL_OK:
break;
case HAL_ERROR:
display_write_data_seq("HAL_ERROR");
break;
case HAL_BUSY:
display_write_data_seq("HAL_BUSY");
break;
case HAL_TIMEOUT:
display_write_data_seq("HAL_TIMEOUT");
break;
default:
display_write_data_seq("WRONG ERROR");
break;
}
}
int DNI_show_celsius(void)
@ -80,23 +99,17 @@ int DNI_show_celsius(void)
display_write_data_seq("DNI Temperature");
uint32_t value = DNI_read();
int value = DNI_read();
if (value < 0) {
print_error(-value);
return 1;
} else {
int temp = DNI_convert_to_celsius(value);
DNI_print_celsius(temp);
return 0;
}
int DNI_show_fahrenheit(void)
{
DISPLAY_CLEAR;
DISPLAY_SET_INCREMENT;
display_write_data_seq("DNI Temperature");
uint32_t value = DNI_read();
int temp = DNI_convert_to_fahrenheit(value);
DNI_print_fahrenheit(temp);
print_ok();
return 0;
}
}