-
Notifications
You must be signed in to change notification settings - Fork 1
/
CC1200Morse.cpp
189 lines (163 loc) · 5.31 KB
/
CC1200Morse.cpp
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
* Copyright (c) 2019-2023 USC Rocket Propulsion Lab
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "CC1200Morse.h"
#include <cinttypes>
// Morse code tables.
// Covers ASCII ranges 0x41-0x5A and 0x61-0x7A
char const * const alphaMorse[] = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---",
"-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-",
"...-",".--","-..-","-.--","--.."};
// Covers ASCII range 0x30-0x39
char const * const numMorse[] = {"-----",".----","..---","...--","....-",".....","-....","--...","---..","----."};
// covers ASCII range 0x21-0x2F
char const * const punctuation1Morse[] = {"-.-.--", ".-..-.", nullptr, "...-..-", nullptr, ".-...", ".----.", "-.--.",
"-.--.-", nullptr, ".-.-.", "--..--", nullptr, ".-.-.-", "-..-."};
// covers ASCII range 0x3A-0x40
char const * const punctuation2Morse[] = {"---...", "-.-.-.", nullptr, "-...-", nullptr, "..--..", ".--.-."};
void CC1200Morse::configure(CC1200::Band band, float radioFrequency, std::chrono::milliseconds morseTimePeriod, float transmitPower)
{
radio.setPacketMode(CC1200::PacketMode::FIXED_LENGTH);
radio.setCRCEnabled(false);
// set frequency
radio.setSymbolRate(1/std::chrono::duration_cast<std::chrono::duration<float>>(morseTimePeriod).count());
radio.setRadioFrequency(band, radioFrequency);
// disable anything getting sent before the data
radio.configureSyncWord(0x0, CC1200::SyncMode::SYNC_NONE, 8);
radio.configurePreamble(0, 0);
// configure OOK modulation
radio.setModulationFormat(CC1200::ModFormat::ASK);
radio.disablePARamping();
radio.setASKPowers(transmitPower, CC1200::ASK_MIN_POWER_OFF);
}
// helper function for convertToMorse
static bool appendBits(uint8_t *outputBuffer, size_t bufferLen, uint8_t & nextBit, size_t & currByte, uint8_t toAppend, size_t count)
{
//printf("appendBits(%" PRIu8 ", %zu)\n", toAppend, count);
for(size_t counter = 0; counter < count; ++counter)
{
outputBuffer[currByte] |= toAppend << nextBit;
if(nextBit == 0)
{
nextBit = 7;
currByte += 1;
if(currByte >= bufferLen)
{
// out of space
return false;
}
}
else
{
nextBit--;
}
}
return true;
}
CC1200Morse::EncodedMorse CC1200Morse::convertToMorse(const char *string, uint8_t *outputBuffer, size_t bufferLen)
{
memset(outputBuffer, 0, bufferLen);
// place in the output buffer where next items will be written
uint8_t nextBit = 7;
size_t currByte = 0;
EncodedMorse encoded;
encoded.buffer = outputBuffer;
encoded.valid = false;
if(!appendBits(outputBuffer, bufferLen, nextBit, currByte, 0, spaceBefore))
{
return encoded;
}
size_t stringLength = strlen(string);
for(size_t charIndex = 0; charIndex < stringLength; ++charIndex)
{
char currChar = string[charIndex];
char const * morseToAppend = nullptr;
if((currChar >= 'A' && currChar <= 'Z'))
{
morseToAppend = alphaMorse[currChar - 'A'];
}
else if(currChar >= 'a' && currChar <= 'z')
{
morseToAppend = alphaMorse[currChar - 'a'];
}
else if(currChar >= '0' && currChar <= '9')
{
morseToAppend = numMorse[currChar - '0'];
}
else if(currChar >= '!' && currChar <= '/')
{
morseToAppend = punctuation1Morse[currChar - '!'];
}
else if(currChar >= ':' && currChar <= '@')
{
morseToAppend = punctuation2Morse[currChar - ':'];
}
else if(currChar == '_') // underscore is off by itself in the ASCII chart
{
morseToAppend = "..--.-";
}
// append bit timings
//printf("currChar = '%c'\n", currChar);
if(currChar == ' ')
{
// space between words is 7 time units
if(!appendBits(outputBuffer, bufferLen, nextBit, currByte, 0, 7))
{
return encoded;
}
}
else if(morseToAppend != nullptr)
{
size_t morseLength = strlen(morseToAppend);
for(size_t morseIndex = 0; morseIndex < morseLength; ++morseIndex)
{
// dot is 1 time unit, dash is 3 time units
if(!appendBits(outputBuffer, bufferLen, nextBit, currByte, 1, morseToAppend[morseIndex] == '-' ? 3 : 1))
{
return encoded;
}
// space between symbols is 1 time unit
if(!appendBits(outputBuffer, bufferLen, nextBit, currByte, 0, 1))
{
return encoded;
}
}
// extra space between letters is 2 time units
if(!appendBits(outputBuffer, bufferLen, nextBit, currByte, 0, 2))
{
return encoded;
}
}
}
if(!appendBits(outputBuffer, bufferLen, nextBit, currByte, 0, spaceAfter))
{
return encoded;
}
encoded.valid = true;
encoded.byteLen = currByte;
encoded.bitLen = 7 - nextBit;
encoded.totalLength = currByte + (encoded.bitLen > 0 ? 1 : 0);
return encoded;
}
void CC1200Morse::transmit(const CC1200Morse::EncodedMorse &morse)
{
if(morse.totalLength > 128)
{
// too large to send in one packet
return;
}
radio.setPacketLength(morse.byteLen, morse.bitLen);
radio.enqueuePacket(reinterpret_cast<const char *>(morse.buffer), morse.totalLength);
}