flipperzero-firmware/lib/irda/encoder_decoder/rc5/irda_decoder_rc5.c

83 lines
2.4 KiB
C
Raw Normal View History

#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) {
[FL-2274] Inventing streams and moving FFF to them (#981) * Streams: string stream * String stream: updated insert/delete api * Streams: generic stream interface and string stream implementation * Streams: helpers for insert and delete_and_insert * FFF: now compatible with streams * MinUnit: introduced tests with arguments * FFF: stream access violation * Streams: copy data between streams * Streams: file stream * FFF: documentation * FFStream: documentation * FFF: alloc as file * MinUnit: support for nested tests * Streams: changed delete_and_insert, now it returns success flag. Added ability dump stream inner parameters and data to cout. * FFF: simplified file open function * Streams: unit tests * FFF: tests * Streams: declare cache_size constant as define, to allow variable modified arrays * FFF: lib moved to a separate folder * iButton: new FFF * RFID: new FFF * Animations: new FFF * IR: new FFF * NFC: new FFF * Flipper file format: delete lib * U2F: new FFF * Subghz: new FFF and streams * Streams: read line * Streams: split * FuriCore: implement memset with extra asserts * FuriCore: implement extra heap asserts without inventing memset * Scene manager: protected access to the scene id stack with a size check * NFC worker: dirty fix for issue where hal_nfc was busy on app start * Furi: update allocator to erase memory on allocation. Replace furi_alloc with malloc. * FuriCore: cleanup memmgr code. * Furi HAL: furi_hal_init is split into critical and non-critical parts. The critical part is currently clock and console. * Memmgr: added ability to track allocations and deallocations through console. * FFStream: some speedup * Streams, FF: minor fixes * Tests: restore * File stream: a slightly more thread-safe version of file_stream_delete_and_insert Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
2022-02-18 19:53:46 +00:00
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);
}