• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 HiSilicon (Shanghai) Technologies CO., LIMITED.
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 <hi_stdlib.h>
17 #include <hi_watchdog.h>
18 #include <hi_early_debug.h>
19 #include <hi_time.h>
20 #include <hi_io.h>
21 #include <hi_gpio.h>
22 #include <hi_adc.h>
23 #include <hi_task.h>
24 #include <hi_pwm.h>
25 #include "ssd1306_oled.h"
26 #include "iot_adc.h"
27 
28 #define     ADC_TEST_LENGTH             (20)
29 #define     VLT_MIN                     (100)
30 #define     OLED_FLAG_ON                ((unsigned char)0x01)
31 #define     OLED_FLAG_OFF               ((unsigned char)0x00)
32 
33 #define VOLTAGE_1_8_V  ((float)1.8)
34 #define VOLTAGE_4_TIMES (4)
35 
36 #define VOLTAGE_0_6_V   ((float)0.6)
37 #define VOLTAGE_1_V   ((float)1.0)
38 #define VOLTAGE_1_5_V   ((float)1.5)
39 #define VOLTAGE_3_V   ((float)3.0)
40 
41 #define ADC_CHANNEL_RANGE ((float)4096.0)
42 
43 unsigned short g_adcBuf[ADC_TEST_LENGTH] = { 0 };
44 unsigned short g_gpio5AdcBuf[ADC_TEST_LENGTH] = { 0 };
45 
46 /* get data from adc test */
GetLightStatus(void)47 unsigned char GetLightStatus(void)
48 {
49     unsigned short data = 0;
50     unsigned short vlt;
51 
52     float voltage;
53     float vltMax = 0;
54     float vltMin = VLT_MIN;
55     memset_s(g_adcBuf, sizeof(g_adcBuf), 0x0, sizeof(g_adcBuf));
56     for (int i = 0; i < ADC_TEST_LENGTH; i++) {
57         // ADC_Channal_6  自动识别模式  CNcomment:4次平均算法模式 CNend
58         unsigned int ret = AdcRead(IOT_ADC_CHANNEL_4, &data, HI_ADC_EQU_MODEL_4, HI_ADC_CUR_BAIS_DEFAULT, 0xF0);
59         if (ret != HI_ERR_SUCCESS) {
60             printf("ADC Read Fail\n");
61             return  HI_NULL;
62         }
63         g_adcBuf[i] = data;
64     }
65     for (int j = 0; j < ADC_TEST_LENGTH; j++) {
66         vlt = g_adcBuf[j];
67         voltage = (float)vlt * VOLTAGE_1_8_V * \
68                         VOLTAGE_4_TIMES / ADC_CHANNEL_RANGE; /* vlt * 1.8 * 4 / 4096.0为将码字转换为电压 */
69         vltMax = (voltage > vltMax) ? voltage : vltMax;
70         vltMin = (voltage < vltMin) ? voltage : vltMin;
71     }
72 
73     if (vltMax > VOLTAGE_3_V) { /* 判断电压是否大于3.0V */
74         return OLED_FLAG_ON;
75     } else {
76         return OLED_FLAG_OFF;
77     }
78 }
79 /* get gpio5 Voltage */
GetGpio5Voltage(const char * param)80 void GetGpio5Voltage(const char *param)
81 {
82     unsigned short data = 0;
83     unsigned short vlt;
84     float voltage;
85     float vltMax = 0;
86     float vltMin = VLT_MIN;
87 
88     hi_unref_param(param);
89     memset_s(g_gpio5AdcBuf, sizeof(g_gpio5AdcBuf), 0x0, sizeof(g_gpio5AdcBuf));
90     for (int i = 0; i < ADC_TEST_LENGTH; i++) {
91         // ADC_Channal_2  自动识别模式  CNcomment:4次平均算法模式 CNend */
92         unsigned int ret = AdcRead(IOT_ADC_CHANNEL_2, &data, IOT_ADC_EQU_MODEL_4, IOT_ADC_CUR_BAIS_DEFAULT, 0xF0);
93         if (ret != HI_ERR_SUCCESS) {
94             printf("ADC Read Fail\n");
95             return  HI_NULL;
96         }
97         g_gpio5AdcBuf[i] = data;
98     }
99 
100     for (int i = 0; i < ADC_TEST_LENGTH; i++) {
101         vlt = g_gpio5AdcBuf[i];
102         /* vlt * 1.8* 4 / 4096.0为将码字转换为电压 */
103         voltage = (float)vlt * VOLTAGE_1_8_V * VOLTAGE_4_TIMES / ADC_CHANNEL_RANGE;
104         vltMax = (voltage > vltMax) ? voltage : vltMax;
105         vltMin = (voltage < vltMin) ? voltage : vltMin;
106     }
107     if (vltMax > VOLTAGE_0_6_V && vltMax < VOLTAGE_1_V) { /* 电压最大值大于0.6小于1.0 */
108         GpioKey1IsrFuncMode();
109     } else if (vltMin > VOLTAGE_1_V && vltMin < VOLTAGE_1_5_V) { /* 电压最大值大于1.0小于1.5 */
110         GpioKey2IsrFuncType();
111     } else if (vltMax < VOLTAGE_0_6_V) { /* 电压最大值小于0.6 */
112         printf("gpio9_led_light:vltMax=%0.2f, vltMin=%0.2f\r\n", vltMax, vltMin);
113         Gpio9LedLightFunc();
114     }
115     printf("key_5:vltMax=%0.2f, vltMin=%0.2f\r\n", vltMax, vltMin);
116 }