b.liu | 15f456b | 2024-10-31 20:16:06 +0800 | [diff] [blame^] | 1 | /** |
| 2 | * \file mbtk_adc.c |
| 3 | * \brief A Documented file. |
| 4 | * |
| 5 | * Detailed description |
| 6 | * \Author: js.wang <js.wang@mobiletek.cn> |
| 7 | * \Version: 1.0.0 |
| 8 | * \Date: 2022-04-22 |
| 9 | */ |
| 10 | #include <fcntl.h> |
| 11 | #include <stdint.h> |
| 12 | #include <limits.h> |
| 13 | #include <termios.h> |
| 14 | #include <stdarg.h> |
| 15 | #include <dirent.h> |
| 16 | #include <sys/stat.h> |
| 17 | #include <sys/statfs.h> |
| 18 | #include <sys/types.h> |
| 19 | #include "mbtk_log.h" |
| 20 | #include "mbtk_type.h" |
| 21 | #include "mbtk_adc.h" |
| 22 | |
| 23 | int mbtk_adc_close(const char* adc_dev) |
| 24 | { |
| 25 | int ret = 0; |
| 26 | int fd = 0; |
| 27 | char adc = '3'; |
| 28 | //system("echo 3 > /sys/kernel/debug/adc"); |
| 29 | if(adc_dev != NULL && !access(adc_dev, R_OK)) |
| 30 | { |
| 31 | //LOGI("DEV:%s", ADC_DEVICE_803); |
| 32 | fd = open(adc_dev, O_RDWR|O_CREAT|O_TRUNC, 0644); |
| 33 | } |
| 34 | else |
| 35 | { |
| 36 | LOGE("No found ADC devices."); |
| 37 | return -1; |
| 38 | } |
| 39 | |
| 40 | if(fd < 0) { |
| 41 | LOGE("[%s] file open error\n", __FUNCTION__); |
| 42 | return -2; |
| 43 | } |
| 44 | ret = write(fd, &adc, 1); |
| 45 | if (ret < 0) { |
| 46 | LOGE("%s: error writing to file!\n", __FUNCTION__); |
| 47 | close(fd); |
| 48 | return -2; |
| 49 | } |
| 50 | close(fd); |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | int mbtk_adc_get(const char* adc_dev, mbtk_adc_enum channle) |
| 55 | { |
| 56 | int ret = 0; |
| 57 | int fd = 0; |
| 58 | char adc_buf[24] = {0}; |
| 59 | char *adc_value = NULL; |
| 60 | char adc =(channle == MBTK_ADC0 ? '0' : (channle == MBTK_ADC1 ? '1' : '2')); |
| 61 | |
| 62 | |
| 63 | if(adc_dev != NULL && !access(adc_dev, R_OK)) |
| 64 | { |
| 65 | LOGI("[adc] DEV:%s", adc_dev); |
| 66 | fd = open(adc_dev, O_RDWR|O_CREAT|O_TRUNC, 0644); |
| 67 | } |
| 68 | else |
| 69 | { |
| 70 | LOGE("No found ADC devices : %s", adc_dev ? adc_dev : "NULL"); |
| 71 | return -1; |
| 72 | } |
| 73 | |
| 74 | if(fd < 0) { |
| 75 | LOGE("[%s] file open error\n", __FUNCTION__); |
| 76 | return -2; |
| 77 | } |
| 78 | ret = write(fd, &adc, 1); |
| 79 | if (ret < 0) { |
| 80 | LOGE("%s: error writing to file!\n", __FUNCTION__); |
| 81 | close(fd); |
| 82 | return -2; |
| 83 | } |
| 84 | ret = read(fd, adc_buf, 24); |
| 85 | if (ret < 0) { |
| 86 | LOGE("%s: error writing to file!\n", __FUNCTION__); |
| 87 | close(fd); |
| 88 | return -2; |
| 89 | }else{ |
| 90 | //LOGI("%s %d adc:%s\n", __FUNCTION__, __LINE__, adc_buf); |
| 91 | adc_value = strstr(adc_buf, "channel"); |
| 92 | } |
| 93 | close(fd); |
| 94 | if(adc_value) |
| 95 | { |
| 96 | //LOGI("%s adc: %s\n", __FUNCTION__, adc_value); |
| 97 | } |
| 98 | else |
| 99 | return -2; |
| 100 | |
| 101 | return atoi(&adc_value[9]); |
| 102 | } |