-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathr3config.c
225 lines (206 loc) · 6.27 KB
/
r3config.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include "fusion-c/header/msx_fusion.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <float.h>
#include <math.h>
char CfgName[20] = "REALADSB.CFG"; // Name of configuration file
char Airport[6] = "KEWR"; // ICAO code of the airport
float Latitude = 40.6924798; // Latitude of the airport
float Longitude = -74.1686868; // Longitude of the airport
char IPPort[80] = "192.168.1.153:5567"; // IP:port of adsb_hub3
char tmpString[256];
char latString[15];
char lonString[15];
FCB file;
// Big thanks to https://github.com/aralbrec (z88dk) for giving me an idea
// x - the number to be converted
// f - number of digits to follow decimal point
// *str - output string
void ftoa(float x, int f,char *str)
{
float scale; /* scale factor */
int i, /* copy of f, and # digits before decimal point */
d; /* a digit */
if( x < 0.0 ) {
*str++ = '-' ;
x = -x ;
}
i = f ;
scale = 2.0 ;
while ( i-- )
scale *= 10.0 ;
x += 1.0 / scale ;
/* count places before decimal & scale the number */
i = 0 ;
scale = 1.0 ;
while ( x >= scale ) {
scale *= 10.0 ;
i++ ;
}
if ( i == 0 )
*str++ = '0';
while ( i-- ) {
/* output digits before decimal */
scale = floorf(0.5 + scale * 0.1 ) ;
d = ( x / scale ) ;
*str++ = d + '0' ;
x -= (float)d * scale ;
}
if ( f <= 0 ) {
*str = 0;
return ;
}
*str++ = '.' ;
while ( f-- ) {
/* output digits after decimal */
x *= 10.0 ;
d = x;
*str++ = d + '0' ;
x -= d ;
}
*str = 0;
}
void FT_SetName( FCB *p_fcb, const char *p_name )
{
char i, j;
memset( p_fcb, 0, sizeof(FCB) );
// TODO: Change number to use another drive
p_fcb->drive_no = 0;
for( i = 0; i < 8; i++ ) {
p_fcb->name[i] = ' ';
}
for( i = 0; (i < 8) && (p_name[i] != 0) && (p_name[i] != '.'); i++ ) {
p_fcb->name[i] = p_name[i];
}
if( p_name[i] == '.' ) {
i++;
for( j = 0; (j < 3) && (p_name[i + j] != 0) && (p_name[i + j] != '.'); j++ ) {
p_fcb->ext[j] = p_name[i + j] ;
}
}
}
void loadConfiguration()
{
unsigned int n, pos, index=0, dstpos=0;
char ch;
char newline=0;
FT_SetName(&file, CfgName);
// Trying to open
if(fcb_open(&file) == FCB_SUCCESS)
{
n = fcb_read(&file, tmpString, sizeof(tmpString));
if(n>0)
{
for(pos=0; pos<n; pos++)
{
ch = tmpString[pos];
if(IsAlpha(ch)>0 || IsDigit(ch)>0 || ch=='-' || ch=='.' || ch==':')
{
// Add character
if(index==0)
Airport[dstpos] = ch;
else
if(index==1)
latString[dstpos] = ch;
else
if(index==2)
lonString[dstpos] = ch;
else
if(index==3)
IPPort[dstpos] = ch;
dstpos++;
newline = 1;
}
else
{
if(newline==1)
{
if(index==0)
{
Airport[dstpos] = 0;
//printf("%s ", Airport);
}
else
if(index==1)
{
latString[dstpos] = 0;
//printf("%s ", latString);
}
else
if(index==2)
{
lonString[dstpos] = 0;
//printf("%s ", lonString);
}
else
if(index==3)
{
IPPort[dstpos] = 0;
//printf("%s ", IPPort);
}
newline=0;
dstpos=0;
index++;
}
}
}
}
fcb_close(&file);
}
}
void saveConfiguration()
{
FT_SetName(&file, CfgName);
// Trying to open
if(fcb_open(&file) != FCB_SUCCESS)
{
// Can't open then create
if(fcb_create(&file) != FCB_SUCCESS)
{
printf("ERROR");
return;
}
}
fcb_write(&file, tmpString, strlen(tmpString));
fcb_close(&file);
printf("OK\r\n");
}
void main(void)
{
Screen(0);
Width(80);
PrintString("RealADSB 0.3 Config for MSX-DOS\r\n");
PrintString("-------------------------------\r\n");
// Load configuration
ftoa(Latitude,7,latString);
ftoa(Longitude,7,lonString);
printf("Loading configuration from %s...\r\n", CfgName);
loadConfiguration();
//
printf("Airport ICAO code [%s]:", Airport);
if(InputString(tmpString, 6)==4)
StrCopy(Airport, tmpString);
printf("Your latitude [%s]:", latString);
if(InputString(tmpString, 20)>0)
{
StrCopy(latString, tmpString);
Latitude = atof(tmpString);
}
printf("Your longitude [%s]:", lonString);
if(InputString(tmpString, 20)>0)
{
StrCopy(lonString, tmpString);
Longitude = atof(tmpString);
}
printf("adsb_hub3 IP:port [%s]:", IPPort);
if(InputString(tmpString, 80)>0)
StrCopy(IPPort, tmpString);
printf("Writing configuration to %s...", CfgName);
// Save configuration
sprintf(tmpString,"%s\r\n%s\r\n%s\r\n%s\r\n",
Airport, latString, lonString, IPPort);
saveConfiguration();
printf("\r\nPress any key...");
InputString(tmpString, 80);
}