-
Notifications
You must be signed in to change notification settings - Fork 0
/
morsecode.c
69 lines (53 loc) · 1.09 KB
/
morsecode.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
/*
* Copyright 2013 Daniel Hjort. All rights reserved.
* Author: Daniel Hjort
*/
#include "morsecode.h"
#include "morsecode_data.h"
#include <string.h>
morsecode_t ham_morse_from_ascii(char character)
{
morsecode_t result = "";
char toFind = character;
if (toFind >= 'a' && toFind <= 'z') {
toFind = toFind ^ (0x1 << 5);
}
for (int i = 0; i < NUMBER_OF_MORSECODES; i++) {
if (toFind == ascii[i]) {
result = morse[i];
break;
}
}
return result;
}
char ham_morse_to_ascii(morsecode_t code)
{
char result;
for (int i = 0; i < NUMBER_OF_MORSECODES; i++) {
if (0 == strcmp(code, morse[i])) {
result = ascii[i];
break;
}
}
return result;
}
double ham_morse_element_time(MorseCodeElement element, uint8_t wordsPerMinute, uint8_t stdWordLength)
{
double unitTime = (1.2 / wordsPerMinute) * (50.0/stdWordLength);
uint8_t numberOfUnits = 0;
switch (element) {
case DOT:
numberOfUnits = 1;
break;
case DASH:
case CHAR_GAP:
numberOfUnits = 3;
break;
case WORD_GAP:
numberOfUnits = 7;
break;
default:
break;
}
return unitTime * numberOfUnits;
}