summaryrefslogtreecommitdiffstats
path: root/sensors/akmdfs/AKFS_FileIO.c
blob: 92c2ce985203cd48ff28223e2aba056e609c1dde (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
/******************************************************************************
 *
 * Copyright (C) 2012 Asahi Kasei Microdevices Corporation, Japan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************************/
#include "AKFS_FileIO.h"

/*** Constant definition ******************************************************/
#ifdef AKFS_PRECISION_DOUBLE
#define AKFS_SCANF_FORMAT	"%63s = %lf"
#else
#define AKFS_SCANF_FORMAT	"%63s = %f"
#endif
#define AKFS_PRINTF_FORMAT	"%s = %f\n"
#define LOAD_BUF_SIZE	64

/*!
 Load parameters from file which is specified with #path.  This function reads 
  data from a beginning of the file line by line, and check parameter name 
  sequentially. In otherword, this function depends on the order of eache 
  parameter described in the file.
 @return If function fails, the return value is #AKM_FAIL. When function fails,
  the output is undefined. Therefore, parameters which are possibly overwritten
  by this function should be initialized again. If function succeeds, the
  return value is #AKM_SUCCESS.
 @param[out] prms A pointer to #AK8975PRMS structure. Loaded parameter is
  stored to the member of this structure.
 @param[in] path A path to the setting file.
 */
int16 AKFS_LoadParameters(AK8975PRMS * prms, const char* path)
{
	int16 ret;
	char buf[LOAD_BUF_SIZE];
	FILE *fp = NULL;

	/* Open setting file for read. */
	if ((fp = fopen(path, "r")) == NULL) {
		AKMERROR_STR("fopen");
		return AKM_FAIL;
	}

	ret = 1;

	/* Load data to HO */
	if (fscanf(fp, AKFS_SCANF_FORMAT, buf, &prms->mfv_ho.u.x) != 2) {
		ret = 0;
	} else {
		if (strncmp(buf, "HO.x", sizeof(buf)) != 0) {
			ret = 0;
		}
	}
	if (fscanf(fp, AKFS_SCANF_FORMAT, buf, &prms->mfv_ho.u.y) != 2) {
		ret = 0;
	} else {
		if (strncmp(buf, "HO.y", sizeof(buf)) != 0) {
			ret = 0;
		}
	}
	if (fscanf(fp, AKFS_SCANF_FORMAT, buf, &prms->mfv_ho.u.z) != 2) {
		ret = 0;
	} else {
		if (strncmp(buf, "HO.z", sizeof(buf)) != 0) {
			ret = 0;
		}
	}

	if (fclose(fp) != 0) {
		AKMERROR_STR("fclose");
		ret = 0;
	}

	if (ret == 0) {
		AKMERROR;
		return AKM_FAIL;
	}

	return AKM_SUCCESS;
}

/*!
 Save parameters to file which is specified with #path. This function saves 
  variables when the offsets of magnetic sensor estimated successfully.
 @return If function fails, the return value is #AKM_FAIL. When function fails,
  the parameter file may collapsed. Therefore, the parameters file should be
  discarded. If function succeeds, the return value is #AKM_SUCCESS.
 @param[out] prms A pointer to #AK8975PRMS structure. Member variables are
  saved to the parameter file.
 @param[in] path A path to the setting file.
 */
int16 AKFS_SaveParameters(AK8975PRMS *prms, const char* path)
{
	int16 ret = 1;
	FILE *fp;

	/*Open setting file for write. */
	if ((fp = fopen(path, "w")) == NULL) {
		AKMERROR_STR("fopen");
		return AKM_FAIL;
	}

	/* Save data to HO */
	if (fprintf(fp, AKFS_PRINTF_FORMAT, "HO.x", prms->mfv_ho.u.x) < 0) { ret = 0; }
	if (fprintf(fp, AKFS_PRINTF_FORMAT, "HO.y", prms->mfv_ho.u.y) < 0) { ret = 0; }
	if (fprintf(fp, AKFS_PRINTF_FORMAT, "HO.z", prms->mfv_ho.u.z) < 0) { ret = 0; }

	if (fclose(fp) != 0) {
		AKMERROR_STR("fclose");
		ret = 0;
	}

	if (ret == 0) {
		AKMERROR;
		return AKM_FAIL;
	}

	return AKM_SUCCESS;
}