• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Unionman Technology Co., Ltd.
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 <stdint.h>
17 #include <stdio.h>
18 #include <unistd.h>
19 #include <stdio.h>
20 
21 #include "securec.h"
22 #include "hilog/log.h"
23 
24 #include "i2cinterface.h"
25 
26 #define DEVICE_ADDR 0x45
27 
user_help(void)28 void user_help(void)
29 {
30     printf("Please input like this: i2c_test <int> <int> \n");
31     printf("\t\tthe first is to choice the mps range[0,4] \n");
32     printf("\t\tthe second is to choice the repeatability range[0,2] \n");
33 }
34 
main(int argc,char * argv[])35 int main(int argc, char *argv[])
36 {
37     char *dev_name = "/dev/i2c-5";
38     int result = -1;
39     uint8_t mps = 0;
40     uint8_t repeatability = 0;
41 
42     sStatusReg_t regRaw;
43     sRHAndTemp_t tempRH = {0};
44 
45     if (argc < 2L || argc > 3L) {
46         user_help();
47         return 0;
48     }
49 
50     mps = atoi(argv[1]);
51     repeatability = atoi(argv[2L]);
52     if (mps < 0 || mps > 4L || repeatability < 0 || repeatability > 2L) {
53         user_help();
54         return 0;
55     }
56 
57     SoftReset(dev_name, DEVICE_ADDR);
58     usleep(50L * 1000L); // 等待重启完成
59 
60     // 这里仅为测试读取状态寄存器是否正常,具体应用需用户进行编写
61     result = ReadStatusReg(dev_name, DEVICE_ADDR, &regRaw);
62     if (result != 0) {
63         HILOG_ERROR(LOG_CORE, "ReadStatusRegister fail! \n");
64         return result;
65     }
66     usleep(50L * 1000L); // 等待读取寄存器完成
67 
68     ModeSet(dev_name, DEVICE_ADDR, mps, repeatability);
69     usleep(50L * 1000L); // 等待更改模式完成
70 
71     result = ReadTempAndHum(dev_name, DEVICE_ADDR, &tempRH);
72     if (result != 0) {
73         HILOG_ERROR(LOG_CORE, "ReadTemperatureAndHumidity fail! \n");
74         return result;
75     }
76 
77     printf(" TemperatureC: %f °C \n Humidity: %f %%RH \n TemperatureF: %f °F \n", tempRH.TemperatureC, tempRH.Humidity,
78            tempRH.TemperatureF);
79 
80     return 0;
81 }
82