Skip to content

Commit

Permalink
Moved common functions
Browse files Browse the repository at this point in the history
  • Loading branch information
David Lee committed Jan 4, 2024
1 parent 50bf4fc commit 65b7247
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 102 deletions.
80 changes: 80 additions & 0 deletions helpers/meal_pager_calc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include "meal_pager_calc.h"

void customConcat(char* dest, const char* src) {
// Find the end of the destination string
while (*dest != '\0') {
dest++;
}

// Copy characters from src to dest
while (*src != '\0') {
*dest = *src;
dest++;
src++;
}

// Null-terminate the concatenated string
*dest = '\0';
}

char* encManchester(const char* bits, int mode) {
// Allocate memory for the result string
char* res = (char*)malloc((strlen(bits) * 2 + 1) * sizeof(char));

int index = 0;
for (int i = 0; bits[i] != '\0'; i++) {
char c = bits[i];
if (c == '0') {
if (mode) {
res[index++] = '1';
res[index++] = '0';
} else {
res[index++] = '0';
res[index++] = '1';
}
} else if (c == '1') {
if (mode) {
res[index++] = '0';
res[index++] = '1';
} else {
res[index++] = '1';
res[index++] = '0';
}
} else {
// Handle 'EE' case (error)
res[index++] = 'E';
res[index++] = 'E';
}
}

// Null-terminate the result string
res[index] = '\0';

return res;
}

void uint32ToBinaray(uint32_t number, char* str, int8_t length) {
int i = 0;
length--; // count length without 0
for (i = length; i >= 0; i--) {
// Bitwise AND extration of the i-th bit
int bit = (number >> i) & 1;
// convert the bit to a character of 1 or 0
str[length - i] = bit + '0';
}
// Terminate the string
str[length+1] = '\0';
}

void reverse(char* str) {
int length = strlen(str);
int start = 0;
int end = length - 1;
while (start < end) {
char temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
}
}
12 changes: 12 additions & 0 deletions helpers/meal_pager_calc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

#pragma once

#include "../meal_pager_i.h"

char* encManchester(const char* bits, int mode);

void uint32ToBinaray(uint32_t number, char* str, int8_t length);

void reverse(char* str);

void customConcat(char* dest, const char* src);
93 changes: 3 additions & 90 deletions helpers/retekess/meal_pager_retekess_t119.c
Original file line number Diff line number Diff line change
@@ -1,44 +1,22 @@

#include "meal_pager_retekess_t119.h"

void customConcat(char* dest, const char* src) {
//FURI_LOG_D(TAG, "adding %s to %s", src, dest);
// Find the end of the destination string
while (*dest != '\0') {
dest++;
}

// Copy characters from src to dest
while (*src != '\0') {
*dest = *src;
dest++;
src++;
}

// Null-terminate the concatenated string
*dest = '\0';
}

char* genRawData(int zero, int one, const char* bits) {
static char* genRawDataT119(int zero, int one, const char* bits) {
int bitsLen = strlen(bits);
int lineLen = 256; // Adjust the line length as needed
char* line = (char*)malloc(lineLen * sizeof(char));

//FURI_LOG_D(TAG, "bitLen = %u", bitsLen);
// Initialize the line with the first part
//snprintf(line, lineLen, "-6000");

//char* res = (char*)malloc((bitsLen * 4) * sizeof(char));
char* res = (char*)malloc(lineLen * sizeof(char));
res[0] = '\0'; // Null-terminate the result string

//customConcat(res, line);
customConcat(res, "-6000");

// Append bits and create the line
for (int i = 0; i < bitsLen; i++) {
char c = bits[i];
//char p = (i > 0) ? bits[i - 1] : '0';
int t = (c == '0') ? zero : one;

if (i % 2 == 0) {
Expand All @@ -48,82 +26,17 @@ char* genRawData(int zero, int one, const char* bits) {
}

// Concatenate the line to the result string
//strncat(res, line, bitsLen * 4);
customConcat(res, line);
}

// Append the closing part to the line
//strncat(line, " -6000\n", lineLen);
customConcat(res, " 200 -6000");
//FURI_LOG_D(TAG, "res is: %s", res);


free(line); // Free memory allocated for the line

return res;
}

char* encManchester(const char* bits, int mode) {
// Allocate memory for the result string
char* res = (char*)malloc((strlen(bits) * 2 + 1) * sizeof(char));

int index = 0;
for (int i = 0; bits[i] != '\0'; i++) {
char c = bits[i];
if (c == '0') {
if (mode) {
res[index++] = '1';
res[index++] = '0';
} else {
res[index++] = '0';
res[index++] = '1';
}
} else if (c == '1') {
if (mode) {
res[index++] = '0';
res[index++] = '1';
} else {
res[index++] = '1';
res[index++] = '0';
}
} else {
// Handle 'EE' case (error)
res[index++] = 'E';
res[index++] = 'E';
}
}

// Null-terminate the result string
res[index] = '\0';

return res;
}

void uint32ToBinaray(uint32_t number, char* str, int8_t length) {
int i = 0;
length--; // count length without 0
for (i = length; i >= 0; i--) {
// Bitwise AND extration of the i-th bit
int bit = (number >> i) & 1;
// convert the bit to a character of 1 or 0
str[length - i] = bit + '0';
}
// Terminate the string
str[length+1] = '\0';
}

void reverse(char* str) {
int length = strlen(str);
int start = 0;
int end = length - 1;
while (start < end) {
char temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
}
}

static void meal_pager_retekess_t119_generate_pager(void* context, char* stationId, uint32_t pager, FlipperFormat* ff) {
Meal_Pager* app = context;
char pagerId[11];
Expand Down Expand Up @@ -155,7 +68,7 @@ static void meal_pager_retekess_t119_generate_pager(void* context, char* station
//FURI_LOG_D(TAG, "Station & Pager & Action: %s", fullId);
char* manchester = encManchester(fullId, 0);
//FURI_LOG_D(TAG, "Manchester: %s", manchester);
char* rawSignal = genRawData(200, 600, manchester);
char* rawSignal = genRawDataT119(200, 600, manchester);
//FURI_LOG_D(TAG, "RAW_Data: %s", rawSignal);
flipper_format_write_string_cstr(ff, "RAW_Data", rawSignal);
free(manchester);
Expand Down
13 changes: 1 addition & 12 deletions helpers/retekess/meal_pager_retekess_t119.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,6 @@
#pragma once

#include "../../meal_pager_i.h"

char* encManchester(const char* bits, int mode);

void uint32ToBinaray(uint32_t number, char* str, int8_t length);

void reverse(char* str);

void customConcat(char* dest, const char* src);

//void meal_pager_retekess_t119_generate_pager(void* context, char* stationId, uint32_t pager);

//void meal_pager_retekess_t119_generate_station(void* context, uint32_t station);
#include "../meal_pager_calc.h"

bool meal_pager_retekess_t119_generate_all(void* context);

0 comments on commit 65b7247

Please sign in to comment.