mirror of
https://github.com/DarkFlippers/unleashed-firmware
synced 2024-11-23 13:03:13 +00:00
fbt format + fix bug
This commit is contained in:
parent
ca3ce2edce
commit
33e4d2a17f
1 changed files with 152 additions and 174 deletions
|
@ -11,12 +11,12 @@
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#define TS_DEFAULT_VALUE 0xFFFF
|
#define TS_DEFAULT_VALUE 0xFFFF
|
||||||
|
|
||||||
#define HTU21D_ADDRESS (0x40 << 1)
|
#define HTU21D_ADDRESS (0x40 << 1)
|
||||||
|
|
||||||
#define HTU21D_CMD_TEMPERATURE 0xE3
|
#define HTU21D_CMD_TEMPERATURE 0xE3
|
||||||
#define HTU21D_CMD_HUMIDITY 0xE5
|
#define HTU21D_CMD_HUMIDITY 0xE5
|
||||||
|
|
||||||
#define DATA_BUFFER_SIZE 8
|
#define DATA_BUFFER_SIZE 8
|
||||||
|
|
||||||
|
@ -24,19 +24,19 @@
|
||||||
#define I2C_BUS &furi_hal_i2c_handle_external
|
#define I2C_BUS &furi_hal_i2c_handle_external
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
TSSInitializing,
|
TSSInitializing,
|
||||||
TSSNoSensor,
|
TSSNoSensor,
|
||||||
TSSPendingUpdate,
|
TSSPendingUpdate,
|
||||||
} TSStatus;
|
} TSStatus;
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
TSEventTypeTick,
|
TSEventTypeTick,
|
||||||
TSEventTypeInput,
|
TSEventTypeInput,
|
||||||
} TSEventType;
|
} TSEventType;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
TSEventType type;
|
TSEventType type;
|
||||||
InputEvent input;
|
InputEvent input;
|
||||||
} TSEvent;
|
} TSEvent;
|
||||||
|
|
||||||
extern const NotificationSequence sequence_blink_red_100;
|
extern const NotificationSequence sequence_blink_red_100;
|
||||||
|
@ -56,36 +56,32 @@ 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);
|
||||||
|
bool ret = false;
|
||||||
|
|
||||||
uint32_t timeout = furi_ms_to_ticks(100);
|
// Aquire I2C and check if device is ready, then release
|
||||||
bool ret = false;
|
furi_hal_i2c_acquire(I2C_BUS);
|
||||||
|
if(furi_hal_i2c_is_device_ready(I2C_BUS, HTU21D_ADDRESS, timeout)) {
|
||||||
|
furi_hal_i2c_release(I2C_BUS);
|
||||||
|
|
||||||
// Aquire I2C and check if device is ready, then release
|
furi_hal_i2c_acquire(I2C_BUS);
|
||||||
furi_hal_i2c_acquire(I2C_BUS);
|
// Transmit given command
|
||||||
if (furi_hal_i2c_is_device_ready(I2C_BUS, HTU21D_ADDRESS, 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);
|
|
||||||
|
|
||||||
furi_hal_i2c_acquire(I2C_BUS);
|
if(ret) {
|
||||||
// Transmit given command
|
uint32_t wait_ticks = furi_ms_to_ticks(50);
|
||||||
ret = furi_hal_i2c_tx(I2C_BUS, HTU21D_ADDRESS, &cmd, 1, timeout);
|
furi_delay_tick(wait_ticks);
|
||||||
furi_hal_i2c_release(I2C_BUS);
|
|
||||||
|
|
||||||
if (ret)
|
furi_hal_i2c_acquire(I2C_BUS);
|
||||||
{
|
// Receive data
|
||||||
uint32_t wait_ticks = furi_ms_to_ticks(50);
|
ret = furi_hal_i2c_rx(I2C_BUS, HTU21D_ADDRESS, buffer, size, timeout);
|
||||||
furi_delay_tick(wait_ticks);
|
furi_hal_i2c_release(I2C_BUS);
|
||||||
|
}
|
||||||
|
} else
|
||||||
|
furi_hal_i2c_release(I2C_BUS);
|
||||||
|
|
||||||
furi_hal_i2c_acquire(I2C_BUS);
|
return ret;
|
||||||
// Receive data
|
|
||||||
ret = furi_hal_i2c_rx(I2C_BUS, HTU21D_ADDRESS, buffer, size, timeout);
|
|
||||||
furi_hal_i2c_release(I2C_BUS);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
furi_hal_i2c_release(I2C_BUS);
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// <sumary>
|
// <sumary>
|
||||||
|
@ -99,201 +95,183 @@ static bool temperature_sensor_cmd(uint8_t cmd, uint8_t* buffer, uint8_t size) {
|
||||||
// true if fetch was successful, false otherwise
|
// true if fetch was successful, false otherwise
|
||||||
// </returns>
|
// </returns>
|
||||||
static bool temperature_sensor_fetch_data(double* temperature, double* humidity) {
|
static bool temperature_sensor_fetch_data(double* temperature, double* humidity) {
|
||||||
bool ret = false;
|
bool ret = false;
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
// 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;
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
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_set_font(canvas, FontPrimary);
|
||||||
|
canvas_draw_str(canvas, 2, 10, "HTU21D Sensor");
|
||||||
|
|
||||||
canvas_clear(canvas);
|
canvas_set_font(canvas, FontSecondary);
|
||||||
canvas_set_font(canvas, FontPrimary);
|
canvas_draw_str(canvas, 2, 62, "Press back to exit.");
|
||||||
canvas_draw_str(canvas, 2, 10, "HTU21D Sensor");
|
|
||||||
|
|
||||||
canvas_set_font(canvas, FontSecondary);
|
switch(temperature_sensor_current_status) {
|
||||||
canvas_draw_str(canvas, 2, 62, "Press back to exit.");
|
case TSSInitializing:
|
||||||
|
canvas_draw_str(canvas, 2, 30, "Initializing..");
|
||||||
|
break;
|
||||||
|
case TSSNoSensor:
|
||||||
|
canvas_draw_str(canvas, 2, 30, "No sensor found!");
|
||||||
|
break;
|
||||||
|
case TSSPendingUpdate: {
|
||||||
|
canvas_draw_str(canvas, 6, 24, "Temperature");
|
||||||
|
canvas_draw_str(canvas, 80, 24, "Humidity");
|
||||||
|
|
||||||
switch (temperature_sensor_current_status) {
|
// Draw vertical lines
|
||||||
case TSSInitializing:
|
canvas_draw_line(canvas, 68, 16, 68, 50);
|
||||||
canvas_draw_str(canvas, 2, 30, "Initializing..");
|
canvas_draw_line(canvas, 69, 16, 69, 50);
|
||||||
break;
|
|
||||||
case TSSNoSensor:
|
|
||||||
canvas_draw_str(canvas, 2, 30, "No sensor found!");
|
|
||||||
break;
|
|
||||||
case TSSPendingUpdate: {
|
|
||||||
|
|
||||||
canvas_draw_str(canvas, 6, 24, "Temperature");
|
// Draw horizontal line
|
||||||
canvas_draw_str(canvas, 80, 24, "Humidity");
|
canvas_draw_line(canvas, 3, 27, 144, 27);
|
||||||
|
|
||||||
// Draw vertical lines
|
// Draw temperature and humidity values
|
||||||
canvas_draw_line(canvas, 68, 16, 68, 50);
|
canvas_draw_str(canvas, 14, 38, ts_data_buffer_temperature_c);
|
||||||
canvas_draw_line(canvas, 69, 16, 69, 50);
|
canvas_draw_str(canvas, 48, 38, "C");
|
||||||
|
canvas_draw_str(canvas, 14, 48, ts_data_buffer_temperature_f);
|
||||||
// Draw horizontal line
|
canvas_draw_str(canvas, 48, 48, "F");
|
||||||
canvas_draw_line(canvas, 3, 27, 144, 27);
|
canvas_draw_str(canvas, 78, 42, ts_data_buffer_humidity);
|
||||||
|
canvas_draw_str(canvas, 112, 42, "%");
|
||||||
// Draw temperature and humidity values
|
|
||||||
canvas_draw_str(canvas, 14, 38, ts_data_buffer_temperature_c);
|
|
||||||
canvas_draw_str(canvas, 48, 38, "C");
|
|
||||||
canvas_draw_str(canvas, 14, 48, ts_data_buffer_temperature_f);
|
|
||||||
canvas_draw_str(canvas, 48, 48, "F");
|
|
||||||
canvas_draw_str(canvas, 78, 42, ts_data_buffer_humidity);
|
|
||||||
canvas_draw_str(canvas, 112, 42, "%");
|
|
||||||
|
|
||||||
} break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
} break;
|
||||||
|
default:
|
||||||
|
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);
|
||||||
|
FuriMessageQueue* event_queue = ctx;
|
||||||
|
|
||||||
furi_assert(ctx);
|
TSEvent event = {.type = TSEventTypeInput, .input = *input_event};
|
||||||
FuriMessageQueue* event_queue = ctx;
|
furi_message_queue_put(event_queue, &event, FuriWaitForever);
|
||||||
|
|
||||||
TSEvent event = { .type = TSEventTypeInput, .input = *input_event };
|
|
||||||
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};
|
||||||
|
furi_message_queue_put(event_queue, &event, 0);
|
||||||
TSEvent event = { .type = TSEventTypeTick };
|
|
||||||
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
|
||||||
|
TSEvent tsEvent;
|
||||||
|
bool sensorFound = false;
|
||||||
|
double celsius, fahrenheit, humidity;
|
||||||
|
|
||||||
// Declare our variables
|
FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(TSEvent));
|
||||||
TSEvent tsEvent;
|
|
||||||
bool sensorFound = false;
|
|
||||||
double celsius, fahrenheit, humidity;
|
|
||||||
|
|
||||||
FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(TSEvent));
|
// Register callbacks
|
||||||
|
ViewPort* view_port = view_port_alloc();
|
||||||
|
view_port_draw_callback_set(view_port, temperature_sensor_draw_callback, NULL);
|
||||||
|
view_port_input_callback_set(view_port, temperature_sensor_input_callback, event_queue);
|
||||||
|
|
||||||
// Register callbacks
|
// Create timer and register its callback
|
||||||
ViewPort* view_port = view_port_alloc();
|
FuriTimer* timer =
|
||||||
view_port_draw_callback_set(view_port, temperature_sensor_draw_callback, NULL);
|
furi_timer_alloc(temperature_sensor_timer_callback, FuriTimerTypePeriodic, event_queue);
|
||||||
view_port_input_callback_set(view_port, temperature_sensor_input_callback, event_queue);
|
furi_timer_start(timer, furi_kernel_get_tick_frequency());
|
||||||
|
|
||||||
// Create timer and register its callback
|
// Register viewport
|
||||||
FuriTimer* timer = furi_timer_alloc(temperature_sensor_timer_callback, FuriTimerTypePeriodic, event_queue);
|
Gui* gui = furi_record_open(RECORD_GUI);
|
||||||
furi_timer_start(timer, furi_kernel_get_tick_frequency());
|
gui_add_view_port(gui, view_port, GuiLayerFullscreen);
|
||||||
|
|
||||||
// Register viewport
|
// Used to notify the user by blinking red (error) or blue (fetch successful)
|
||||||
Gui* gui = furi_record_open(RECORD_GUI);
|
NotificationApp* notifications = furi_record_open(RECORD_NOTIFICATION);
|
||||||
gui_add_view_port(gui, view_port, GuiLayerFullscreen);
|
|
||||||
|
|
||||||
// Used to notify the user by blinking red (error) or blue (fetch successful)
|
// Assign variables a default value
|
||||||
NotificationApp* notifications = furi_record_open(RECORD_NOTIFICATION);
|
celsius = fahrenheit = humidity = TS_DEFAULT_VALUE;
|
||||||
|
|
||||||
// Assign variables a default value
|
while(1) {
|
||||||
celsius = fahrenheit = humidity = TS_DEFAULT_VALUE;
|
furi_check(furi_message_queue_get(event_queue, &tsEvent, FuriWaitForever) == FuriStatusOk);
|
||||||
|
|
||||||
while (1) {
|
// Handle events
|
||||||
|
if(tsEvent.type == TSEventTypeInput) {
|
||||||
|
// 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)
|
||||||
|
break;
|
||||||
|
|
||||||
furi_check(furi_message_queue_get(event_queue, &tsEvent, FuriWaitForever) == FuriStatusOk);
|
} else if(tsEvent.type == TSEventTypeTick) {
|
||||||
|
// Update sensor data
|
||||||
|
// Fetch data and set the sensor current status accordingly
|
||||||
|
sensorFound = temperature_sensor_fetch_data(&celsius, &humidity);
|
||||||
|
temperature_sensor_current_status = (sensorFound ? TSSPendingUpdate : TSSNoSensor);
|
||||||
|
|
||||||
// Handle events
|
if(sensorFound) {
|
||||||
if (tsEvent.type == TSEventTypeInput) {
|
// Blink blue
|
||||||
|
notification_message(notifications, &sequence_blink_blue_100);
|
||||||
|
|
||||||
// Exit on back key
|
if(celsius != TS_DEFAULT_VALUE && humidity != TS_DEFAULT_VALUE) {
|
||||||
if (tsEvent.input.key == InputKeyBack) // We dont check for type here, we can check the type of keypress like: (event.input.type == InputTypeShort)
|
// Convert celsius to fahrenheit
|
||||||
break;
|
fahrenheit = (celsius * 9 / 5) + 32;
|
||||||
|
|
||||||
}
|
// Fill our buffers here, not on the canvas draw callback
|
||||||
else if (tsEvent.type == TSEventTypeTick) {
|
snprintf(ts_data_buffer_temperature_c, DATA_BUFFER_SIZE, "%.2f", celsius);
|
||||||
|
snprintf(ts_data_buffer_temperature_f, DATA_BUFFER_SIZE, "%.2f", fahrenheit);
|
||||||
|
snprintf(ts_data_buffer_humidity, DATA_BUFFER_SIZE, "%.2f", humidity);
|
||||||
|
}
|
||||||
|
|
||||||
// Update sensor data
|
} else {
|
||||||
// Fetch data and set the sensor current status accordingly
|
// Reset our variables to their default values
|
||||||
sensorFound = temperature_sensor_fetch_data(&celsius, &humidity);
|
celsius = fahrenheit = humidity = TS_DEFAULT_VALUE;
|
||||||
temperature_sensor_current_status = (sensorFound ? TSSPendingUpdate : TSSNoSensor);
|
|
||||||
|
|
||||||
if (sensorFound) {
|
// Blink red
|
||||||
|
notification_message(notifications, &sequence_blink_red_100);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Blink blue
|
uint32_t wait_ticks = furi_ms_to_ticks(!sensorFound ? 100 : 500);
|
||||||
notification_message(notifications, &sequence_blink_blue_100);
|
furi_delay_tick(wait_ticks);
|
||||||
|
}
|
||||||
|
|
||||||
if (celsius != TS_DEFAULT_VALUE && humidity != TS_DEFAULT_VALUE)) {
|
// Dobby is freee (free our variables, Flipper will crash if we don't do this!)
|
||||||
|
furi_timer_free(timer);
|
||||||
|
gui_remove_view_port(gui, view_port);
|
||||||
|
view_port_free(view_port);
|
||||||
|
furi_message_queue_free(event_queue);
|
||||||
|
|
||||||
// Convert celsius to fahrenheit
|
furi_record_close(RECORD_NOTIFICATION);
|
||||||
fahrenheit = (celsius * 9 / 5) + 32;
|
furi_record_close(RECORD_GUI);
|
||||||
|
|
||||||
// Fill our buffers here, not on the canvas draw callback
|
return 0;
|
||||||
snprintf(ts_data_buffer_temperature_c, DATA_BUFFER_SIZE, "%.2f", celsius);
|
|
||||||
snprintf(ts_data_buffer_temperature_f, DATA_BUFFER_SIZE, "%.2f", fahrenheit);
|
|
||||||
snprintf(ts_data_buffer_humidity, DATA_BUFFER_SIZE, "%.2f", humidity);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
|
|
||||||
// Reset our variables to their default values
|
|
||||||
celsius = fahrenheit = humidity = TS_DEFAULT_VALUE;
|
|
||||||
|
|
||||||
// Blink red
|
|
||||||
notification_message(notifications, &sequence_blink_red_100);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
uint32_t wait_ticks = furi_ms_to_ticks(!sensorFound ? 100 : 500);
|
|
||||||
furi_delay_tick(wait_ticks);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Dobby is freee (free our variables, Flipper will crash if we don't do this!)
|
|
||||||
furi_timer_free(timer);
|
|
||||||
gui_remove_view_port(gui, view_port);
|
|
||||||
view_port_free(view_port);
|
|
||||||
furi_message_queue_free(event_queue);
|
|
||||||
|
|
||||||
furi_record_close(RECORD_NOTIFICATION);
|
|
||||||
furi_record_close(RECORD_GUI);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue