fbt format + fix bug

This commit is contained in:
MX 2022-10-20 23:53:08 +03:00
parent ca3ce2edce
commit 33e4d2a17f
No known key found for this signature in database
GPG key ID: 6C4C311DFD4B4AB5

View file

@ -56,14 +56,12 @@ char ts_data_buffer_humidity[DATA_BUFFER_SIZE];
// true if fetch was successful, false otherwise // true if fetch was successful, false otherwise
// </returns> // </returns>
static bool temperature_sensor_cmd(uint8_t cmd, uint8_t* buffer, uint8_t size) { static bool temperature_sensor_cmd(uint8_t cmd, uint8_t* buffer, uint8_t size) {
uint32_t timeout = furi_ms_to_ticks(100); uint32_t timeout = furi_ms_to_ticks(100);
bool ret = false; bool ret = false;
// Aquire I2C and check if device is ready, then release // Aquire I2C and check if device is ready, then release
furi_hal_i2c_acquire(I2C_BUS); furi_hal_i2c_acquire(I2C_BUS);
if (furi_hal_i2c_is_device_ready(I2C_BUS, HTU21D_ADDRESS, timeout)) if(furi_hal_i2c_is_device_ready(I2C_BUS, HTU21D_ADDRESS, timeout)) {
{
furi_hal_i2c_release(I2C_BUS); furi_hal_i2c_release(I2C_BUS);
furi_hal_i2c_acquire(I2C_BUS); furi_hal_i2c_acquire(I2C_BUS);
@ -71,8 +69,7 @@ static bool temperature_sensor_cmd(uint8_t cmd, uint8_t* buffer, uint8_t size) {
ret = furi_hal_i2c_tx(I2C_BUS, HTU21D_ADDRESS, &cmd, 1, timeout); ret = furi_hal_i2c_tx(I2C_BUS, HTU21D_ADDRESS, &cmd, 1, timeout);
furi_hal_i2c_release(I2C_BUS); furi_hal_i2c_release(I2C_BUS);
if (ret) if(ret) {
{
uint32_t wait_ticks = furi_ms_to_ticks(50); uint32_t wait_ticks = furi_ms_to_ticks(50);
furi_delay_tick(wait_ticks); furi_delay_tick(wait_ticks);
@ -81,8 +78,7 @@ static bool temperature_sensor_cmd(uint8_t cmd, uint8_t* buffer, uint8_t size) {
ret = furi_hal_i2c_rx(I2C_BUS, HTU21D_ADDRESS, buffer, size, timeout); ret = furi_hal_i2c_rx(I2C_BUS, HTU21D_ADDRESS, buffer, size, timeout);
furi_hal_i2c_release(I2C_BUS); furi_hal_i2c_release(I2C_BUS);
} }
} } else
else
furi_hal_i2c_release(I2C_BUS); furi_hal_i2c_release(I2C_BUS);
return ret; return ret;
@ -103,13 +99,12 @@ static bool temperature_sensor_fetch_data(double* temperature, double* humidity)
uint16_t adc_raw; uint16_t adc_raw;
uint8_t buffer[2] = { 0x00 }; uint8_t buffer[2] = {0x00};
// Fetch temperature // Fetch temperature
ret = temperature_sensor_cmd((uint8_t)HTU21D_CMD_TEMPERATURE, buffer, 2); ret = temperature_sensor_cmd((uint8_t)HTU21D_CMD_TEMPERATURE, buffer, 2);
if (ret) if(ret) {
{
// Calculate temperature // Calculate temperature
adc_raw = ((uint16_t)(buffer[0] << 8) | (buffer[1])); adc_raw = ((uint16_t)(buffer[0] << 8) | (buffer[1]));
*temperature = (float)(adc_raw * 175.72 / 65536.00) - 46.85; *temperature = (float)(adc_raw * 175.72 / 65536.00) - 46.85;
@ -117,8 +112,7 @@ static bool temperature_sensor_fetch_data(double* temperature, double* humidity)
// Fetch humidity // Fetch humidity
ret = temperature_sensor_cmd((uint8_t)HTU21D_CMD_TEMPERATURE, buffer, 2); ret = temperature_sensor_cmd((uint8_t)HTU21D_CMD_TEMPERATURE, buffer, 2);
if (ret) if(ret) {
{
// Calculate humidity // Calculate humidity
adc_raw = ((uint16_t)(buffer[0] << 8) | (buffer[1])); adc_raw = ((uint16_t)(buffer[0] << 8) | (buffer[1]));
*humidity = (float)(adc_raw * 125.0 / 65536.00) - 6.0; *humidity = (float)(adc_raw * 125.0 / 65536.00) - 6.0;
@ -128,12 +122,10 @@ static bool temperature_sensor_fetch_data(double* temperature, double* humidity)
return ret; return ret;
} }
// <sumary> // <sumary>
// Draw callback // Draw callback
// </sumary> // </sumary>
static void temperature_sensor_draw_callback(Canvas* canvas, void* ctx) { static void temperature_sensor_draw_callback(Canvas* canvas, void* ctx) {
UNUSED(ctx); UNUSED(ctx);
canvas_clear(canvas); canvas_clear(canvas);
@ -143,7 +135,7 @@ static void temperature_sensor_draw_callback(Canvas* canvas, void* ctx) {
canvas_set_font(canvas, FontSecondary); canvas_set_font(canvas, FontSecondary);
canvas_draw_str(canvas, 2, 62, "Press back to exit."); canvas_draw_str(canvas, 2, 62, "Press back to exit.");
switch (temperature_sensor_current_status) { switch(temperature_sensor_current_status) {
case TSSInitializing: case TSSInitializing:
canvas_draw_str(canvas, 2, 30, "Initializing.."); canvas_draw_str(canvas, 2, 30, "Initializing..");
break; break;
@ -151,7 +143,6 @@ static void temperature_sensor_draw_callback(Canvas* canvas, void* ctx) {
canvas_draw_str(canvas, 2, 30, "No sensor found!"); canvas_draw_str(canvas, 2, 30, "No sensor found!");
break; break;
case TSSPendingUpdate: { case TSSPendingUpdate: {
canvas_draw_str(canvas, 6, 24, "Temperature"); canvas_draw_str(canvas, 6, 24, "Temperature");
canvas_draw_str(canvas, 80, 24, "Humidity"); canvas_draw_str(canvas, 80, 24, "Humidity");
@ -174,39 +165,33 @@ static void temperature_sensor_draw_callback(Canvas* canvas, void* ctx) {
default: default:
break; break;
} }
} }
// <sumary> // <sumary>
// Input callback // Input callback
// </sumary> // </sumary>
static void temperature_sensor_input_callback(InputEvent* input_event, void* ctx) { static void temperature_sensor_input_callback(InputEvent* input_event, void* ctx) {
furi_assert(ctx); furi_assert(ctx);
FuriMessageQueue* event_queue = ctx; FuriMessageQueue* event_queue = ctx;
TSEvent event = { .type = TSEventTypeInput, .input = *input_event }; TSEvent event = {.type = TSEventTypeInput, .input = *input_event};
furi_message_queue_put(event_queue, &event, FuriWaitForever); furi_message_queue_put(event_queue, &event, FuriWaitForever);
} }
// <sumary> // <sumary>
// Timer callback // Timer callback
// </sumary> // </sumary>
static void temperature_sensor_timer_callback(FuriMessageQueue* event_queue) { static void temperature_sensor_timer_callback(FuriMessageQueue* event_queue) {
furi_assert(event_queue); furi_assert(event_queue);
TSEvent event = { .type = TSEventTypeTick }; TSEvent event = {.type = TSEventTypeTick};
furi_message_queue_put(event_queue, &event, 0); furi_message_queue_put(event_queue, &event, 0);
} }
// <sumary> // <sumary>
// App entry point // App entry point
// </sumary> // </sumary>
int32_t temperature_sensor_app(void* p) { int32_t temperature_sensor_app(void* p) {
UNUSED(p); UNUSED(p);
// Declare our variables // Declare our variables
@ -222,7 +207,8 @@ int32_t temperature_sensor_app(void* p) {
view_port_input_callback_set(view_port, temperature_sensor_input_callback, event_queue); view_port_input_callback_set(view_port, temperature_sensor_input_callback, event_queue);
// Create timer and register its callback // Create timer and register its callback
FuriTimer* timer = furi_timer_alloc(temperature_sensor_timer_callback, FuriTimerTypePeriodic, event_queue); FuriTimer* timer =
furi_timer_alloc(temperature_sensor_timer_callback, FuriTimerTypePeriodic, event_queue);
furi_timer_start(timer, furi_kernel_get_tick_frequency()); furi_timer_start(timer, furi_kernel_get_tick_frequency());
// Register viewport // Register viewport
@ -235,32 +221,27 @@ int32_t temperature_sensor_app(void* p) {
// Assign variables a default value // Assign variables a default value
celsius = fahrenheit = humidity = TS_DEFAULT_VALUE; celsius = fahrenheit = humidity = TS_DEFAULT_VALUE;
while (1) { while(1) {
furi_check(furi_message_queue_get(event_queue, &tsEvent, FuriWaitForever) == FuriStatusOk); furi_check(furi_message_queue_get(event_queue, &tsEvent, FuriWaitForever) == FuriStatusOk);
// Handle events // Handle events
if (tsEvent.type == TSEventTypeInput) { if(tsEvent.type == TSEventTypeInput) {
// Exit on back key // Exit on back key
if (tsEvent.input.key == InputKeyBack) // We dont check for type here, we can check the type of keypress like: (event.input.type == InputTypeShort) if(tsEvent.input.key ==
InputKeyBack) // We dont check for type here, we can check the type of keypress like: (event.input.type == InputTypeShort)
break; break;
} } else if(tsEvent.type == TSEventTypeTick) {
else if (tsEvent.type == TSEventTypeTick) {
// Update sensor data // Update sensor data
// Fetch data and set the sensor current status accordingly // Fetch data and set the sensor current status accordingly
sensorFound = temperature_sensor_fetch_data(&celsius, &humidity); sensorFound = temperature_sensor_fetch_data(&celsius, &humidity);
temperature_sensor_current_status = (sensorFound ? TSSPendingUpdate : TSSNoSensor); temperature_sensor_current_status = (sensorFound ? TSSPendingUpdate : TSSNoSensor);
if (sensorFound) { if(sensorFound) {
// Blink blue // Blink blue
notification_message(notifications, &sequence_blink_blue_100); notification_message(notifications, &sequence_blink_blue_100);
if (celsius != TS_DEFAULT_VALUE && humidity != TS_DEFAULT_VALUE)) { if(celsius != TS_DEFAULT_VALUE && humidity != TS_DEFAULT_VALUE) {
// Convert celsius to fahrenheit // Convert celsius to fahrenheit
fahrenheit = (celsius * 9 / 5) + 32; fahrenheit = (celsius * 9 / 5) + 32;
@ -270,15 +251,12 @@ int32_t temperature_sensor_app(void* p) {
snprintf(ts_data_buffer_humidity, DATA_BUFFER_SIZE, "%.2f", humidity); snprintf(ts_data_buffer_humidity, DATA_BUFFER_SIZE, "%.2f", humidity);
} }
} } else {
else {
// Reset our variables to their default values // Reset our variables to their default values
celsius = fahrenheit = humidity = TS_DEFAULT_VALUE; celsius = fahrenheit = humidity = TS_DEFAULT_VALUE;
// Blink red // Blink red
notification_message(notifications, &sequence_blink_red_100); notification_message(notifications, &sequence_blink_red_100);
} }
} }