1 /*
2  * Tlf - contest logging program for amateur radio operators
3  * Copyright (C) 2018 Thomas Beierlein <tb@forth-ev.de>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19 
20 /* Definitions and functions related to manipulation of frequencies and
21  * ham radio bands
22  */
23 
24 #include "bands.h"
25 
26 const unsigned int bandcorner[NBANDS][2] = {
27     { 1800000, 2000000 },	// band bottom, band top
28     { 3500000, 4000000 },
29     { 5250000, 5450000 },       // 5351500-5356500 worldwide
30     { 7000000, 7300000 },
31     { 10100000, 10150000 },
32     { 14000000, 14350000 },
33     { 18068000, 18168000 },
34     { 21000000, 21450000 },
35     { 24890000, 24990000 },
36     { 28000000, 29700000 },
37     {        0,        0 }
38 };
39 
40 const unsigned int cwcorner[NBANDS] = {
41     1838000,
42     3580000,
43     5354000,
44     7040000,
45     10140000,
46     14070000,
47     18095000,
48     21070000,
49     24915000,
50     28070000,
51     0
52 };
53 
54 const unsigned int ssbcorner[NBANDS] = {
55     1840000,
56     3600000,
57     5354000,
58     7040000,
59     10150000,
60     14100000,
61     18120000,
62     21150000,
63     24930000,
64     28300000,
65     0
66 };
67 
68 /** Converts bandindex to bandmask */
69 int inxes[NBANDS] = \
70 {
71     BAND160, BAND80, BAND60, BAND40, BAND30, BAND20, BAND17, BAND15, BAND12, BAND10,
72     BANDOOB
73 };
74 
75 
76 /** Converts bandnumber to bandindex */
bandnr2index(int nr)77 int bandnr2index(int nr) {
78     switch (nr) {
79 
80 	case 160:
81 	    return BANDINDEX_160;
82 
83 	case 80:
84 	    return BANDINDEX_80;
85 
86 	case 40:
87 	    return BANDINDEX_40;
88 
89 	case 60:
90 	    return BANDINDEX_60;
91 
92 	case 20:
93 	    return BANDINDEX_20;
94 
95 	case 15:
96 	    return BANDINDEX_15;
97 
98 	case 10:
99 	    return BANDINDEX_10;
100 
101 	case 12:
102 	    return BANDINDEX_12;
103 
104 	case 17:
105 	    return BANDINDEX_17;
106 
107 	case 30:
108 	    return BANDINDEX_30;
109 	default:
110 	    return BANDINDEX_OOB;
111     }
112 }
113 
114 
115 /* converts bandindex to bandnumber */
116 static int bandnr[NBANDS] =
117 { 160, 80, 60, 40, 30, 20, 17, 15, 12, 10, 0 };
118 
bandindex2nr(int index)119 int bandindex2nr(int index) {
120     return bandnr[index];
121 }
122 
123 
124 extern int bandinx;
125 
next_band(int direction)126 void next_band(int direction) {
127     bandinx += direction;
128 
129     if (bandinx < 0) {
130 	bandinx = BANDINDEX_OOB - 1;
131     }
132 
133     if (bandinx >= BANDINDEX_OOB) {
134 	bandinx = 0;
135     }
136 }
137 
138 
139 /** \brief convert frequency in Hz to bandindex
140  *
141  * \return	bandindex or BANDINDEX_OOB if not in any band
142  */
freq2band(unsigned int freq)143 int freq2band(unsigned int freq) {
144     int i;
145 
146     for (i = 0; i < NBANDS; i++) {
147 	if (freq >= bandcorner[i][0] &&
148 		freq <= bandcorner[i][1])
149 	    return i;	/* in actual band */
150     }
151 
152     return BANDINDEX_OOB;   /* not in any band (out of band) */
153 }
154