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

irda_decoder_rc5.c « rc5 « encoder_decoder « irda « lib - github.com/ClusterM/flipperzero-firmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a9c72e65f12b69eeadb84bb12800d768e80a5e38 (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
#include "irda.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <furi.h>
#include "../irda_i.h"
#include "../irda_protocol_defs_i.h"

typedef struct {
    IrdaCommonDecoder* common_decoder;
    bool toggle;
} IrdaRc5Decoder;

IrdaMessage* irda_decoder_rc5_check_ready(void* ctx) {
    IrdaRc5Decoder* decoder = ctx;
    return irda_common_decoder_check_ready(decoder->common_decoder);
}

bool irda_decoder_rc5_interpret(IrdaCommonDecoder* decoder) {
    furi_assert(decoder);

    bool result = false;
    uint32_t* data = (void*)&decoder->data[0];
    /* Manchester (inverse):
     *      0->1 : 1
     *      1->0 : 0
     */
    decoder->data[0] = ~decoder->data[0];
    decoder->data[1] = ~decoder->data[1];

    // MSB first
    uint8_t address = reverse((uint8_t)decoder->data[0]) & 0x1F;
    uint8_t command = (reverse((uint8_t)decoder->data[1]) >> 2) & 0x3F;
    bool start_bit1 = *data & 0x01;
    bool start_bit2 = *data & 0x02;
    bool toggle = !!(*data & 0x04);

    if(start_bit1 == 1) {
        IrdaProtocol protocol = start_bit2 ? IrdaProtocolRC5 : IrdaProtocolRC5X;
        IrdaMessage* message = &decoder->message;
        IrdaRc5Decoder* rc5_decoder = decoder->context;
        bool* prev_toggle = &rc5_decoder->toggle;
        if((message->address == address) && (message->command == command) &&
           (message->protocol == protocol)) {
            message->repeat = (toggle == *prev_toggle);
        } else {
            message->repeat = false;
        }
        *prev_toggle = toggle;
        message->command = command;
        message->address = address;
        message->protocol = protocol;

        result = true;
    }

    return result;
}

void* irda_decoder_rc5_alloc(void) {
    IrdaRc5Decoder* decoder = malloc(sizeof(IrdaRc5Decoder));
    decoder->toggle = false;
    decoder->common_decoder = irda_common_decoder_alloc(&protocol_rc5);
    decoder->common_decoder->context = decoder;
    return decoder;
}

IrdaMessage* irda_decoder_rc5_decode(void* decoder, bool level, uint32_t duration) {
    IrdaRc5Decoder* decoder_rc5 = decoder;
    return irda_common_decode(decoder_rc5->common_decoder, level, duration);
}

void irda_decoder_rc5_free(void* decoder) {
    IrdaRc5Decoder* decoder_rc5 = decoder;
    irda_common_decoder_free(decoder_rc5->common_decoder);
    free(decoder_rc5);
}

void irda_decoder_rc5_reset(void* decoder) {
    IrdaRc5Decoder* decoder_rc5 = decoder;
    irda_common_decoder_reset(decoder_rc5->common_decoder);
}