you.chen | 79d8f93 | 2023-12-26 17:03:44 +0800 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <liblog/lynq_deflog.h> |
| 4 | #include "include/liblynq-qser-adc.h" |
| 5 | #define USER_LOG_TAG "LIBLYNQ_QSER_ADC" |
| 6 | #undef LOG_TAG |
| 7 | #define LOG_TAG "LIBLYNQ_QSER_ADC" |
| 8 | int qser_adc_show(ADC_CHANNEL_E qadc) |
| 9 | { |
| 10 | int adc_value; |
| 11 | char cmd[1024]; |
| 12 | char cmd_ret[1024]; |
| 13 | FILE *fp; |
| 14 | switch(qadc) |
| 15 | { |
| 16 | case QADC_NONE: |
| 17 | { |
| 18 | LYERRLOG("function %s line %d\n", __FUNCTION__, __LINE__); |
| 19 | return 0; |
| 20 | } |
| 21 | case ADC0: |
| 22 | { |
| 23 | LYDBGLOG("function %s line %d\n", __FUNCTION__, __LINE__); |
| 24 | sprintf(cmd,"cat /sys/bus/iio/devices/iio:device1/in_voltage0_input"); |
| 25 | break; |
| 26 | } |
| 27 | case ADC1: |
| 28 | { |
| 29 | LYDBGLOG("function %s line %d\n", __FUNCTION__, __LINE__); |
| 30 | sprintf(cmd,"cat /sys/bus/iio/devices/iio:device1/in_voltage1_input"); |
| 31 | break; |
| 32 | } |
| 33 | case ADC2: |
| 34 | { |
| 35 | LYDBGLOG("function %s line %d\n", __FUNCTION__, __LINE__); |
| 36 | sprintf(cmd,"cat /sys/bus/iio/devices/iio:device1/in_voltage2_input"); |
| 37 | break; |
| 38 | } |
| 39 | default: |
| 40 | { |
| 41 | LYERRLOG("input error\n"); |
| 42 | return -1; |
| 43 | } |
| 44 | } |
| 45 | if((fp = popen(cmd,"r")) == NULL) |
| 46 | { |
| 47 | LYERRLOG("popen error!"); |
| 48 | return -1; |
| 49 | } |
| 50 | if((fread(cmd_ret,sizeof(cmd_ret),1,fp))<0) |
| 51 | { |
| 52 | LYERRLOG("fread fail!"); |
| 53 | fclose(fp); |
| 54 | return -1; |
| 55 | } |
| 56 | fclose(fp); |
| 57 | |
| 58 | adc_value = atoi(cmd_ret); |
| 59 | if (adc_value < 0 || adc_value > 12000) |
| 60 | { |
| 61 | LYERRLOG("bad adc value %s!", cmd_ret); |
| 62 | return -1; |
| 63 | } |
| 64 | |
| 65 | return adc_value; |
| 66 | } |