99 lines
2.0 KiB
C
99 lines
2.0 KiB
C
#include "main.h"
|
|
#include "generic_macros.h"
|
|
#include "lcd.h"
|
|
#include "external_temp.h"
|
|
|
|
static uint32_t external_temp_read(void)
|
|
{
|
|
HAL_ADC_Start(&hadc1);
|
|
if (HAL_ADC_PollForConversion(&hadc1, 100) != HAL_OK)
|
|
PANIC(0x4000);
|
|
|
|
return HAL_ADC_GetValue(&hadc1);
|
|
}
|
|
|
|
static int external_temp_convert_to_celsius(uint32_t value)
|
|
{
|
|
return (2512 - value) << 2;
|
|
}
|
|
|
|
static int external_temp_convert_to_fahrenheit(uint32_t value)
|
|
{
|
|
return (2953 - value) * 50 / 7;
|
|
}
|
|
|
|
static void external_temp_print(int temperature)
|
|
{
|
|
int add_sign = temperature < 0;
|
|
if (add_sign)
|
|
temperature = ~(temperature - 1); // if value is not positive, the string conversion will break
|
|
|
|
int temp1 = temperature;
|
|
|
|
for (int i = 0; i < 2; i++) {
|
|
temperature /= 10;
|
|
display_write_data_byte('0' + (char) (temp1 - temperature * 10));
|
|
temp1 = temperature;
|
|
}
|
|
|
|
display_write_data_byte('.');
|
|
|
|
for (int i = 0; i < 3; i++) {
|
|
temperature /= 10;
|
|
display_write_data_byte('0' + (char) (temp1 - temperature * 10));
|
|
temp1 = temperature;
|
|
|
|
if (temp1 == 0) {
|
|
display_write_data_seq(" ");
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (add_sign) {
|
|
DISPLAY_SET_CURSOR(1, 0);
|
|
display_write_data_byte('-');
|
|
}
|
|
}
|
|
|
|
static void external_temp_print_celsius(int temperature)
|
|
{
|
|
DISPLAY_SET_CURSOR(1, 7);
|
|
DISPLAY_SET_DECREMENT;
|
|
display_write_data_seq("C ");
|
|
|
|
external_temp_print(temperature);
|
|
}
|
|
|
|
static void external_temp_print_fahrenheit(int temperature)
|
|
{
|
|
DISPLAY_SET_CURSOR(1, 7);
|
|
DISPLAY_SET_DECREMENT;
|
|
display_write_data_seq("F ");
|
|
|
|
external_temp_print(temperature);
|
|
}
|
|
|
|
void external_temp_show_celsius(void)
|
|
{
|
|
DISPLAY_CLEAR;
|
|
DISPLAY_SET_INCREMENT;
|
|
|
|
display_write_data_seq("Temperature");
|
|
|
|
uint32_t value = external_temp_read();
|
|
int temp = external_temp_convert_to_celsius(value);
|
|
external_temp_print_celsius(temp);
|
|
}
|
|
|
|
void external_temp_show_fahrenheit(void)
|
|
{
|
|
DISPLAY_CLEAR;
|
|
DISPLAY_SET_INCREMENT;
|
|
|
|
display_write_data_seq("Temperature");
|
|
|
|
uint32_t value = external_temp_read();
|
|
int temp = external_temp_convert_to_fahrenheit(value);
|
|
external_temp_print_fahrenheit(temp);
|
|
}
|