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 "iot_adc.h"
26 #include "ssd1306_oled.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 = 0;
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_Channel_6 自动识别模式 CNcomment:4次平均算法模式 CNend
58 unsigned int ret = AdcRead(IOT_ADC_CHANNEL_4, &data,
59 HI_ADC_EQU_MODEL_4, HI_ADC_CUR_BAIS_DEFAULT, 0xF0);
60 if (ret != HI_ERR_SUCCESS) {
61 printf("ADC Read Fail\n");
62 return HI_NULL;
63 }
64 g_adcBuf[i] = data;
65 }
66 for (int j = 0; j < ADC_TEST_LENGTH; j++) {
67 vlt = g_adcBuf[j];
68 voltage = (float)vlt * VOLTAGE_1_8_V *
69 VOLTAGE_4_TIMES / ADC_CHANNEL_RANGE; /* vlt * 1.8 * 4 / 4096.0为将码字转换为电压 */
70 vltMax = (voltage > vltMax) ? voltage : vltMax;
71 vltMin = (voltage < vltMin) ? voltage : vltMin;
72 }
73
74 if (vltMax > VOLTAGE_3_V) { /* 判断电压是否大于3.0V */
75 return OLED_FLAG_ON;
76 } else {
77 return OLED_FLAG_OFF;
78 }
79 }
80
81 /* get gpio5 Voltage */
GetGpio5Voltage(const char * param)82 void GetGpio5Voltage(const char *param)
83 {
84 unsigned short data = 0;
85 unsigned short vlt = 0;
86 float voltage;
87 float vltMax = 0;
88 float vltMin = VLT_MIN;
89
90 hi_unref_param(param);
91 memset_s(g_gpio5AdcBuf, sizeof(g_gpio5AdcBuf), 0x0, sizeof(g_gpio5AdcBuf));
92 for (int i = 0; i < ADC_TEST_LENGTH; i++) {
93 // ADC_Channal_2 自动识别模式 CNcomment:4次平均算法模式 CNend
94 unsigned int ret = AdcRead(IOT_ADC_CHANNEL_2, &data,
95 IOT_ADC_EQU_MODEL_4, IOT_ADC_CUR_BAIS_DEFAULT, 0xF0);
96 if (ret != HI_ERR_SUCCESS) {
97 printf("ADC Read Fail\n");
98 return HI_NULL;
99 }
100 g_gpio5AdcBuf[i] = data;
101 }
102
103 for (int i = 0; i < ADC_TEST_LENGTH; i++) {
104 vlt = g_gpio5AdcBuf[i];
105 voltage = (float)vlt * VOLTAGE_1_8_V *
106 VOLTAGE_4_TIMES / ADC_CHANNEL_RANGE; /* vlt * 1.8* 4 / 4096.0为将码字转换为电压 */
107 vltMax = (voltage > vltMax) ? voltage : vltMax;
108 vltMin = (voltage < vltMin) ? voltage : vltMin;
109 }
110 if (vltMax > VOLTAGE_0_6_V && vltMax < VOLTAGE_1_V) { /* 电压最大值大于0.6小于1.0 */
111 GpioKey1IsrFuncMode();
112 } else if (vltMax > VOLTAGE_1_V && vltMax < VOLTAGE_1_5_V) { /* 电压最大值大于1.0小于1.5 */
113 GpioKey2IsrFuncType();
114 } else if (vltMax < VOLTAGE_0_6_V) { /* 电压最大值小于0.6 */
115 printf("gpio9_led_light:vltMax=%0.2f, vltMin=%0.2f\r\n", vltMax, vltMin);
116 Gpio9LedLightFunc();
117 }
118 printf("key_5:vltMax=%0.2f, vltMin=%0.2f\r\n", vltMax, vltMin);
119 }