Welcome to mirror list, hosted at ThFree Co, Russian Federation.

subghz_read_raw.c « views « subghz « applications - github.com/ClusterM/flipperzero-firmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: de8f371b71f86e0cb0451ee14b5c58886c188d65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
#include "subghz_read_raw.h"
#include "../subghz_i.h"

#include <math.h>
#include <furi.h>
#include <furi_hal.h>
#include <input/input.h>
#include <gui/elements.h>

#include <assets_icons.h>
#define SUBGHZ_READ_RAW_RSSI_HISTORY_SIZE 100
#define TAG "SubGhzReadRAW"

struct SubGhzReadRAW {
    View* view;
    SubGhzReadRAWCallback callback;
    void* context;
};

typedef struct {
    string_t frequency_str;
    string_t preset_str;
    string_t sample_write;
    string_t file_name;
    uint8_t* rssi_history;
    bool rssi_history_end;
    uint8_t ind_write;
    uint8_t ind_sin;
    SubGhzReadRAWStatus status;
} SubGhzReadRAWModel;

void subghz_read_raw_set_callback(
    SubGhzReadRAW* subghz_read_raw,
    SubGhzReadRAWCallback callback,
    void* context) {
    furi_assert(subghz_read_raw);
    furi_assert(callback);
    subghz_read_raw->callback = callback;
    subghz_read_raw->context = context;
}

void subghz_read_raw_add_data_statusbar(
    SubGhzReadRAW* instance,
    const char* frequency_str,
    const char* preset_str) {
    furi_assert(instance);
    with_view_model(
        instance->view, (SubGhzReadRAWModel * model) {
            string_set_str(model->frequency_str, frequency_str);
            string_set_str(model->preset_str, preset_str);
            return true;
        });
}

void subghz_read_raw_add_data_rssi(SubGhzReadRAW* instance, float rssi) {
    furi_assert(instance);
    uint8_t u_rssi = 0;

    if(rssi < -90) {
        u_rssi = 0;
    } else {
        u_rssi = (uint8_t)((rssi + 90) / 2.7);
    }

    with_view_model(
        instance->view, (SubGhzReadRAWModel * model) {
            model->rssi_history[model->ind_write++] = u_rssi;
            if(model->ind_write > SUBGHZ_READ_RAW_RSSI_HISTORY_SIZE) {
                model->rssi_history_end = true;
                model->ind_write = 0;
            }
            return true;
        });
}

void subghz_read_raw_update_sample_write(SubGhzReadRAW* instance, size_t sample) {
    furi_assert(instance);

    with_view_model(
        instance->view, (SubGhzReadRAWModel * model) {
            string_printf(model->sample_write, "%d spl.", sample);
            return false;
        });
}

void subghz_read_raw_stop_send(SubGhzReadRAW* instance) {
    furi_assert(instance);

    with_view_model(
        instance->view, (SubGhzReadRAWModel * model) {
            switch(model->status) {
            case SubGhzReadRAWStatusTXRepeat:
            case SubGhzReadRAWStatusLoadKeyTXRepeat:
                instance->callback(SubGhzCustomEventViewReadRAWSendStart, instance->context);
                break;
            case SubGhzReadRAWStatusTX:
                model->status = SubGhzReadRAWStatusIDLE;
                break;
            case SubGhzReadRAWStatusLoadKeyTX:
                model->status = SubGhzReadRAWStatusLoadKeyIDLE;
                break;

            default:
                FURI_LOG_W(TAG, "unknown status");
                model->status = SubGhzReadRAWStatusIDLE;
                break;
            }
            return true;
        });
}

void subghz_read_raw_update_sin(SubGhzReadRAW* instance) {
    furi_assert(instance);
    with_view_model(
        instance->view, (SubGhzReadRAWModel * model) {
            if(model->ind_sin++ > 62) {
                model->ind_sin = 0;
            }
            return true;
        });
}

static int8_t subghz_read_raw_tab_sin(uint8_t x) {
    const uint8_t tab_sin[64] = {0,   3,   6,   9,   12,  16,  19,  22,  25,  28,  31,  34,  37,
                                 40,  43,  46,  49,  51,  54,  57,  60,  63,  65,  68,  71,  73,
                                 76,  78,  81,  83,  85,  88,  90,  92,  94,  96,  98,  100, 102,
                                 104, 106, 107, 109, 111, 112, 113, 115, 116, 117, 118, 120, 121,
                                 122, 122, 123, 124, 125, 125, 126, 126, 126, 127, 127, 127};

    int8_t r = tab_sin[((x & 0x40) ? -x - 1 : x) & 0x3f];
    if(x & 0x80) return -r;
    return r;
}

void subghz_read_raw_draw_sin(Canvas* canvas, SubGhzReadRAWModel* model) {
#define SUBGHZ_RAW_SIN_AMPLITUDE 11
    for(int i = 113; i > 0; i--) {
        canvas_draw_line(
            canvas,
            i,
            32 - subghz_read_raw_tab_sin(i + model->ind_sin * 16) / SUBGHZ_RAW_SIN_AMPLITUDE,
            i + 1,
            32 + subghz_read_raw_tab_sin((i + model->ind_sin * 16 + 1) * 2) /
                     SUBGHZ_RAW_SIN_AMPLITUDE);
        canvas_draw_line(
            canvas,
            i + 1,
            32 - subghz_read_raw_tab_sin((i + model->ind_sin * 16)) / SUBGHZ_RAW_SIN_AMPLITUDE,
            i + 2,
            32 + subghz_read_raw_tab_sin((i + model->ind_sin * 16 + 1) * 2) /
                     SUBGHZ_RAW_SIN_AMPLITUDE);
    }
}

void subghz_read_raw_draw_scale(Canvas* canvas, SubGhzReadRAWModel* model) {
#define SUBGHZ_RAW_TOP_SCALE 14
#define SUBGHZ_RAW_END_SCALE 115

    if(model->rssi_history_end == false) {
        for(int i = SUBGHZ_RAW_END_SCALE; i > 0; i -= 15) {
            canvas_draw_line(canvas, i, SUBGHZ_RAW_TOP_SCALE, i, SUBGHZ_RAW_TOP_SCALE + 4);
            canvas_draw_line(canvas, i - 5, SUBGHZ_RAW_TOP_SCALE, i - 5, SUBGHZ_RAW_TOP_SCALE + 2);
            canvas_draw_line(
                canvas, i - 10, SUBGHZ_RAW_TOP_SCALE, i - 10, SUBGHZ_RAW_TOP_SCALE + 2);
        }
    } else {
        for(int i = SUBGHZ_RAW_END_SCALE - model->ind_write % 15; i > -15; i -= 15) {
            canvas_draw_line(canvas, i, SUBGHZ_RAW_TOP_SCALE, i, SUBGHZ_RAW_TOP_SCALE + 4);
            if(SUBGHZ_RAW_END_SCALE > i + 5)
                canvas_draw_line(
                    canvas, i + 5, SUBGHZ_RAW_TOP_SCALE, i + 5, SUBGHZ_RAW_TOP_SCALE + 2);
            if(SUBGHZ_RAW_END_SCALE > i + 10)
                canvas_draw_line(
                    canvas, i + 10, SUBGHZ_RAW_TOP_SCALE, i + 10, SUBGHZ_RAW_TOP_SCALE + 2);
        }
    }
}

void subghz_read_raw_draw_rssi(Canvas* canvas, SubGhzReadRAWModel* model) {
    int ind = 0;
    int base = 0;
    if(model->rssi_history_end == false) {
        for(int i = model->ind_write; i >= 0; i--) {
            canvas_draw_line(canvas, i, 47, i, 47 - model->rssi_history[i]);
        }
        if(model->ind_write > 3) {
            canvas_draw_line(canvas, model->ind_write, 47, model->ind_write, 13);
            canvas_draw_line(canvas, model->ind_write - 2, 12, model->ind_write + 2, 12);
            canvas_draw_line(canvas, model->ind_write - 1, 13, model->ind_write + 1, 13);
        }
    } else {
        base = SUBGHZ_READ_RAW_RSSI_HISTORY_SIZE - model->ind_write;
        for(int i = SUBGHZ_READ_RAW_RSSI_HISTORY_SIZE; i >= 0; i--) {
            ind = i - base;
            if(ind < 0) ind += SUBGHZ_READ_RAW_RSSI_HISTORY_SIZE;
            canvas_draw_line(canvas, i, 47, i, 47 - model->rssi_history[ind]);
        }
        canvas_draw_line(
            canvas, SUBGHZ_READ_RAW_RSSI_HISTORY_SIZE, 47, SUBGHZ_READ_RAW_RSSI_HISTORY_SIZE, 13);
        canvas_draw_line(
            canvas,
            SUBGHZ_READ_RAW_RSSI_HISTORY_SIZE - 2,
            12,
            SUBGHZ_READ_RAW_RSSI_HISTORY_SIZE + 2,
            12);
        canvas_draw_line(
            canvas,
            SUBGHZ_READ_RAW_RSSI_HISTORY_SIZE - 1,
            13,
            SUBGHZ_READ_RAW_RSSI_HISTORY_SIZE + 1,
            13);
    }
}

void subghz_read_raw_draw(Canvas* canvas, SubGhzReadRAWModel* model) {
    uint8_t graphics_mode = 1;
    canvas_set_color(canvas, ColorBlack);
    canvas_set_font(canvas, FontSecondary);
    canvas_draw_str(canvas, 5, 8, string_get_cstr(model->frequency_str));
    canvas_draw_str(canvas, 40, 8, string_get_cstr(model->preset_str));
    canvas_draw_str_aligned(
        canvas, 126, 0, AlignRight, AlignTop, string_get_cstr(model->sample_write));

    canvas_draw_line(canvas, 0, 14, 115, 14);
    canvas_draw_line(canvas, 0, 48, 115, 48);
    canvas_draw_line(canvas, 115, 14, 115, 48);

    switch(model->status) {
    case SubGhzReadRAWStatusIDLE:
        elements_button_left(canvas, "Erase");
        elements_button_center(canvas, "Send");
        elements_button_right(canvas, "Save");
        break;
    case SubGhzReadRAWStatusLoadKeyIDLE:
        elements_button_left(canvas, "New");
        elements_button_center(canvas, "Send");
        elements_button_right(canvas, "More");
        elements_text_box(
            canvas,
            4,
            20,
            110,
            30,
            AlignCenter,
            AlignCenter,
            string_get_cstr(model->file_name),
            true);
        break;

    case SubGhzReadRAWStatusTX:
    case SubGhzReadRAWStatusTXRepeat:
    case SubGhzReadRAWStatusLoadKeyTX:
    case SubGhzReadRAWStatusLoadKeyTXRepeat:
        graphics_mode = 0;
        elements_button_center(canvas, "Send");
        break;

    case SubGhzReadRAWStatusStart:
        elements_button_left(canvas, "Config");
        elements_button_center(canvas, "REC");
        break;

    default:
        elements_button_center(canvas, "Stop");
        break;
    }

    if(graphics_mode == 0) {
        subghz_read_raw_draw_sin(canvas, model);
    } else {
        subghz_read_raw_draw_rssi(canvas, model);
        subghz_read_raw_draw_scale(canvas, model);
        canvas_set_font_direction(canvas, CanvasDirectionBottomToTop);
        canvas_draw_str(canvas, 126, 40, "RSSI");
        canvas_set_font_direction(canvas, CanvasDirectionLeftToRight);
    }
}

bool subghz_read_raw_input(InputEvent* event, void* context) {
    furi_assert(context);
    SubGhzReadRAW* instance = context;

    if((event->key == InputKeyOk) &&
       (event->type == InputTypeLong || event->type == InputTypeRepeat)) {
        //we check that if we hold the transfer button,
        //further check of events is not needed, we exit
        return false;
    } else if(event->key == InputKeyOk && event->type == InputTypePress) {
        with_view_model(
            instance->view, (SubGhzReadRAWModel * model) {
                uint8_t ret = false;
                switch(model->status) {
                case SubGhzReadRAWStatusIDLE:
                    // Start TX
                    instance->callback(SubGhzCustomEventViewReadRAWSendStart, instance->context);
                    model->status = SubGhzReadRAWStatusTXRepeat;
                    ret = true;
                    break;
                case SubGhzReadRAWStatusTX:
                    // Start TXRepeat
                    model->status = SubGhzReadRAWStatusTXRepeat;
                    break;
                case SubGhzReadRAWStatusLoadKeyIDLE:
                    // Start Load Key TX
                    instance->callback(SubGhzCustomEventViewReadRAWSendStart, instance->context);
                    model->status = SubGhzReadRAWStatusLoadKeyTXRepeat;
                    ret = true;
                    break;
                case SubGhzReadRAWStatusLoadKeyTX:
                    // Start Load Key TXRepeat
                    model->status = SubGhzReadRAWStatusLoadKeyTXRepeat;
                    break;

                default:
                    break;
                }
                return ret;
            });
    } else if(event->key == InputKeyOk && event->type == InputTypeRelease) {
        with_view_model(
            instance->view, (SubGhzReadRAWModel * model) {
                if(model->status == SubGhzReadRAWStatusTXRepeat) {
                    // Stop repeat TX
                    model->status = SubGhzReadRAWStatusTX;
                } else if(model->status == SubGhzReadRAWStatusLoadKeyTXRepeat) {
                    // Stop repeat TX
                    model->status = SubGhzReadRAWStatusLoadKeyTX;
                }
                return false;
            });
    } else if(event->key == InputKeyBack && event->type == InputTypeShort) {
        with_view_model(
            instance->view, (SubGhzReadRAWModel * model) {
                switch(model->status) {
                case SubGhzReadRAWStatusREC:
                    //Stop REC
                    instance->callback(SubGhzCustomEventViewReadRAWIDLE, instance->context);
                    model->status = SubGhzReadRAWStatusIDLE;
                    break;
                case SubGhzReadRAWStatusLoadKeyTX:
                    //Stop TxRx
                    instance->callback(SubGhzCustomEventViewReadRAWTXRXStop, instance->context);
                    model->status = SubGhzReadRAWStatusLoadKeyIDLE;
                    break;
                case SubGhzReadRAWStatusTX:
                    //Stop TxRx
                    instance->callback(SubGhzCustomEventViewReadRAWTXRXStop, instance->context);
                    model->status = SubGhzReadRAWStatusIDLE;
                    break;
                case SubGhzReadRAWStatusLoadKeyIDLE:
                    //Exit
                    instance->callback(SubGhzCustomEventViewReadRAWBack, instance->context);
                    break;

                default:
                    //Exit
                    instance->callback(SubGhzCustomEventViewReadRAWBack, instance->context);
                    break;
                }
                return true;
            });
    } else if(event->key == InputKeyLeft && event->type == InputTypeShort) {
        with_view_model(
            instance->view, (SubGhzReadRAWModel * model) {
                if(model->status == SubGhzReadRAWStatusStart) {
                    //Config
                    instance->callback(SubGhzCustomEventViewReadRAWConfig, instance->context);
                } else if(
                    (model->status == SubGhzReadRAWStatusIDLE) ||
                    (model->status == SubGhzReadRAWStatusLoadKeyIDLE)) {
                    //Erase
                    model->status = SubGhzReadRAWStatusStart;
                    model->rssi_history_end = false;
                    model->ind_write = 0;
                    string_set_str(model->sample_write, "0 spl.");
                    string_reset(model->file_name);
                    instance->callback(SubGhzCustomEventViewReadRAWErase, instance->context);
                }
                return true;
            });
    } else if(event->key == InputKeyRight && event->type == InputTypeShort) {
        with_view_model(
            instance->view, (SubGhzReadRAWModel * model) {
                if(model->status == SubGhzReadRAWStatusIDLE) {
                    //Save
                    instance->callback(SubGhzCustomEventViewReadRAWSave, instance->context);
                } else if(model->status == SubGhzReadRAWStatusLoadKeyIDLE) {
                    //More
                    instance->callback(SubGhzCustomEventViewReadRAWMore, instance->context);
                }
                return true;
            });
    } else if(event->key == InputKeyOk && event->type == InputTypeShort) {
        with_view_model(
            instance->view, (SubGhzReadRAWModel * model) {
                if(model->status == SubGhzReadRAWStatusStart) {
                    //Record
                    instance->callback(SubGhzCustomEventViewReadRAWREC, instance->context);
                    model->status = SubGhzReadRAWStatusREC;
                    model->ind_write = 0;
                    model->rssi_history_end = false;
                } else if(model->status == SubGhzReadRAWStatusREC) {
                    //Stop
                    instance->callback(SubGhzCustomEventViewReadRAWIDLE, instance->context);
                    model->status = SubGhzReadRAWStatusIDLE;
                }
                return true;
            });
    }
    return true;
}

void subghz_read_raw_set_status(
    SubGhzReadRAW* instance,
    SubGhzReadRAWStatus status,
    const char* file_name) {
    furi_assert(instance);

    switch(status) {
    case SubGhzReadRAWStatusStart:
        with_view_model(
            instance->view, (SubGhzReadRAWModel * model) {
                model->status = SubGhzReadRAWStatusStart;
                model->rssi_history_end = false;
                model->ind_write = 0;
                string_reset(model->file_name);
                string_set_str(model->sample_write, "0 spl.");
                return true;
            });
        break;
    case SubGhzReadRAWStatusIDLE:
        with_view_model(
            instance->view, (SubGhzReadRAWModel * model) {
                model->status = SubGhzReadRAWStatusIDLE;
                return true;
            });
        break;
    case SubGhzReadRAWStatusLoadKeyTX:
        with_view_model(
            instance->view, (SubGhzReadRAWModel * model) {
                model->status = SubGhzReadRAWStatusLoadKeyIDLE;
                model->rssi_history_end = false;
                model->ind_write = 0;
                string_set_str(model->file_name, file_name);
                string_set_str(model->sample_write, "RAW");
                return true;
            });
        break;
    case SubGhzReadRAWStatusSaveKey:
        with_view_model(
            instance->view, (SubGhzReadRAWModel * model) {
                model->status = SubGhzReadRAWStatusLoadKeyIDLE;
                if(!model->ind_write) {
                    string_set_str(model->file_name, file_name);
                    string_set_str(model->sample_write, "RAW");
                } else {
                    string_reset(model->file_name);
                }
                return true;
            });
        break;

    default:
        FURI_LOG_W(TAG, "unknown status");
        break;
    }
}

void subghz_read_raw_enter(void* context) {
    furi_assert(context);
    //SubGhzReadRAW* instance = context;
}

void subghz_read_raw_exit(void* context) {
    furi_assert(context);
    SubGhzReadRAW* instance = context;

    with_view_model(
        instance->view, (SubGhzReadRAWModel * model) {
            if(model->status != SubGhzReadRAWStatusIDLE &&
               model->status != SubGhzReadRAWStatusStart &&
               model->status != SubGhzReadRAWStatusLoadKeyIDLE) {
                instance->callback(SubGhzCustomEventViewReadRAWIDLE, instance->context);
                model->status = SubGhzReadRAWStatusStart;
            }
            return true;
        });
}

SubGhzReadRAW* subghz_read_raw_alloc() {
    SubGhzReadRAW* instance = malloc(sizeof(SubGhzReadRAW));

    // View allocation and configuration
    instance->view = view_alloc();
    view_allocate_model(instance->view, ViewModelTypeLocking, sizeof(SubGhzReadRAWModel));
    view_set_context(instance->view, instance);
    view_set_draw_callback(instance->view, (ViewDrawCallback)subghz_read_raw_draw);
    view_set_input_callback(instance->view, subghz_read_raw_input);
    view_set_enter_callback(instance->view, subghz_read_raw_enter);
    view_set_exit_callback(instance->view, subghz_read_raw_exit);

    with_view_model(
        instance->view, (SubGhzReadRAWModel * model) {
            string_init(model->frequency_str);
            string_init(model->preset_str);
            string_init(model->sample_write);
            string_init(model->file_name);
            model->rssi_history = malloc(SUBGHZ_READ_RAW_RSSI_HISTORY_SIZE * sizeof(uint8_t));
            return true;
        });

    return instance;
}

void subghz_read_raw_free(SubGhzReadRAW* instance) {
    furi_assert(instance);

    with_view_model(
        instance->view, (SubGhzReadRAWModel * model) {
            string_clear(model->frequency_str);
            string_clear(model->preset_str);
            string_clear(model->sample_write);
            string_clear(model->file_name);
            free(model->rssi_history);
            return true;
        });
    view_free(instance->view);
    free(instance);
}

View* subghz_read_raw_get_view(SubGhzReadRAW* instance) {
    furi_assert(instance);
    return instance->view;
}