[PCF7931] getting things ready for PR

since this is somekind of work in progress, I'm still going for a PR.
This commit is reworking some comments and making the code stable
(at least as good es or better as before).

Also made als const as #define
This commit is contained in:
tinooo 2025-03-12 17:11:28 +01:00 committed by Tino
commit 0b2b238457

View file

@ -32,15 +32,28 @@
#define IIR_CONST1 0.1f #define IIR_CONST1 0.1f
#define IIR_CONST2 0.9f #define IIR_CONST2 0.9f
// used to decimate samples. this allows DoAcquisition to sample for a longer duration.
// Decimation of 4 makes sure that all blocks can be sampled at once!
#define DECIMATION 4
#define CLOCK (64/DECIMATION) // this actually is 64, but since samples are decimated by 2, CLOCK is also /2
#define TOLERANCE (CLOCK / 8)
#define _16T0 (CLOCK/4)
#define _32T0 (CLOCK/2)
#define _64T0 (CLOCK)
// calculating the two possible pmc lengths, based on the clock. -4 at the end is to make sure not to increment too far
#define PMC_16T0_LEN ((128 + 127 + 16 + 32 + 33 + 16) * CLOCK/64);
#define PMC_32T0_LEN ((128 + 127 + 16 + 32 + 33 ) * CLOCK/64);
// theshold for recognition of positive/negative slope // theshold for recognition of positive/negative slope
#define THRESHOLD 80 #define THRESHOLD 80
size_t DemodPCF7931(uint8_t **outBlocks, bool ledcontrol) { size_t DemodPCF7931(uint8_t **outBlocks, bool ledcontrol) {
const uint8_t DECIMATION = 4;
uint8_t bits[256] = {0x00}; uint8_t bits[256] = {0x00};
uint8_t blocks[8][16]; uint8_t blocks[8][16];
uint8_t *dest = BigBuf_get_addr(); uint8_t *dest = BigBuf_get_addr();
int g_GraphTraceLen = BigBuf_max_traceLen(); uint16_t g_GraphTraceLen = BigBuf_max_traceLen();
// limit g_GraphTraceLen to a little more than 2 data frames. // limit g_GraphTraceLen to a little more than 2 data frames.
// To make sure a complete dataframe is in the dataset. // To make sure a complete dataframe is in the dataset.
// 1 Frame is 16 Byte -> 128byte. at a T0 of 64 -> 8129 Samples per frame. // 1 Frame is 16 Byte -> 128byte. at a T0 of 64 -> 8129 Samples per frame.
@ -48,7 +61,7 @@ size_t DemodPCF7931(uint8_t **outBlocks, bool ledcontrol) {
// to make sure that one complete block is definitely being sampled, we need 2 times that // to make sure that one complete block is definitely being sampled, we need 2 times that
// which is ~17.xxx samples. round up. and clamp to this value. // which is ~17.xxx samples. round up. and clamp to this value.
// TODO: Doublecheck why this is being limited? // TODO: Doublecheck why this is being limited? - seems not to be needed.
// g_GraphTraceLen = (g_GraphTraceLen > 18000) ? 18000 : g_GraphTraceLen; // g_GraphTraceLen = (g_GraphTraceLen > 18000) ? 18000 : g_GraphTraceLen;
BigBuf_Clear_keep_EM(); BigBuf_Clear_keep_EM();
@ -66,15 +79,6 @@ size_t DemodPCF7931(uint8_t **outBlocks, bool ledcontrol) {
uint8_t beforeLastClockDuration; // store the clock duration of the cycle before the last Clock duration. Basically clockduration -2 uint8_t beforeLastClockDuration; // store the clock duration of the cycle before the last Clock duration. Basically clockduration -2
const uint8_t clock = 64/DECIMATION; // this actually is 64, but since samples are decimated by 2, clock is also /2
const uint8_t tolerance = clock / 8;
const uint8_t _16T0 = clock/4;
const uint8_t _32T0 = clock/2;
const uint8_t _64T0 = clock;
const uint16_t pmc16T0Len = (128 + 127 + 16 + 32 + 33 + 16) * clock/64; // calculating the two possible pmc lengths, based on the clock. -4 at the end is to make sure not to increment too far
const uint16_t pmc32T0Len = (128 + 127 + 16 + 32 + 33 ) * clock/64;
uint8_t block_done; uint8_t block_done;
size_t num_blocks = 0; size_t num_blocks = 0;
EdgeType expectedNextEdge = FALLING; // direction in which the next edge is expected should go. EdgeType expectedNextEdge = FALLING; // direction in which the next edge is expected should go.
@ -104,13 +108,13 @@ size_t DemodPCF7931(uint8_t **outBlocks, bool ledcontrol) {
// Switch depending on lastClockDuration length: // Switch depending on lastClockDuration length:
// 16T0 // 16T0
if (ABS(lastClockDuration - _16T0) < tolerance) { if (ABS(lastClockDuration - _16T0) < TOLERANCE) {
// if the clock before also was 16T0, it is a PMC! // if the clock before also was 16T0, it is a PMC!
if (ABS(beforeLastClockDuration - _16T0) < tolerance) { if (ABS(beforeLastClockDuration - _16T0) < TOLERANCE) {
// It's a PMC // It's a PMC
Dbprintf(_GREEN_("PMC 16T0 FOUND:") " bitPos: %d, sample: %d", bitPos, sample); Dbprintf(_GREEN_("PMC 16T0 FOUND:") " bitPos: %d, sample: %d", bitPos, sample);
sample += pmc16T0Len; // move to the sample after PMC sample += PMC_16T0_LEN; // move to the sample after PMC
expectedNextEdge = FALLING; expectedNextEdge = FALLING;
samplePosLastEdge = sample; samplePosLastEdge = sample;
@ -118,13 +122,13 @@ size_t DemodPCF7931(uint8_t **outBlocks, bool ledcontrol) {
} }
// 32TO // 32TO
} else if (ABS(lastClockDuration - _32T0) < tolerance) { } else if (ABS(lastClockDuration - _32T0) < TOLERANCE) {
// if the clock before also was 16T0, it is a PMC! // if the clock before also was 16T0, it is a PMC!
if (ABS(beforeLastClockDuration - _16T0) < tolerance) { if (ABS(beforeLastClockDuration - _16T0) < TOLERANCE) {
// It's a PMC ! // It's a PMC !
Dbprintf(_GREEN_("PMC 32T0 FOUND:") " bitPos: %d, sample: %d", bitPos, sample); Dbprintf(_GREEN_("PMC 32T0 FOUND:") " bitPos: %d, sample: %d", bitPos, sample);
sample += pmc32T0Len; // move to the sample after PMC sample += PMC_32T0_LEN; // move to the sample after PMC
expectedNextEdge = FALLING; expectedNextEdge = FALLING;
samplePosLastEdge = sample; samplePosLastEdge = sample;
@ -144,7 +148,7 @@ size_t DemodPCF7931(uint8_t **outBlocks, bool ledcontrol) {
half_switch++; half_switch++;
// 64T0 // 64T0
} else if (ABS(lastClockDuration - _64T0) < tolerance) { } else if (ABS(lastClockDuration - _64T0) < TOLERANCE) {
// this means, bit here is 1 // this means, bit here is 1
bits[bitPos] = 1; bits[bitPos] = 1;
bitPos++; bitPos++;
@ -248,10 +252,7 @@ bool IsBlock1PCF7931(const uint8_t *block) {
void ReadPCF7931(bool ledcontrol) { void ReadPCF7931(bool ledcontrol) {
Dbprintf("ReadPCF7931()==========");
uint8_t maxBlocks = 8; // readable blocks uint8_t maxBlocks = 8; // readable blocks
int found_blocks = 0; // successfully read blocks int found_blocks = 0; // successfully read blocks
// TODO: Why 17 byte len? 16 should be good. // TODO: Why 17 byte len? 16 should be good.
@ -275,7 +276,7 @@ void ReadPCF7931(bool ledcontrol) {
//j = 0; //j = 0;
do { do {
Dbprintf("ReadPCF7931() -- DO LOOP =========="); Dbprintf("ReadPCF7931() -- Reading Loop ==========");
i = 0; i = 0;
memset(tmp_blocks, 0, 4 * 16 * sizeof(uint8_t)); memset(tmp_blocks, 0, 4 * 16 * sizeof(uint8_t));
@ -299,6 +300,9 @@ void ReadPCF7931(bool ledcontrol) {
goto end; goto end;
} }
// This part was not working properly.
// So currently the blocks are not being sorted, but at least printed.
// // our logic breaks if we don't get at least two blocks // // our logic breaks if we don't get at least two blocks
// if (n < 2) { // if (n < 2) {
// // skip if all 0s block or no blocks // // skip if all 0s block or no blocks
@ -328,8 +332,9 @@ void ReadPCF7931(bool ledcontrol) {
// if (g_dbglevel >= DBG_EXTENDED) // if (g_dbglevel >= DBG_EXTENDED)
// Dbprintf("(dbg) got %d blocks (%d/%d found) (%d tries, %d errors)", n, found_blocks, (maxBlocks == 0 ? found_blocks : maxBlocks), tries, errors); // Dbprintf("(dbg) got %d blocks (%d/%d found) (%d tries, %d errors)", n, found_blocks, (maxBlocks == 0 ? found_blocks : maxBlocks), tries, errors);
// print blocks that have been found
for (i = 0; i < n; ++i) { for (i = 0; i < n; ++i) {
print_result("got consecutive blocks", tmp_blocks[i], 16); print_result("Block found: ", tmp_blocks[i], 16);
} }
// i = 0; // i = 0;
@ -396,7 +401,9 @@ void ReadPCF7931(bool ledcontrol) {
} }
} while (found_blocks < maxBlocks); } while (found_blocks < maxBlocks);
end: end:
/*
Dbprintf("-----------------------------------------"); Dbprintf("-----------------------------------------");
Dbprintf("Memory content:"); Dbprintf("Memory content:");
Dbprintf("-----------------------------------------"); Dbprintf("-----------------------------------------");
@ -417,6 +424,8 @@ end:
Dbprintf("-----------------------------------------"); Dbprintf("-----------------------------------------");
} }
*/
reply_mix(CMD_ACK, 0, 0, 0, 0, 0); reply_mix(CMD_ACK, 0, 0, 0, 0, 0);
} }