1 /*
2 * Copyright (c) 2022 Unionman Technology Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <stdio.h>
17 #include <securec.h>
18 #include "um_adc.h"
19
get_adc_data(int adc_channel,int * value)20 int get_adc_data(int adc_channel, int *value)
21 {
22 if (value == NULL) {
23 printf("value pointer error\n");
24 return ADC_ERROR;
25 }
26
27 char adc_path[128];
28 (void)memset_s(adc_path, sizeof(adc_path), 0, sizeof(adc_path));
29
30 if (adc_channel == ADC_1) {
31 (void)sprintf_s(adc_path, sizeof(adc_path), "%s", ADC_CHANNEL_1);
32 } else if (adc_channel == ADC_2) {
33 (void)sprintf_s(adc_path, sizeof(adc_path), "%s", ADC_CHANNEL_2);
34 } else {
35 printf("no such a adc_channel\n");
36 return ADC_ERROR;
37 }
38
39 FILE *fp = NULL;
40 char buffer[sizeof(int)];
41 fp = fopen(adc_path, "r");
42 if (!fp) {
43 perror("fopen");
44 return ADC_ERROR;
45 }
46 (void)fread(buffer, sizeof(buffer), 1, fp);
47 (void)fclose(fp);
48 fp = NULL;
49
50 *value = atoi(buffer);
51
52 return 0;
53 }
54