mirror of
https://github.com/Next-Flip/Momentum-Firmware.git
synced 2026-07-31 02:23:04 -07:00
Merge branch 'UNLEASHED' into 420
This commit is contained in:
@@ -17,6 +17,7 @@ typedef struct {
|
||||
bool down_pressed;
|
||||
bool ok_pressed;
|
||||
bool connected;
|
||||
bool is_cursor_set;
|
||||
} BtHidTikTokModel;
|
||||
|
||||
static void bt_hid_tiktok_draw_callback(Canvas* canvas, void* context) {
|
||||
@@ -88,48 +89,48 @@ static void bt_hid_tiktok_draw_callback(Canvas* canvas, void* context) {
|
||||
elements_multiline_text_aligned(canvas, 13, 62, AlignLeft, AlignBottom, "Hold to exit");
|
||||
}
|
||||
|
||||
static void bt_hid_tiktok_process_press(BtHidTikTok* bt_hid_tiktok, InputEvent* event) {
|
||||
with_view_model(
|
||||
bt_hid_tiktok->view,
|
||||
BtHidTikTokModel * model,
|
||||
{
|
||||
if(event->key == InputKeyUp) {
|
||||
model->up_pressed = true;
|
||||
} else if(event->key == InputKeyDown) {
|
||||
model->down_pressed = true;
|
||||
} else if(event->key == InputKeyLeft) {
|
||||
model->left_pressed = true;
|
||||
furi_hal_bt_hid_consumer_key_press(HID_CONSUMER_VOLUME_DECREMENT);
|
||||
} else if(event->key == InputKeyRight) {
|
||||
model->right_pressed = true;
|
||||
furi_hal_bt_hid_consumer_key_press(HID_CONSUMER_VOLUME_INCREMENT);
|
||||
} else if(event->key == InputKeyOk) {
|
||||
model->ok_pressed = true;
|
||||
}
|
||||
},
|
||||
true);
|
||||
static void bt_hid_tiktok_reset_cursor() {
|
||||
// Set cursor to the phone's left up corner
|
||||
// Delays to guarantee one packet per connection interval
|
||||
for(size_t i = 0; i < 8; i++) {
|
||||
furi_hal_bt_hid_mouse_move(-127, -127);
|
||||
furi_delay_ms(50);
|
||||
}
|
||||
// Move cursor from the corner
|
||||
furi_hal_bt_hid_mouse_move(20, 120);
|
||||
furi_delay_ms(50);
|
||||
}
|
||||
|
||||
static void bt_hid_tiktok_process_release(BtHidTikTok* bt_hid_tiktok, InputEvent* event) {
|
||||
with_view_model(
|
||||
bt_hid_tiktok->view,
|
||||
BtHidTikTokModel * model,
|
||||
{
|
||||
if(event->key == InputKeyUp) {
|
||||
model->up_pressed = false;
|
||||
} else if(event->key == InputKeyDown) {
|
||||
model->down_pressed = false;
|
||||
} else if(event->key == InputKeyLeft) {
|
||||
model->left_pressed = false;
|
||||
furi_hal_bt_hid_consumer_key_release(HID_CONSUMER_VOLUME_DECREMENT);
|
||||
} else if(event->key == InputKeyRight) {
|
||||
model->right_pressed = false;
|
||||
furi_hal_bt_hid_consumer_key_release(HID_CONSUMER_VOLUME_INCREMENT);
|
||||
} else if(event->key == InputKeyOk) {
|
||||
model->ok_pressed = false;
|
||||
}
|
||||
},
|
||||
true);
|
||||
static void bt_hid_tiktok_process_press(BtHidTikTokModel* model, InputEvent* event) {
|
||||
if(event->key == InputKeyUp) {
|
||||
model->up_pressed = true;
|
||||
} else if(event->key == InputKeyDown) {
|
||||
model->down_pressed = true;
|
||||
} else if(event->key == InputKeyLeft) {
|
||||
model->left_pressed = true;
|
||||
furi_hal_bt_hid_consumer_key_press(HID_CONSUMER_VOLUME_DECREMENT);
|
||||
} else if(event->key == InputKeyRight) {
|
||||
model->right_pressed = true;
|
||||
furi_hal_bt_hid_consumer_key_press(HID_CONSUMER_VOLUME_INCREMENT);
|
||||
} else if(event->key == InputKeyOk) {
|
||||
model->ok_pressed = true;
|
||||
}
|
||||
}
|
||||
|
||||
static void bt_hid_tiktok_process_release(BtHidTikTokModel* model, InputEvent* event) {
|
||||
if(event->key == InputKeyUp) {
|
||||
model->up_pressed = false;
|
||||
} else if(event->key == InputKeyDown) {
|
||||
model->down_pressed = false;
|
||||
} else if(event->key == InputKeyLeft) {
|
||||
model->left_pressed = false;
|
||||
furi_hal_bt_hid_consumer_key_release(HID_CONSUMER_VOLUME_DECREMENT);
|
||||
} else if(event->key == InputKeyRight) {
|
||||
model->right_pressed = false;
|
||||
furi_hal_bt_hid_consumer_key_release(HID_CONSUMER_VOLUME_INCREMENT);
|
||||
} else if(event->key == InputKeyOk) {
|
||||
model->ok_pressed = false;
|
||||
}
|
||||
}
|
||||
|
||||
static bool bt_hid_tiktok_input_callback(InputEvent* event, void* context) {
|
||||
@@ -137,43 +138,59 @@ static bool bt_hid_tiktok_input_callback(InputEvent* event, void* context) {
|
||||
BtHidTikTok* bt_hid_tiktok = context;
|
||||
bool consumed = false;
|
||||
|
||||
if(event->type == InputTypePress) {
|
||||
bt_hid_tiktok_process_press(bt_hid_tiktok, event);
|
||||
consumed = true;
|
||||
} else if(event->type == InputTypeRelease) {
|
||||
bt_hid_tiktok_process_release(bt_hid_tiktok, event);
|
||||
consumed = true;
|
||||
} else if(event->type == InputTypeShort) {
|
||||
if(event->key == InputKeyOk) {
|
||||
furi_hal_bt_hid_mouse_press(HID_MOUSE_BTN_LEFT);
|
||||
furi_delay_ms(50);
|
||||
furi_hal_bt_hid_mouse_release(HID_MOUSE_BTN_LEFT);
|
||||
furi_delay_ms(50);
|
||||
furi_hal_bt_hid_mouse_press(HID_MOUSE_BTN_LEFT);
|
||||
furi_delay_ms(50);
|
||||
furi_hal_bt_hid_mouse_release(HID_MOUSE_BTN_LEFT);
|
||||
consumed = true;
|
||||
} else if(event->key == InputKeyUp) {
|
||||
// Emulate up swipe
|
||||
furi_hal_bt_hid_mouse_scroll(-6);
|
||||
furi_hal_bt_hid_mouse_scroll(-12);
|
||||
furi_hal_bt_hid_mouse_scroll(-19);
|
||||
furi_hal_bt_hid_mouse_scroll(-12);
|
||||
furi_hal_bt_hid_mouse_scroll(-6);
|
||||
consumed = true;
|
||||
} else if(event->key == InputKeyDown) {
|
||||
// Emulate down swipe
|
||||
furi_hal_bt_hid_mouse_scroll(6);
|
||||
furi_hal_bt_hid_mouse_scroll(12);
|
||||
furi_hal_bt_hid_mouse_scroll(19);
|
||||
furi_hal_bt_hid_mouse_scroll(12);
|
||||
furi_hal_bt_hid_mouse_scroll(6);
|
||||
consumed = true;
|
||||
} else if(event->key == InputKeyBack) {
|
||||
furi_hal_bt_hid_consumer_key_release_all();
|
||||
consumed = true;
|
||||
}
|
||||
}
|
||||
with_view_model(
|
||||
bt_hid_tiktok->view,
|
||||
BtHidTikTokModel * model,
|
||||
{
|
||||
if(event->type == InputTypePress) {
|
||||
bt_hid_tiktok_process_press(model, event);
|
||||
if(model->connected && !model->is_cursor_set) {
|
||||
bt_hid_tiktok_reset_cursor();
|
||||
model->is_cursor_set = true;
|
||||
}
|
||||
consumed = true;
|
||||
} else if(event->type == InputTypeRelease) {
|
||||
bt_hid_tiktok_process_release(model, event);
|
||||
consumed = true;
|
||||
} else if(event->type == InputTypeShort) {
|
||||
if(event->key == InputKeyOk) {
|
||||
furi_hal_bt_hid_mouse_press(HID_MOUSE_BTN_LEFT);
|
||||
furi_delay_ms(50);
|
||||
furi_hal_bt_hid_mouse_release(HID_MOUSE_BTN_LEFT);
|
||||
furi_delay_ms(50);
|
||||
furi_hal_bt_hid_mouse_press(HID_MOUSE_BTN_LEFT);
|
||||
furi_delay_ms(50);
|
||||
furi_hal_bt_hid_mouse_release(HID_MOUSE_BTN_LEFT);
|
||||
consumed = true;
|
||||
} else if(event->key == InputKeyUp) {
|
||||
// Emulate up swipe
|
||||
furi_hal_bt_hid_mouse_scroll(-6);
|
||||
furi_hal_bt_hid_mouse_scroll(-12);
|
||||
furi_hal_bt_hid_mouse_scroll(-19);
|
||||
furi_hal_bt_hid_mouse_scroll(-12);
|
||||
furi_hal_bt_hid_mouse_scroll(-6);
|
||||
consumed = true;
|
||||
} else if(event->key == InputKeyDown) {
|
||||
// Emulate down swipe
|
||||
furi_hal_bt_hid_mouse_scroll(6);
|
||||
furi_hal_bt_hid_mouse_scroll(12);
|
||||
furi_hal_bt_hid_mouse_scroll(19);
|
||||
furi_hal_bt_hid_mouse_scroll(12);
|
||||
furi_hal_bt_hid_mouse_scroll(6);
|
||||
consumed = true;
|
||||
} else if(event->key == InputKeyBack) {
|
||||
furi_hal_bt_hid_consumer_key_release_all();
|
||||
consumed = true;
|
||||
}
|
||||
} else if(event->type == InputTypeLong) {
|
||||
if(event->key == InputKeyBack) {
|
||||
furi_hal_bt_hid_consumer_key_release_all();
|
||||
model->is_cursor_set = false;
|
||||
consumed = false;
|
||||
}
|
||||
}
|
||||
},
|
||||
true);
|
||||
|
||||
return consumed;
|
||||
}
|
||||
@@ -203,5 +220,11 @@ View* bt_hid_tiktok_get_view(BtHidTikTok* bt_hid_tiktok) {
|
||||
void bt_hid_tiktok_set_connected_status(BtHidTikTok* bt_hid_tiktok, bool connected) {
|
||||
furi_assert(bt_hid_tiktok);
|
||||
with_view_model(
|
||||
bt_hid_tiktok->view, BtHidTikTokModel * model, { model->connected = connected; }, true);
|
||||
bt_hid_tiktok->view,
|
||||
BtHidTikTokModel * model,
|
||||
{
|
||||
model->connected = connected;
|
||||
model->is_cursor_set = false;
|
||||
},
|
||||
true);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
App(
|
||||
appid="hc_sr04",
|
||||
name="HC-SR04 Dist. Sensor",
|
||||
apptype=FlipperAppType.EXTERNAL,
|
||||
entry_point="hc_sr04_app",
|
||||
cdefines=["APP_HC_SR04"],
|
||||
requires=[
|
||||
"gui",
|
||||
],
|
||||
stack_size=2 * 1024,
|
||||
order=20,
|
||||
fap_icon="dist_sensor10px.png",
|
||||
fap_category="GPIO",
|
||||
)
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 141 B |
@@ -0,0 +1,251 @@
|
||||
// insired by
|
||||
// https://github.com/esphome/esphome/blob/ac0d921413c3884752193fe568fa82853f0f99e9/esphome/components/ultrasonic/ultrasonic_sensor.cpp
|
||||
// Ported and modified by @xMasterX
|
||||
|
||||
#include <furi.h>
|
||||
#include <furi_hal_power.h>
|
||||
#include <gui/gui.h>
|
||||
#include <input/input.h>
|
||||
#include <stdlib.h>
|
||||
#include <gui/elements.h>
|
||||
#include <notification/notification.h>
|
||||
#include <notification/notification_messages.h>
|
||||
|
||||
typedef enum {
|
||||
EventTypeTick,
|
||||
EventTypeKey,
|
||||
} EventType;
|
||||
|
||||
typedef struct {
|
||||
EventType type;
|
||||
InputEvent input;
|
||||
} PluginEvent;
|
||||
|
||||
typedef struct {
|
||||
NotificationApp* notification;
|
||||
bool have_5v;
|
||||
bool measurement_made;
|
||||
uint32_t echo; // ms
|
||||
float distance; // meters
|
||||
} PluginState;
|
||||
|
||||
const NotificationSequence sequence_done = {
|
||||
&message_display_backlight_on,
|
||||
&message_green_255,
|
||||
&message_note_c5,
|
||||
&message_delay_50,
|
||||
&message_sound_off,
|
||||
NULL,
|
||||
};
|
||||
|
||||
static void render_callback(Canvas* const canvas, void* ctx) {
|
||||
const PluginState* plugin_state = acquire_mutex((ValueMutex*)ctx, 25);
|
||||
if(plugin_state == NULL) {
|
||||
return;
|
||||
}
|
||||
// border around the edge of the screen
|
||||
// canvas_draw_frame(canvas, 0, 0, 128, 64);
|
||||
|
||||
canvas_set_font(canvas, FontPrimary);
|
||||
elements_multiline_text_aligned(
|
||||
canvas, 64, 2, AlignCenter, AlignTop, "HC-SR04 Ultrasonic\nDistance Sensor");
|
||||
|
||||
canvas_set_font(canvas, FontSecondary);
|
||||
|
||||
if(!plugin_state->have_5v) {
|
||||
elements_multiline_text_aligned(
|
||||
canvas,
|
||||
4,
|
||||
28,
|
||||
AlignLeft,
|
||||
AlignTop,
|
||||
"5V on GPIO must be\nenabled, or USB must\nbe connected.");
|
||||
} else {
|
||||
if(!plugin_state->measurement_made) {
|
||||
elements_multiline_text_aligned(
|
||||
canvas, 64, 28, AlignCenter, AlignTop, "Press OK button to measure");
|
||||
elements_multiline_text_aligned(
|
||||
canvas, 64, 40, AlignCenter, AlignTop, "13/TX -> Trig\n14/RX -> Echo");
|
||||
} else {
|
||||
elements_multiline_text_aligned(canvas, 4, 28, AlignLeft, AlignTop, "Readout:");
|
||||
|
||||
FuriString* str_buf;
|
||||
str_buf = furi_string_alloc();
|
||||
furi_string_printf(str_buf, "Echo: %ld ms", plugin_state->echo);
|
||||
|
||||
canvas_draw_str_aligned(
|
||||
canvas, 8, 38, AlignLeft, AlignTop, furi_string_get_cstr(str_buf));
|
||||
furi_string_printf(str_buf, "Distance: %02f m", (double)plugin_state->distance);
|
||||
canvas_draw_str_aligned(
|
||||
canvas, 8, 48, AlignLeft, AlignTop, furi_string_get_cstr(str_buf));
|
||||
|
||||
furi_string_free(str_buf);
|
||||
}
|
||||
}
|
||||
|
||||
release_mutex((ValueMutex*)ctx, plugin_state);
|
||||
}
|
||||
|
||||
static void input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
|
||||
furi_assert(event_queue);
|
||||
|
||||
PluginEvent event = {.type = EventTypeKey, .input = *input_event};
|
||||
furi_message_queue_put(event_queue, &event, FuriWaitForever);
|
||||
}
|
||||
|
||||
static void hc_sr04_state_init(PluginState* const plugin_state) {
|
||||
plugin_state->echo = -1;
|
||||
plugin_state->distance = -1;
|
||||
plugin_state->measurement_made = false;
|
||||
|
||||
furi_hal_power_suppress_charge_enter();
|
||||
|
||||
plugin_state->have_5v = false;
|
||||
if(furi_hal_power_is_otg_enabled() || furi_hal_power_is_charging()) {
|
||||
plugin_state->have_5v = true;
|
||||
} else {
|
||||
furi_hal_power_enable_otg();
|
||||
plugin_state->have_5v = true;
|
||||
}
|
||||
}
|
||||
|
||||
float hc_sr04_ms_to_m(uint32_t ms) {
|
||||
const float speed_sound_m_per_s = 343.0f;
|
||||
const float time_s = ms / 1e3f;
|
||||
const float total_dist = time_s * speed_sound_m_per_s;
|
||||
return total_dist / 2.0f;
|
||||
}
|
||||
|
||||
static void hc_sr04_measure(PluginState* const plugin_state) {
|
||||
//plugin_state->echo = 1;
|
||||
//return;
|
||||
|
||||
if(!plugin_state->have_5v) {
|
||||
if(furi_hal_power_is_otg_enabled() || furi_hal_power_is_charging()) {
|
||||
plugin_state->have_5v = true;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
//furi_hal_light_set(LightRed, 0xFF);
|
||||
notification_message(plugin_state->notification, &sequence_blink_start_yellow);
|
||||
|
||||
const uint32_t timeout_ms = 2000;
|
||||
// Pin 13 / TX -> Trig
|
||||
furi_hal_gpio_write(&gpio_usart_tx, false);
|
||||
furi_hal_gpio_init(&gpio_usart_tx, GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh);
|
||||
|
||||
// Pin 14 / RX -> Echo
|
||||
furi_hal_gpio_write(&gpio_usart_rx, false);
|
||||
furi_hal_gpio_init(&gpio_usart_rx, GpioModeInput, GpioPullNo, GpioSpeedVeryHigh);
|
||||
|
||||
//FURI_CRITICAL_ENTER();
|
||||
// 10 ms pulse on TX
|
||||
furi_hal_gpio_write(&gpio_usart_tx, true);
|
||||
furi_delay_ms(10);
|
||||
furi_hal_gpio_write(&gpio_usart_tx, false);
|
||||
|
||||
// TODO change from furi_get_tick(), which returns ms,
|
||||
// to DWT->CYCCNT, which is a more precise counter with
|
||||
// us precision (see furi_hal_cortex_delay_us)
|
||||
|
||||
const uint32_t start = furi_get_tick();
|
||||
|
||||
while(furi_get_tick() - start < timeout_ms && furi_hal_gpio_read(&gpio_usart_rx))
|
||||
;
|
||||
while(furi_get_tick() - start < timeout_ms && !furi_hal_gpio_read(&gpio_usart_rx))
|
||||
;
|
||||
|
||||
const uint32_t pulse_start = furi_get_tick();
|
||||
|
||||
while(furi_get_tick() - start < timeout_ms && furi_hal_gpio_read(&gpio_usart_rx))
|
||||
;
|
||||
|
||||
const uint32_t pulse_end = furi_get_tick();
|
||||
//FURI_CRITICAL_EXIT();
|
||||
|
||||
plugin_state->echo = pulse_end - pulse_start;
|
||||
plugin_state->distance = hc_sr04_ms_to_m(pulse_end - pulse_start);
|
||||
plugin_state->measurement_made = true;
|
||||
|
||||
//furi_hal_light_set(LightRed, 0x00);
|
||||
notification_message(plugin_state->notification, &sequence_blink_stop);
|
||||
notification_message(plugin_state->notification, &sequence_done);
|
||||
}
|
||||
|
||||
int32_t hc_sr04_app() {
|
||||
FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
|
||||
|
||||
PluginState* plugin_state = malloc(sizeof(PluginState));
|
||||
|
||||
hc_sr04_state_init(plugin_state);
|
||||
|
||||
ValueMutex state_mutex;
|
||||
if(!init_mutex(&state_mutex, plugin_state, sizeof(PluginState))) {
|
||||
FURI_LOG_E("hc_sr04", "cannot create mutex\r\n");
|
||||
if(furi_hal_power_is_otg_enabled()) {
|
||||
furi_hal_power_disable_otg();
|
||||
}
|
||||
furi_hal_power_suppress_charge_exit();
|
||||
furi_message_queue_free(event_queue);
|
||||
free(plugin_state);
|
||||
return 255;
|
||||
}
|
||||
|
||||
plugin_state->notification = furi_record_open(RECORD_NOTIFICATION);
|
||||
|
||||
// Set system callbacks
|
||||
ViewPort* view_port = view_port_alloc();
|
||||
view_port_draw_callback_set(view_port, render_callback, &state_mutex);
|
||||
view_port_input_callback_set(view_port, input_callback, event_queue);
|
||||
|
||||
// Open GUI and register view_port
|
||||
Gui* gui = furi_record_open(RECORD_GUI);
|
||||
gui_add_view_port(gui, view_port, GuiLayerFullscreen);
|
||||
|
||||
PluginEvent event;
|
||||
for(bool processing = true; processing;) {
|
||||
FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
|
||||
|
||||
PluginState* plugin_state = (PluginState*)acquire_mutex_block(&state_mutex);
|
||||
|
||||
if(event_status == FuriStatusOk) {
|
||||
// press events
|
||||
if(event.type == EventTypeKey) {
|
||||
if(event.input.type == InputTypePress) {
|
||||
switch(event.input.key) {
|
||||
case InputKeyUp:
|
||||
case InputKeyDown:
|
||||
case InputKeyRight:
|
||||
case InputKeyLeft:
|
||||
break;
|
||||
case InputKeyOk:
|
||||
hc_sr04_measure(plugin_state);
|
||||
break;
|
||||
case InputKeyBack:
|
||||
processing = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
view_port_update(view_port);
|
||||
release_mutex(&state_mutex, plugin_state);
|
||||
}
|
||||
|
||||
if(furi_hal_power_is_otg_enabled()) {
|
||||
furi_hal_power_disable_otg();
|
||||
}
|
||||
furi_hal_power_suppress_charge_exit();
|
||||
view_port_enabled_set(view_port, false);
|
||||
gui_remove_view_port(gui, view_port);
|
||||
furi_record_close(RECORD_GUI);
|
||||
furi_record_close(RECORD_NOTIFICATION);
|
||||
view_port_free(view_port);
|
||||
furi_message_queue_free(event_queue);
|
||||
delete_mutex(&state_mutex);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -194,6 +194,7 @@ static void temperature_sensor_timer_callback(FuriMessageQueue* event_queue) {
|
||||
int32_t temperature_sensor_app(void* p) {
|
||||
UNUSED(p);
|
||||
|
||||
furi_hal_power_suppress_charge_enter();
|
||||
// Declare our variables
|
||||
TSEvent tsEvent;
|
||||
bool sensorFound = false;
|
||||
@@ -264,6 +265,7 @@ int32_t temperature_sensor_app(void* p) {
|
||||
furi_delay_tick(wait_ticks);
|
||||
}
|
||||
|
||||
furi_hal_power_suppress_charge_exit();
|
||||
// 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);
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
#include <furi.h>
|
||||
#include <furi_hal.h>
|
||||
|
||||
#define WS_VERSION_APP "0.2"
|
||||
#define WS_VERSION_APP "0.3"
|
||||
#define WS_DEVELOPED "SkorP"
|
||||
#define WS_GITHUB "https://github.com/flipperdevices/flipperzero-firmware"
|
||||
|
||||
|
||||
@@ -0,0 +1,307 @@
|
||||
#include "acurite_592txr.h"
|
||||
|
||||
#define TAG "WSProtocolAcurite_592TXR"
|
||||
|
||||
/*
|
||||
* Help
|
||||
* https://github.com/merbanan/rtl_433/blob/5bef4e43133ac4c0e2d18d36f87c52b4f9458453/src/devices/acurite.c
|
||||
*
|
||||
* Acurite 592TXR Temperature Humidity sensor decoder
|
||||
* Message Type 0x04, 7 bytes
|
||||
* | Byte 0 | Byte 1 | Byte 2 | Byte 3 | Byte 4 | Byte 5 | Byte 6 |
|
||||
* | --------- | --------- | --------- | --------- | --------- | --------- | --------- |
|
||||
* | CCII IIII | IIII IIII | pB00 0100 | pHHH HHHH | p??T TTTT | pTTT TTTT | KKKK KKKK |
|
||||
* - C: Channel 00: C, 10: B, 11: A, (01 is invalid)
|
||||
* - I: Device ID (14 bits)
|
||||
* - B: Battery, 1 is battery OK, 0 is battery low
|
||||
* - M: Message type (6 bits), 0x04
|
||||
* - T: Temperature Celsius (11 - 14 bits?), + 1000 * 10
|
||||
* - H: Relative Humidity (%) (7 bits)
|
||||
* - K: Checksum (8 bits)
|
||||
* - p: Parity bit
|
||||
* Notes:
|
||||
* - Temperature
|
||||
* - Encoded as Celsius + 1000 * 10
|
||||
* - only 11 bits needed for specified range -40 C to 70 C (-40 F - 158 F)
|
||||
* - However 14 bits available for temperature, giving possible range of -100 C to 1538.4 C
|
||||
* - @todo - check if high 3 bits ever used for anything else
|
||||
*
|
||||
*/
|
||||
|
||||
static const SubGhzBlockConst ws_protocol_acurite_592txr_const = {
|
||||
.te_short = 200,
|
||||
.te_long = 400,
|
||||
.te_delta = 90,
|
||||
.min_count_bit_for_found = 56,
|
||||
};
|
||||
|
||||
struct WSProtocolDecoderAcurite_592TXR {
|
||||
SubGhzProtocolDecoderBase base;
|
||||
|
||||
SubGhzBlockDecoder decoder;
|
||||
WSBlockGeneric generic;
|
||||
|
||||
uint16_t header_count;
|
||||
};
|
||||
|
||||
struct WSProtocolEncoderAcurite_592TXR {
|
||||
SubGhzProtocolEncoderBase base;
|
||||
|
||||
SubGhzProtocolBlockEncoder encoder;
|
||||
WSBlockGeneric generic;
|
||||
};
|
||||
|
||||
typedef enum {
|
||||
Acurite_592TXRDecoderStepReset = 0,
|
||||
Acurite_592TXRDecoderStepCheckPreambule,
|
||||
Acurite_592TXRDecoderStepSaveDuration,
|
||||
Acurite_592TXRDecoderStepCheckDuration,
|
||||
} Acurite_592TXRDecoderStep;
|
||||
|
||||
const SubGhzProtocolDecoder ws_protocol_acurite_592txr_decoder = {
|
||||
.alloc = ws_protocol_decoder_acurite_592txr_alloc,
|
||||
.free = ws_protocol_decoder_acurite_592txr_free,
|
||||
|
||||
.feed = ws_protocol_decoder_acurite_592txr_feed,
|
||||
.reset = ws_protocol_decoder_acurite_592txr_reset,
|
||||
|
||||
.get_hash_data = ws_protocol_decoder_acurite_592txr_get_hash_data,
|
||||
.serialize = ws_protocol_decoder_acurite_592txr_serialize,
|
||||
.deserialize = ws_protocol_decoder_acurite_592txr_deserialize,
|
||||
.get_string = ws_protocol_decoder_acurite_592txr_get_string,
|
||||
};
|
||||
|
||||
const SubGhzProtocolEncoder ws_protocol_acurite_592txr_encoder = {
|
||||
.alloc = NULL,
|
||||
.free = NULL,
|
||||
|
||||
.deserialize = NULL,
|
||||
.stop = NULL,
|
||||
.yield = NULL,
|
||||
};
|
||||
|
||||
const SubGhzProtocol ws_protocol_acurite_592txr = {
|
||||
.name = WS_PROTOCOL_ACURITE_592TXR_NAME,
|
||||
.type = SubGhzProtocolWeatherStation,
|
||||
.flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_315 | SubGhzProtocolFlag_868 |
|
||||
SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable,
|
||||
|
||||
.decoder = &ws_protocol_acurite_592txr_decoder,
|
||||
.encoder = &ws_protocol_acurite_592txr_encoder,
|
||||
};
|
||||
|
||||
void* ws_protocol_decoder_acurite_592txr_alloc(SubGhzEnvironment* environment) {
|
||||
UNUSED(environment);
|
||||
WSProtocolDecoderAcurite_592TXR* instance = malloc(sizeof(WSProtocolDecoderAcurite_592TXR));
|
||||
instance->base.protocol = &ws_protocol_acurite_592txr;
|
||||
instance->generic.protocol_name = instance->base.protocol->name;
|
||||
return instance;
|
||||
}
|
||||
|
||||
void ws_protocol_decoder_acurite_592txr_free(void* context) {
|
||||
furi_assert(context);
|
||||
WSProtocolDecoderAcurite_592TXR* instance = context;
|
||||
free(instance);
|
||||
}
|
||||
|
||||
void ws_protocol_decoder_acurite_592txr_reset(void* context) {
|
||||
furi_assert(context);
|
||||
WSProtocolDecoderAcurite_592TXR* instance = context;
|
||||
instance->decoder.parser_step = Acurite_592TXRDecoderStepReset;
|
||||
}
|
||||
|
||||
static bool ws_protocol_acurite_592txr_check_crc(WSProtocolDecoderAcurite_592TXR* instance) {
|
||||
uint8_t msg[] = {
|
||||
instance->decoder.decode_data >> 48,
|
||||
instance->decoder.decode_data >> 40,
|
||||
instance->decoder.decode_data >> 32,
|
||||
instance->decoder.decode_data >> 24,
|
||||
instance->decoder.decode_data >> 16,
|
||||
instance->decoder.decode_data >> 8};
|
||||
|
||||
if((subghz_protocol_blocks_add_bytes(msg, 6) ==
|
||||
(uint8_t)(instance->decoder.decode_data & 0xFF)) &&
|
||||
(!subghz_protocol_blocks_parity_bytes(&msg[2], 4))) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Analysis of received data
|
||||
* @param instance Pointer to a WSBlockGeneric* instance
|
||||
*/
|
||||
static void ws_protocol_acurite_592txr_remote_controller(WSBlockGeneric* instance) {
|
||||
uint8_t channel[] = {3, 0, 2, 1};
|
||||
uint8_t channel_raw = ((instance->data >> 54) & 0x03);
|
||||
instance->channel = channel[channel_raw];
|
||||
instance->id = (instance->data >> 40) & 0x3FFF;
|
||||
instance->battery_low = !((instance->data >> 38) & 1);
|
||||
instance->humidity = (instance->data >> 24) & 0x7F;
|
||||
|
||||
uint16_t temp_raw = ((instance->data >> 9) & 0xF80) | ((instance->data >> 8) & 0x7F);
|
||||
instance->temp = ((float)(temp_raw)-1000) / 10.0f;
|
||||
|
||||
instance->btn = WS_NO_BTN;
|
||||
}
|
||||
|
||||
void ws_protocol_decoder_acurite_592txr_feed(void* context, bool level, uint32_t duration) {
|
||||
furi_assert(context);
|
||||
WSProtocolDecoderAcurite_592TXR* instance = context;
|
||||
|
||||
switch(instance->decoder.parser_step) {
|
||||
case Acurite_592TXRDecoderStepReset:
|
||||
if((level) && (DURATION_DIFF(duration, ws_protocol_acurite_592txr_const.te_short * 3) <
|
||||
ws_protocol_acurite_592txr_const.te_delta * 2)) {
|
||||
instance->decoder.parser_step = Acurite_592TXRDecoderStepCheckPreambule;
|
||||
instance->decoder.te_last = duration;
|
||||
instance->header_count = 0;
|
||||
}
|
||||
break;
|
||||
|
||||
case Acurite_592TXRDecoderStepCheckPreambule:
|
||||
if(level) {
|
||||
instance->decoder.te_last = duration;
|
||||
} else {
|
||||
if((DURATION_DIFF(
|
||||
instance->decoder.te_last, ws_protocol_acurite_592txr_const.te_short * 3) <
|
||||
ws_protocol_acurite_592txr_const.te_delta * 2) &&
|
||||
(DURATION_DIFF(duration, ws_protocol_acurite_592txr_const.te_short * 3) <
|
||||
ws_protocol_acurite_592txr_const.te_delta * 2)) {
|
||||
//Found preambule
|
||||
instance->header_count++;
|
||||
} else if((instance->header_count > 2) && (instance->header_count < 5)) {
|
||||
if((DURATION_DIFF(
|
||||
instance->decoder.te_last, ws_protocol_acurite_592txr_const.te_short) <
|
||||
ws_protocol_acurite_592txr_const.te_delta) &&
|
||||
(DURATION_DIFF(duration, ws_protocol_acurite_592txr_const.te_long) <
|
||||
ws_protocol_acurite_592txr_const.te_delta)) {
|
||||
instance->decoder.decode_data = 0;
|
||||
instance->decoder.decode_count_bit = 0;
|
||||
subghz_protocol_blocks_add_bit(&instance->decoder, 0);
|
||||
instance->decoder.parser_step = Acurite_592TXRDecoderStepSaveDuration;
|
||||
} else if(
|
||||
(DURATION_DIFF(
|
||||
instance->decoder.te_last, ws_protocol_acurite_592txr_const.te_long) <
|
||||
ws_protocol_acurite_592txr_const.te_delta) &&
|
||||
(DURATION_DIFF(duration, ws_protocol_acurite_592txr_const.te_short) <
|
||||
ws_protocol_acurite_592txr_const.te_delta)) {
|
||||
instance->decoder.decode_data = 0;
|
||||
instance->decoder.decode_count_bit = 0;
|
||||
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
|
||||
instance->decoder.parser_step = Acurite_592TXRDecoderStepSaveDuration;
|
||||
} else {
|
||||
instance->decoder.parser_step = Acurite_592TXRDecoderStepReset;
|
||||
}
|
||||
} else {
|
||||
instance->decoder.parser_step = Acurite_592TXRDecoderStepReset;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case Acurite_592TXRDecoderStepSaveDuration:
|
||||
if(level) {
|
||||
instance->decoder.te_last = duration;
|
||||
instance->decoder.parser_step = Acurite_592TXRDecoderStepCheckDuration;
|
||||
} else {
|
||||
instance->decoder.parser_step = Acurite_592TXRDecoderStepReset;
|
||||
}
|
||||
break;
|
||||
|
||||
case Acurite_592TXRDecoderStepCheckDuration:
|
||||
if(!level) {
|
||||
if(duration >= ((uint32_t)ws_protocol_acurite_592txr_const.te_short * 5)) {
|
||||
if((instance->decoder.decode_count_bit ==
|
||||
ws_protocol_acurite_592txr_const.min_count_bit_for_found) &&
|
||||
ws_protocol_acurite_592txr_check_crc(instance)) {
|
||||
instance->generic.data = instance->decoder.decode_data;
|
||||
instance->generic.data_count_bit = instance->decoder.decode_count_bit;
|
||||
ws_protocol_acurite_592txr_remote_controller(&instance->generic);
|
||||
if(instance->base.callback)
|
||||
instance->base.callback(&instance->base, instance->base.context);
|
||||
}
|
||||
instance->decoder.decode_data = 0;
|
||||
instance->decoder.decode_count_bit = 0;
|
||||
instance->decoder.parser_step = Acurite_592TXRDecoderStepReset;
|
||||
break;
|
||||
} else if(
|
||||
(DURATION_DIFF(
|
||||
instance->decoder.te_last, ws_protocol_acurite_592txr_const.te_short) <
|
||||
ws_protocol_acurite_592txr_const.te_delta) &&
|
||||
(DURATION_DIFF(duration, ws_protocol_acurite_592txr_const.te_long) <
|
||||
ws_protocol_acurite_592txr_const.te_delta)) {
|
||||
subghz_protocol_blocks_add_bit(&instance->decoder, 0);
|
||||
instance->decoder.parser_step = Acurite_592TXRDecoderStepSaveDuration;
|
||||
} else if(
|
||||
(DURATION_DIFF(
|
||||
instance->decoder.te_last, ws_protocol_acurite_592txr_const.te_long) <
|
||||
ws_protocol_acurite_592txr_const.te_delta) &&
|
||||
(DURATION_DIFF(duration, ws_protocol_acurite_592txr_const.te_short) <
|
||||
ws_protocol_acurite_592txr_const.te_delta)) {
|
||||
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
|
||||
instance->decoder.parser_step = Acurite_592TXRDecoderStepSaveDuration;
|
||||
} else {
|
||||
instance->decoder.parser_step = Acurite_592TXRDecoderStepReset;
|
||||
}
|
||||
} else {
|
||||
instance->decoder.parser_step = Acurite_592TXRDecoderStepReset;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t ws_protocol_decoder_acurite_592txr_get_hash_data(void* context) {
|
||||
furi_assert(context);
|
||||
WSProtocolDecoderAcurite_592TXR* instance = context;
|
||||
return subghz_protocol_blocks_get_hash_data(
|
||||
&instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
|
||||
}
|
||||
|
||||
bool ws_protocol_decoder_acurite_592txr_serialize(
|
||||
void* context,
|
||||
FlipperFormat* flipper_format,
|
||||
SubGhzRadioPreset* preset) {
|
||||
furi_assert(context);
|
||||
WSProtocolDecoderAcurite_592TXR* instance = context;
|
||||
return ws_block_generic_serialize(&instance->generic, flipper_format, preset);
|
||||
}
|
||||
|
||||
bool ws_protocol_decoder_acurite_592txr_deserialize(void* context, FlipperFormat* flipper_format) {
|
||||
furi_assert(context);
|
||||
WSProtocolDecoderAcurite_592TXR* instance = context;
|
||||
bool ret = false;
|
||||
do {
|
||||
if(!ws_block_generic_deserialize(&instance->generic, flipper_format)) {
|
||||
break;
|
||||
}
|
||||
if(instance->generic.data_count_bit !=
|
||||
ws_protocol_acurite_592txr_const.min_count_bit_for_found) {
|
||||
FURI_LOG_E(TAG, "Wrong number of bits in key");
|
||||
break;
|
||||
}
|
||||
ret = true;
|
||||
} while(false);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void ws_protocol_decoder_acurite_592txr_get_string(void* context, FuriString* output) {
|
||||
furi_assert(context);
|
||||
WSProtocolDecoderAcurite_592TXR* instance = context;
|
||||
furi_string_printf(
|
||||
output,
|
||||
"%s %dbit\r\n"
|
||||
"Key:0x%lX%08lX\r\n"
|
||||
"Sn:0x%lX Ch:%d Bat:%d\r\n"
|
||||
"Temp:%d.%d C Hum:%d%%",
|
||||
instance->generic.protocol_name,
|
||||
instance->generic.data_count_bit,
|
||||
(uint32_t)(instance->generic.data >> 32),
|
||||
(uint32_t)(instance->generic.data),
|
||||
instance->generic.id,
|
||||
instance->generic.channel,
|
||||
instance->generic.battery_low,
|
||||
(int16_t)instance->generic.temp,
|
||||
abs(((int16_t)(instance->generic.temp * 10) - (((int16_t)instance->generic.temp) * 10))),
|
||||
instance->generic.humidity);
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
#pragma once
|
||||
|
||||
#include <lib/subghz/protocols/base.h>
|
||||
|
||||
#include <lib/subghz/blocks/const.h>
|
||||
#include <lib/subghz/blocks/decoder.h>
|
||||
#include <lib/subghz/blocks/encoder.h>
|
||||
#include "ws_generic.h"
|
||||
#include <lib/subghz/blocks/math.h>
|
||||
|
||||
#define WS_PROTOCOL_ACURITE_592TXR_NAME "Acurite 592TXR"
|
||||
|
||||
typedef struct WSProtocolDecoderAcurite_592TXR WSProtocolDecoderAcurite_592TXR;
|
||||
typedef struct WSProtocolEncoderAcurite_592TXR WSProtocolEncoderAcurite_592TXR;
|
||||
|
||||
extern const SubGhzProtocolDecoder ws_protocol_acurite_592txr_decoder;
|
||||
extern const SubGhzProtocolEncoder ws_protocol_acurite_592txr_encoder;
|
||||
extern const SubGhzProtocol ws_protocol_acurite_592txr;
|
||||
|
||||
/**
|
||||
* Allocate WSProtocolDecoderAcurite_592TXR.
|
||||
* @param environment Pointer to a SubGhzEnvironment instance
|
||||
* @return WSProtocolDecoderAcurite_592TXR* pointer to a WSProtocolDecoderAcurite_592TXR instance
|
||||
*/
|
||||
void* ws_protocol_decoder_acurite_592txr_alloc(SubGhzEnvironment* environment);
|
||||
|
||||
/**
|
||||
* Free WSProtocolDecoderAcurite_592TXR.
|
||||
* @param context Pointer to a WSProtocolDecoderAcurite_592TXR instance
|
||||
*/
|
||||
void ws_protocol_decoder_acurite_592txr_free(void* context);
|
||||
|
||||
/**
|
||||
* Reset decoder WSProtocolDecoderAcurite_592TXR.
|
||||
* @param context Pointer to a WSProtocolDecoderAcurite_592TXR instance
|
||||
*/
|
||||
void ws_protocol_decoder_acurite_592txr_reset(void* context);
|
||||
|
||||
/**
|
||||
* Parse a raw sequence of levels and durations received from the air.
|
||||
* @param context Pointer to a WSProtocolDecoderAcurite_592TXR instance
|
||||
* @param level Signal level true-high false-low
|
||||
* @param duration Duration of this level in, us
|
||||
*/
|
||||
void ws_protocol_decoder_acurite_592txr_feed(void* context, bool level, uint32_t duration);
|
||||
|
||||
/**
|
||||
* Getting the hash sum of the last randomly received parcel.
|
||||
* @param context Pointer to a WSProtocolDecoderAcurite_592TXR instance
|
||||
* @return hash Hash sum
|
||||
*/
|
||||
uint8_t ws_protocol_decoder_acurite_592txr_get_hash_data(void* context);
|
||||
|
||||
/**
|
||||
* Serialize data WSProtocolDecoderAcurite_592TXR.
|
||||
* @param context Pointer to a WSProtocolDecoderAcurite_592TXR instance
|
||||
* @param flipper_format Pointer to a FlipperFormat instance
|
||||
* @param preset The modulation on which the signal was received, SubGhzRadioPreset
|
||||
* @return true On success
|
||||
*/
|
||||
bool ws_protocol_decoder_acurite_592txr_serialize(
|
||||
void* context,
|
||||
FlipperFormat* flipper_format,
|
||||
SubGhzRadioPreset* preset);
|
||||
|
||||
/**
|
||||
* Deserialize data WSProtocolDecoderAcurite_592TXR.
|
||||
* @param context Pointer to a WSProtocolDecoderAcurite_592TXR instance
|
||||
* @param flipper_format Pointer to a FlipperFormat instance
|
||||
* @return true On success
|
||||
*/
|
||||
bool ws_protocol_decoder_acurite_592txr_deserialize(void* context, FlipperFormat* flipper_format);
|
||||
|
||||
/**
|
||||
* Getting a textual representation of the received data.
|
||||
* @param context Pointer to a WSProtocolDecoderAcurite_592TXR instance
|
||||
* @param output Resulting text
|
||||
*/
|
||||
void ws_protocol_decoder_acurite_592txr_get_string(void* context, FuriString* output);
|
||||
@@ -8,6 +8,7 @@ const SubGhzProtocol* weather_station_protocol_registry_items[] = {
|
||||
&ws_protocol_acurite_606tx,
|
||||
&ws_protocol_lacrosse_tx141thbv2,
|
||||
&ws_protocol_oregon2,
|
||||
&ws_protocol_acurite_592txr,
|
||||
};
|
||||
|
||||
const SubGhzProtocolRegistry weather_station_protocol_registry = {
|
||||
|
||||
@@ -8,5 +8,6 @@
|
||||
#include "acurite_606tx.h"
|
||||
#include "lacrosse_tx141thbv2.h"
|
||||
#include "oregon2.h"
|
||||
#include "acurite_592txr.h"
|
||||
|
||||
extern const SubGhzProtocolRegistry weather_station_protocol_registry;
|
||||
|
||||
Reference in New Issue
Block a user