├── .github ├── FUNDING.yml └── workflows │ └── arduino-lint.yml ├── COPYING ├── README.md ├── examples └── Si5351JTDemo │ └── Si5351JTDemo.ino ├── keywords.txt ├── library.properties └── src ├── JTEncode.cpp ├── JTEncode.h ├── crc14.c ├── crc14.h ├── encode_rs.h ├── encode_rs_int.cpp ├── generator.h ├── init_rs.h ├── init_rs_int.cpp ├── int.h ├── nhash.c ├── nhash.h └── rs_common.h /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 4 | custom: ['https://paypal.me/NT7S', 'https://www.subscribestar.com/nt7s'] 5 | -------------------------------------------------------------------------------- /.github/workflows/arduino-lint.yml: -------------------------------------------------------------------------------- 1 | name: arduino-lint 2 | 3 | on: [push, pull_request] 4 | jobs: 5 | lint: 6 | runs-on: ubuntu-latest 7 | steps: 8 | - uses: actions/checkout@v2 9 | - uses: arduino/arduino-lint-action@v1.0.0 10 | with: 11 | library-manager: update 12 | compliance: specification 13 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | JT65/JT9/JT4/FT8/WSPR/FSQ Encoder Library for Arduino 2 | ===================================================== 3 | 4 | This library very simply generates a set of channel symbols for JT65, JT9, JT4, FT8, or WSPR based on the user providing a properly formatted Type 6 message for JT65, JT9, or JT4 (which is 13 valid characters), Type 0.0 or 0.5 message for FT8 (v2.0.0 protocol) or a Type 1, Type 2, or Type 3 message for WSPR. It will also generate an arbitrary FSQ message of up to 200 characters in both directed and non-directed format. When paired with a synthesizer that can output frequencies in fine, phase-continuous tuning steps (such as the Si5351), then a beacon or telemetry transmitter can be created which can change the transmitted characters as needed from the Arduino. 5 | 6 | Please feel free to use the issues feature of GitHub if you run into problems or have suggestions for important features to implement. 7 | 8 | Thanks For Your Support! 9 | ------------------------ 10 | If you would like to support my library development efforts, I would ask that you please consider sending a [one-time PayPal tip](https://paypal.me/NT7S) or [subscribe to me on SubscribeStar](https://www.subscribestar.com/nt7s) for an ongoing contribution.. Thank you! 11 | 12 | Hardware Requirements and Setup 13 | ------------------------------- 14 | This library has been written for the Arduino platform and has been successfully tested on the Arduino Uno, an Uno clone, an Arduino Zero clone, and a NodeMCU. Since the library itself does not access the hardware, there is no reason it should not run on any Arduino model of recent vintage as long as it has at least 2 kB of RAM. 15 | 16 | How To Install 17 | -------------- 18 | The best way to install the library is via the Arduino Library Manager, which is available if you are using Arduino IDE version 1.6.2 or greater. To install it this way, simply go to the menu Sketch > Include Library > Manage Libraries..., and then in the search box at the upper-right, type "Etherkit JTEncode". Click on the entry in the list below, then click on the provided "Install" button. By installing the library this way, you will always have notifications of future library updates, and can easily switch between library versions. 19 | 20 | If you need to or would like to install the library in the old way, then you can download a copy of the library in a ZIP file. Download a ZIP file of the library from the GitHub repository by going to [this page](https://github.com/etherkit/JTEncode/releases) and clicking the "Source code (zip)" link under the latest release. Finally, open the Arduino IDE, select menu Sketch > Import Library... > Add Library..., and select the ZIP that you just downloaded. 21 | 22 | RAM Usage 23 | --------- 24 | Most of the encoding functions need to manipulate multiple arrays of symbols in RAM at the same time, and therefore are quite RAM intensive. Care has been taken to put as much data into program memory as is possible, but the encoding functions still can cause problems with the low RAM microcontrollers such as the ATmegaxx8 series. If you are using these, then please be sure to call them only once when a transmit buffer needs to be created or changed, and call them separately of other subroutine calls. When using other microcontrollers that have more RAM, such as most of the ARM ICs, this won't be as much of a problem. If you see unusual freezes, that almost certainly indicates a RAM shortage. 25 | 26 | WSPR Messages 27 | ------------- 28 | JTEncode includes support for all three WSPR message types. A brief listing of the three types is given below: 29 | 30 | | Message Type | Fields | Example | 31 | |--------------|--------|---------| 32 | | Type 1 | Callsign, Grid (4 digit), Power | NT7S CN85 30 | 33 | | Type 2 | Callsign with prefix or suffix, Power | NT7S/P 30 | 34 | | Type 3 | Callsign Hash, Grid (6 digit), Power | \ CN85NM 30 | 35 | 36 | Most WSPR messages are type 1, however sometimes type 2 and 3 messages are needed. Type 2 messages allow you to send a callsign with a prefix of up to three characters, a suffix of a single character, or a suffix consisting of two numerical digits. Type 3 messages are typically used in conjunction with type 2 messages since type 2 messages don't include a grid locator. The type 3 message sends a 15-bit hash of the included callsign, along with a 6 digit grid locator and the power. 37 | 38 | Type 2 messages can be sent in JTEncode simply by including the slashed prefix or suffix in the callsign field. A type 3 message can be sent by enclosing a callsign with angle brackets (as seen in the example above). 39 | 40 | Example 41 | ------- 42 | There is a simple example that is placed in your examples menu under JTEncode. Open this to see how to incorporate this library with your code. The example provided with with the library is meant to be used in conjunction with the [Etherkit Si5351A Breakout Board](https://www.etherkit.com/rf-modules/si5351a-breakout-board.html), although it could be modified to use with other synthesizers which meet the technical requirements of the JT65/JT9/JT4/WSPR/FSQ modes. 43 | 44 | To run this example, be sure to download the [Si5351Arduino](https://github.com/etherkit/Si5351Arduino) library and follow the instructions there to connect the Si5351A Breakout Board to your Arduino. In order to trigger transmissions, you will also need to connect a momentary pushbutton from pin 12 of the Arduino to ground. 45 | 46 | The example sketch itself is fairly straightforward. JT65, JT9, JT4, FT8, WSPR, and FSQ modes are modulated in same way: phase-continuous multiple-frequency shift keying (MFSK). The message to be transmitted is passed to the JTEncode method corresponding to the desired mode, along with a pointer to an array which holds the returned channel symbols. When the pushbutton is pushed, the sketch then transmits each channel symbol sequentially as an offset from the base frequency given in the sketch define section. 47 | 48 | An instance of the JTEncode object is created: 49 | 50 | JTEncode jtencode; 51 | 52 | On sketch startup, the mode parameters are set based on which mode is currently selected (by the DEFAULT_MODE define): 53 | 54 | // Set the proper frequency, tone spacing, symbol count, and 55 | // tone delay depending on mode 56 | switch(cur_mode) 57 | { 58 | case MODE_JT9: 59 | freq = JT9_DEFAULT_FREQ; 60 | symbol_count = JT9_SYMBOL_COUNT; // From the library defines 61 | tone_spacing = JT9_TONE_SPACING; 62 | tone_delay = JT9_DELAY; 63 | break; 64 | case MODE_JT65: 65 | freq = JT65_DEFAULT_FREQ; 66 | symbol_count = JT65_SYMBOL_COUNT; // From the library defines 67 | tone_spacing = JT65_TONE_SPACING; 68 | tone_delay = JT65_DELAY; 69 | break; 70 | case MODE_JT4: 71 | freq = JT4_DEFAULT_FREQ; 72 | symbol_count = JT4_SYMBOL_COUNT; // From the library defines 73 | tone_spacing = JT4_TONE_SPACING; 74 | tone_delay = JT4_DELAY; 75 | break; 76 | case MODE_WSPR: 77 | freq = WSPR_DEFAULT_FREQ; 78 | symbol_count = WSPR_SYMBOL_COUNT; // From the library defines 79 | tone_spacing = WSPR_TONE_SPACING; 80 | tone_delay = WSPR_DELAY; 81 | break; 82 | case MODE_FT8: 83 | freq = FT8_DEFAULT_FREQ; 84 | symbol_count = FT8_SYMBOL_COUNT; // From the library defines 85 | tone_spacing = FT8_TONE_SPACING; 86 | tone_delay = FT8_DELAY; 87 | break; 88 | case MODE_FSQ_2: 89 | freq = FSQ_DEFAULT_FREQ; 90 | tone_spacing = FSQ_TONE_SPACING; 91 | tone_delay = FSQ_2_DELAY; 92 | break; 93 | case MODE_FSQ_3: 94 | freq = FSQ_DEFAULT_FREQ; 95 | tone_spacing = FSQ_TONE_SPACING; 96 | tone_delay = FSQ_3_DELAY; 97 | break; 98 | case MODE_FSQ_4_5: 99 | freq = FSQ_DEFAULT_FREQ; 100 | tone_spacing = FSQ_TONE_SPACING; 101 | tone_delay = FSQ_4_5_DELAY; 102 | break; 103 | case MODE_FSQ_6: 104 | freq = FSQ_DEFAULT_FREQ; 105 | tone_spacing = FSQ_TONE_SPACING; 106 | tone_delay = FSQ_6_DELAY; 107 | break; 108 | } 109 | 110 | Note that the number of channel symbols for each mode is defined in the library, so you can use those defines to initialize your own symbol array sizes. 111 | 112 | Before transmit, the proper class method is chosen based on the desired mode, then the transmit symbol buffer and the other mode information is set: 113 | 114 | // Set the proper frequency and timer CTC depending on mode 115 | switch(cur_mode) 116 | { 117 | case MODE_JT9: 118 | jtencode.jt9_encode(message, tx_buffer); 119 | break; 120 | case MODE_JT65: 121 | jtencode.jt65_encode(message, tx_buffer); 122 | break; 123 | case MODE_JT4: 124 | jtencode.jt4_encode(message, tx_buffer); 125 | break; 126 | case MODE_WSPR: 127 | jtencode.wspr_encode(call, loc, dbm, tx_buffer); 128 | break; 129 | case MODE_FT8: 130 | jtencode.ft_encode(message, tx_buffer); 131 | break; 132 | case MODE_FSQ_2: 133 | case MODE_FSQ_3: 134 | case MODE_FSQ_4_5: 135 | case MODE_FSQ_6: 136 | jtencode.fsq_dir_encode(call, "n0call", " ", "hello world", tx_buffer); 137 | break; 138 | } 139 | 140 | As mentioned above, it is best if the message encoding functions are called only when needed, in its own subroutine. 141 | 142 | Once the channel symbols have been generated, it is a simple matter of transmitting them in sequence, each the correct amount of time: 143 | 144 | // Now transmit the channel symbols 145 | for(i = 0; i < symbol_count; i++) 146 | { 147 | si5351.set_freq((freq * 100) + (tx_buffer[i] * tone_spacing), SI5351_CLK0); 148 | delay(tone_delay); 149 | } 150 | 151 | Public Methods 152 | ------------------ 153 | ### jt65_encode() 154 | ``` 155 | /* 156 | * jt65_encode(const char * message, uint8_t * symbols) 157 | * 158 | * Takes an arbitrary message of up to 13 allowable characters and returns 159 | * a channel symbol table. 160 | * 161 | * message - Plaintext Type 6 message. 162 | * symbols - Array of channel symbols to transmit returned by the method. 163 | * Ensure that you pass a uint8_t array of at least size JT65_SYMBOL_COUNT to the method. 164 | * 165 | */ 166 | ``` 167 | ### jt9_encode() 168 | ``` 169 | /* 170 | * jt9_encode(const char * message, uint8_t * symbols) 171 | * 172 | * Takes an arbitrary message of up to 13 allowable characters and returns 173 | * a channel symbol table. 174 | * 175 | * message - Plaintext Type 6 message. 176 | * symbols - Array of channel symbols to transmit returned by the method. 177 | * Ensure that you pass a uint8_t array of at least size JT9_SYMBOL_COUNT to the method. 178 | * 179 | */ 180 | ``` 181 | 182 | ### jt4_encode() 183 | ``` 184 | /* 185 | * jt4_encode(const char * message, uint8_t * symbols) 186 | * 187 | * Takes an arbitrary message of up to 13 allowable characters and returns 188 | * a channel symbol table. 189 | * 190 | * message - Plaintext Type 6 message. 191 | * symbols - Array of channel symbols to transmit returned by the method. 192 | * Ensure that you pass a uint8_t array of at least size JT9_SYMBOL_COUNT to the method. 193 | * 194 | */ 195 | ``` 196 | 197 | ### wspr_encode() 198 | ``` 199 | /* 200 | * wspr_encode(const char * call, const char * loc, const uint8_t dbm, uint8_t * symbols) 201 | * 202 | * Takes a callsign, grid locator, and power level and returns a WSPR symbol 203 | * table for a Type 1, 2, or 3 message. 204 | * 205 | * call - Callsign (12 characters maximum). 206 | * loc - Maidenhead grid locator (6 characters maximum). 207 | * dbm - Output power in dBm. 208 | * symbols - Array of channel symbols to transmit returned by the method. 209 | * Ensure that you pass a uint8_t array of at least size WSPR_SYMBOL_COUNT to the method. 210 | * 211 | */ 212 | ``` 213 | 214 | ### ft8_encode() 215 | ``` 216 | /* 217 | * ft8_encode(const char * message, uint8_t * symbols) 218 | * 219 | * Takes an arbitrary message of up to 13 allowable characters or a telemetry message 220 | * of up to 18 hexadecimal digit (in string format) and returns a channel symbol table. 221 | * Encoded for the FT8 protocol used in WSJT-X v2.0 and beyond (79 channel symbols). 222 | * 223 | * message - Type 0.0 free text message or Type 0.5 telemetry message. 224 | * symbols - Array of channel symbols to transmit returned by the method. 225 | * Ensure that you pass a uint8_t array of at least size FT8_SYMBOL_COUNT to the method. 226 | * 227 | */ 228 | ``` 229 | 230 | ### fsq_encode() 231 | ``` 232 | /* 233 | * fsq_encode(const char * from_call, const char * message, uint8_t * symbols) 234 | * 235 | * Takes an arbitrary message and returns a FSQ channel symbol table. 236 | * 237 | * from_call - Callsign of issuing station (maximum size: 20) 238 | * message - Null-terminated message string, no greater than 130 chars in length 239 | * symbols - Array of channel symbols to transmit returned by the method. 240 | * Ensure that you pass a uint8_t array of at least the size of the message 241 | * plus 5 characters to the method. Terminated in 0xFF. 242 | * 243 | */ 244 | ``` 245 | 246 | ### fsq_dir_encode() 247 | ``` 248 | /* 249 | * fsq_dir_encode(const char * from_call, const char * to_call, const char cmd, const char * message, uint8_t * symbols) 250 | * 251 | * Takes an arbitrary message and returns a FSQ channel symbol table. 252 | * 253 | * from_call - Callsign from which message is directed (maximum size: 20) 254 | * to_call - Callsign to which message is directed (maximum size: 20) 255 | * cmd - Directed command 256 | * message - Null-terminated message string, no greater than 100 chars in length 257 | * symbols - Array of channel symbols to transmit returned by the method. 258 | * Ensure that you pass a uint8_t array of at least the size of the message 259 | * plus 5 characters to the method. Terminated in 0xFF. 260 | * 261 | */ 262 | ``` 263 | 264 | ### latlon_to_grid() 265 | ``` 266 | /* 267 | * latlon_to_grid(float lat, float lon, char* ret_grid) 268 | * 269 | * Takes a station latitude and longitude provided in decimal degrees format and 270 | * returns a string with the 6-digit Maidenhead grid designator. 271 | * 272 | * lat - Latitude in decimal degrees format. 273 | * lon - Longitude in decimal degrees format. 274 | * ret_grid - Derived Maidenhead grid square. A pointer to a character array of 275 | * at least 7 bytes must be provided here for the function return value. 276 | * 277 | */ 278 | ``` 279 | 280 | Tokens 281 | ------ 282 | Here are the defines, structs, and enumerations you will find handy to use with the library. 283 | 284 | Defines: 285 | 286 | JT65_SYMBOL_COUNT, JT9_SYMBOL_COUNT, JT4_SYMBOL_COUNT, WSPR_SYMBOL_COUNT, FT8_SYMBOL_COUNT 287 | 288 | Acknowledgements 289 | ---------------- 290 | Many thanks to Joe Taylor K1JT for his innovative work in amateur radio. We are lucky to have him. The algorithms in this program were derived from the source code in the [WSJT-X](https://sourceforge.net/p/wsjt/) suite of applications. Also, many thanks for Andy Talbot G4JNT for [his paper](http://www.g4jnt.com/JTModesBcns.htm) on the WSPR coding protocol, which helped me to understand the WSPR encoding process, which in turn helped me to understand the related JT protocols. 291 | 292 | Also, a big thank you to Murray Greenman, ZL1BPU for working allowing me to pick his brain regarding his neat new mode FSQ. 293 | 294 | Changelog 295 | --------- 296 | * v1.3.1 297 | 298 | * Added latitude/longitude to Maidenhead grid convenience function 299 | 300 | * v1.3.0 301 | 302 | * WSPR Type 2 and Type 3 message capability added 303 | 304 | * v1.2.1 305 | 306 | * Fix keywords.txt 307 | 308 | * v1.2.0 309 | 310 | * Add support for FT8 protocol (79 symbol version introduced December 2018) 311 | 312 | * v1.1.3 313 | 314 | * Add support for ESP8266 315 | * Fix WSPR regression in last release 316 | 317 | * v1.1.2 318 | 319 | * Fix buffer bug in _jt_message_prep()_ that caused messages of 11 chars to lock up the processor 320 | * Made a handful of changes to make the library more friendly to ATmegaxx8 processors 321 | * Rewrote example sketch to be generically compatible with most Arduino platforms 322 | 323 | * v1.1.1 324 | 325 | * Update example sketch for Si5351Arduino v2.0.0 326 | 327 | * v1.1.0 328 | 329 | * Added FSQ 330 | 331 | * v1.0.1 332 | 333 | * Fixed a bug in _jt65_interleave()_ that was causing a buffer overrun. 334 | 335 | * v1.0.0 336 | 337 | * Initial Release 338 | 339 | Arduino Lint Status 340 | ------------------- 341 | [![arduino-lint Actions Status](https://github.com/etherkit/JTEncode/workflows/arduino-lint/badge.svg)](https://github.com/etherkit/JTEncode/actions) 342 | 343 | 344 | License 345 | ------- 346 | JTEncode is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. 347 | 348 | JTEncode is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. 349 | 350 | You should have received a copy of the GNU General Public License along with JTEncode. If not, see . 351 | -------------------------------------------------------------------------------- /examples/Si5351JTDemo/Si5351JTDemo.ino: -------------------------------------------------------------------------------- 1 | // 2 | // Simple JT65/JT9/JT4/FT8/WSPR/FSQ beacon for Arduino, with the Etherkit 3 | // Si5351A Breakout Board, by Jason Milldrum NT7S. 4 | // 5 | // Transmit an abritrary message of up to 13 valid characters 6 | // (a Type 6 message) in JT65, JT9, JT4, a type 0.0 or type 0.5 FT8 message, 7 | // a FSQ message, or a standard Type 1 message in WSPR. 8 | // 9 | // Connect a momentary push button to pin 12 to use as the 10 | // transmit trigger. Get fancy by adding your own code to trigger 11 | // off of the time from a GPS or your PC via virtual serial. 12 | // 13 | // Original code based on Feld Hell beacon for Arduino by Mark 14 | // Vandewettering K6HX, adapted for the Si5351A by Robert 15 | // Liesenfeld AK6L . 16 | // 17 | // Permission is hereby granted, free of charge, to any person obtaining 18 | // a copy of this software and associated documentation files (the 19 | // "Software"), to deal in the Software without restriction, including 20 | // without limitation the rights to use, copy, modify, merge, publish, 21 | // distribute, sublicense, and/or sell copies of the Software, and to 22 | // permit persons to whom the Software is furnished to do so, subject 23 | // to the following conditions: 24 | // 25 | // The above copyright notice and this permission notice shall be 26 | // included in all copies or substantial portions of the Software. 27 | // 28 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 29 | // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 30 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 31 | // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR 32 | // ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 33 | // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 34 | // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 35 | // 36 | 37 | #include 38 | #include 39 | #include 40 | #include 41 | #include 42 | 43 | #include "Wire.h" 44 | 45 | // Mode defines 46 | #define JT9_TONE_SPACING 174 // ~1.74 Hz 47 | #define JT65_TONE_SPACING 269 // ~2.69 Hz 48 | #define JT4_TONE_SPACING 437 // ~4.37 Hz 49 | #define WSPR_TONE_SPACING 146 // ~1.46 Hz 50 | #define FSQ_TONE_SPACING 879 // ~8.79 Hz 51 | #define FT8_TONE_SPACING 625 // ~6.25 Hz 52 | 53 | #define JT9_DELAY 576 // Delay value for JT9-1 54 | #define JT65_DELAY 371 // Delay in ms for JT65A 55 | #define JT4_DELAY 229 // Delay value for JT4A 56 | #define WSPR_DELAY 683 // Delay value for WSPR 57 | #define FSQ_2_DELAY 500 // Delay value for 2 baud FSQ 58 | #define FSQ_3_DELAY 333 // Delay value for 3 baud FSQ 59 | #define FSQ_4_5_DELAY 222 // Delay value for 4.5 baud FSQ 60 | #define FSQ_6_DELAY 167 // Delay value for 6 baud FSQ 61 | #define FT8_DELAY 159 // Delay value for FT8 62 | 63 | #define JT9_DEFAULT_FREQ 14078700UL 64 | #define JT65_DEFAULT_FREQ 14078300UL 65 | #define JT4_DEFAULT_FREQ 14078500UL 66 | #define WSPR_DEFAULT_FREQ 14097200UL 67 | #define FSQ_DEFAULT_FREQ 7105350UL // Base freq is 1350 Hz higher than dial freq in USB 68 | #define FT8_DEFAULT_FREQ 14075000UL 69 | 70 | #define DEFAULT_MODE MODE_JT65 71 | 72 | // Hardware defines 73 | #define BUTTON 12 74 | #define LED_PIN 13 75 | 76 | // Enumerations 77 | enum mode {MODE_JT9, MODE_JT65, MODE_JT4, MODE_WSPR, MODE_FSQ_2, MODE_FSQ_3, 78 | MODE_FSQ_4_5, MODE_FSQ_6, MODE_FT8}; 79 | 80 | // Class instantiation 81 | Si5351 si5351; 82 | JTEncode jtencode; 83 | 84 | // Global variables 85 | unsigned long freq; 86 | char message[] = "N0CALL AA00"; 87 | char call[] = "N0CALL"; 88 | char loc[] = "AA00"; 89 | uint8_t dbm = 27; 90 | uint8_t tx_buffer[255]; 91 | enum mode cur_mode = DEFAULT_MODE; 92 | uint8_t symbol_count; 93 | uint16_t tone_delay, tone_spacing; 94 | 95 | // Loop through the string, transmitting one character at a time. 96 | void encode() 97 | { 98 | uint8_t i; 99 | 100 | // Reset the tone to the base frequency and turn on the output 101 | si5351.output_enable(SI5351_CLK0, 1); 102 | digitalWrite(LED_PIN, HIGH); 103 | 104 | // Now transmit the channel symbols 105 | if(cur_mode == MODE_FSQ_2 || cur_mode == MODE_FSQ_3 || cur_mode == MODE_FSQ_4_5 || cur_mode == MODE_FSQ_6) 106 | { 107 | uint8_t j = 0; 108 | 109 | while(tx_buffer[j++] != 0xff); 110 | 111 | symbol_count = j - 1; 112 | } 113 | 114 | for(i = 0; i < symbol_count; i++) 115 | { 116 | si5351.set_freq((freq * 100) + (tx_buffer[i] * tone_spacing), SI5351_CLK0); 117 | delay(tone_delay); 118 | } 119 | 120 | // Turn off the output 121 | si5351.output_enable(SI5351_CLK0, 0); 122 | digitalWrite(LED_PIN, LOW); 123 | } 124 | 125 | void set_tx_buffer() 126 | { 127 | // Clear out the transmit buffer 128 | memset(tx_buffer, 0, 255); 129 | 130 | // Set the proper frequency and timer CTC depending on mode 131 | switch(cur_mode) 132 | { 133 | case MODE_JT9: 134 | jtencode.jt9_encode(message, tx_buffer); 135 | break; 136 | case MODE_JT65: 137 | jtencode.jt65_encode(message, tx_buffer); 138 | break; 139 | case MODE_JT4: 140 | jtencode.jt4_encode(message, tx_buffer); 141 | break; 142 | case MODE_WSPR: 143 | jtencode.wspr_encode(call, loc, dbm, tx_buffer); 144 | break; 145 | case MODE_FT8: 146 | jtencode.ft8_encode(message, tx_buffer); 147 | break; 148 | case MODE_FSQ_2: 149 | case MODE_FSQ_3: 150 | case MODE_FSQ_4_5: 151 | case MODE_FSQ_6: 152 | jtencode.fsq_dir_encode(call, "n0call", ' ', "hello world", tx_buffer); 153 | break; 154 | } 155 | } 156 | 157 | void setup() 158 | { 159 | // Initialize the Si5351 160 | // Change the 2nd parameter in init if using a ref osc other 161 | // than 25 MHz 162 | si5351.init(SI5351_CRYSTAL_LOAD_8PF, 0, 0); 163 | 164 | // Use the Arduino's on-board LED as a keying indicator. 165 | pinMode(LED_PIN, OUTPUT); 166 | digitalWrite(LED_PIN, LOW); 167 | 168 | // Use a button connected to pin 12 as a transmit trigger 169 | pinMode(BUTTON, INPUT_PULLUP); 170 | 171 | // Set the mode to use 172 | cur_mode = MODE_JT65; 173 | 174 | // Set the proper frequency, tone spacing, symbol count, and 175 | // tone delay depending on mode 176 | switch(cur_mode) 177 | { 178 | case MODE_JT9: 179 | freq = JT9_DEFAULT_FREQ; 180 | symbol_count = JT9_SYMBOL_COUNT; // From the library defines 181 | tone_spacing = JT9_TONE_SPACING; 182 | tone_delay = JT9_DELAY; 183 | break; 184 | case MODE_JT65: 185 | freq = JT65_DEFAULT_FREQ; 186 | symbol_count = JT65_SYMBOL_COUNT; // From the library defines 187 | tone_spacing = JT65_TONE_SPACING; 188 | tone_delay = JT65_DELAY; 189 | break; 190 | case MODE_JT4: 191 | freq = JT4_DEFAULT_FREQ; 192 | symbol_count = JT4_SYMBOL_COUNT; // From the library defines 193 | tone_spacing = JT4_TONE_SPACING; 194 | tone_delay = JT4_DELAY; 195 | break; 196 | case MODE_WSPR: 197 | freq = WSPR_DEFAULT_FREQ; 198 | symbol_count = WSPR_SYMBOL_COUNT; // From the library defines 199 | tone_spacing = WSPR_TONE_SPACING; 200 | tone_delay = WSPR_DELAY; 201 | break; 202 | case MODE_FT8: 203 | freq = FT8_DEFAULT_FREQ; 204 | symbol_count = FT8_SYMBOL_COUNT; // From the library defines 205 | tone_spacing = FT8_TONE_SPACING; 206 | tone_delay = FT8_DELAY; 207 | break; 208 | case MODE_FSQ_2: 209 | freq = FSQ_DEFAULT_FREQ; 210 | tone_spacing = FSQ_TONE_SPACING; 211 | tone_delay = FSQ_2_DELAY; 212 | break; 213 | case MODE_FSQ_3: 214 | freq = FSQ_DEFAULT_FREQ; 215 | tone_spacing = FSQ_TONE_SPACING; 216 | tone_delay = FSQ_3_DELAY; 217 | break; 218 | case MODE_FSQ_4_5: 219 | freq = FSQ_DEFAULT_FREQ; 220 | tone_spacing = FSQ_TONE_SPACING; 221 | tone_delay = FSQ_4_5_DELAY; 222 | break; 223 | case MODE_FSQ_6: 224 | freq = FSQ_DEFAULT_FREQ; 225 | tone_spacing = FSQ_TONE_SPACING; 226 | tone_delay = FSQ_6_DELAY; 227 | break; 228 | } 229 | 230 | // Set CLK0 output 231 | si5351.drive_strength(SI5351_CLK0, SI5351_DRIVE_8MA); // Set for max power if desired 232 | si5351.output_enable(SI5351_CLK0, 0); // Disable the clock initially 233 | 234 | // Encode the message in the transmit buffer 235 | // This is RAM intensive and should be done separately from other subroutines 236 | set_tx_buffer(); 237 | } 238 | 239 | void loop() 240 | { 241 | // Debounce the button and trigger TX on push 242 | if(digitalRead(BUTTON) == LOW) 243 | { 244 | delay(50); // delay to debounce 245 | if (digitalRead(BUTTON) == LOW) 246 | { 247 | encode(); 248 | delay(50); //delay to avoid extra triggers 249 | } 250 | } 251 | } 252 | -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | JTEncode KEYWORD1 2 | 3 | jt65_encode KEYWORD2 4 | jt9_encode KEYWORD2 5 | jt4_encode KEYWORD2 6 | wspr_encode KEYWORD2 7 | ft8_encode KEYWORD2 8 | fsq_encode KEYWORD2 9 | fsq_dir_encode KEYWORD2 10 | 11 | JT65_SYMBOL_COUNT LITERAL1 12 | JT9_SYMBOL_COUNT LITERAL1 13 | JT4_SYMBOL_COUNT LITERAL1 14 | WSPR_SYMBOL_COUNT LITERAL1 15 | FT8_SYMBOL_COUNT LITERAL1 16 | JT65_ENCODE_COUNT LITERAL1 17 | JT9_ENCODE_COUNT LITERAL1 18 | FT8_ENCODE_COUNT LITERAL1 19 | JT9_BIT_COUNT LITERAL1 20 | JT4_BIT_COUNT LITERAL1 21 | WSPR_BIT_COUNT LITERAL1 22 | FT8_BIT_COUNT LITERAL1 23 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=Etherkit JTEncode 2 | version=1.3.1 3 | author=Jason Milldrum 4 | maintainer=Jason Milldrum 5 | sentence=Generate JT65, JT9, JT4, FT8, WSPR, and FSQ symbols on your Arduino. 6 | paragraph=This library very simply generates a set of channel symbols for JT65, JT9, JT4, FT8, or WSPR based on the user providing a properly formatted Type 6 message for JT65, JT9, or JT4 (which is 13 valid characters), Type 0.0 or 0.5 message for FT8 (v2.0.0 protocol) or a callsign, Maidenhead grid locator, and power output for WSPR. It will also generate an arbitrary FSQ message of up to 200 characters in both directed and non-directed format. When paired with a synthesizer that can output frequencies in fine, phase-continuous tuning steps (such as the Si5351), then a beacon or telemetry transmitter can be created which can change the transmitted characters as needed from the Arduino. 7 | category=Data Processing 8 | url=https://github.com/etherkit/JTEncode 9 | architectures=* 10 | -------------------------------------------------------------------------------- /src/JTEncode.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * JTEncode.cpp - JT65/JT9/WSPR/FSQ encoder library for Arduino 3 | * 4 | * Copyright (C) 2015-2021 Jason Milldrum 5 | * 6 | * Based on the algorithms presented in the WSJT software suite. 7 | * Thanks to Andy Talbot G4JNT for the whitepaper on the WSPR encoding 8 | * process that helped me to understand all of this. 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | 35 | #if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__) || defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega16U4__) 36 | #include 37 | #endif 38 | 39 | #include "Arduino.h" 40 | 41 | // Define an upper bound on the number of glyphs. Defining it this 42 | // way allows adding characters without having to update a hard-coded 43 | // upper bound. 44 | #define NGLYPHS (sizeof(fsq_code_table)/sizeof(fsq_code_table[0])) 45 | 46 | /* Public Class Members */ 47 | 48 | JTEncode::JTEncode(void) 49 | { 50 | // Initialize the Reed-Solomon encoder 51 | rs_inst = (struct rs *)(intptr_t)init_rs_int(6, 0x43, 3, 1, 51, 0); 52 | // memset(callsign, 0, 13); 53 | } 54 | 55 | /* 56 | * jt65_encode(const char * message, uint8_t * symbols) 57 | * 58 | * Takes an arbitrary message of up to 13 allowable characters and returns 59 | * a channel symbol table. 60 | * 61 | * message - Plaintext Type 6 message. 62 | * symbols - Array of channel symbols to transmit returned by the method. 63 | * Ensure that you pass a uint8_t array of at least size JT65_SYMBOL_COUNT to the method. 64 | * 65 | */ 66 | void JTEncode::jt65_encode(const char * msg, uint8_t * symbols) 67 | { 68 | char message[14]; 69 | memset(message, 0, 14); 70 | strcpy(message, msg); 71 | 72 | // Ensure that the message text conforms to standards 73 | // -------------------------------------------------- 74 | jt_message_prep(message); 75 | 76 | // Bit packing 77 | // ----------- 78 | uint8_t c[12]; 79 | jt65_bit_packing(message, c); 80 | 81 | // Reed-Solomon encoding 82 | // --------------------- 83 | uint8_t s[JT65_ENCODE_COUNT]; 84 | rs_encode(c, s); 85 | 86 | // Interleaving 87 | // ------------ 88 | jt65_interleave(s); 89 | 90 | // Gray Code 91 | // --------- 92 | jt_gray_code(s, JT65_ENCODE_COUNT); 93 | 94 | // Merge with sync vector 95 | // ---------------------- 96 | jt65_merge_sync_vector(s, symbols); 97 | } 98 | 99 | /* 100 | * jt9_encode(const char * message, uint8_t * symbols) 101 | * 102 | * Takes an arbitrary message of up to 13 allowable characters and returns 103 | * a channel symbol table. 104 | * 105 | * message - Plaintext Type 6 message. 106 | * symbols - Array of channel symbols to transmit returned by the method. 107 | * Ensure that you pass a uint8_t array of at least size JT9_SYMBOL_COUNT to the method. 108 | * 109 | */ 110 | void JTEncode::jt9_encode(const char * msg, uint8_t * symbols) 111 | { 112 | char message[14]; 113 | memset(message, 0, 14); 114 | strcpy(message, msg); 115 | 116 | // Ensure that the message text conforms to standards 117 | // -------------------------------------------------- 118 | jt_message_prep(message); 119 | 120 | // Bit packing 121 | // ----------- 122 | uint8_t c[13]; 123 | jt9_bit_packing(message, c); 124 | 125 | // Convolutional Encoding 126 | // --------------------- 127 | uint8_t s[JT9_BIT_COUNT]; 128 | convolve(c, s, 13, JT9_BIT_COUNT); 129 | 130 | // Interleaving 131 | // ------------ 132 | jt9_interleave(s); 133 | 134 | // Pack into 3-bit symbols 135 | // ----------------------- 136 | uint8_t a[JT9_ENCODE_COUNT]; 137 | jt9_packbits(s, a); 138 | 139 | // Gray Code 140 | // --------- 141 | jt_gray_code(a, JT9_ENCODE_COUNT); 142 | 143 | // Merge with sync vector 144 | // ---------------------- 145 | jt9_merge_sync_vector(a, symbols); 146 | } 147 | 148 | /* 149 | * jt4_encode(const char * message, uint8_t * symbols) 150 | * 151 | * Takes an arbitrary message of up to 13 allowable characters and returns 152 | * a channel symbol table. 153 | * 154 | * message - Plaintext Type 6 message. 155 | * symbols - Array of channel symbols to transmit returned by the method. 156 | * Ensure that you pass a uint8_t array of at least size JT9_SYMBOL_COUNT to the method. 157 | * 158 | */ 159 | void JTEncode::jt4_encode(const char * msg, uint8_t * symbols) 160 | { 161 | char message[14]; 162 | memset(message, 0, 14); 163 | strcpy(message, msg); 164 | 165 | // Ensure that the message text conforms to standards 166 | // -------------------------------------------------- 167 | jt_message_prep(message); 168 | 169 | // Bit packing 170 | // ----------- 171 | uint8_t c[13]; 172 | jt9_bit_packing(message, c); 173 | 174 | // Convolutional Encoding 175 | // --------------------- 176 | uint8_t s[JT4_SYMBOL_COUNT]; 177 | convolve(c, s, 13, JT4_BIT_COUNT); 178 | 179 | // Interleaving 180 | // ------------ 181 | jt9_interleave(s); 182 | memmove(s + 1, s, JT4_BIT_COUNT); 183 | s[0] = 0; // Append a 0 bit to start of sequence 184 | 185 | // Merge with sync vector 186 | // ---------------------- 187 | jt4_merge_sync_vector(s, symbols); 188 | } 189 | 190 | /* 191 | * wspr_encode(const char * call, const char * loc, const uint8_t dbm, uint8_t * symbols) 192 | * 193 | * Takes a callsign, grid locator, and power level and returns a WSPR symbol 194 | * table for a Type 1, 2, or 3 message. 195 | * 196 | * call - Callsign (12 characters maximum). 197 | * loc - Maidenhead grid locator (6 characters maximum). 198 | * dbm - Output power in dBm. 199 | * symbols - Array of channel symbols to transmit returned by the method. 200 | * Ensure that you pass a uint8_t array of at least size WSPR_SYMBOL_COUNT to the method. 201 | * 202 | */ 203 | void JTEncode::wspr_encode(const char * call, const char * loc, const int8_t dbm, uint8_t * symbols) 204 | { 205 | char call_[13]; 206 | char loc_[7]; 207 | uint8_t dbm_ = dbm; 208 | strcpy(call_, call); 209 | strcpy(loc_, loc); 210 | 211 | // Ensure that the message text conforms to standards 212 | // -------------------------------------------------- 213 | wspr_message_prep(call_, loc_, dbm_); 214 | 215 | // Bit packing 216 | // ----------- 217 | uint8_t c[11]; 218 | wspr_bit_packing(c); 219 | 220 | // Convolutional Encoding 221 | // --------------------- 222 | uint8_t s[WSPR_SYMBOL_COUNT]; 223 | convolve(c, s, 11, WSPR_BIT_COUNT); 224 | 225 | // Interleaving 226 | // ------------ 227 | wspr_interleave(s); 228 | 229 | // Merge with sync vector 230 | // ---------------------- 231 | wspr_merge_sync_vector(s, symbols); 232 | } 233 | 234 | /* 235 | * fsq_encode(const char * from_call, const char * message, uint8_t * symbols) 236 | * 237 | * Takes an arbitrary message and returns a FSQ channel symbol table. 238 | * 239 | * from_call - Callsign of issuing station (maximum size: 20) 240 | * message - Null-terminated message string, no greater than 130 chars in length 241 | * symbols - Array of channel symbols to transmit returned by the method. 242 | * Ensure that you pass a uint8_t array of at least the size of the message 243 | * plus 5 characters to the method. Terminated in 0xFF. 244 | * 245 | */ 246 | void JTEncode::fsq_encode(const char * from_call, const char * message, uint8_t * symbols) 247 | { 248 | char tx_buffer[155]; 249 | char * tx_message; 250 | uint16_t symbol_pos = 0; 251 | uint8_t i, fch, vcode1, vcode2, tone; 252 | uint8_t cur_tone = 0; 253 | 254 | // Clear out the transmit buffer 255 | // ----------------------------- 256 | memset(tx_buffer, 0, 155); 257 | 258 | // Create the message to be transmitted 259 | // ------------------------------------ 260 | sprintf(tx_buffer, " \n%s: %s", from_call, message); 261 | 262 | tx_message = tx_buffer; 263 | 264 | // Iterate through the message and encode 265 | // -------------------------------------- 266 | while(*tx_message != '\0') 267 | { 268 | for(i = 0; i < NGLYPHS; i++) 269 | { 270 | uint8_t ch = (uint8_t)*tx_message; 271 | 272 | // Check each element of the varicode table to see if we've found the 273 | // character we're trying to send. 274 | fch = pgm_read_byte(&fsq_code_table[i].ch); 275 | 276 | if(fch == ch) 277 | { 278 | // Found the character, now fetch the varicode chars 279 | vcode1 = pgm_read_byte(&(fsq_code_table[i].var[0])); 280 | vcode2 = pgm_read_byte(&(fsq_code_table[i].var[1])); 281 | 282 | // Transmit the appropriate tone per a varicode char 283 | if(vcode2 == 0) 284 | { 285 | // If the 2nd varicode char is a 0 in the table, 286 | // we are transmitting a lowercase character, and thus 287 | // only transmit one tone for this character. 288 | 289 | // Generate tone 290 | cur_tone = ((cur_tone + vcode1 + 1) % 33); 291 | symbols[symbol_pos++] = cur_tone; 292 | } 293 | else 294 | { 295 | // If the 2nd varicode char is anything other than 0 in 296 | // the table, then we need to transmit both 297 | 298 | // Generate 1st tone 299 | cur_tone = ((cur_tone + vcode1 + 1) % 33); 300 | symbols[symbol_pos++] = cur_tone; 301 | 302 | // Generate 2nd tone 303 | cur_tone = ((cur_tone + vcode2 + 1) % 33); 304 | symbols[symbol_pos++] = cur_tone; 305 | } 306 | break; // We've found and transmitted the char, 307 | // so exit the for loop 308 | } 309 | } 310 | 311 | tx_message++; 312 | } 313 | 314 | // Message termination 315 | // ---------------- 316 | symbols[symbol_pos] = 0xff; 317 | } 318 | 319 | /* 320 | * fsq_dir_encode(const char * from_call, const char * to_call, const char cmd, const char * message, uint8_t * symbols) 321 | * 322 | * Takes an arbitrary message and returns a FSQ channel symbol table. 323 | * 324 | * from_call - Callsign from which message is directed (maximum size: 20) 325 | * to_call - Callsign to which message is directed (maximum size: 20) 326 | * cmd - Directed command 327 | * message - Null-terminated message string, no greater than 100 chars in length 328 | * symbols - Array of channel symbols to transmit returned by the method. 329 | * Ensure that you pass a uint8_t array of at least the size of the message 330 | * plus 5 characters to the method. Terminated in 0xFF. 331 | * 332 | */ 333 | void JTEncode::fsq_dir_encode(const char * from_call, const char * to_call, const char cmd, const char * message, uint8_t * symbols) 334 | { 335 | char tx_buffer[155]; 336 | char * tx_message; 337 | uint16_t symbol_pos = 0; 338 | uint8_t i, fch, vcode1, vcode2, tone, from_call_crc; 339 | uint8_t cur_tone = 0; 340 | 341 | // Generate a CRC on from_call 342 | // --------------------------- 343 | from_call_crc = crc8(from_call); 344 | 345 | // Clear out the transmit buffer 346 | // ----------------------------- 347 | memset(tx_buffer, 0, 155); 348 | 349 | // Create the message to be transmitted 350 | // We are building a directed message here. 351 | // FSQ very specifically needs " \b " in 352 | // directed mode to indicate EOT. A single backspace won't do it. 353 | sprintf(tx_buffer, " \n%s:%02x%s%c%s%s", from_call, from_call_crc, to_call, cmd, message, " \b "); 354 | 355 | tx_message = tx_buffer; 356 | 357 | // Iterate through the message and encode 358 | // -------------------------------------- 359 | while(*tx_message != '\0') 360 | { 361 | for(i = 0; i < NGLYPHS; i++) 362 | { 363 | uint8_t ch = (uint8_t)*tx_message; 364 | 365 | // Check each element of the varicode table to see if we've found the 366 | // character we're trying to send. 367 | fch = pgm_read_byte(&fsq_code_table[i].ch); 368 | 369 | if(fch == ch) 370 | { 371 | // Found the character, now fetch the varicode chars 372 | vcode1 = pgm_read_byte(&(fsq_code_table[i].var[0])); 373 | vcode2 = pgm_read_byte(&(fsq_code_table[i].var[1])); 374 | 375 | // Transmit the appropriate tone per a varicode char 376 | if(vcode2 == 0) 377 | { 378 | // If the 2nd varicode char is a 0 in the table, 379 | // we are transmitting a lowercase character, and thus 380 | // only transmit one tone for this character. 381 | 382 | // Generate tone 383 | cur_tone = ((cur_tone + vcode1 + 1) % 33); 384 | symbols[symbol_pos++] = cur_tone; 385 | } 386 | else 387 | { 388 | // If the 2nd varicode char is anything other than 0 in 389 | // the table, then we need to transmit both 390 | 391 | // Generate 1st tone 392 | cur_tone = ((cur_tone + vcode1 + 1) % 33); 393 | symbols[symbol_pos++] = cur_tone; 394 | 395 | // Generate 2nd tone 396 | cur_tone = ((cur_tone + vcode2 + 1) % 33); 397 | symbols[symbol_pos++] = cur_tone; 398 | } 399 | break; // We've found and transmitted the char, 400 | // so exit the for loop 401 | } 402 | } 403 | 404 | tx_message++; 405 | } 406 | 407 | // Message termination 408 | // ---------------- 409 | symbols[symbol_pos] = 0xff; 410 | } 411 | 412 | /* 413 | * ft8_encode(const char * message, uint8_t * symbols) 414 | * 415 | * Takes an arbitrary message of up to 13 allowable characters or a telemetry message 416 | * of up to 18 hexadecimal digit (in string format) and returns a channel symbol table. 417 | * Encoded for the FT8 protocol used in WSJT-X v2.0 and beyond (79 channel symbols). 418 | * 419 | * message - Type 0.0 free text message or Type 0.5 telemetry message. 420 | * symbols - Array of channel symbols to transmit returned by the method. 421 | * Ensure that you pass a uint8_t array of at least size FT8_SYMBOL_COUNT to the method. 422 | * 423 | */ 424 | void JTEncode::ft8_encode(const char * msg, uint8_t * symbols) 425 | { 426 | uint8_t i; 427 | 428 | char message[19]; 429 | memset(message, 0, 19); 430 | strcpy(message, msg); 431 | 432 | // Bit packing 433 | // ----------- 434 | uint8_t c[77]; 435 | memset(c, 0, 77); 436 | ft8_bit_packing(message, c); 437 | 438 | // Message Encoding 439 | // ---------------- 440 | uint8_t s[FT8_BIT_COUNT]; 441 | ft8_encode(c, s); 442 | 443 | // Merge with sync vector 444 | // ---------------------- 445 | ft8_merge_sync_vector(s, symbols); 446 | } 447 | 448 | /* 449 | * latlon_to_grid(float lat, float lon, char* ret_grid) 450 | * 451 | * Takes a station latitude and longitude provided in decimal degrees format and 452 | * returns a string with the 6-digit Maidenhead grid designator. 453 | * 454 | * lat - Latitude in decimal degrees format. 455 | * lon - Longitude in decimal degrees format. 456 | * ret_grid - Derived Maidenhead grid square. A pointer to a character array of 457 | * at least 7 bytes must be provided here for the function return value. 458 | * 459 | */ 460 | void JTEncode::latlon_to_grid(float lat, float lon, char* ret_grid) 461 | { 462 | char grid[7]; 463 | memset(grid, 0, 7); 464 | 465 | // Bounds checks 466 | if(lat < -90.0) { 467 | lat = -90.0; 468 | } 469 | if(lat > 90.0) { 470 | lat = 90.0; 471 | } 472 | if(lon < -180.0) { 473 | lon = -180.0; 474 | } 475 | if(lon > 180.0) { 476 | lon = 180.0; 477 | } 478 | 479 | // Normalize lat and lon 480 | lon += 180.0; 481 | lat += 90.0; 482 | 483 | // Derive first coordinate pair 484 | grid[0] = (char)((uint8_t)(lon / 20) + 'A'); 485 | grid[1] = (char)((uint8_t)(lat / 10) + 'A'); 486 | 487 | // Derive second coordinate pair 488 | lon = lon - ((uint8_t)(lon / 20) * 20); 489 | lat = lat - ((uint8_t)(lat / 10) * 10); 490 | grid[2] = (char)((uint8_t)(lon / 2) + '0'); 491 | grid[3] = (char)((uint8_t)(lat) + '0'); 492 | 493 | // Derive third coordinate pair 494 | lon = lon - ((uint8_t)(lon / 2) * 2); 495 | lat = lat - ((uint8_t)(lat)); 496 | grid[4] = (char)((uint8_t)(lon * 12) + 'a'); 497 | grid[5] = (char)((uint8_t)(lat * 24) + 'a'); 498 | 499 | strncpy(ret_grid, grid, 6); 500 | } 501 | 502 | /* Private Class Members */ 503 | 504 | uint8_t JTEncode::jt_code(char c) 505 | { 506 | // Validate the input then return the proper integer code. 507 | // Return 255 as an error code if the char is not allowed. 508 | 509 | if(isdigit(c)) 510 | { 511 | return (uint8_t)(c - 48); 512 | } 513 | else if(c >= 'A' && c <= 'Z') 514 | { 515 | return (uint8_t)(c - 55); 516 | } 517 | else if(c == ' ') 518 | { 519 | return 36; 520 | } 521 | else if(c == '+') 522 | { 523 | return 37; 524 | } 525 | else if(c == '-') 526 | { 527 | return 38; 528 | } 529 | else if(c == '.') 530 | { 531 | return 39; 532 | } 533 | else if(c == '/') 534 | { 535 | return 40; 536 | } 537 | else if(c == '?') 538 | { 539 | return 41; 540 | } 541 | else 542 | { 543 | return 255; 544 | } 545 | } 546 | 547 | uint8_t JTEncode::ft_code(char c) 548 | { 549 | /* Validate the input then return the proper integer code */ 550 | // Return 255 as an error code if the char is not allowed 551 | 552 | if(isdigit(c)) 553 | { 554 | return (uint8_t)(c) - 47; 555 | } 556 | else if(c >= 'A' && c <= 'Z') 557 | { 558 | return (uint8_t)(c) - 54; 559 | } 560 | else if(c == ' ') 561 | { 562 | return 0; 563 | } 564 | else if(c == '+') 565 | { 566 | return 37; 567 | } 568 | else if(c == '-') 569 | { 570 | return 38; 571 | } 572 | else if(c == '.') 573 | { 574 | return 39; 575 | } 576 | else if(c == '/') 577 | { 578 | return 40; 579 | } 580 | else if(c == '?') 581 | { 582 | return 41; 583 | } 584 | else 585 | { 586 | return 255; 587 | } 588 | } 589 | 590 | uint8_t JTEncode::wspr_code(char c) 591 | { 592 | // Validate the input then return the proper integer code. 593 | // Change character to a space if the char is not allowed. 594 | 595 | if(isdigit(c)) 596 | { 597 | return (uint8_t)(c - 48); 598 | } 599 | else if(c == ' ') 600 | { 601 | return 36; 602 | } 603 | else if(c >= 'A' && c <= 'Z') 604 | { 605 | return (uint8_t)(c - 55); 606 | } 607 | else 608 | { 609 | return 36; 610 | } 611 | } 612 | 613 | uint8_t JTEncode::gray_code(uint8_t c) 614 | { 615 | return (c >> 1) ^ c; 616 | } 617 | 618 | int8_t JTEncode::hex2int(char ch) 619 | { 620 | if (ch >= '0' && ch <= '9') 621 | return ch - '0'; 622 | if (ch >= 'A' && ch <= 'F') 623 | return ch - 'A' + 10; 624 | if (ch >= 'a' && ch <= 'f') 625 | return ch - 'a' + 10; 626 | return -1; 627 | } 628 | 629 | void JTEncode::jt_message_prep(char * message) 630 | { 631 | uint8_t i; 632 | 633 | // Pad the message with trailing spaces 634 | uint8_t len = strlen(message); 635 | 636 | for(i = len; i < 13; i++) 637 | { 638 | message[i] = ' '; 639 | } 640 | 641 | // Convert all chars to uppercase 642 | for(i = 0; i < 13; i++) 643 | { 644 | if(islower(message[i])) 645 | { 646 | message[i] = toupper(message[i]); 647 | } 648 | } 649 | } 650 | 651 | void JTEncode::ft_message_prep(char * message) 652 | { 653 | uint8_t i; 654 | char temp_msg[14]; 655 | 656 | snprintf(temp_msg, 14, "%13s", message); 657 | 658 | // Convert all chars to uppercase 659 | for(i = 0; i < 13; i++) 660 | { 661 | if(islower(temp_msg[i])) 662 | { 663 | temp_msg[i] = toupper(temp_msg[i]); 664 | } 665 | } 666 | 667 | strcpy(message, temp_msg); 668 | } 669 | 670 | void JTEncode::wspr_message_prep(char * call, char * loc, int8_t dbm) 671 | { 672 | // Callsign validation and padding 673 | // ------------------------------- 674 | 675 | // Ensure that the only allowed characters are digits, uppercase letters, slash, and angle brackets 676 | uint8_t i; 677 | for(i = 0; i < 12; i++) 678 | { 679 | if(call[i] != '/' && call[i] != '<' && call[i] != '>') 680 | { 681 | call[i] = toupper(call[i]); 682 | if(!(isdigit(call[i]) || isupper(call[i]))) 683 | { 684 | call[i] = ' '; 685 | } 686 | } 687 | } 688 | call[12] = 0; 689 | 690 | strncpy(callsign, call, 12); 691 | 692 | // Grid locator validation 693 | if(strlen(loc) == 4 || strlen(loc) == 6) 694 | { 695 | for(i = 0; i <= 1; i++) 696 | { 697 | loc[i] = toupper(loc[i]); 698 | if((loc[i] < 'A' || loc[i] > 'R')) 699 | { 700 | strncpy(loc, "AA00AA", 7); 701 | } 702 | } 703 | for(i = 2; i <= 3; i++) 704 | { 705 | if(!(isdigit(loc[i]))) 706 | { 707 | strncpy(loc, "AA00AA", 7); 708 | } 709 | } 710 | } 711 | else 712 | { 713 | strncpy(loc, "AA00AA", 7); 714 | } 715 | 716 | if(strlen(loc) == 6) 717 | { 718 | for(i = 4; i <= 5; i++) 719 | { 720 | loc[i] = toupper(loc[i]); 721 | if((loc[i] < 'A' || loc[i] > 'X')) 722 | { 723 | strncpy(loc, "AA00AA", 7); 724 | } 725 | } 726 | } 727 | 728 | strncpy(locator, loc, 7); 729 | 730 | // Power level validation 731 | // Only certain increments are allowed 732 | if(dbm > 60) 733 | { 734 | dbm = 60; 735 | } 736 | const uint8_t VALID_DBM_SIZE = 28; 737 | const int8_t valid_dbm[VALID_DBM_SIZE] = 738 | {-30, -27, -23, -20, -17, -13, -10, -7, -3, 739 | 0, 3, 7, 10, 13, 17, 20, 23, 27, 30, 33, 37, 40, 740 | 43, 47, 50, 53, 57, 60}; 741 | for(i = 0; i < VALID_DBM_SIZE; i++) 742 | { 743 | if(dbm == valid_dbm[i]) 744 | { 745 | power = dbm; 746 | } 747 | } 748 | // If we got this far, we have an invalid power level, so we'll round down 749 | for(i = 1; i < VALID_DBM_SIZE; i++) 750 | { 751 | if(dbm < valid_dbm[i] && dbm >= valid_dbm[i - 1]) 752 | { 753 | power = valid_dbm[i - 1]; 754 | } 755 | } 756 | } 757 | 758 | void JTEncode::jt65_bit_packing(char * message, uint8_t * c) 759 | { 760 | uint32_t n1, n2, n3; 761 | 762 | // Find the N values 763 | n1 = jt_code(message[0]); 764 | n1 = n1 * 42 + jt_code(message[1]); 765 | n1 = n1 * 42 + jt_code(message[2]); 766 | n1 = n1 * 42 + jt_code(message[3]); 767 | n1 = n1 * 42 + jt_code(message[4]); 768 | 769 | n2 = jt_code(message[5]); 770 | n2 = n2 * 42 + jt_code(message[6]); 771 | n2 = n2 * 42 + jt_code(message[7]); 772 | n2 = n2 * 42 + jt_code(message[8]); 773 | n2 = n2 * 42 + jt_code(message[9]); 774 | 775 | n3 = jt_code(message[10]); 776 | n3 = n3 * 42 + jt_code(message[11]); 777 | n3 = n3 * 42 + jt_code(message[12]); 778 | 779 | // Pack bits 15 and 16 of N3 into N1 and N2, 780 | // then mask reset of N3 bits 781 | n1 = (n1 << 1) + ((n3 >> 15) & 1); 782 | n2 = (n2 << 1) + ((n3 >> 16) & 1); 783 | n3 = n3 & 0x7fff; 784 | 785 | // Set the freeform message flag 786 | n3 += 32768; 787 | 788 | c[0] = (n1 >> 22) & 0x003f; 789 | c[1] = (n1 >> 16) & 0x003f; 790 | c[2] = (n1 >> 10) & 0x003f; 791 | c[3] = (n1 >> 4) & 0x003f; 792 | c[4] = ((n1 & 0x000f) << 2) + ((n2 >> 26) & 0x0003); 793 | c[5] = (n2 >> 20) & 0x003f; 794 | c[6] = (n2 >> 14) & 0x003f; 795 | c[7] = (n2 >> 8) & 0x003f; 796 | c[8] = (n2 >> 2) & 0x003f; 797 | c[9] = ((n2 & 0x0003) << 4) + ((n3 >> 12) & 0x000f); 798 | c[10] = (n3 >> 6) & 0x003f; 799 | c[11] = n3 & 0x003f; 800 | } 801 | 802 | void JTEncode::jt9_bit_packing(char * message, uint8_t * c) 803 | { 804 | uint32_t n1, n2, n3; 805 | 806 | // Find the N values 807 | n1 = jt_code(message[0]); 808 | n1 = n1 * 42 + jt_code(message[1]); 809 | n1 = n1 * 42 + jt_code(message[2]); 810 | n1 = n1 * 42 + jt_code(message[3]); 811 | n1 = n1 * 42 + jt_code(message[4]); 812 | 813 | n2 = jt_code(message[5]); 814 | n2 = n2 * 42 + jt_code(message[6]); 815 | n2 = n2 * 42 + jt_code(message[7]); 816 | n2 = n2 * 42 + jt_code(message[8]); 817 | n2 = n2 * 42 + jt_code(message[9]); 818 | 819 | n3 = jt_code(message[10]); 820 | n3 = n3 * 42 + jt_code(message[11]); 821 | n3 = n3 * 42 + jt_code(message[12]); 822 | 823 | // Pack bits 15 and 16 of N3 into N1 and N2, 824 | // then mask reset of N3 bits 825 | n1 = (n1 << 1) + ((n3 >> 15) & 1); 826 | n2 = (n2 << 1) + ((n3 >> 16) & 1); 827 | n3 = n3 & 0x7fff; 828 | 829 | // Set the freeform message flag 830 | n3 += 32768; 831 | 832 | // 71 message bits to pack, plus 1 bit flag for freeform message. 833 | // 31 zero bits appended to end. 834 | // N1 and N2 are 28 bits each, N3 is 16 bits 835 | // A little less work to start with the least-significant bits 836 | c[3] = (uint8_t)((n1 & 0x0f) << 4); 837 | n1 = n1 >> 4; 838 | c[2] = (uint8_t)(n1 & 0xff); 839 | n1 = n1 >> 8; 840 | c[1] = (uint8_t)(n1 & 0xff); 841 | n1 = n1 >> 8; 842 | c[0] = (uint8_t)(n1 & 0xff); 843 | 844 | c[6] = (uint8_t)(n2 & 0xff); 845 | n2 = n2 >> 8; 846 | c[5] = (uint8_t)(n2 & 0xff); 847 | n2 = n2 >> 8; 848 | c[4] = (uint8_t)(n2 & 0xff); 849 | n2 = n2 >> 8; 850 | c[3] |= (uint8_t)(n2 & 0x0f); 851 | 852 | c[8] = (uint8_t)(n3 & 0xff); 853 | n3 = n3 >> 8; 854 | c[7] = (uint8_t)(n3 & 0xff); 855 | 856 | c[9] = 0; 857 | c[10] = 0; 858 | c[11] = 0; 859 | c[12] = 0; 860 | } 861 | 862 | void JTEncode::wspr_bit_packing(uint8_t * c) 863 | { 864 | uint32_t n, m; 865 | 866 | // Determine if type 1, 2 or 3 message 867 | char* slash_avail = strchr(callsign, (int)'/'); 868 | if(callsign[0] == '<') 869 | { 870 | // Type 3 message 871 | char base_call[13]; 872 | memset(base_call, 0, 13); 873 | uint32_t init_val = 146; 874 | char* bracket_avail = strchr(callsign, (int)'>'); 875 | int call_len = bracket_avail - callsign - 1; 876 | strncpy(base_call, callsign + 1, call_len); 877 | uint32_t hash = nhash_(base_call, &call_len, &init_val); 878 | hash &= 32767; 879 | 880 | // Convert 6 char grid square to "callsign" format for transmission 881 | // by putting the first character at the end 882 | char temp_loc = locator[0]; 883 | locator[0] = locator[1]; 884 | locator[1] = locator[2]; 885 | locator[2] = locator[3]; 886 | locator[3] = locator[4]; 887 | locator[4] = locator[5]; 888 | locator[5] = temp_loc; 889 | 890 | n = wspr_code(locator[0]); 891 | n = n * 36 + wspr_code(locator[1]); 892 | n = n * 10 + wspr_code(locator[2]); 893 | n = n * 27 + (wspr_code(locator[3]) - 10); 894 | n = n * 27 + (wspr_code(locator[4]) - 10); 895 | n = n * 27 + (wspr_code(locator[5]) - 10); 896 | 897 | m = (hash * 128) - (power + 1) + 64; 898 | } 899 | else if(slash_avail == (void *)0) 900 | { 901 | // Type 1 message 902 | pad_callsign(callsign); 903 | n = wspr_code(callsign[0]); 904 | n = n * 36 + wspr_code(callsign[1]); 905 | n = n * 10 + wspr_code(callsign[2]); 906 | n = n * 27 + (wspr_code(callsign[3]) - 10); 907 | n = n * 27 + (wspr_code(callsign[4]) - 10); 908 | n = n * 27 + (wspr_code(callsign[5]) - 10); 909 | 910 | m = ((179 - 10 * (locator[0] - 'A') - (locator[2] - '0')) * 180) + 911 | (10 * (locator[1] - 'A')) + (locator[3] - '0'); 912 | m = (m * 128) + power + 64; 913 | } 914 | else if(slash_avail) 915 | { 916 | // Type 2 message 917 | int slash_pos = slash_avail - callsign; 918 | uint8_t i; 919 | 920 | // Determine prefix or suffix 921 | if(callsign[slash_pos + 2] == ' ' || callsign[slash_pos + 2] == 0) 922 | { 923 | // Single character suffix 924 | char base_call[7]; 925 | memset(base_call, 0, 7); 926 | strncpy(base_call, callsign, slash_pos); 927 | for(i = 0; i < 7; i++) 928 | { 929 | base_call[i] = toupper(base_call[i]); 930 | if(!(isdigit(base_call[i]) || isupper(base_call[i]))) 931 | { 932 | base_call[i] = ' '; 933 | } 934 | } 935 | pad_callsign(base_call); 936 | 937 | n = wspr_code(base_call[0]); 938 | n = n * 36 + wspr_code(base_call[1]); 939 | n = n * 10 + wspr_code(base_call[2]); 940 | n = n * 27 + (wspr_code(base_call[3]) - 10); 941 | n = n * 27 + (wspr_code(base_call[4]) - 10); 942 | n = n * 27 + (wspr_code(base_call[5]) - 10); 943 | 944 | char x = callsign[slash_pos + 1]; 945 | if(x >= 48 && x <= 57) 946 | { 947 | x -= 48; 948 | } 949 | else if(x >= 65 && x <= 90) 950 | { 951 | x -= 55; 952 | } 953 | else 954 | { 955 | x = 38; 956 | } 957 | 958 | m = 60000 - 32768 + x; 959 | 960 | m = (m * 128) + power + 2 + 64; 961 | } 962 | else if(callsign[slash_pos + 3] == ' ' || callsign[slash_pos + 3] == 0) 963 | { 964 | // Two-digit numerical suffix 965 | char base_call[7]; 966 | memset(base_call, 0, 7); 967 | strncpy(base_call, callsign, slash_pos); 968 | for(i = 0; i < 6; i++) 969 | { 970 | base_call[i] = toupper(base_call[i]); 971 | if(!(isdigit(base_call[i]) || isupper(base_call[i]))) 972 | { 973 | base_call[i] = ' '; 974 | } 975 | } 976 | pad_callsign(base_call); 977 | 978 | n = wspr_code(base_call[0]); 979 | n = n * 36 + wspr_code(base_call[1]); 980 | n = n * 10 + wspr_code(base_call[2]); 981 | n = n * 27 + (wspr_code(base_call[3]) - 10); 982 | n = n * 27 + (wspr_code(base_call[4]) - 10); 983 | n = n * 27 + (wspr_code(base_call[5]) - 10); 984 | 985 | // TODO: needs validation of digit 986 | m = 10 * (callsign[slash_pos + 1] - 48) + callsign[slash_pos + 2] - 48; 987 | m = 60000 + 26 + m; 988 | m = (m * 128) + power + 2 + 64; 989 | } 990 | else 991 | { 992 | // Prefix 993 | char prefix[4]; 994 | char base_call[7]; 995 | memset(prefix, 0, 4); 996 | memset(base_call, 0, 7); 997 | strncpy(prefix, callsign, slash_pos); 998 | strncpy(base_call, callsign + slash_pos + 1, 7); 999 | 1000 | if(prefix[2] == ' ' || prefix[2] == 0) 1001 | { 1002 | // Right align prefix 1003 | prefix[3] = 0; 1004 | prefix[2] = prefix[1]; 1005 | prefix[1] = prefix[0]; 1006 | prefix[0] = ' '; 1007 | } 1008 | 1009 | for(uint8_t i = 0; i < 6; i++) 1010 | { 1011 | base_call[i] = toupper(base_call[i]); 1012 | if(!(isdigit(base_call[i]) || isupper(base_call[i]))) 1013 | { 1014 | base_call[i] = ' '; 1015 | } 1016 | } 1017 | pad_callsign(base_call); 1018 | 1019 | n = wspr_code(base_call[0]); 1020 | n = n * 36 + wspr_code(base_call[1]); 1021 | n = n * 10 + wspr_code(base_call[2]); 1022 | n = n * 27 + (wspr_code(base_call[3]) - 10); 1023 | n = n * 27 + (wspr_code(base_call[4]) - 10); 1024 | n = n * 27 + (wspr_code(base_call[5]) - 10); 1025 | 1026 | m = 0; 1027 | for(uint8_t i = 0; i < 3; ++i) 1028 | { 1029 | m = 37 * m + wspr_code(prefix[i]); 1030 | } 1031 | 1032 | if(m >= 32768) 1033 | { 1034 | m -= 32768; 1035 | m = (m * 128) + power + 2 + 64; 1036 | } 1037 | else 1038 | { 1039 | m = (m * 128) + power + 1 + 64; 1040 | } 1041 | } 1042 | } 1043 | 1044 | // Callsign is 28 bits, locator/power is 22 bits. 1045 | // A little less work to start with the least-significant bits 1046 | c[3] = (uint8_t)((n & 0x0f) << 4); 1047 | n = n >> 4; 1048 | c[2] = (uint8_t)(n & 0xff); 1049 | n = n >> 8; 1050 | c[1] = (uint8_t)(n & 0xff); 1051 | n = n >> 8; 1052 | c[0] = (uint8_t)(n & 0xff); 1053 | 1054 | c[6] = (uint8_t)((m & 0x03) << 6); 1055 | m = m >> 2; 1056 | c[5] = (uint8_t)(m & 0xff); 1057 | m = m >> 8; 1058 | c[4] = (uint8_t)(m & 0xff); 1059 | m = m >> 8; 1060 | c[3] |= (uint8_t)(m & 0x0f); 1061 | c[7] = 0; 1062 | c[8] = 0; 1063 | c[9] = 0; 1064 | c[10] = 0; 1065 | } 1066 | 1067 | void JTEncode::ft8_bit_packing(char* message, uint8_t* codeword) 1068 | { 1069 | // Just encoding type 0 free text and type 0.5 telemetry for now 1070 | 1071 | // The bit packing algorithm is: 1072 | // sum(message(pos) * 42^pos) 1073 | 1074 | uint8_t i3 = 0; 1075 | uint8_t n3 = 0; 1076 | uint8_t qa[10]; 1077 | uint8_t qb[10]; 1078 | char c18[19]; 1079 | bool telem = false; 1080 | char temp_msg[19]; 1081 | memset(qa, 0, 10); 1082 | memset(qb, 0, 10); 1083 | 1084 | uint8_t i, j, x, i0; 1085 | uint32_t ireg = 0; 1086 | 1087 | // See if this is a telemetry message 1088 | // Has to be hex digits, can be no more than 18 1089 | for(i = 0; i < 19; ++i) 1090 | { 1091 | if(message[i] == 0 || message[i] == ' ') 1092 | { 1093 | break; 1094 | } 1095 | else if(hex2int(message[i]) == -1) 1096 | { 1097 | telem = false; 1098 | break; 1099 | } 1100 | else 1101 | { 1102 | c18[i] = message[i]; 1103 | telem = true; 1104 | } 1105 | } 1106 | 1107 | // If telemetry 1108 | if(telem) 1109 | { 1110 | // Get the first 18 hex digits 1111 | for(i = 0; i < strlen(message); ++i) 1112 | { 1113 | i0 = i; 1114 | if(message[i] == ' ') 1115 | { 1116 | --i0; 1117 | break; 1118 | } 1119 | } 1120 | 1121 | memset(c18, 0, 19); 1122 | memmove(c18, message, i0 + 1); 1123 | snprintf(temp_msg, 19, "%*s", 18, c18); 1124 | 1125 | // Convert all chars to uppercase 1126 | for(i = 0; i < strlen(temp_msg); i++) 1127 | { 1128 | if(islower(temp_msg[i])) 1129 | { 1130 | temp_msg[i] = toupper(temp_msg[i]); 1131 | } 1132 | } 1133 | strcpy(message, temp_msg); 1134 | 1135 | 1136 | uint8_t temp_int; 1137 | temp_int = message[0] == ' ' ? 0 : hex2int(message[0]); 1138 | for(i = 1; i < 4; ++i) 1139 | { 1140 | codeword[i - 1] = (((temp_int << i) & 0x8) >> 3) & 1; 1141 | } 1142 | temp_int = message[1] == ' ' ? 0 : hex2int(message[1]); 1143 | for(i = 0; i < 4; ++i) 1144 | { 1145 | codeword[i + 3] = (((temp_int << i) & 0x8) >> 3) & 1; 1146 | } 1147 | for(i = 0; i < 8; ++i) 1148 | { 1149 | if(message[2 * i + 2] == ' ') 1150 | { 1151 | temp_int = 0; 1152 | } 1153 | else 1154 | { 1155 | temp_int = hex2int(message[2 * i + 2]); 1156 | } 1157 | for(j = 0; j < 4; ++j) 1158 | { 1159 | codeword[(i + 1) * 8 + j - 1] = (((temp_int << j) & 0x8) >> 3) & 1; 1160 | } 1161 | if(message[2 * i + 3] == ' ') 1162 | { 1163 | temp_int = 0; 1164 | } 1165 | else 1166 | { 1167 | temp_int = hex2int(message[2 * i + 3]); 1168 | } 1169 | for(j = 0; j < 4; ++j) 1170 | { 1171 | codeword[(i + 1) * 8 + j + 3] = (((temp_int << j) & 0x8) >> 3) & 1; 1172 | } 1173 | } 1174 | 1175 | i3 = 0; 1176 | n3 = 5; 1177 | } 1178 | else 1179 | { 1180 | ft_message_prep(message); 1181 | 1182 | for(i = 0; i < 13; ++i) 1183 | { 1184 | x = ft_code(message[i]); 1185 | 1186 | // mult 1187 | ireg = 0; 1188 | for(j = 0; j < 9; ++j) 1189 | { 1190 | ireg = (uint8_t)qa[j] * 42 + (uint8_t)((ireg >> 8) & 0xff); 1191 | qb[j] = (uint8_t)(ireg & 0xff); 1192 | } 1193 | qb[9] = (uint8_t)((ireg >> 8) & 0xff); 1194 | 1195 | // add 1196 | ireg = x << 8; 1197 | for(j = 0; j < 9; ++j) 1198 | { 1199 | ireg = (uint8_t)qb[j] + (uint8_t)((ireg >> 8) & 0xff); 1200 | qa[j] = (uint8_t)(ireg & 0xff); 1201 | } 1202 | qa[9] = (uint8_t)((ireg >> 8) & 0xff); 1203 | } 1204 | 1205 | // Format bits to output array 1206 | for(i = 1; i < 8; ++i) 1207 | { 1208 | codeword[i - 1] = (((qa[8] << i) & 0x80) >> 7) & 1; 1209 | } 1210 | for(i = 0; i < 8; ++i) 1211 | { 1212 | for(j = 0; j < 8; ++j) 1213 | { 1214 | codeword[(i + 1) * 8 + j - 1] = (((qa[7 - i] << j) & 0x80) >> 7) & 1; 1215 | } 1216 | } 1217 | } 1218 | 1219 | // Write the message type bits at the end of the array 1220 | for(i = 0; i < 3; ++i) 1221 | { 1222 | codeword[i + 71] = (n3 >> i) & 1; 1223 | } 1224 | for(i = 0; i < 3; ++i) 1225 | { 1226 | codeword[i + 74] = (i3 >> i) & 1; 1227 | } 1228 | } 1229 | 1230 | void JTEncode::jt65_interleave(uint8_t * s) 1231 | { 1232 | uint8_t i, j; 1233 | uint8_t d[JT65_ENCODE_COUNT]; 1234 | 1235 | // Interleave 1236 | for(i = 0; i < 9; i++) 1237 | { 1238 | for(j = 0; j < 7; j++) 1239 | { 1240 | d[(j * 9) + i] = s[(i * 7) + j]; 1241 | } 1242 | } 1243 | 1244 | memcpy(s, d, JT65_ENCODE_COUNT); 1245 | } 1246 | 1247 | void JTEncode::jt9_interleave(uint8_t * s) 1248 | { 1249 | uint8_t i, j; 1250 | uint8_t d[JT9_BIT_COUNT]; 1251 | 1252 | // Do the interleave 1253 | for(i = 0; i < JT9_BIT_COUNT; i++) 1254 | { 1255 | //#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__) || defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega16U4__) 1256 | #if defined(__arm__) 1257 | d[jt9i[i]] = s[i]; 1258 | #else 1259 | j = pgm_read_byte(&jt9i[i]); 1260 | d[j] = s[i]; 1261 | #endif 1262 | } 1263 | 1264 | memcpy(s, d, JT9_BIT_COUNT); 1265 | } 1266 | 1267 | void JTEncode::wspr_interleave(uint8_t * s) 1268 | { 1269 | uint8_t d[WSPR_BIT_COUNT]; 1270 | uint8_t rev, index_temp, i, j, k; 1271 | 1272 | i = 0; 1273 | 1274 | for(j = 0; j < 255; j++) 1275 | { 1276 | // Bit reverse the index 1277 | index_temp = j; 1278 | rev = 0; 1279 | 1280 | for(k = 0; k < 8; k++) 1281 | { 1282 | if(index_temp & 0x01) 1283 | { 1284 | rev = rev | (1 << (7 - k)); 1285 | } 1286 | index_temp = index_temp >> 1; 1287 | } 1288 | 1289 | if(rev < WSPR_BIT_COUNT) 1290 | { 1291 | d[rev] = s[i]; 1292 | i++; 1293 | } 1294 | 1295 | if(i >= WSPR_BIT_COUNT) 1296 | { 1297 | break; 1298 | } 1299 | } 1300 | 1301 | memcpy(s, d, WSPR_BIT_COUNT); 1302 | } 1303 | 1304 | void JTEncode::jt9_packbits(uint8_t * d, uint8_t * a) 1305 | { 1306 | uint8_t i, k; 1307 | k = 0; 1308 | memset(a, 0, JT9_ENCODE_COUNT); 1309 | 1310 | for(i = 0; i < JT9_ENCODE_COUNT; i++) 1311 | { 1312 | a[i] = (d[k] & 1) << 2; 1313 | k++; 1314 | 1315 | a[i] |= ((d[k] & 1) << 1); 1316 | k++; 1317 | 1318 | a[i] |= (d[k] & 1); 1319 | k++; 1320 | } 1321 | } 1322 | 1323 | void JTEncode::jt_gray_code(uint8_t * g, uint8_t symbol_count) 1324 | { 1325 | uint8_t i; 1326 | 1327 | for(i = 0; i < symbol_count; i++) 1328 | { 1329 | g[i] = gray_code(g[i]); 1330 | } 1331 | } 1332 | 1333 | void JTEncode::ft8_encode(uint8_t* codeword, uint8_t* symbols) 1334 | { 1335 | const uint8_t FT8_N = 174; 1336 | const uint8_t FT8_K = 91; 1337 | const uint8_t FT8_M = FT8_N - FT8_K; 1338 | 1339 | uint8_t tempchar[FT8_K]; 1340 | uint8_t message91[FT8_K]; 1341 | uint8_t pchecks[FT8_M]; 1342 | uint8_t i1_msg_bytes[12]; 1343 | uint8_t i, j; 1344 | uint16_t ncrc14; 1345 | 1346 | crc_t crc; 1347 | crc_cfg_t crc_cfg; 1348 | crc_cfg.reflect_in = 0; 1349 | crc_cfg.xor_in = 0; 1350 | crc_cfg.reflect_out = 0; 1351 | crc_cfg.xor_out = 0; 1352 | crc = crc_init(&crc_cfg); 1353 | 1354 | // Add 14-bit CRC to form 91-bit message 1355 | memset(tempchar, 0, 91); 1356 | memcpy(tempchar, codeword, 77); 1357 | tempchar[77] = 0; 1358 | tempchar[78] = 0; 1359 | tempchar[79] = 0; 1360 | memset(i1_msg_bytes, 0, 12); 1361 | for(i = 0; i < 10; ++i) 1362 | { 1363 | for(j = 0; j < 8; ++j) 1364 | { 1365 | i1_msg_bytes[i] <<= 1; 1366 | i1_msg_bytes[i] |= tempchar[i * 8 + j]; 1367 | } 1368 | } 1369 | 1370 | ncrc14 = crc_update(&crc_cfg, crc, (unsigned char *)i1_msg_bytes, 12); 1371 | crc = crc_finalize(&crc_cfg, crc); 1372 | 1373 | for(i = 0; i < 14; ++i) 1374 | { 1375 | if((((ncrc14 << (i + 2)) & 0x8000) >> 15) & 1) 1376 | { 1377 | tempchar[i + 77] = 1; 1378 | } 1379 | else 1380 | { 1381 | tempchar[i + 77] = 0; 1382 | } 1383 | } 1384 | memcpy(message91, tempchar, 91); 1385 | 1386 | for(i = 0; i < FT8_M; ++i) 1387 | { 1388 | uint32_t nsum = 0; 1389 | for(j = 0; j < FT8_K; ++j) 1390 | { 1391 | #if defined(__arm__) 1392 | uint8_t bits = generator_bits[i][j / 8]; 1393 | #else 1394 | uint8_t bits = pgm_read_byte(&(generator_bits[i][j / 8])); 1395 | #endif 1396 | bits <<= (j % 8); 1397 | bits &= 0x80; 1398 | bits >>= 7; 1399 | bits &= 1; 1400 | nsum += (message91[j] * bits); 1401 | } 1402 | pchecks[i] = nsum % 2; 1403 | } 1404 | 1405 | memcpy(symbols, message91, FT8_K); 1406 | memcpy(symbols + FT8_K, pchecks, FT8_M); 1407 | } 1408 | 1409 | void JTEncode::jt65_merge_sync_vector(uint8_t * g, uint8_t * symbols) 1410 | { 1411 | uint8_t i, j = 0; 1412 | const uint8_t sync_vector[JT65_SYMBOL_COUNT] = 1413 | {1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1414 | 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1415 | 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1416 | 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1417 | 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1418 | 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1419 | 1, 1, 1, 1, 1, 1}; 1420 | 1421 | for(i = 0; i < JT65_SYMBOL_COUNT; i++) 1422 | { 1423 | if(sync_vector[i]) 1424 | { 1425 | symbols[i] = 0; 1426 | } 1427 | else 1428 | { 1429 | symbols[i] = g[j] + 2; 1430 | j++; 1431 | } 1432 | } 1433 | } 1434 | 1435 | void JTEncode::jt9_merge_sync_vector(uint8_t * g, uint8_t * symbols) 1436 | { 1437 | uint8_t i, j = 0; 1438 | const uint8_t sync_vector[JT9_SYMBOL_COUNT] = 1439 | {1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1440 | 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1441 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1442 | 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1443 | 0, 0, 1, 0, 1}; 1444 | 1445 | for(i = 0; i < JT9_SYMBOL_COUNT; i++) 1446 | { 1447 | if(sync_vector[i]) 1448 | { 1449 | symbols[i] = 0; 1450 | } 1451 | else 1452 | { 1453 | symbols[i] = g[j] + 1; 1454 | j++; 1455 | } 1456 | } 1457 | } 1458 | 1459 | void JTEncode::jt4_merge_sync_vector(uint8_t * g, uint8_t * symbols) 1460 | { 1461 | uint8_t i; 1462 | const uint8_t sync_vector[JT4_SYMBOL_COUNT] = 1463 | {0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1464 | 0, 0, 0, 0, 1, 1, 0, 0, 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,1 ,0 ,1 ,1, 1465 | 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1466 | 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1467 | 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1468 | 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1469 | 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1470 | 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1471 | 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1472 | 0, 1, 1, 1, 1, 0, 1, 0, 1}; 1473 | 1474 | for(i = 0; i < JT4_SYMBOL_COUNT; i++) 1475 | { 1476 | symbols[i] = sync_vector[i] + (2 * g[i]); 1477 | } 1478 | } 1479 | 1480 | void JTEncode::wspr_merge_sync_vector(uint8_t * g, uint8_t * symbols) 1481 | { 1482 | uint8_t i; 1483 | const uint8_t sync_vector[WSPR_SYMBOL_COUNT] = 1484 | {1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1485 | 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1486 | 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1487 | 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1488 | 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1489 | 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1490 | 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1491 | 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0}; 1492 | 1493 | for(i = 0; i < WSPR_SYMBOL_COUNT; i++) 1494 | { 1495 | symbols[i] = sync_vector[i] + (2 * g[i]); 1496 | } 1497 | } 1498 | 1499 | void JTEncode::ft8_merge_sync_vector(uint8_t* symbols, uint8_t* output) 1500 | { 1501 | const uint8_t costas7x7[7] = {3, 1, 4, 0, 6, 5, 2}; 1502 | const uint8_t graymap[8] = {0, 1, 3, 2, 5, 6, 4, 7}; 1503 | uint8_t i, j, k, idx; 1504 | 1505 | // Insert Costas sync arrays 1506 | memcpy(output, costas7x7, 7); 1507 | memcpy(output + 36, costas7x7, 7); 1508 | memcpy(output + FT8_SYMBOL_COUNT - 7, costas7x7, 7); 1509 | 1510 | k = 6; 1511 | for(j = 0; j < 58; ++j) // 58 data symbols 1512 | { 1513 | i = 3 * j; 1514 | ++k; 1515 | if(j == 29) 1516 | { 1517 | k += 7; 1518 | } 1519 | idx = symbols[i] * 4 + symbols[i + 1] * 2 + symbols[i + 2]; 1520 | output[k] = graymap[idx]; 1521 | } 1522 | } 1523 | 1524 | void JTEncode::convolve(uint8_t * c, uint8_t * s, uint8_t message_size, uint8_t bit_size) 1525 | { 1526 | uint32_t reg_0 = 0; 1527 | uint32_t reg_1 = 0; 1528 | uint32_t reg_temp = 0; 1529 | uint8_t input_bit, parity_bit; 1530 | uint8_t bit_count = 0; 1531 | uint8_t i, j, k; 1532 | 1533 | for(i = 0; i < message_size; i++) 1534 | { 1535 | for(j = 0; j < 8; j++) 1536 | { 1537 | // Set input bit according the MSB of current element 1538 | input_bit = (((c[i] << j) & 0x80) == 0x80) ? 1 : 0; 1539 | 1540 | // Shift both registers and put in the new input bit 1541 | reg_0 = reg_0 << 1; 1542 | reg_1 = reg_1 << 1; 1543 | reg_0 |= (uint32_t)input_bit; 1544 | reg_1 |= (uint32_t)input_bit; 1545 | 1546 | // AND Register 0 with feedback taps, calculate parity 1547 | reg_temp = reg_0 & 0xf2d05351; 1548 | parity_bit = 0; 1549 | for(k = 0; k < 32; k++) 1550 | { 1551 | parity_bit = parity_bit ^ (reg_temp & 0x01); 1552 | reg_temp = reg_temp >> 1; 1553 | } 1554 | s[bit_count] = parity_bit; 1555 | bit_count++; 1556 | 1557 | // AND Register 1 with feedback taps, calculate parity 1558 | reg_temp = reg_1 & 0xe4613c47; 1559 | parity_bit = 0; 1560 | for(k = 0; k < 32; k++) 1561 | { 1562 | parity_bit = parity_bit ^ (reg_temp & 0x01); 1563 | reg_temp = reg_temp >> 1; 1564 | } 1565 | s[bit_count] = parity_bit; 1566 | bit_count++; 1567 | if(bit_count >= bit_size) 1568 | { 1569 | break; 1570 | } 1571 | } 1572 | } 1573 | } 1574 | 1575 | void JTEncode::rs_encode(uint8_t * data, uint8_t * symbols) 1576 | { 1577 | // Adapted from wrapkarn.c in the WSJT-X source code 1578 | uint8_t dat1[12]; 1579 | uint8_t b[51]; 1580 | uint8_t sym[JT65_ENCODE_COUNT]; 1581 | uint8_t i; 1582 | 1583 | // Reverse data order for the Karn codec. 1584 | for(i = 0; i < 12; i++) 1585 | { 1586 | dat1[i] = data[11 - i]; 1587 | } 1588 | 1589 | // Compute the parity symbols 1590 | encode_rs_int(rs_inst, dat1, b); 1591 | 1592 | // Move parity symbols and data into symbols array, in reverse order. 1593 | for (i = 0; i < 51; i++) 1594 | { 1595 | sym[50 - i] = b[i]; 1596 | } 1597 | 1598 | for (i = 0; i < 12; i++) 1599 | { 1600 | sym[i + 51] = dat1[11 - i]; 1601 | } 1602 | 1603 | memcpy(symbols, sym, JT65_ENCODE_COUNT); 1604 | } 1605 | 1606 | uint8_t JTEncode::crc8(const char * text) 1607 | { 1608 | uint8_t crc = '\0'; 1609 | uint8_t ch; 1610 | 1611 | int i; 1612 | for(i = 0; i < strlen(text); i++) 1613 | { 1614 | ch = text[i]; 1615 | //#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__) || defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega16U4__) 1616 | #if defined(__arm__) 1617 | crc = crc8_table[(crc) ^ ch]; 1618 | #else 1619 | crc = pgm_read_byte(&(crc8_table[(crc) ^ ch])); 1620 | #endif 1621 | crc &= 0xFF; 1622 | } 1623 | 1624 | return crc; 1625 | } 1626 | 1627 | void JTEncode::pad_callsign(char * call) 1628 | { 1629 | // If only the 2nd character is a digit, then pad with a space. 1630 | // If this happens, then the callsign will be truncated if it is 1631 | // longer than 6 characters. 1632 | if(isdigit(call[1]) && isupper(call[2])) 1633 | { 1634 | // memmove(call + 1, call, 6); 1635 | call[5] = call[4]; 1636 | call[4] = call[3]; 1637 | call[3] = call[2]; 1638 | call[2] = call[1]; 1639 | call[1] = call[0]; 1640 | call[0] = ' '; 1641 | } 1642 | 1643 | // Now the 3rd charcter in the callsign must be a digit 1644 | // if(call[2] < '0' || call[2] > '9') 1645 | // { 1646 | // // return 1; 1647 | // } 1648 | } -------------------------------------------------------------------------------- /src/JTEncode.h: -------------------------------------------------------------------------------- 1 | /* 2 | * JTEncode.h - JT65/JT9/WSPR/FSQ encoder library for Arduino 3 | * 4 | * Copyright (C) 2015-2021 Jason Milldrum 5 | * 6 | * Based on the algorithms presented in the WSJT software suite. 7 | * Thanks to Andy Talbot G4JNT for the whitepaper on the WSPR encoding 8 | * process that helped me to understand all of this. 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | 24 | #ifndef JTENCODE_H 25 | #define JTENCODE_H 26 | 27 | #include "int.h" 28 | #include "rs_common.h" 29 | #include "nhash.h" 30 | 31 | #include "Arduino.h" 32 | 33 | #include 34 | 35 | #if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__) || defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega16U4__) 36 | #include 37 | #endif 38 | 39 | #define JT65_SYMBOL_COUNT 126 40 | #define JT9_SYMBOL_COUNT 85 41 | #define JT4_SYMBOL_COUNT 207 42 | #define WSPR_SYMBOL_COUNT 162 43 | #define FT8_SYMBOL_COUNT 79 44 | 45 | #define JT65_ENCODE_COUNT 63 46 | #define JT9_ENCODE_COUNT 69 47 | #define FT8_ENCODE_COUNT 77 48 | 49 | #define JT9_BIT_COUNT 206 50 | #define JT4_BIT_COUNT 206 51 | #define WSPR_BIT_COUNT 162 52 | #define FT8_BIT_COUNT 174 53 | 54 | // Define the structure of a varicode table 55 | typedef struct fsq_varicode 56 | { 57 | uint8_t ch; 58 | uint8_t var[2]; 59 | } Varicode; 60 | 61 | // The FSQ varicode table, based on the FSQ Varicode V3.0 62 | // document provided by Murray Greenman, ZL1BPU 63 | 64 | const Varicode fsq_code_table[] PROGMEM = 65 | { 66 | {' ', {00, 00}}, // space 67 | {'!', {11, 30}}, 68 | {'"', {12, 30}}, 69 | {'#', {13, 30}}, 70 | {'$', {14, 30}}, 71 | {'%', {15, 30}}, 72 | {'&', {16, 30}}, 73 | {'\'', {17, 30}}, 74 | {'(', {18, 30}}, 75 | {')', {19, 30}}, 76 | {'*', {20, 30}}, 77 | {'+', {21, 30}}, 78 | {',', {27, 29}}, 79 | {'-', {22, 30}}, 80 | {'.', {27, 00}}, 81 | {'/', {23, 30}}, 82 | {'0', {10, 30}}, 83 | {'1', {01, 30}}, 84 | {'2', {02, 30}}, 85 | {'3', {03, 30}}, 86 | {'4', {04, 30}}, 87 | {'5', {05, 30}}, 88 | {'6', {06, 30}}, 89 | {'7', {07, 30}}, 90 | {'8', {8, 30}}, 91 | {'9', {9, 30}}, 92 | {':', {24, 30}}, 93 | {';', {25, 30}}, 94 | {'<', {26, 30}}, 95 | {'=', {00, 31}}, 96 | {'>', {27, 30}}, 97 | {'?', {28, 29}}, 98 | {'@', {00, 29}}, 99 | {'A', {01, 29}}, 100 | {'B', {02, 29}}, 101 | {'C', {03, 29}}, 102 | {'D', {04, 29}}, 103 | {'E', {05, 29}}, 104 | {'F', {06, 29}}, 105 | {'G', {07, 29}}, 106 | {'H', {8, 29}}, 107 | {'I', {9, 29}}, 108 | {'J', {10, 29}}, 109 | {'K', {11, 29}}, 110 | {'L', {12, 29}}, 111 | {'M', {13, 29}}, 112 | {'N', {14, 29}}, 113 | {'O', {15, 29}}, 114 | {'P', {16, 29}}, 115 | {'Q', {17, 29}}, 116 | {'R', {18, 29}}, 117 | {'S', {19, 29}}, 118 | {'T', {20, 29}}, 119 | {'U', {21, 29}}, 120 | {'V', {22, 29}}, 121 | {'W', {23, 29}}, 122 | {'X', {24, 29}}, 123 | {'Y', {25, 29}}, 124 | {'Z', {26, 29}}, 125 | {'[', {01, 31}}, 126 | {'\\', {02, 31}}, 127 | {']', {03, 31}}, 128 | {'^', {04, 31}}, 129 | {'_', {05, 31}}, 130 | {'`', {9, 31}}, 131 | {'a', {01, 00}}, 132 | {'b', {02, 00}}, 133 | {'c', {03, 00}}, 134 | {'d', {04, 00}}, 135 | {'e', {05, 00}}, 136 | {'f', {06, 00}}, 137 | {'g', {07, 00}}, 138 | {'h', {8, 00}}, 139 | {'i', {9, 00}}, 140 | {'j', {10, 00}}, 141 | {'k', {11, 00}}, 142 | {'l', {12, 00}}, 143 | {'m', {13, 00}}, 144 | {'n', {14, 00}}, 145 | {'o', {15, 00}}, 146 | {'p', {16, 00}}, 147 | {'q', {17, 00}}, 148 | {'r', {18, 00}}, 149 | {'s', {19, 00}}, 150 | {'t', {20, 00}}, 151 | {'u', {21, 00}}, 152 | {'v', {22, 00}}, 153 | {'w', {23, 00}}, 154 | {'x', {24, 00}}, 155 | {'y', {25, 00}}, 156 | {'z', {26, 00}}, 157 | {'{', {06, 31}}, 158 | {'|', {07, 31}}, 159 | {'}', {8, 31}}, 160 | {'~', {00, 30}}, 161 | {127, {28, 31}}, // DEL 162 | {13, {28, 00}}, // CR 163 | {10, {28, 00}}, // LF 164 | {0, {28, 30}}, // IDLE 165 | {241, {10, 31}}, // plus/minus 166 | {246, {11, 31}}, // division sign 167 | {248, {12, 31}}, // degrees sign 168 | {158, {13, 31}}, // multiply sign 169 | {156, {14, 31}}, // pound sterling sign 170 | {8, {27, 31}} // BS 171 | }; 172 | 173 | const uint8_t crc8_table[] PROGMEM = { 174 | 0x00, 0x07, 0x0e, 0x09, 0x1c, 0x1b, 0x12, 0x15, 0x38, 0x3f, 0x36, 0x31, 175 | 0x24, 0x23, 0x2a, 0x2d, 0x70, 0x77, 0x7e, 0x79, 0x6c, 0x6b, 0x62, 0x65, 176 | 0x48, 0x4f, 0x46, 0x41, 0x54, 0x53, 0x5a, 0x5d, 0xe0, 0xe7, 0xee, 0xe9, 177 | 0xfc, 0xfb, 0xf2, 0xf5, 0xd8, 0xdf, 0xd6, 0xd1, 0xc4, 0xc3, 0xca, 0xcd, 178 | 0x90, 0x97, 0x9e, 0x99, 0x8c, 0x8b, 0x82, 0x85, 0xa8, 0xaf, 0xa6, 0xa1, 179 | 0xb4, 0xb3, 0xba, 0xbd, 0xc7, 0xc0, 0xc9, 0xce, 0xdb, 0xdc, 0xd5, 0xd2, 180 | 0xff, 0xf8, 0xf1, 0xf6, 0xe3, 0xe4, 0xed, 0xea, 0xb7, 0xb0, 0xb9, 0xbe, 181 | 0xab, 0xac, 0xa5, 0xa2, 0x8f, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9d, 0x9a, 182 | 0x27, 0x20, 0x29, 0x2e, 0x3b, 0x3c, 0x35, 0x32, 0x1f, 0x18, 0x11, 0x16, 183 | 0x03, 0x04, 0x0d, 0x0a, 0x57, 0x50, 0x59, 0x5e, 0x4b, 0x4c, 0x45, 0x42, 184 | 0x6f, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7d, 0x7a, 0x89, 0x8e, 0x87, 0x80, 185 | 0x95, 0x92, 0x9b, 0x9c, 0xb1, 0xb6, 0xbf, 0xb8, 0xad, 0xaa, 0xa3, 0xa4, 186 | 0xf9, 0xfe, 0xf7, 0xf0, 0xe5, 0xe2, 0xeb, 0xec, 0xc1, 0xc6, 0xcf, 0xc8, 187 | 0xdd, 0xda, 0xd3, 0xd4, 0x69, 0x6e, 0x67, 0x60, 0x75, 0x72, 0x7b, 0x7c, 188 | 0x51, 0x56, 0x5f, 0x58, 0x4d, 0x4a, 0x43, 0x44, 0x19, 0x1e, 0x17, 0x10, 189 | 0x05, 0x02, 0x0b, 0x0c, 0x21, 0x26, 0x2f, 0x28, 0x3d, 0x3a, 0x33, 0x34, 190 | 0x4e, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5c, 0x5b, 0x76, 0x71, 0x78, 0x7f, 191 | 0x6a, 0x6d, 0x64, 0x63, 0x3e, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2c, 0x2b, 192 | 0x06, 0x01, 0x08, 0x0f, 0x1a, 0x1d, 0x14, 0x13, 0xae, 0xa9, 0xa0, 0xa7, 193 | 0xb2, 0xb5, 0xbc, 0xbb, 0x96, 0x91, 0x98, 0x9f, 0x8a, 0x8d, 0x84, 0x83, 194 | 0xde, 0xd9, 0xd0, 0xd7, 0xc2, 0xc5, 0xcc, 0xcb, 0xe6, 0xe1, 0xe8, 0xef, 195 | 0xfa, 0xfd, 0xf4, 0xf3 196 | }; 197 | 198 | const uint8_t jt9i[JT9_BIT_COUNT] PROGMEM = { 199 | 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0x10, 0x90, 0x50, 0x30, 0xb0, 0x70, 200 | 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0x18, 0x98, 0x58, 0x38, 0xb8, 0x78, 201 | 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0x14, 0x94, 0x54, 0x34, 0xb4, 0x74, 202 | 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0x1c, 0x9c, 0x5c, 0x3c, 0xbc, 0x7c, 203 | 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0x12, 0x92, 0x52, 0x32, 0xb2, 0x72, 204 | 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0x1a, 0x9a, 0x5a, 0x3a, 0xba, 0x7a, 205 | 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0x16, 0x96, 0x56, 0x36, 0xb6, 0x76, 206 | 0x0e, 0x8e, 0x4e, 0x2e, 0xae, 0x6e, 0x1e, 0x9e, 0x5e, 0x3e, 0xbe, 0x7e, 0x01, 207 | 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0x11, 0x91, 0x51, 0x31, 0xb1, 0x71, 0x09, 208 | 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0x19, 0x99, 0x59, 0x39, 0xb9, 0x79, 0x05, 209 | 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0x15, 0x95, 0x55, 0x35, 0xb5, 0x75, 0x0d, 210 | 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0x1d, 0x9d, 0x5d, 0x3d, 0xbd, 0x7d, 0x03, 211 | 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0x13, 0x93, 0x53, 0x33, 0xb3, 0x73, 0x0b, 212 | 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0x1b, 0x9b, 0x5b, 0x3b, 0xbb, 0x7b, 0x07, 213 | 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0x17, 0x97, 0x57, 0x37, 0xb7, 0x77, 0x0f, 214 | 0x8f, 0x4f, 0x2f, 0xaf, 0x6f, 0x1f, 0x9f, 0x5f, 0x3f, 0xbf, 0x7f 215 | }; 216 | 217 | class JTEncode 218 | { 219 | public: 220 | JTEncode(void); 221 | void jt65_encode(const char *, uint8_t *); 222 | void jt9_encode(const char *, uint8_t *); 223 | void jt4_encode(const char *, uint8_t *); 224 | void wspr_encode(const char *, const char *, const int8_t, uint8_t *); 225 | void fsq_encode(const char *, const char *, uint8_t *); 226 | void fsq_dir_encode(const char *, const char *, const char, const char *, uint8_t *); 227 | void ft8_encode(const char *, uint8_t *); 228 | void latlon_to_grid(float, float, char*); 229 | private: 230 | uint8_t jt_code(char); 231 | uint8_t ft_code(char); 232 | uint8_t wspr_code(char); 233 | uint8_t gray_code(uint8_t); 234 | int8_t hex2int(char); 235 | void jt_message_prep(char *); 236 | void ft_message_prep(char *); 237 | void wspr_message_prep(char *, char *, int8_t); 238 | void jt65_bit_packing(char *, uint8_t *); 239 | void jt9_bit_packing(char *, uint8_t *); 240 | void wspr_bit_packing(uint8_t *); 241 | void ft8_bit_packing(char*, uint8_t*); 242 | void jt65_interleave(uint8_t *); 243 | void jt9_interleave(uint8_t *); 244 | void wspr_interleave(uint8_t *); 245 | void jt9_packbits(uint8_t *, uint8_t *); 246 | void jt_gray_code(uint8_t *, uint8_t); 247 | void ft8_encode(uint8_t*, uint8_t*); 248 | void jt65_merge_sync_vector(uint8_t *, uint8_t *); 249 | void jt9_merge_sync_vector(uint8_t *, uint8_t *); 250 | void jt4_merge_sync_vector(uint8_t *, uint8_t *); 251 | void wspr_merge_sync_vector(uint8_t *, uint8_t *); 252 | void ft8_merge_sync_vector(uint8_t*, uint8_t*); 253 | void convolve(uint8_t *, uint8_t *, uint8_t, uint8_t); 254 | void rs_encode(uint8_t *, uint8_t *); 255 | void encode_rs_int(void *,data_t *, data_t *); 256 | void free_rs_int(void *); 257 | void * init_rs_int(int, int, int, int, int, int); 258 | uint8_t crc8(const char *); 259 | void pad_callsign(char *); 260 | void * rs_inst; 261 | char callsign[12]; 262 | char locator[7]; 263 | int8_t power; 264 | }; 265 | 266 | #endif 267 | -------------------------------------------------------------------------------- /src/crc14.c: -------------------------------------------------------------------------------- 1 | /** 2 | * \file 3 | * Functions and types for CRC checks. 4 | * 5 | * Generated on Thu Dec 6 17:52:34 2018 6 | * by pycrc v0.9.1, https://pycrc.org 7 | * using the configuration: 8 | * - Width = 14 9 | * - Poly = 0x2757 10 | * - XorIn = Undefined 11 | * - ReflectIn = Undefined 12 | * - XorOut = Undefined 13 | * - ReflectOut = Undefined 14 | * - Algorithm = bit-by-bit 15 | */ 16 | #include "crc14.h" /* include the header file generated with pycrc */ 17 | #include 18 | #include 19 | #include 20 | 21 | static crc_t crc_reflect(crc_t data, size_t data_len); 22 | 23 | 24 | 25 | crc_t crc_reflect(crc_t data, size_t data_len) 26 | { 27 | unsigned int i; 28 | crc_t ret; 29 | 30 | ret = data & 0x01; 31 | for (i = 1; i < data_len; i++) { 32 | data >>= 1; 33 | ret = (ret << 1) | (data & 0x01); 34 | } 35 | return ret; 36 | } 37 | 38 | 39 | crc_t crc_init(const crc_cfg_t *cfg) 40 | { 41 | unsigned int i; 42 | bool bit; 43 | crc_t crc = cfg->xor_in; 44 | for (i = 0; i < 14; i++) { 45 | bit = crc & 0x01; 46 | if (bit) { 47 | crc = ((crc ^ 0x2757) >> 1) | 0x2000; 48 | } else { 49 | crc >>= 1; 50 | } 51 | } 52 | return crc & 0x3fff; 53 | } 54 | 55 | 56 | crc_t crc_update(const crc_cfg_t *cfg, crc_t crc, const void *data, size_t data_len) 57 | { 58 | const unsigned char *d = (const unsigned char *)data; 59 | unsigned int i; 60 | bool bit; 61 | unsigned char c; 62 | 63 | while (data_len--) { 64 | if (cfg->reflect_in) { 65 | c = crc_reflect(*d++, 8); 66 | } else { 67 | c = *d++; 68 | } 69 | for (i = 0; i < 8; i++) { 70 | bit = crc & 0x2000; 71 | crc = (crc << 1) | ((c >> (7 - i)) & 0x01); 72 | if (bit) { 73 | crc ^= 0x2757; 74 | } 75 | } 76 | crc &= 0x3fff; 77 | } 78 | return crc & 0x3fff; 79 | } 80 | 81 | 82 | crc_t crc_finalize(const crc_cfg_t *cfg, crc_t crc) 83 | { 84 | unsigned int i; 85 | bool bit; 86 | 87 | for (i = 0; i < 14; i++) { 88 | bit = crc & 0x2000; 89 | crc <<= 1; 90 | if (bit) { 91 | crc ^= 0x2757; 92 | } 93 | } 94 | if (cfg->reflect_out) { 95 | crc = crc_reflect(crc, 14); 96 | } 97 | return (crc ^ cfg->xor_out) & 0x3fff; 98 | } 99 | -------------------------------------------------------------------------------- /src/crc14.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file 3 | * Functions and types for CRC checks. 4 | * 5 | * Generated on Thu Dec 6 17:52:01 2018 6 | * by pycrc v0.9.1, https://pycrc.org 7 | * using the configuration: 8 | * - Width = 14 9 | * - Poly = 0x2757 10 | * - XorIn = Undefined 11 | * - ReflectIn = Undefined 12 | * - XorOut = Undefined 13 | * - ReflectOut = Undefined 14 | * - Algorithm = bit-by-bit 15 | * 16 | * This file defines the functions crc_init(), crc_update() and crc_finalize(). 17 | * 18 | * The crc_init() function returns the inital \c crc value and must be called 19 | * before the first call to crc_update(). 20 | * Similarly, the crc_finalize() function must be called after the last call 21 | * to crc_update(), before the \c crc is being used. 22 | * is being used. 23 | * 24 | * The crc_update() function can be called any number of times (including zero 25 | * times) in between the crc_init() and crc_finalize() calls. 26 | * 27 | * This pseudo-code shows an example usage of the API: 28 | * \code{.c} 29 | * crc_cfg_t cfg = { 30 | * 0, // reflect_in 31 | * 0, // xor_in 32 | * 0, // reflect_out 33 | * 0, // xor_out 34 | * }; 35 | * crc_t crc; 36 | * unsigned char data[MAX_DATA_LEN]; 37 | * size_t data_len; 38 | * 39 | * crc = crc_init(&cfg); 40 | * while ((data_len = read_data(data, MAX_DATA_LEN)) > 0) { 41 | * crc = crc_update(&cfg, crc, data, data_len); 42 | * } 43 | * crc = crc_finalize(&cfg, crc); 44 | * \endcode 45 | */ 46 | #ifndef CRC14_H 47 | #define CRC14_H 48 | 49 | #include 50 | #include 51 | #include 52 | 53 | #ifdef __cplusplus 54 | extern "C" { 55 | #endif 56 | 57 | 58 | /** 59 | * The definition of the used algorithm. 60 | * 61 | * This is not used anywhere in the generated code, but it may be used by the 62 | * application code to call algorithm-specific code, if desired. 63 | */ 64 | #define CRC_ALGO_BIT_BY_BIT 1 65 | 66 | 67 | /** 68 | * The type of the CRC values. 69 | * 70 | * This type must be big enough to contain at least 14 bits. 71 | */ 72 | typedef uint_fast16_t crc_t; 73 | 74 | 75 | /** 76 | * The configuration type of the CRC algorithm. 77 | */ 78 | typedef struct { 79 | bool reflect_in; /*!< Whether the input shall be reflected or not */ 80 | crc_t xor_in; /*!< The initial value of the register */ 81 | bool reflect_out; /*!< Whether the output shall be reflected or not */ 82 | crc_t xor_out; /*!< The value which shall be XOR-ed to the final CRC value */ 83 | } crc_cfg_t; 84 | 85 | 86 | /** 87 | * Calculate the initial crc value. 88 | * 89 | * \param[in] cfg A pointer to an initialised crc_cfg_t structure. 90 | * \return The initial crc value. 91 | */ 92 | crc_t crc_init(const crc_cfg_t *cfg); 93 | 94 | 95 | /** 96 | * Update the crc value with new data. 97 | * 98 | * \param[in] crc The current crc value. 99 | * \param[in] cfg A pointer to an initialised crc_cfg_t structure. 100 | * \param[in] data Pointer to a buffer of \a data_len bytes. 101 | * \param[in] data_len Number of bytes in the \a data buffer. 102 | * \return The updated crc value. 103 | */ 104 | crc_t crc_update(const crc_cfg_t *cfg, crc_t crc, const void *data, size_t data_len); 105 | 106 | 107 | /** 108 | * Calculate the final crc value. 109 | * 110 | * \param[in] cfg A pointer to an initialised crc_cfg_t structure. 111 | * \param[in] crc The current crc value. 112 | * \return The final crc value. 113 | */ 114 | crc_t crc_finalize(const crc_cfg_t *cfg, crc_t crc); 115 | 116 | 117 | #ifdef __cplusplus 118 | } /* closing brace for extern "C" */ 119 | #endif 120 | 121 | #endif /* CRC14_H */ 122 | -------------------------------------------------------------------------------- /src/encode_rs.h: -------------------------------------------------------------------------------- 1 | /* The guts of the Reed-Solomon encoder, meant to be #included 2 | * into a function body with the following typedefs, macros and variables supplied 3 | * according to the code parameters: 4 | 5 | * data_t - a typedef for the data symbol 6 | * data_t data[] - array of NN-NROOTS-PAD and type data_t to be encoded 7 | * data_t parity[] - an array of NROOTS and type data_t to be written with parity symbols 8 | * NROOTS - the number of roots in the RS code generator polynomial, 9 | * which is the same as the number of parity symbols in a block. 10 | Integer variable or literal. 11 | * 12 | * NN - the total number of symbols in a RS block. Integer variable or literal. 13 | * PAD - the number of pad symbols in a block. Integer variable or literal. 14 | * ALPHA_TO - The address of an array of NN elements to convert Galois field 15 | * elements in index (log) form to polynomial form. Read only. 16 | * INDEX_OF - The address of an array of NN elements to convert Galois field 17 | * elements in polynomial form to index (log) form. Read only. 18 | * MODNN - a function to reduce its argument modulo NN. May be inline or a macro. 19 | * GENPOLY - an array of NROOTS+1 elements containing the generator polynomial in index form 20 | 21 | * The memset() and memmove() functions are used. The appropriate header 22 | * file declaring these functions (usually ) must be included by the calling 23 | * program. 24 | 25 | * Copyright 2004, Phil Karn, KA9Q 26 | * May be used under the terms of the GNU Lesser General Public License (LGPL) 27 | */ 28 | 29 | 30 | #undef A0 31 | #define A0 (NN) /* Special reserved value encoding zero in index form */ 32 | 33 | { 34 | int i, j; 35 | data_t feedback; 36 | 37 | memset(parity,0,NROOTS*sizeof(data_t)); 38 | 39 | for(i=0;i) must be included by the calling 29 | * program. 30 | */ 31 | 32 | #include 33 | #include 34 | #include "int.h" 35 | #include "rs_common.h" 36 | 37 | void JTEncode::encode_rs_int(void *p, data_t *data, data_t *parity) 38 | { 39 | struct rs *rs = (struct rs *)p; 40 | 41 | #undef A_0 42 | #define A_0 (NN) /* Special reserved value encoding zero in index form */ 43 | 44 | { 45 | int i, j; 46 | data_t feedback; 47 | 48 | memset(parity,0,NROOTS*sizeof(data_t)); 49 | 50 | for(i=0;i 5 | 6 | #if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__) || defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega16U4__) 7 | #include 8 | #endif 9 | 10 | const uint8_t generator_bits[83][12] PROGMEM = 11 | { 12 | {0b10000011, 0b00101001, 0b11001110, 0b00010001, 0b10111111, 0b00110001, 0b11101010, 0b11110101, 0b00001001, 0b11110010, 0b01111111, 0b11000000}, 13 | {0b01110110, 0b00011100, 0b00100110, 0b01001110, 0b00100101, 0b11000010, 0b01011001, 0b00110011, 0b01010100, 0b10010011, 0b00010011, 0b00100000}, 14 | {0b11011100, 0b00100110, 0b01011001, 0b00000010, 0b11111011, 0b00100111, 0b01111100, 0b01100100, 0b00010000, 0b10100001, 0b10111101, 0b11000000}, 15 | {0b00011011, 0b00111111, 0b01000001, 0b01111000, 0b01011000, 0b11001101, 0b00101101, 0b11010011, 0b00111110, 0b11000111, 0b11110110, 0b00100000}, 16 | {0b00001001, 0b11111101, 0b10100100, 0b11111110, 0b11100000, 0b01000001, 0b10010101, 0b11111101, 0b00000011, 0b01000111, 0b10000011, 0b10100000}, 17 | {0b00000111, 0b01111100, 0b11001100, 0b11000001, 0b00011011, 0b10001000, 0b01110011, 0b11101101, 0b01011100, 0b00111101, 0b01001000, 0b10100000}, 18 | {0b00101001, 0b10110110, 0b00101010, 0b11111110, 0b00111100, 0b10100000, 0b00110110, 0b11110100, 0b11111110, 0b00011010, 0b10011101, 0b10100000}, 19 | {0b01100000, 0b01010100, 0b11111010, 0b11110101, 0b11110011, 0b01011101, 0b10010110, 0b11010011, 0b10110000, 0b11001000, 0b11000011, 0b11100000}, 20 | {0b11100010, 0b00000111, 0b10011000, 0b11100100, 0b00110001, 0b00001110, 0b11101101, 0b00100111, 0b10001000, 0b01001010, 0b11101001, 0b00000000}, 21 | {0b01110111, 0b01011100, 0b10011100, 0b00001000, 0b11101000, 0b00001110, 0b00100110, 0b11011101, 0b10101110, 0b01010110, 0b00110001, 0b10000000}, 22 | {0b10110000, 0b10111000, 0b00010001, 0b00000010, 0b10001100, 0b00101011, 0b11111001, 0b10010111, 0b00100001, 0b00110100, 0b10000111, 0b11000000}, 23 | {0b00011000, 0b10100000, 0b11001001, 0b00100011, 0b00011111, 0b11000110, 0b00001010, 0b11011111, 0b01011100, 0b01011110, 0b10100011, 0b00100000}, 24 | {0b01110110, 0b01000111, 0b00011110, 0b10000011, 0b00000010, 0b10100000, 0b01110010, 0b00011110, 0b00000001, 0b10110001, 0b00101011, 0b10000000}, 25 | {0b11111111, 0b10111100, 0b11001011, 0b10000000, 0b11001010, 0b10000011, 0b01000001, 0b11111010, 0b11111011, 0b01000111, 0b10110010, 0b11100000}, 26 | {0b01100110, 0b10100111, 0b00101010, 0b00010101, 0b10001111, 0b10010011, 0b00100101, 0b10100010, 0b10111111, 0b01100111, 0b00010111, 0b00000000}, 27 | {0b11000100, 0b00100100, 0b00110110, 0b10001001, 0b11111110, 0b10000101, 0b10110001, 0b11000101, 0b00010011, 0b01100011, 0b10100001, 0b10000000}, 28 | {0b00001101, 0b11111111, 0b01110011, 0b10010100, 0b00010100, 0b11010001, 0b10100001, 0b10110011, 0b01001011, 0b00011100, 0b00100111, 0b00000000}, 29 | {0b00010101, 0b10110100, 0b10001000, 0b00110000, 0b01100011, 0b01101100, 0b10001011, 0b10011001, 0b10001001, 0b01001001, 0b01110010, 0b11100000}, 30 | {0b00101001, 0b10101000, 0b10011100, 0b00001101, 0b00111101, 0b11101000, 0b00011101, 0b01100110, 0b01010100, 0b10001001, 0b10110000, 0b11100000}, 31 | {0b01001111, 0b00010010, 0b01101111, 0b00110111, 0b11111010, 0b01010001, 0b11001011, 0b11100110, 0b00011011, 0b11010110, 0b10111001, 0b01000000}, 32 | {0b10011001, 0b11000100, 0b01110010, 0b00111001, 0b11010000, 0b11011001, 0b01111101, 0b00111100, 0b10000100, 0b11100000, 0b10010100, 0b00000000}, 33 | {0b00011001, 0b00011001, 0b10110111, 0b01010001, 0b00011001, 0b01110110, 0b01010110, 0b00100001, 0b10111011, 0b01001111, 0b00011110, 0b10000000}, 34 | {0b00001001, 0b11011011, 0b00010010, 0b11010111, 0b00110001, 0b11111010, 0b11101110, 0b00001011, 0b10000110, 0b11011111, 0b01101011, 0b10000000}, 35 | {0b01001000, 0b10001111, 0b11000011, 0b00111101, 0b11110100, 0b00111111, 0b10111101, 0b11101110, 0b10100100, 0b11101010, 0b11111011, 0b01000000}, 36 | {0b10000010, 0b01110100, 0b00100011, 0b11101110, 0b01000000, 0b10110110, 0b01110101, 0b11110111, 0b01010110, 0b11101011, 0b01011111, 0b11100000}, 37 | {0b10101011, 0b11100001, 0b10010111, 0b11000100, 0b10000100, 0b11001011, 0b01110100, 0b01110101, 0b01110001, 0b01000100, 0b10101001, 0b10100000}, 38 | {0b00101011, 0b01010000, 0b00001110, 0b01001011, 0b11000000, 0b11101100, 0b01011010, 0b01101101, 0b00101011, 0b11011011, 0b11011101, 0b00000000}, 39 | {0b11000100, 0b01110100, 0b10101010, 0b01010011, 0b11010111, 0b00000010, 0b00011000, 0b01110110, 0b00010110, 0b01101001, 0b00110110, 0b00000000}, 40 | {0b10001110, 0b10111010, 0b00011010, 0b00010011, 0b11011011, 0b00110011, 0b10010000, 0b10111101, 0b01100111, 0b00011000, 0b11001110, 0b11000000}, 41 | {0b01110101, 0b00111000, 0b01000100, 0b01100111, 0b00111010, 0b00100111, 0b01111000, 0b00101100, 0b11000100, 0b00100000, 0b00010010, 0b11100000}, 42 | {0b00000110, 0b11111111, 0b10000011, 0b10100001, 0b01000101, 0b11000011, 0b01110000, 0b00110101, 0b10100101, 0b11000001, 0b00100110, 0b10000000}, 43 | {0b00111011, 0b00110111, 0b01000001, 0b01111000, 0b01011000, 0b11001100, 0b00101101, 0b11010011, 0b00111110, 0b11000011, 0b11110110, 0b00100000}, 44 | {0b10011010, 0b01001010, 0b01011010, 0b00101000, 0b11101110, 0b00010111, 0b11001010, 0b10011100, 0b00110010, 0b01001000, 0b01000010, 0b11000000}, 45 | {0b10111100, 0b00101001, 0b11110100, 0b01100101, 0b00110000, 0b10011100, 0b10010111, 0b01111110, 0b10001001, 0b01100001, 0b00001010, 0b01000000}, 46 | {0b00100110, 0b01100011, 0b10101110, 0b01101101, 0b11011111, 0b10001011, 0b01011100, 0b11100010, 0b10111011, 0b00101001, 0b01001000, 0b10000000}, 47 | {0b01000110, 0b11110010, 0b00110001, 0b11101111, 0b11100100, 0b01010111, 0b00000011, 0b01001100, 0b00011000, 0b00010100, 0b01000001, 0b10000000}, 48 | {0b00111111, 0b10110010, 0b11001110, 0b10000101, 0b10101011, 0b11101001, 0b10110000, 0b11000111, 0b00101110, 0b00000110, 0b11111011, 0b11100000}, 49 | {0b11011110, 0b10000111, 0b01001000, 0b00011111, 0b00101000, 0b00101100, 0b00010101, 0b00111001, 0b01110001, 0b10100000, 0b10100010, 0b11100000}, 50 | {0b11111100, 0b11010111, 0b11001100, 0b11110010, 0b00111100, 0b01101001, 0b11111010, 0b10011001, 0b10111011, 0b10100001, 0b01000001, 0b00100000}, 51 | {0b11110000, 0b00100110, 0b00010100, 0b01000111, 0b11101001, 0b01001001, 0b00001100, 0b10101000, 0b11100100, 0b01110100, 0b11001110, 0b11000000}, 52 | {0b01000100, 0b00010000, 0b00010001, 0b01011000, 0b00011000, 0b00011001, 0b01101111, 0b10010101, 0b11001101, 0b11010111, 0b00000001, 0b00100000}, 53 | {0b00001000, 0b10001111, 0b11000011, 0b00011101, 0b11110100, 0b10111111, 0b10111101, 0b11100010, 0b10100100, 0b11101010, 0b11111011, 0b01000000}, 54 | {0b10111000, 0b11111110, 0b11110001, 0b10110110, 0b00110000, 0b01110111, 0b00101001, 0b11111011, 0b00001010, 0b00000111, 0b10001100, 0b00000000}, 55 | {0b01011010, 0b11111110, 0b10100111, 0b10101100, 0b11001100, 0b10110111, 0b01111011, 0b10111100, 0b10011101, 0b10011001, 0b10101001, 0b00000000}, 56 | {0b01001001, 0b10100111, 0b00000001, 0b01101010, 0b11000110, 0b01010011, 0b11110110, 0b01011110, 0b11001101, 0b11001001, 0b00000111, 0b01100000}, 57 | {0b00011001, 0b01000100, 0b11010000, 0b10000101, 0b10111110, 0b01001110, 0b01111101, 0b10101000, 0b11010110, 0b11001100, 0b01111101, 0b00000000}, 58 | {0b00100101, 0b00011111, 0b01100010, 0b10101101, 0b11000100, 0b00000011, 0b00101111, 0b00001110, 0b11100111, 0b00010100, 0b00000000, 0b00100000}, 59 | {0b01010110, 0b01000111, 0b00011111, 0b10000111, 0b00000010, 0b10100000, 0b01110010, 0b00011110, 0b00000000, 0b10110001, 0b00101011, 0b10000000}, 60 | {0b00101011, 0b10001110, 0b01001001, 0b00100011, 0b11110010, 0b11011101, 0b01010001, 0b11100010, 0b11010101, 0b00110111, 0b11111010, 0b00000000}, 61 | {0b01101011, 0b01010101, 0b00001010, 0b01000000, 0b10100110, 0b01101111, 0b01000111, 0b01010101, 0b11011110, 0b10010101, 0b11000010, 0b01100000}, 62 | {0b10100001, 0b10001010, 0b11010010, 0b10001101, 0b01001110, 0b00100111, 0b11111110, 0b10010010, 0b10100100, 0b11110110, 0b11001000, 0b01000000}, 63 | {0b00010000, 0b11000010, 0b11100101, 0b10000110, 0b00111000, 0b10001100, 0b10111000, 0b00101010, 0b00111101, 0b10000000, 0b01110101, 0b10000000}, 64 | {0b11101111, 0b00110100, 0b10100100, 0b00011000, 0b00010111, 0b11101110, 0b00000010, 0b00010011, 0b00111101, 0b10110010, 0b11101011, 0b00000000}, 65 | {0b01111110, 0b10011100, 0b00001100, 0b01010100, 0b00110010, 0b01011010, 0b10011100, 0b00010101, 0b10000011, 0b01101110, 0b00000000, 0b00000000}, 66 | {0b00110110, 0b10010011, 0b11100101, 0b01110010, 0b11010001, 0b11111101, 0b11100100, 0b11001101, 0b11110000, 0b01111001, 0b11101000, 0b01100000}, 67 | {0b10111111, 0b10110010, 0b11001110, 0b11000101, 0b10101011, 0b11100001, 0b10110000, 0b11000111, 0b00101110, 0b00000111, 0b11111011, 0b11100000}, 68 | {0b01111110, 0b11100001, 0b10000010, 0b00110000, 0b11000101, 0b10000011, 0b11001100, 0b11001100, 0b01010111, 0b11010100, 0b10110000, 0b10000000}, 69 | {0b10100000, 0b01100110, 0b11001011, 0b00101111, 0b11101101, 0b10101111, 0b11001001, 0b11110101, 0b00100110, 0b01100100, 0b00010010, 0b01100000}, 70 | {0b10111011, 0b00100011, 0b01110010, 0b01011010, 0b10111100, 0b01000111, 0b11001100, 0b01011111, 0b01001100, 0b11000100, 0b11001101, 0b00100000}, 71 | {0b11011110, 0b11011001, 0b11011011, 0b10100011, 0b10111110, 0b11100100, 0b00001100, 0b01011001, 0b10110101, 0b01100000, 0b10011011, 0b01000000}, 72 | {0b11011001, 0b10100111, 0b00000001, 0b01101010, 0b11000110, 0b01010011, 0b11100110, 0b11011110, 0b11001101, 0b11001001, 0b00000011, 0b01100000}, 73 | {0b10011010, 0b11010100, 0b01101010, 0b11101101, 0b01011111, 0b01110000, 0b01111111, 0b00101000, 0b00001010, 0b10110101, 0b11111100, 0b01000000}, 74 | {0b11100101, 0b10010010, 0b00011100, 0b01110111, 0b10000010, 0b00100101, 0b10000111, 0b00110001, 0b01101101, 0b01111101, 0b00111100, 0b00100000}, 75 | {0b01001111, 0b00010100, 0b11011010, 0b10000010, 0b01000010, 0b10101000, 0b10111000, 0b01101101, 0b11001010, 0b01110011, 0b00110101, 0b00100000}, 76 | {0b10001011, 0b10001011, 0b01010000, 0b01111010, 0b11010100, 0b01100111, 0b11010100, 0b01000100, 0b00011101, 0b11110111, 0b01110000, 0b11100000}, 77 | {0b00100010, 0b10000011, 0b00011100, 0b10011100, 0b11110001, 0b00010110, 0b10010100, 0b01100111, 0b10101101, 0b00000100, 0b10110110, 0b10000000}, 78 | {0b00100001, 0b00111011, 0b10000011, 0b10001111, 0b11100010, 0b10101110, 0b01010100, 0b11000011, 0b10001110, 0b11100111, 0b00011000, 0b00000000}, 79 | {0b01011101, 0b10010010, 0b01101011, 0b01101101, 0b11010111, 0b00011111, 0b00001000, 0b01010001, 0b10000001, 0b10100100, 0b11100001, 0b00100000}, 80 | {0b01100110, 0b10101011, 0b01111001, 0b11010100, 0b10110010, 0b10011110, 0b11100110, 0b11100110, 0b10010101, 0b00001001, 0b11100101, 0b01100000}, 81 | {0b10010101, 0b10000001, 0b01001000, 0b01101000, 0b00101101, 0b01110100, 0b10001010, 0b00111000, 0b11011101, 0b01101000, 0b10111010, 0b10100000}, 82 | {0b10111000, 0b11001110, 0b00000010, 0b00001100, 0b11110000, 0b01101001, 0b11000011, 0b00101010, 0b01110010, 0b00111010, 0b10110001, 0b01000000}, 83 | {0b11110100, 0b00110011, 0b00011101, 0b01101101, 0b01000110, 0b00010110, 0b00000111, 0b11101001, 0b01010111, 0b01010010, 0b01110100, 0b01100000}, 84 | {0b01101101, 0b10100010, 0b00111011, 0b10100100, 0b00100100, 0b10111001, 0b01011001, 0b01100001, 0b00110011, 0b11001111, 0b10011100, 0b10000000}, 85 | {0b10100110, 0b00110110, 0b10111100, 0b10111100, 0b01111011, 0b00110000, 0b11000101, 0b11111011, 0b11101010, 0b11100110, 0b01111111, 0b11100000}, 86 | {0b01011100, 0b10110000, 0b11011000, 0b01101010, 0b00000111, 0b11011111, 0b01100101, 0b01001010, 0b10010000, 0b10001001, 0b10100010, 0b00000000}, 87 | {0b11110001, 0b00011111, 0b00010000, 0b01101000, 0b01001000, 0b01111000, 0b00001111, 0b11001001, 0b11101100, 0b11011101, 0b10000000, 0b10100000}, 88 | {0b00011111, 0b10111011, 0b01010011, 0b01100100, 0b11111011, 0b10001101, 0b00101100, 0b10011101, 0b01110011, 0b00001101, 0b01011011, 0b10100000}, 89 | {0b11111100, 0b10111000, 0b01101011, 0b11000111, 0b00001010, 0b01010000, 0b11001001, 0b11010000, 0b00101010, 0b01011101, 0b00000011, 0b01000000}, 90 | {0b10100101, 0b00110100, 0b01000011, 0b00110000, 0b00101001, 0b11101010, 0b11000001, 0b01011111, 0b00110010, 0b00101110, 0b00110100, 0b11000000}, 91 | {0b11001001, 0b10001001, 0b11011001, 0b11000111, 0b11000011, 0b11010011, 0b10111000, 0b11000101, 0b01011101, 0b01110101, 0b00010011, 0b00000000}, 92 | {0b01111011, 0b10110011, 0b10001011, 0b00101111, 0b00000001, 0b10000110, 0b11010100, 0b01100110, 0b01000011, 0b10101110, 0b10010110, 0b00100000}, 93 | {0b00100110, 0b01000100, 0b11101011, 0b10101101, 0b11101011, 0b01000100, 0b10111001, 0b01000110, 0b01111101, 0b00011111, 0b01000010, 0b11000000}, 94 | {0b01100000, 0b10001100, 0b11001000, 0b01010111, 0b01011001, 0b01001011, 0b11111011, 0b10110101, 0b01011101, 0b01101001, 0b01100000, 0b00000000} 95 | }; 96 | 97 | #endif 98 | -------------------------------------------------------------------------------- /src/init_rs.h: -------------------------------------------------------------------------------- 1 | /* Common code for intializing a Reed-Solomon control block (char or int symbols) 2 | * Copyright 2004 Phil Karn, KA9Q 3 | * May be used under the terms of the GNU Lesser General Public License (LGPL) 4 | */ 5 | #undef NULL 6 | #define NULL ((void *)0) 7 | 8 | //{ 9 | int i, j, sr,root,iprim; 10 | 11 | rs = NULL; 12 | /* Check parameter ranges */ 13 | if(symsize < 0 || symsize > 8*sizeof(data_t)){ 14 | goto done; 15 | } 16 | 17 | if(fcr < 0 || fcr >= (1<= (1<= (1<= ((1<mm = symsize; 31 | rs->nn = (1<pad = pad; 33 | 34 | rs->alpha_to = (data_t *)malloc(sizeof(data_t)*(rs->nn+1)); 35 | if(rs->alpha_to == NULL){ 36 | free(rs); 37 | rs = NULL; 38 | goto done; 39 | } 40 | rs->index_of = (data_t *)malloc(sizeof(data_t)*(rs->nn+1)); 41 | if(rs->index_of == NULL){ 42 | free(rs->alpha_to); 43 | free(rs); 44 | rs = NULL; 45 | goto done; 46 | } 47 | 48 | /* Generate Galois field lookup tables */ 49 | rs->index_of[0] = A0; /* log(zero) = -inf */ 50 | rs->alpha_to[A0] = 0; /* alpha**-inf = 0 */ 51 | sr = 1; 52 | for(i=0;inn;i++){ 53 | rs->index_of[sr] = i; 54 | rs->alpha_to[i] = sr; 55 | sr <<= 1; 56 | if(sr & (1<nn; 59 | } 60 | if(sr != 1){ 61 | /* field generator polynomial is not primitive! */ 62 | free(rs->alpha_to); 63 | free(rs->index_of); 64 | free(rs); 65 | rs = NULL; 66 | goto done; 67 | } 68 | 69 | /* Form RS code generator polynomial from its roots */ 70 | rs->genpoly = (data_t *)malloc(sizeof(data_t)*(nroots+1)); 71 | if(rs->genpoly == NULL){ 72 | free(rs->alpha_to); 73 | free(rs->index_of); 74 | free(rs); 75 | rs = NULL; 76 | goto done; 77 | } 78 | rs->fcr = fcr; 79 | rs->prim = prim; 80 | rs->nroots = nroots; 81 | 82 | /* Find prim-th root of 1, used in decoding */ 83 | for(iprim=1;(iprim % prim) != 0;iprim += rs->nn) 84 | ; 85 | rs->iprim = iprim / prim; 86 | 87 | rs->genpoly[0] = 1; 88 | for (i = 0,root=fcr*prim; i < nroots; i++,root += prim) { 89 | rs->genpoly[i+1] = 1; 90 | 91 | /* Multiply rs->genpoly[] by @**(root + x) */ 92 | for (j = i; j > 0; j--){ 93 | if (rs->genpoly[j] != 0) 94 | rs->genpoly[j] = rs->genpoly[j-1] ^ rs->alpha_to[modnn(rs,rs->index_of[rs->genpoly[j]] + root)]; 95 | else 96 | rs->genpoly[j] = rs->genpoly[j-1]; 97 | } 98 | /* rs->genpoly[0] can never be zero */ 99 | rs->genpoly[0] = rs->alpha_to[modnn(rs,rs->index_of[rs->genpoly[0]] + root)]; 100 | } 101 | /* convert rs->genpoly[] to index form for quicker encoding */ 102 | for (i = 0; i <= nroots; i++) 103 | rs->genpoly[i] = rs->index_of[rs->genpoly[i]]; 104 | done:; 105 | 106 | //} 107 | -------------------------------------------------------------------------------- /src/init_rs_int.cpp: -------------------------------------------------------------------------------- 1 | /* Initialize a RS codec 2 | * 3 | * Copyright 2002 Phil Karn, KA9Q 4 | * May be used under the terms of the GNU Lesser General Public License (LGPL) 5 | * 6 | * Slightly modified by Jason Milldrum NT7S, 2015 to fit into the Arduino framework 7 | */ 8 | 9 | #include 10 | #include 11 | #include 12 | #include "rs_common.h" 13 | 14 | void JTEncode::free_rs_int(void * p) 15 | { 16 | struct rs *rs = (struct rs *)p; 17 | 18 | free(rs->alpha_to); 19 | free(rs->index_of); 20 | free(rs->genpoly); 21 | free(rs); 22 | } 23 | 24 | void * JTEncode::init_rs_int(int symsize, int gfpoly, int fcr, int prim, 25 | int nroots, int pad) 26 | { 27 | struct rs *rs; 28 | 29 | int i, j, sr,root,iprim; 30 | 31 | rs = ((struct rs *)0); 32 | /* Check parameter ranges */ 33 | if(symsize < 0 || symsize > 8*sizeof(data_t)){ 34 | goto done; 35 | } 36 | 37 | if(fcr < 0 || fcr >= (1<= (1<= (1<= ((1<mm = symsize; 51 | rs->nn = (1<pad = pad; 53 | 54 | rs->alpha_to = (data_t *)malloc(sizeof(data_t)*(rs->nn+1)); 55 | if(rs->alpha_to == NULL){ 56 | free(rs); 57 | rs = ((struct rs *)0); 58 | goto done; 59 | } 60 | rs->index_of = (data_t *)malloc(sizeof(data_t)*(rs->nn+1)); 61 | if(rs->index_of == NULL){ 62 | free(rs->alpha_to); 63 | free(rs); 64 | rs = ((struct rs *)0); 65 | goto done; 66 | } 67 | 68 | /* Generate Galois field lookup tables */ 69 | rs->index_of[0] = A_0; /* log(zero) = -inf */ 70 | rs->alpha_to[A_0] = 0; /* alpha**-inf = 0 */ 71 | sr = 1; 72 | for(i=0;inn;i++){ 73 | rs->index_of[sr] = i; 74 | rs->alpha_to[i] = sr; 75 | sr <<= 1; 76 | if(sr & (1<nn; 79 | } 80 | if(sr != 1){ 81 | /* field generator polynomial is not primitive! */ 82 | free(rs->alpha_to); 83 | free(rs->index_of); 84 | free(rs); 85 | rs = ((struct rs *)0); 86 | goto done; 87 | } 88 | 89 | /* Form RS code generator polynomial from its roots */ 90 | rs->genpoly = (data_t *)malloc(sizeof(data_t)*(nroots+1)); 91 | if(rs->genpoly == NULL){ 92 | free(rs->alpha_to); 93 | free(rs->index_of); 94 | free(rs); 95 | rs = ((struct rs *)0); 96 | goto done; 97 | } 98 | rs->fcr = fcr; 99 | rs->prim = prim; 100 | rs->nroots = nroots; 101 | 102 | /* Find prim-th root of 1, used in decoding */ 103 | for(iprim=1;(iprim % prim) != 0;iprim += rs->nn) 104 | ; 105 | rs->iprim = iprim / prim; 106 | 107 | rs->genpoly[0] = 1; 108 | for (i = 0,root=fcr*prim; i < nroots; i++,root += prim) { 109 | rs->genpoly[i+1] = 1; 110 | 111 | /* Multiply rs->genpoly[] by @**(root + x) */ 112 | for (j = i; j > 0; j--){ 113 | if (rs->genpoly[j] != 0) 114 | rs->genpoly[j] = rs->genpoly[j-1] ^ rs->alpha_to[modnn(rs,rs->index_of[rs->genpoly[j]] + root)]; 115 | else 116 | rs->genpoly[j] = rs->genpoly[j-1]; 117 | } 118 | /* rs->genpoly[0] can never be zero */ 119 | rs->genpoly[0] = rs->alpha_to[modnn(rs,rs->index_of[rs->genpoly[0]] + root)]; 120 | } 121 | /* convert rs->genpoly[] to index form for quicker encoding */ 122 | for (i = 0; i <= nroots; i++) 123 | rs->genpoly[i] = rs->index_of[rs->genpoly[i]]; 124 | done:; 125 | 126 | return rs; 127 | } 128 | -------------------------------------------------------------------------------- /src/int.h: -------------------------------------------------------------------------------- 1 | /* Stuff specific to the general (integer) version of the Reed-Solomon codecs 2 | * 3 | * Copyright 2003, Phil Karn, KA9Q 4 | * May be used under the terms of the GNU Lesser General Public License (LGPL) 5 | */ 6 | #ifndef INT_H_ 7 | #define INT_H_ 8 | 9 | #include 10 | 11 | typedef uint8_t data_t; 12 | //typedef unsigned int data_t; 13 | 14 | #define MODNN(x) modnn(rs,x) 15 | #define MM (rs->mm) 16 | #define NN (rs->nn) 17 | #define ALPHA_TO (rs->alpha_to) 18 | #define INDEX_OF (rs->index_of) 19 | #define GENPOLY (rs->genpoly) 20 | #define NROOTS (rs->nroots) 21 | #define FCR (rs->fcr) 22 | #define PRIM (rs->prim) 23 | #define IPRIM (rs->iprim) 24 | #define PAD (rs->pad) 25 | #define A_0 (NN) 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /src/nhash.c: -------------------------------------------------------------------------------- 1 | /* 2 | *------------------------------------------------------------------------------- 3 | * 4 | * This file is part of the WSPR application, Weak Signal Propagation Reporter 5 | * 6 | * File Name: nhash.c 7 | * Description: Functions to produce 32-bit hashes for hash table lookup 8 | * 9 | * Copyright (C) 2008-2014 Joseph Taylor, K1JT 10 | * License: GPL-3 11 | * 12 | * This program is free software; you can redistribute it and/or modify it under 13 | * the terms of the GNU General Public License as published by the Free Software 14 | * Foundation; either version 3 of the License, or (at your option) any later 15 | * version. 16 | * 17 | * This program is distributed in the hope that it will be useful, but WITHOUT 18 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 19 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 20 | * details. 21 | * 22 | * You should have received a copy of the GNU General Public License along with 23 | * this program; if not, write to the Free Software Foundation, Inc., 51 Franklin 24 | * Street, Fifth Floor, Boston, MA 02110-1301, USA. 25 | * 26 | * Files: lookup3.c 27 | * Copyright: Copyright (C) 2006 Bob Jenkins 28 | * License: public-domain 29 | * You may use this code any way you wish, private, educational, or commercial. 30 | * It's free. 31 | * 32 | *------------------------------------------------------------------------------- 33 | */ 34 | 35 | /* 36 | These are functions for producing 32-bit hashes for hash table lookup. 37 | hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final() 38 | are externally useful functions. Routines to test the hash are included 39 | if SELF_TEST is defined. You can use this free for any purpose. It's in 40 | the public domain. It has no warranty. 41 | 42 | You probably want to use hashlittle(). hashlittle() and hashbig() 43 | hash byte arrays. hashlittle() is is faster than hashbig() on 44 | little-endian machines. Intel and AMD are little-endian machines. 45 | On second thought, you probably want hashlittle2(), which is identical to 46 | hashlittle() except it returns two 32-bit hashes for the price of one. 47 | You could implement hashbig2() if you wanted but I haven't bothered here. 48 | 49 | If you want to find a hash of, say, exactly 7 integers, do 50 | a = i1; b = i2; c = i3; 51 | mix(a,b,c); 52 | a += i4; b += i5; c += i6; 53 | mix(a,b,c); 54 | a += i7; 55 | final(a,b,c); 56 | then use c as the hash value. If you have a variable length array of 57 | 4-byte integers to hash, use hashword(). If you have a byte array (like 58 | a character string), use hashlittle(). If you have several byte arrays, or 59 | a mix of things, see the comments above hashlittle(). 60 | 61 | Why is this so big? I read 12 bytes at a time into 3 4-byte integers, 62 | then mix those integers. This is fast (you can do a lot more thorough 63 | mixing with 12*3 instructions on 3 integers than you can with 3 instructions 64 | on 1 byte), but shoehorning those bytes into integers efficiently is messy. 65 | */ 66 | 67 | #define SELF_TEST 1 68 | 69 | #include /* defines printf for tests */ 70 | #include /* defines time_t for timings in the test */ 71 | #ifdef Win32 72 | #include "win_stdint.h" /* defines uint32_t etc */ 73 | #else 74 | #include /* defines uint32_t etc */ 75 | #endif 76 | //#include /* attempt to define endianness */ 77 | //#ifdef linux 78 | //# include /* attempt to define endianness */ 79 | //#endif 80 | 81 | #define HASH_LITTLE_ENDIAN 1 82 | 83 | #define hashsize(n) ((uint32_t)1<<(n)) 84 | #define hashmask(n) (hashsize(n)-1) 85 | #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k)))) 86 | 87 | /* 88 | ------------------------------------------------------------------------------- 89 | mix -- mix 3 32-bit values reversibly. 90 | 91 | This is reversible, so any information in (a,b,c) before mix() is 92 | still in (a,b,c) after mix(). 93 | 94 | If four pairs of (a,b,c) inputs are run through mix(), or through 95 | mix() in reverse, there are at least 32 bits of the output that 96 | are sometimes the same for one pair and different for another pair. 97 | This was tested for: 98 | * pairs that differed by one bit, by two bits, in any combination 99 | of top bits of (a,b,c), or in any combination of bottom bits of 100 | (a,b,c). 101 | * "differ" is defined as +, -, ^, or ~^. For + and -, I transformed 102 | the output delta to a Gray code (a^(a>>1)) so a string of 1's (as 103 | is commonly produced by subtraction) look like a single 1-bit 104 | difference. 105 | * the base values were pseudorandom, all zero but one bit set, or 106 | all zero plus a counter that starts at zero. 107 | 108 | Some k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that 109 | satisfy this are 110 | 4 6 8 16 19 4 111 | 9 15 3 18 27 15 112 | 14 9 3 7 17 3 113 | Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing 114 | for "differ" defined as + with a one-bit base and a two-bit delta. I 115 | used http://burtleburtle.net/bob/hash/avalanche.html to choose 116 | the operations, constants, and arrangements of the variables. 117 | 118 | This does not achieve avalanche. There are input bits of (a,b,c) 119 | that fail to affect some output bits of (a,b,c), especially of a. The 120 | most thoroughly mixed value is c, but it doesn't really even achieve 121 | avalanche in c. 122 | 123 | This allows some parallelism. Read-after-writes are good at doubling 124 | the number of bits affected, so the goal of mixing pulls in the opposite 125 | direction as the goal of parallelism. I did what I could. Rotates 126 | seem to cost as much as shifts on every machine I could lay my hands 127 | on, and rotates are much kinder to the top and bottom bits, so I used 128 | rotates. 129 | ------------------------------------------------------------------------------- 130 | */ 131 | #define mix(a,b,c) \ 132 | { \ 133 | a -= c; a ^= rot(c, 4); c += b; \ 134 | b -= a; b ^= rot(a, 6); a += c; \ 135 | c -= b; c ^= rot(b, 8); b += a; \ 136 | a -= c; a ^= rot(c,16); c += b; \ 137 | b -= a; b ^= rot(a,19); a += c; \ 138 | c -= b; c ^= rot(b, 4); b += a; \ 139 | } 140 | 141 | /* 142 | ------------------------------------------------------------------------------- 143 | final -- final mixing of 3 32-bit values (a,b,c) into c 144 | 145 | Pairs of (a,b,c) values differing in only a few bits will usually 146 | produce values of c that look totally different. This was tested for 147 | * pairs that differed by one bit, by two bits, in any combination 148 | of top bits of (a,b,c), or in any combination of bottom bits of 149 | (a,b,c). 150 | * "differ" is defined as +, -, ^, or ~^. For + and -, I transformed 151 | the output delta to a Gray code (a^(a>>1)) so a string of 1's (as 152 | is commonly produced by subtraction) look like a single 1-bit 153 | difference. 154 | * the base values were pseudorandom, all zero but one bit set, or 155 | all zero plus a counter that starts at zero. 156 | 157 | These constants passed: 158 | 14 11 25 16 4 14 24 159 | 12 14 25 16 4 14 24 160 | and these came close: 161 | 4 8 15 26 3 22 24 162 | 10 8 15 26 3 22 24 163 | 11 8 15 26 3 22 24 164 | ------------------------------------------------------------------------------- 165 | */ 166 | #define final(a,b,c) \ 167 | { \ 168 | c ^= b; c -= rot(b,14); \ 169 | a ^= c; a -= rot(c,11); \ 170 | b ^= a; b -= rot(a,25); \ 171 | c ^= b; c -= rot(b,16); \ 172 | a ^= c; a -= rot(c,4); \ 173 | b ^= a; b -= rot(a,14); \ 174 | c ^= b; c -= rot(b,24); \ 175 | } 176 | 177 | /* 178 | ------------------------------------------------------------------------------- 179 | hashlittle() -- hash a variable-length key into a 32-bit value 180 | k : the key (the unaligned variable-length array of bytes) 181 | length : the length of the key, counting by bytes 182 | initval : can be any 4-byte value 183 | Returns a 32-bit value. Every bit of the key affects every bit of 184 | the return value. Two keys differing by one or two bits will have 185 | totally different hash values. 186 | 187 | The best hash table sizes are powers of 2. There is no need to do 188 | mod a prime (mod is sooo slow!). If you need less than 32 bits, 189 | use a bitmask. For example, if you need only 10 bits, do 190 | h = (h & hashmask(10)); 191 | In which case, the hash table should have hashsize(10) elements. 192 | 193 | If you are hashing n strings (uint8_t **)k, do it like this: 194 | for (i=0, h=0; i 12) 230 | { 231 | a += k[0]; 232 | b += k[1]; 233 | c += k[2]; 234 | mix(a,b,c); 235 | length -= 12; 236 | k += 3; 237 | } 238 | 239 | /*----------------------------- handle the last (probably partial) block */ 240 | /* 241 | * "k[2]&0xffffff" actually reads beyond the end of the string, but 242 | * then masks off the part it's not allowed to read. Because the 243 | * string is aligned, the masked-off tail is in the same word as the 244 | * rest of the string. Every machine with memory protection I've seen 245 | * does it on word boundaries, so is OK with this. But VALGRIND will 246 | * still catch it and complain. The masking trick does make the hash 247 | * noticably faster for short strings (like English words). 248 | */ 249 | #ifndef VALGRIND 250 | 251 | switch(length) 252 | { 253 | case 12: c+=k[2]; b+=k[1]; a+=k[0]; break; 254 | case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break; 255 | case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break; 256 | case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break; 257 | case 8 : b+=k[1]; a+=k[0]; break; 258 | case 7 : b+=k[1]&0xffffff; a+=k[0]; break; 259 | case 6 : b+=k[1]&0xffff; a+=k[0]; break; 260 | case 5 : b+=k[1]&0xff; a+=k[0]; break; 261 | case 4 : a+=k[0]; break; 262 | case 3 : a+=k[0]&0xffffff; break; 263 | case 2 : a+=k[0]&0xffff; break; 264 | case 1 : a+=k[0]&0xff; break; 265 | case 0 : return c; /* zero length strings require no mixing */ 266 | } 267 | 268 | #else /* make valgrind happy */ 269 | 270 | k8 = (const uint8_t *)k; 271 | switch(length) 272 | { 273 | case 12: c+=k[2]; b+=k[1]; a+=k[0]; break; 274 | case 11: c+=((uint32_t)k8[10])<<16; /* fall through */ 275 | case 10: c+=((uint32_t)k8[9])<<8; /* fall through */ 276 | case 9 : c+=k8[8]; /* fall through */ 277 | case 8 : b+=k[1]; a+=k[0]; break; 278 | case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */ 279 | case 6 : b+=((uint32_t)k8[5])<<8; /* fall through */ 280 | case 5 : b+=k8[4]; /* fall through */ 281 | case 4 : a+=k[0]; break; 282 | case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */ 283 | case 2 : a+=((uint32_t)k8[1])<<8; /* fall through */ 284 | case 1 : a+=k8[0]; break; 285 | case 0 : return c; 286 | } 287 | 288 | #endif /* !valgrind */ 289 | 290 | } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) { 291 | const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */ 292 | const uint8_t *k8; 293 | 294 | /*--------------- all but last block: aligned reads and different mixing */ 295 | while (length > 12) 296 | { 297 | a += k[0] + (((uint32_t)k[1])<<16); 298 | b += k[2] + (((uint32_t)k[3])<<16); 299 | c += k[4] + (((uint32_t)k[5])<<16); 300 | mix(a,b,c); 301 | length -= 12; 302 | k += 6; 303 | } 304 | 305 | /*----------------------------- handle the last (probably partial) block */ 306 | k8 = (const uint8_t *)k; 307 | switch(length) 308 | { 309 | case 12: c+=k[4]+(((uint32_t)k[5])<<16); 310 | b+=k[2]+(((uint32_t)k[3])<<16); 311 | a+=k[0]+(((uint32_t)k[1])<<16); 312 | break; 313 | case 11: c+=((uint32_t)k8[10])<<16; /* fall through */ 314 | case 10: c+=k[4]; 315 | b+=k[2]+(((uint32_t)k[3])<<16); 316 | a+=k[0]+(((uint32_t)k[1])<<16); 317 | break; 318 | case 9 : c+=k8[8]; /* fall through */ 319 | case 8 : b+=k[2]+(((uint32_t)k[3])<<16); 320 | a+=k[0]+(((uint32_t)k[1])<<16); 321 | break; 322 | case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */ 323 | case 6 : b+=k[2]; 324 | a+=k[0]+(((uint32_t)k[1])<<16); 325 | break; 326 | case 5 : b+=k8[4]; /* fall through */ 327 | case 4 : a+=k[0]+(((uint32_t)k[1])<<16); 328 | break; 329 | case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */ 330 | case 2 : a+=k[0]; 331 | break; 332 | case 1 : a+=k8[0]; 333 | break; 334 | case 0 : return c; /* zero length requires no mixing */ 335 | } 336 | 337 | } else { /* need to read the key one byte at a time */ 338 | const uint8_t *k = (const uint8_t *)key; 339 | 340 | /*--------------- all but the last block: affect some 32 bits of (a,b,c) */ 341 | while (length > 12) 342 | { 343 | a += k[0]; 344 | a += ((uint32_t)k[1])<<8; 345 | a += ((uint32_t)k[2])<<16; 346 | a += ((uint32_t)k[3])<<24; 347 | b += k[4]; 348 | b += ((uint32_t)k[5])<<8; 349 | b += ((uint32_t)k[6])<<16; 350 | b += ((uint32_t)k[7])<<24; 351 | c += k[8]; 352 | c += ((uint32_t)k[9])<<8; 353 | c += ((uint32_t)k[10])<<16; 354 | c += ((uint32_t)k[11])<<24; 355 | mix(a,b,c); 356 | length -= 12; 357 | k += 12; 358 | } 359 | 360 | /*-------------------------------- last block: affect all 32 bits of (c) */ 361 | switch(length) /* all the case statements fall through */ 362 | { 363 | case 12: c+=((uint32_t)k[11])<<24; /* fall through */ 364 | case 11: c+=((uint32_t)k[10])<<16; /* fall through */ 365 | case 10: c+=((uint32_t)k[9])<<8; /* fall through */ 366 | case 9 : c+=k[8]; /* fall through */ 367 | case 8 : b+=((uint32_t)k[7])<<24; /* fall through */ 368 | case 7 : b+=((uint32_t)k[6])<<16; /* fall through */ 369 | case 6 : b+=((uint32_t)k[5])<<8; /* fall through */ 370 | case 5 : b+=k[4]; /* fall through */ 371 | case 4 : a+=((uint32_t)k[3])<<24; /* fall through */ 372 | case 3 : a+=((uint32_t)k[2])<<16; /* fall through */ 373 | case 2 : a+=((uint32_t)k[1])<<8; /* fall through */ 374 | case 1 : a+=k[0]; 375 | break; 376 | case 0 : return c; 377 | } 378 | } 379 | 380 | final(a,b,c); 381 | return c; 382 | } 383 | 384 | //uint32_t __stdcall NHASH(const void *key, size_t length, uint32_t initval) 385 | -------------------------------------------------------------------------------- /src/nhash.h: -------------------------------------------------------------------------------- 1 | #ifdef __cplusplus 2 | extern "C" { 3 | #endif 4 | 5 | #ifndef NHASH_H_ 6 | #define NHASH_H_ 7 | 8 | uint32_t nhash_( const void *, int *, uint32_t *); 9 | 10 | #endif 11 | 12 | #ifdef __cplusplus 13 | } 14 | #endif -------------------------------------------------------------------------------- /src/rs_common.h: -------------------------------------------------------------------------------- 1 | /* Stuff common to all the general-purpose Reed-Solomon codecs 2 | * Copyright 2004 Phil Karn, KA9Q 3 | * May be used under the terms of the GNU Lesser General Public License (LGPL) 4 | */ 5 | 6 | #ifndef RS_COMMON_H_ 7 | #define RS_COMMON_H_ 8 | 9 | #include "int.h" 10 | 11 | /* Reed-Solomon codec control block */ 12 | struct rs { 13 | int mm; /* Bits per symbol */ 14 | int nn; /* Symbols per block (= (1<= rs->nn) { 27 | x -= rs->nn; 28 | x = (x >> rs->mm) + (x & rs->nn); 29 | } 30 | return x; 31 | } 32 | 33 | #endif 34 | --------------------------------------------------------------------------------