unleashed-firmware/lib/one_wire/ibutton/encoder/encoder_cyfral.c
SG bdba15b366
[FL-2393][FL-2381] iButton, OneWire: move to plain C ()
* 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

126 lines
3.3 KiB
C

#include "encoder_cyfral.h"
#include <furi_hal.h>
#define CYFRAL_DATA_SIZE sizeof(uint16_t)
#define CYFRAL_PERIOD (125 * instructions_per_us)
#define CYFRAL_0_LOW (CYFRAL_PERIOD * 0.66f)
#define CYFRAL_0_HI (CYFRAL_PERIOD * 0.33f)
#define CYFRAL_1_LOW (CYFRAL_PERIOD * 0.33f)
#define CYFRAL_1_HI (CYFRAL_PERIOD * 0.66f)
#define CYFRAL_SET_DATA(level, len) \
*polarity = level; \
*length = len;
struct EncoderCyfral {
uint32_t data;
uint32_t index;
};
EncoderCyfral* encoder_cyfral_alloc() {
EncoderCyfral* cyfral = malloc(sizeof(EncoderCyfral));
encoder_cyfral_reset(cyfral);
return cyfral;
}
void encoder_cyfral_free(EncoderCyfral* cyfral) {
free(cyfral);
}
void encoder_cyfral_reset(EncoderCyfral* cyfral) {
cyfral->data = 0;
cyfral->index = 0;
}
uint32_t cyfral_encoder_encode(const uint16_t data) {
uint32_t value = 0;
for(int8_t i = 0; i <= 7; i++) {
switch((data >> (i * 2)) & 0b00000011) {
case 0b11:
value = value << 4;
value += 0b00000111;
break;
case 0b10:
value = value << 4;
value += 0b00001011;
break;
case 0b01:
value = value << 4;
value += 0b00001101;
break;
case 0b00:
value = value << 4;
value += 0b00001110;
break;
default:
break;
}
}
return value;
}
void encoder_cyfral_set_data(EncoderCyfral* cyfral, const uint8_t* data, size_t data_size) {
furi_assert(cyfral);
furi_check(data_size >= CYFRAL_DATA_SIZE);
uint16_t intermediate;
memcpy(&intermediate, data, CYFRAL_DATA_SIZE);
cyfral->data = cyfral_encoder_encode(intermediate);
}
void encoder_cyfral_get_pulse(EncoderCyfral* cyfral, bool* polarity, uint32_t* length) {
if(cyfral->index < 8) {
// start word (0b0001)
switch(cyfral->index) {
case 0:
CYFRAL_SET_DATA(false, CYFRAL_0_LOW);
break;
case 1:
CYFRAL_SET_DATA(true, CYFRAL_0_HI);
break;
case 2:
CYFRAL_SET_DATA(false, CYFRAL_0_LOW);
break;
case 3:
CYFRAL_SET_DATA(true, CYFRAL_0_HI);
break;
case 4:
CYFRAL_SET_DATA(false, CYFRAL_0_LOW);
break;
case 5:
CYFRAL_SET_DATA(true, CYFRAL_0_HI);
break;
case 6:
CYFRAL_SET_DATA(false, CYFRAL_1_LOW);
break;
case 7:
CYFRAL_SET_DATA(true, CYFRAL_1_HI);
break;
}
} else {
// data
uint8_t data_start_index = cyfral->index - 8;
bool clock_polarity = (data_start_index) % 2;
uint8_t bit_index = (data_start_index) / 2;
bool bit_value = ((cyfral->data >> bit_index) & 1);
if(!clock_polarity) {
if(bit_value) {
CYFRAL_SET_DATA(false, CYFRAL_1_LOW);
} else {
CYFRAL_SET_DATA(false, CYFRAL_0_LOW);
}
} else {
if(bit_value) {
CYFRAL_SET_DATA(true, CYFRAL_1_HI);
} else {
CYFRAL_SET_DATA(true, CYFRAL_0_HI);
}
}
}
cyfral->index++;
if(cyfral->index >= (9 * 4 * 2)) {
cyfral->index = 0;
}
}