unleashed-firmware/applications/ibutton/helpers/cyfral_decoder.h
あく 389ff92cc1
Naming and coding style convention, new linter tool. (#945)
* Makefile, Scripts: new linter
* About: remove ID from IC
* Firmware: remove double define for DIVC/DIVR
* Scripts: check folder names too. Docker: replace syntax check with make lint.
* Reformat Sources and Migrate to new file naming convention
* Docker: symlink clang-format-12 to clang-format
* Add coding style guide
2022-01-05 19:10:18 +03:00

55 lines
1.1 KiB
C++

#pragma once
#include <stdint.h>
#include <atomic>
class CyfralDecoder {
public:
bool read(uint8_t* data, uint8_t data_size);
void process_front(bool polarity, uint32_t time);
CyfralDecoder();
private:
enum class BitState : uint8_t {
WAIT_FRONT_HIGH,
WAIT_FRONT_LOW,
};
enum class State : uint8_t {
WAIT_START_NIBBLE,
READ_NIBBLE,
READ_STOP_NIBBLE,
};
State state;
BitState bit_state;
bool process_bit(bool polarity, uint32_t time, bool* readed, bool* readed_value);
void reset_state();
bool nibble_valid(uint8_t data);
// high + low period time
uint32_t period_time;
// ready flag, key is readed and valid
std::atomic<bool> ready;
// key data storage
uint16_t key_data;
// temporary nibble storage
uint8_t readed_nibble;
// data valid flag
// MUST be checked only in READ_STOP_NIBBLE state
bool data_valid;
// nibble index, we expect 8 nibbles
uint8_t index;
// bit index in nibble, 4 bit per nibble
uint8_t bit_index;
// max period, 230us x clock per us
uint32_t max_period;
};