-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMorseCodeTranslator.c
106 lines (79 loc) · 2.52 KB
/
MorseCodeTranslator.c
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
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <pthread.h>
#include <math.h>
#include "./address_map_arm.h"
#include "./Hardware.h"
#include "./MorseCodeLookupTable.c"
#define DEBOUNCE_TIME 100
#define DOT_THRESHOLD 100
#define DASH_THRESHOLD 300
volatile int *KEY_ptr;
long getCurrTime()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
}
void delayMilliseconds(int milliseconds)
{
usleep(milliseconds*1000);
}
void concatStr(char* dest, const char* src, size_t destSize) {
if (strlen(dest) + strlen(src) < destSize) {
strcat(dest, src);
}
}
int main(void){
int fd = -1;
init(fd);
KEY_ptr = readKey();
int prev_key_value = *KEY_ptr;
int buttonValue = *KEY_ptr;
int pressTime;
int releaseTime;
char *result;
char codeArray[10] = "";
char sentences[10000] = "";
while(1) {
buttonValue = *KEY_ptr;
// State Change
if(buttonValue!= prev_key_value) {
if(buttonValue == 4) {
printf("%s\n","Exiting Program...");
return 0;
}
//delayMilliseconds(DEBOUNCE_TIME);
if(buttonValue == 2) {
result = lookup(codeArray);
char resultStr[2] = {result, '\0'}; // Convert char to string
concatStr(sentences, resultStr, sizeof(sentences)); // Concatenate string
printf("%s\n", sentences);
memset(codeArray, '\0', sizeof(codeArray));
}
if(buttonValue == 1) {
// Get initial Time
pressTime = getCurrTime();
}else if(buttonValue == 0 && prev_key_value != 2) {
// Get release Time & Find its duration
releaseTime = getCurrTime();
int duration = releaseTime - pressTime;
// Check if its dot or dash
if(duration > DOT_THRESHOLD && duration < DASH_THRESHOLD) {
concatStr(codeArray, ".", sizeof(codeArray));
}else if(duration >= DASH_THRESHOLD) {
concatStr(codeArray, "-", sizeof(codeArray));
}
printf("%s\n", codeArray);
}
// UpdatepPrevious button value
prev_key_value = *KEY_ptr;
}
}
close_physical(fd);
return 0;
}