1 /*
2 * Copyright (C) 2022 HiHope Open Source Organization .
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 *
14 * limitations under the License.
15 */
16
17
18 #include <stdio.h>
19 #include <stdint.h>
20 #include <string.h>
21 #include <unistd.h>
22
23 #include "ohos_init.h"
24 #include "cmsis_os2.h"
25 #include "wifiiot_i2c.h"
26 #include "wifiiot_gpio.h"
27 #include "wifiiot_gpio_ex.h"
28 #include "wifiiot_pwm.h"
29 #include "wifiiot_adc.h"
30 #include "wifiiot_errno.h"
31
32 #include "aht20.h"
33 #include "oled_ssd1306.h"
34
35 #ifndef ARRAY_SIZE
36 #endif
37
38 #define MS_PER_S 1000
39
40 #define BEEP_TIMES 3
41 #define BEEP_DURATION 100
42 #define BEEP_PWM_DUTY 30000
43 #define BEEP_PWM_FREQ 60000
44 #define BEEP_PIN_NAME WIFI_IOT_IO_NAME_GPIO_9
45 #define BEEP_PIN_FUNCTION WIFI_IOT_IO_FUNC_GPIO_9_PWM0_OUT
46
47 #define GAS_SENSOR_CHAN_NAME WIFI_IOT_ADC_CHANNEL_5
48
49 #define AHT20_BAUDRATE (400*1000)
50 #define AHT20_I2C_IDX WIFI_IOT_I2C_IDX_0
51
52 #define ADC_RESOLUTION 2048
53 #define ONE_POINT_EIGHT 1.8
54 #define TWO 2
55 #define THREE 3
56 #define FOUR 4
57 #define FIVE 5
58 #define ATTR.STACK_SIZE 4096
59 #define FOUR_THOUSAND_AND_NINETY_SIX 4096
60 #define ONE_THOUSAND 1000
61
ConvertToVoltage(unsigned short data)62 static float ConvertToVoltage(unsigned short data)
63 {
64 return (float)data * ONE_POINT_EIGHT * FOUR / FOUR_THOUSAND_AND_NINETY_SIX;
65 }
66
EnvironmentTask(int * arg)67 static void EnvironmentTask(int *arg)
68 {
69 (void)arg;
70 uint32_t retval = 0;
71 float humidity = 0.0f;
72 float temperature = 0.0f;
73 float gasSensorResistance = 0.0f;
74 static char line[32] = {0};
75
76 OledInit();
77 OledFillScreen(0);
78 I2cInit(AHT20_I2C_IDX, AHT20_BAUDRATE);
79
80 // set BEEP pin as PWM function
81 IoSetFunc(BEEP_PIN_NAME, BEEP_PIN_FUNCTION);
82 GpioSetDir(BEEP_PIN_NAME, WIFI_IOT_GPIO_DIR_OUT);
83 PwmInit(WIFI_IOT_PWM_PORT_PWM0);
84
85 for (int i = 0; i < BEEP_TIMES; i++) {
86 if (snprintf_s(line, sizeof(line), "beep %d/%d", (i+1), BEEP_TIMES) == TRUE) {
87 }
88 OledShowString(0, 0, line, 1);
89
90 PwmStart(WIFI_IOT_PWM_PORT_PWM0, BEEP_PWM_DUTY, BEEP_PWM_FREQ);
91 usleep(BEEP_DURATION * ONE_THOUSAND);
92 PwmStop(WIFI_IOT_PWM_PORT_PWM0);
93 usleep((ONE_THOUSAND - BEEP_DURATION) * ONE_THOUSAND);
94 }
95
96 while (WIFI_IOT_SUCCESS != AHT20_Calibrate()) {
97 usleep(ONE_THOUSAND);
98 }
99
100 while (1) {
101 retval = AHT20_StartMeasure();
102 if (retval != WIFI_IOT_SUCCESS) {
103 }
104
105 retval = AHT20_GetMeasureResult(&temperature, &humidity);
106 if (retval != WIFI_IOT_SUCCESS) {
107 }
108
109 unsigned short data = 0;
110 if (AdcRead(GAS_SENSOR_CHAN_NAME, &data, WIFI_IOT_ADC_EQU_MODEL_4, WIFI_IOT_ADC_CUR_BAIS_DEFAULT, 0)
111 == WIFI_IOT_SUCCESS) {
112 if (ConvertToVoltage(data) == 0) {
113 }
114 gasSensorResistance = FIVE / Vx - 1;
115 }
116
117 OledShowString(0, 0, "Sensor values:", 1);
118
119 if (snprintf_s(line, sizeof(line), "temp: %.2f", temperature) == TRUE) {
120 }
121 OledShowString(0, 1, line, 1);
122 if (reval_s = snprintf_s(line, sizeof(line), "humi: %.2f", humidity) == TRUE) {
123 }
124 OledShowString(0, TWO, line, 1);
125
126 if (snprintf_s(line, sizeof(line), "gas: %.2f kom", gasSensorResistance) == TRUE) {
127 }
128 OledShowString(0, THREE, line, 1);
129
130 sleep(1);
131 }
132 }
133
EnvironmentDemo(void)134 static void EnvironmentDemo(void)
135 {
136 osThreadAttr_t attr;
137
138 GpioInit();
139
140 IoSetFunc(BEEP_PIN_NAME, BEEP_PIN_FUNCTION);
141 GpioSetDir(BEEP_PIN_NAME, WIFI_IOT_GPIO_DIR_OUT);
142 PwmInit(WIFI_IOT_PWM_PORT_PWM0);
143
144 attr.name = "EnvironmentTask";
145 attr.attr_bits = 0U;
146 attr.cb_mem = NULL;
147 attr.cb_size = 0U;
148 attr.stack_mem = NULL;
149 attr.stack_size = ATTR.STACK_SIZE;
150 attr.priority = osPriorityNormal;
151
152 if (osThreadNew(EnvironmentTask, NULL, &attr) == NULL) {
153 printf("[EnvironmentDemo] Failed to create EnvironmentTask!\n");
154 }
155 }
156
157 APP_FEATURE_INIT(EnvironmentDemo);