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 <stdio.h>
17 #include <unistd.h>
18 #include <iot_pwm.h>
19 #include "ohos_init.h"
20 #include "cmsis_os2.h"
21 #include "iot_i2c.h"
22 #include "app_demo_multi_sample.h"
23 #include "app_demo_mq2.h"
24 #include "app_demo_i2c_oled.h"
25 #include "app_demo_aht20.h"
26 #include "app_demo_config.h"
27 #include "app_demo_environment.h"
28 
29 /* temperature menu display */
ShowTemperatureValue(void)30 void ShowTemperatureValue(void)
31 {
32     static unsigned short currentMode = 0;
33     unsigned short temperatureStr[6] = {0};
34     IoTI2cInit(0, HI_I2C_IDX_BAUDRATE); /* baudrate: 400000 */
35     IoTI2cSetBaudrate(0, HI_I2C_IDX_BAUDRATE);
36 
37     currentMode = GetKeyStatus(CURRENT_MODE);
38 
39     while (1) {
40         hi_udelay(DELAY_10_MS); // delay 10ms
41         GetAht20SensorData();
42         (void*)FloatToString(GetAhtSensorValue(AHT_TEMPERATURE), temperatureStr);
43         OledShowStr(OLED_X_POSITION_40, OLED_Y_POSITION_5,
44                     temperatureStr, OLED_DISPLAY_STRING_TYPE_1); /* 40, 5, x.xx, 1 */
45         if (currentMode != GetKeyStatus(CURRENT_MODE)) {
46             currentMode = GetKeyStatus(CURRENT_MODE);
47             break;
48         }
49         TaskMsleep(SLEEP_10_MS); // 10ms
50     }
51 }
52 
53 /* humidity value display */
ShowHumidityValue(void)54 void ShowHumidityValue(void)
55 {
56     static unsigned short currentMode = 0;
57     unsigned short humidityStr[6] = {0};
58     IoTI2cInit(0, HI_I2C_IDX_BAUDRATE); /* baudrate: 400000 */
59     IoTI2cSetBaudrate(0, HI_I2C_IDX_BAUDRATE);
60 
61     currentMode = GetKeyStatus(CURRENT_MODE);
62     while (1) {
63         hi_udelay(DELAY_10_MS); // delay 10ms
64         GetAht20SensorData();
65         (void*)FloatToString(GetAhtSensorValue(AHT_HUMIDITY), humidityStr);
66         OledShowStr(OLED_X_POSITION_56, OLED_Y_POSITION_5,
67                     humidityStr,        OLED_DISPLAY_STRING_TYPE_1); /* 56, 5, x.xx, 1 */
68         if (currentMode != GetKeyStatus(CURRENT_MODE)) {
69             break;
70         }
71         TaskMsleep(SLEEP_10_MS); // 10ms
72     }
73 }
74 
75 /* combustible gas value display */
ShowCombustibleGasValue(void)76 void ShowCombustibleGasValue(void)
77 {
78     unsigned short combustibleGasValueStr[10] = {0};
79     unsigned short currentMode = 0;
80     IoTI2cInit(0, HI_I2C_IDX_BAUDRATE); /* baudrate: 400000 */
81     IoTI2cSetBaudrate(0, HI_I2C_IDX_BAUDRATE);
82     currentMode = GetKeyStatus(CURRENT_MODE);
83 
84     while (1) {
85         SetCombuSensorValue();
86         Mq2GetData();
87         (void*)FloatToString(GetCombuSensorValue(), combustibleGasValueStr);
88         if (!GetCombuSensorValue()) {
89             OledShowStr(OLED_X_POSITION_60, OLED_Y_POSITION_5,
90                         "0.00    ", OLED_DISPLAY_STRING_TYPE_1); /* 60, 5, x.xx, 1 */
91         } else {
92             OledShowStr(OLED_X_POSITION_60, OLED_Y_POSITION_5,
93                         combustibleGasValueStr, OLED_DISPLAY_STRING_TYPE_1); /* 60, 5, x.xx, 1 */
94         }
95         if (currentMode != GetKeyStatus(CURRENT_MODE)) {
96             currentMode = GetKeyStatus(CURRENT_MODE);
97             break;
98         }
99         TaskMsleep(SLEEP_10_MS); // 10ms
100     }
101 }
102 
103 /* environment function hamdle and display */
EnvironmentFunc(void)104 void EnvironmentFunc(void)
105 {
106     /* 初始化时屏幕 i2c baudrate setting */
107     IoTI2cInit(0, HI_I2C_IDX_BAUDRATE); /* baudrate: 400kbps */
108     IoTI2cSetBaudrate(0, HI_I2C_IDX_BAUDRATE); /* 0, 400kbps */
109     /* init oled i2c */
110     IoTGpioInit(HI_GPIO_13); /* GPIO13 */
111     IoSetFunc(HI_GPIO_13, HI_I2C_SDA_SCL); /* GPIO13,  SDA */
112     IoTGpioInit(HI_GPIO_14); /* GPIO 14 */
113     IoSetFunc(HI_GPIO_14, HI_I2C_SDA_SCL); /* GPIO14  SCL */
114     EnvironmentDisplay();
115 }