-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbright_distance_sensor.c
54 lines (50 loc) · 1.96 KB
/
bright_distance_sensor.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "include/bright_dist_sensor.h"
int initSO6203(uint8_t chanStart, uint8_t chanEnd)
{
uint8_t errcnt;
for (int c = chanStart; c <= chanEnd; c++)
{
if (TCA9548SetChannel(HAMSTRONE_GLOBAL_I2C_PORT, c) < 0)
errcnt++;
if (I2CWriteRegisterSingle(HAMSTRONE_GLOBAL_I2C_PORT, HAMSTRONE_CONFIG_I2C_ADDRESS_SO6203, HAMSTRONE_CONFIG_SO6203_EN, 0b00001011) < 0)
errcnt++;
}
if (errcnt > 0)
return ERROR_READ_FAIL;
return 0;
}
int readSO6203(uint8_t chanStart, uint8_t chanEnd, uint16_t *result)
{
uint8_t valueh, valuel, errcnt;
for (int c = chanStart; c <= chanEnd; c++)
{
if (TCA9548SetChannel(HAMSTRONE_GLOBAL_I2C_PORT, c) < 0)
errcnt++;
if (I2CReadSingle(HAMSTRONE_GLOBAL_I2C_PORT, HAMSTRONE_CONFIG_I2C_ADDRESS_SO6203, HAMSTRONE_CONFIG_SO6203_ADCW_H, &valueh) < 0)
errcnt++;
if (I2CReadSingle(HAMSTRONE_GLOBAL_I2C_PORT, HAMSTRONE_CONFIG_I2C_ADDRESS_SO6203, HAMSTRONE_CONFIG_SO6203_ADCW_H + 1, &valuel) < 0)
errcnt++;
result[c] = (valueh << 8) | valuel;
}
if (errcnt > 0)
return ERROR_READ_FAIL;
return 0;
}
int readTFmini(uint8_t chanStart, uint8_t chanEnd, uint16_t *result)
{
uint8_t errcnt;
uint8_t data[7];
for (int c = chanStart; c <= chanEnd; c++)
{
if (TCA9548SetChannel(HAMSTRONE_GLOBAL_I2C_PORT, c) < 0)
errcnt++;
I2CWriteSingle(HAMSTRONE_GLOBAL_I2C_PORT, HAMSTRONE_CONFIG_I2C_ADDRESS_TFmini, 0x01);
I2CWriteSingle(HAMSTRONE_GLOBAL_I2C_PORT, HAMSTRONE_CONFIG_I2C_ADDRESS_TFmini, 0x02);
I2CWriteSingle(HAMSTRONE_GLOBAL_I2C_PORT, HAMSTRONE_CONFIG_I2C_ADDRESS_TFmini, 0x07);
usleep(1000);
if (I2CRead(HAMSTRONE_GLOBAL_I2C_PORT, HAMSTRONE_CONFIG_I2C_ADDRESS_TFmini, 7, data) < 0)
errcnt++;
// data[0]=isValid? [2]=distl [3]=disth [4]=strengthl [5]=strengthh [7]=rangetype
result[c] = (data[3] << 8) | data[2];
}
}