unleashed-firmware/lib/one_wire/ibutton/encoder/encoder_metakom.c
SG bdba15b366
[FL-2393][FL-2381] iButton, OneWire: move to plain C (#1068)
* iButton: getting started on the worker concept
* Hal delay: added global instructions_per_us variable
* iButton: one wire slave
* iButton: ibutton key setter
* iButton: one wire host, use ibutton_hal
* iButton\RFID: common pulse decoder concept
* iButton: cyfral decoder
* iButton: worker thread concept
* iButton: metakom decoder
* iButton: write key through worker
* iButton: worker mode holder
* iButton: worker improvements
* iButton: Cyfral encoder
* iButton: Metakom encoder
* lib: pulse protocol helpers
* iButton: Metakom decoder
* iButton: Cyfral decoder
* iButton worker: separate modes
* iButton: libs documentation
* HAL: iButton gpio modes
* iButton worker: rename modes file
* iButton worker, hal: move to LL
* iButton CLI: worker for reading and emulation commands
* iButton HAL: correct init and emulation sequence
* iButton cli: moved to plain C
* iButton: move to worker, small step to plain C
* Libs, one wire: move to plain C
* Libs: added forgotten files to compilation
* iButton writer: get rid of manual disable/enable irq
2022-03-29 16:01:56 +03:00

93 lines
2.6 KiB
C

#include "encoder_metakom.h"
#include <furi_hal.h>
#define METAKOM_DATA_SIZE sizeof(uint32_t)
#define METAKOM_PERIOD (125 * instructions_per_us)
#define METAKOM_0_LOW (METAKOM_PERIOD * 0.33f)
#define METAKOM_0_HI (METAKOM_PERIOD * 0.66f)
#define METAKOM_1_LOW (METAKOM_PERIOD * 0.66f)
#define METAKOM_1_HI (METAKOM_PERIOD * 0.33f)
#define METAKOM_SET_DATA(level, len) \
*polarity = !level; \
*length = len;
struct EncoderMetakom {
uint32_t data;
uint32_t index;
};
EncoderMetakom* encoder_metakom_alloc() {
EncoderMetakom* metakom = malloc(sizeof(EncoderMetakom));
encoder_metakom_reset(metakom);
return metakom;
}
void encoder_metakom_free(EncoderMetakom* metakom) {
free(metakom);
}
void encoder_metakom_reset(EncoderMetakom* metakom) {
metakom->data = 0;
metakom->index = 0;
}
void encoder_metakom_set_data(EncoderMetakom* metakom, const uint8_t* data, size_t data_size) {
furi_assert(metakom);
furi_check(data_size >= METAKOM_DATA_SIZE);
memcpy(&metakom->data, data, METAKOM_DATA_SIZE);
}
void encoder_metakom_get_pulse(EncoderMetakom* metakom, bool* polarity, uint32_t* length) {
if(metakom->index == 0) {
// sync bit
METAKOM_SET_DATA(true, METAKOM_PERIOD);
} else if(metakom->index >= 1 && metakom->index <= 6) {
// start word (0b010)
switch(metakom->index) {
case 1:
METAKOM_SET_DATA(false, METAKOM_0_LOW);
break;
case 2:
METAKOM_SET_DATA(true, METAKOM_0_HI);
break;
case 3:
METAKOM_SET_DATA(false, METAKOM_1_LOW);
break;
case 4:
METAKOM_SET_DATA(true, METAKOM_1_HI);
break;
case 5:
METAKOM_SET_DATA(false, METAKOM_0_LOW);
break;
case 6:
METAKOM_SET_DATA(true, METAKOM_0_HI);
break;
}
} else {
// data
uint8_t data_start_index = metakom->index - 7;
bool clock_polarity = (data_start_index) % 2;
uint8_t bit_index = (data_start_index) / 2;
bool bit_value = (metakom->data >> (32 - 1 - bit_index)) & 1;
if(!clock_polarity) {
if(bit_value) {
METAKOM_SET_DATA(false, METAKOM_1_LOW);
} else {
METAKOM_SET_DATA(false, METAKOM_0_LOW);
}
} else {
if(bit_value) {
METAKOM_SET_DATA(true, METAKOM_1_HI);
} else {
METAKOM_SET_DATA(true, METAKOM_0_HI);
}
}
}
metakom->index++;
if(metakom->index >= (1 + 3 * 2 + 32 * 2)) {
metakom->index = 0;
}
}