-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodule.c
More file actions
155 lines (127 loc) · 3.63 KB
/
module.c
File metadata and controls
155 lines (127 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <stdio.h>
#include <malloc.h>
#include <sys/time.h>
#include <string.h>
#include <gpiod.h>
void custom_delay(double milliseconds) {
struct timeval tval_before, tval_after, tval_result;
gettimeofday(&tval_before, NULL);
double time_elapsed = 0;
while (time_elapsed < milliseconds / 1000.0) {
gettimeofday(&tval_after, NULL);
timersub(&tval_after, &tval_before, &tval_result);
time_elapsed = (double)tval_result.tv_sec + ((double)tval_result.tv_usec / 1000000.0);
}
}
void division(int frame_copy[],int poly[],int n,int k,int p)
{
int i=0;
while ( i < k )
{
for( int j=0 ; j < p ; j++)
{
if( frame_copy[i+j] == poly[j] )
{
frame_copy[i+j]=0;
}
else
{
frame_copy[i+j]=1;
}
}
while( i< n && frame_copy[i] != 1)
i++;
}
}
void transmit_message(const char *msg) {
char result[3000] = {0};
int counter = 20;
int pos = 0;
struct timeval tval_before, tval_after, tval_result;
// GPIO Initialization
struct gpiod_chip *chip;
struct gpiod_line *line;
chip = gpiod_chip_open("/dev/gpiochip4");
if (!chip) {
perror("Open chip failed\n");
}
line = gpiod_chip_get_line(chip, 4);
if (!line) {
perror("Get line failed\n");
gpiod_chip_close(chip);
}
int ret = gpiod_line_request_output(line, "example", 0);
if (ret < 0) {
perror("Request line as output failed\n");
gpiod_chip_close(chip);
}
int len, length;
gpiod_line_set_value(line, 0);
// Append preamble
strcpy(result, "101010");
int poly[4]={1,0,1,1};
// lenght of string data
int k = strlen(msg);
// lenght of polynomail
int p = 4;
// the end frame must have an appended bit less than the polynomail
int n=k+p-1;
// creating a frame to send data
int frame[n];
// creating a copy of frame
// as it will get modified during calculation
int frame_copy[n];
// creating int array from message also appending data
for(int i=0;i<n;i++){
if(i<k){
frame_copy[i]=frame[i]=msg[i]-'0';
}
else{
frame_copy[i]=frame[i]=0;
}
}
int i,j;
//Division
division(frame_copy,poly,n,k,p);
for(int i=0;i<n;i++){
if(i<k){
frame_copy[i]=frame[i]=msg[i]-'0';
}
}
char frame_str[n + 1]; // +1 for null terminator
for (int i = 0; i < n; i++) {
frame_str[i] = frame_copy[i] + '0'; // Convert integer to character
}
frame_str[n] = '\0'; // Null terminate the string
// Now you can use frame_str with string functions
length = strlen(frame_str);
strncat(result, frame_str, length);
length = strlen(result);
gettimeofday(&tval_before, NULL);
while(pos != length) {
gettimeofday(&tval_after, NULL);
timersub(&tval_after, &tval_before, &tval_result);
double time_elapsed = (double)tval_result.tv_sec + ((double)tval_result.tv_usec / 1000000.0f);
while(time_elapsed < 0.001) {
gettimeofday(&tval_after, NULL);
timersub(&tval_after, &tval_before, &tval_result);
time_elapsed = (double)tval_result.tv_sec + ((double)tval_result.tv_usec / 1000000.0f);
}
gettimeofday(&tval_before, NULL);
if (result[pos] == '1') {
gpiod_line_set_value(line, 1);
pos++;
} else if(result[pos] == '0') {
gpiod_line_set_value(line, 0);
pos++;
}
}
// Cleanup
custom_delay(1);
gpiod_line_set_value(line, 0);
gpiod_line_release(line);
gpiod_chip_close(chip);
}
int main(){
return 0;
}