aboutsummaryrefslogtreecommitdiff
path: root/src/survive_config.c
blob: 42c2c890b4ec3ac6242de721978f35276af98a1a (plain)
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
#include <string.h>
#include <assert.h>
#include "survive_config.h"

#define MAX_CONFIG_ENTRIES 100

config_val config_values[MAX_CONFIG_ENTRIES];
static uint16_t used_entries = 0;

static FILE *config_file = NULL;

void config_init() {
	uint16_t i = 0;
	for (i=0;i<MAX_CONFIG_ENTRIES;++i) {
		config_values[i].str = NULL;
		config_values[i].tag = NULL;
	}

	used_entries = 0;
}

void write_float(char* tag, float x) {
	fprintf(config_file, "\"%s\":\"%f\"\n", tag, x);
}

void write_float_a(char* tag, float *x, uint8_t count) {
	uint8_t i = 0;
	char idx[4];
	for (i=0;i<count;++i) {
		sprintf(idx,"%d",i);
		fprintf(config_file, "\"%s%s\":\"%f\"\n", tag,idx, x);
	}
	fprintf(config_file, "\"%s\":\"%f\"\n", tag, x);
}

void write_uint32(char* tag, uint32_t x) {
	fprintf(config_file, "\"%s\":\"%d\"\n", tag, x);
}

void config_open(const char* path, const char* mode) {
	config_file = fopen(path, mode);
}

void config_close() {
	fclose(config_file);
}

void config_write_lighthouse(struct BaseStationData* bsd, uint8_t length) {
	uint8_t i = 0;

	float Position[3];
	float Quaternion[4];
	uint32_t BaseStationID;
	float fcalphase[2];
	float fcaltilt[2];
	float fcalcurve[2];
	float fcalgibpha[2];
	float fcalgibmag[2];

	for (i=0;i<length; ++i) {
		write_uint32("id", bsd[i].BaseStationID);
		write_float_a("position", bsd[i].Position, 3);
		write_float_a("quaternion", bsd[i].Quaternion, 4);
		write_float_a("quaternion", bsd[i].Quaternion, 4);
		write_float_a("fcalphase", bsd[i].fcalphase, 2);
		write_float_a("fcaltilt", bsd[i].fcaltilt,2);
		write_float_a("fcalcurve", bsd[i].fcalcurve,2);
		write_float_a("fcalgibpha", bsd[i].fcalgibpha,2);
		write_float_a("fcalgibmag", bsd[i].fcalgibmag,2);
	}
}

void sstrcpy(char* dest, const char *src) {
	uint32_t len = strlen(src)+1;
	if (dest == NULL) {
		dest = (char*)malloc(len);
	} else {
		dest = (char*)realloc(dest, len);
	}
	strcpy(dest,src);
}

const char* config_read_str(const char *tag, const char *value, const char *def_str) {
	uint16_t i = 0;
	for (i=0;i<used_entries;++i) {
		if ( strcmp(config_values[i].tag, tag) == 0 ) {
			return config_values[i].str;
		}
	}
	assert(used_entries<MAX_CONFIG_ENTRIES);

	i = used_entries++;
	sstrcpy(config_values[i].tag, tag);
	sstrcpy(config_values[i].str, def_str);

	return config_values[i].str;
}

uint32_t config_read_uint32(const char *tag, const uint32_t value, const uint32_t def) {
	uint16_t i = 0;
	for (i=0;i<used_entries;++i) {
		if ( strcmp(config_values[i].tag, tag) == 0 ) {
			return config_values[i].numeric.i;
		}
	}
	assert(used_entries<MAX_CONFIG_ENTRIES);

	i = used_entries++;
	sstrcpy(config_values[i].tag, tag);
	config_values[i].numeric.i = def;

	return config_values[i].numeric.i;
}

float config_read_float(const char *tag, const float value, const float def) {
	uint16_t i = 0;
	for (i=0;i<used_entries;++i) {
		if ( strcmp(config_values[i].tag, tag) == 0 ) {
			return config_values[i].numeric.f;
		}
	}
	assert(used_entries<MAX_CONFIG_ENTRIES);

	i = used_entries++;
	sstrcpy(config_values[i].tag, tag);
	config_values[i].numeric.f = def;

	return config_values[i].numeric.f;
}

const char* config_set_str(const char *tag, const char* value) {
	uint16_t i = 0;

	assert(used_entries<MAX_CONFIG_ENTRIES);

	i = used_entries++;
	sstrcpy(config_values[i].tag, tag);
	sstrcpy(config_values[i].str, value);

	return value;
}

const uint32_t config_set_uint32(const char *tag, const uint32_t value) {
	uint16_t i = 0;

	assert(used_entries<MAX_CONFIG_ENTRIES);

	i = used_entries++;
	sstrcpy(config_values[i].tag, tag);
	config_values[i].numeric.i = value;

	return value;
}

const float config_set_float(const char *tag, const float value) {
	uint16_t i = 0;

	assert(used_entries<MAX_CONFIG_ENTRIES);

	i = used_entries++;
	sstrcpy(config_values[i].tag, tag);
	config_values[i].numeric.f = value;

	return value;
}
void config_save(const char* path) {
	uint16_t i = 0;

	FILE* f = fopen(path, "w");

	for (i=0;i<=used_entries;++i) {
		if (config_values[i].type == CONFIG_FLOAT) {
			fprintf(f, "\"%s\":\"%F\"\n", config_values[i].tag, config_values[i].numeric.f);
		} else if (config_values[i].type == CONFIG_UINT32) {
			fprintf(f, "\"%s\":\"%F\"\n", config_values[i].tag, config_values[i].numeric.i);
		} else if (config_values[i].type == CONFIG_STRING) {
			fprintf(f, "\"%s\":\"%s\"\n", config_values[i].tag, config_values[i].str);
		}
	};

	fclose(f);
}