-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathv_light.c
216 lines (177 loc) · 5.26 KB
/
v_light.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
* Copyright (c) 2006-2020, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2020-12-19 zhangsz add virtual sensor device
*/
#include <rtthread.h>
#ifdef PKG_USING_VIRTUAL_SENSOR_LIGHT
#include "sensor.h"
#include <stdlib.h>
#define DBG_TAG "v_li"
#ifdef PKG_USING_VIRTUAL_SENSOR_DBG
#define DBG_LVL DBG_LOG
#else
#define DBG_LVL DBG_INFO
#endif
#include <rtdbg.h>
enum SENS_LIGHT_ID
{
SENS_LIGHT_01 = 0, //Light
SENS_LIGHT_MAX,
};
#define SENS_BUS_NAME "sens_bus"
#define SENS_LIGHT_01_SENSOR_ID (RT_SENSOR_CLASS_LIGHT + 0x10)
struct sens_light
{
char* dev_name;
rt_uint8_t sens_id;
};
static struct sens_light sens_light_tbl[SENS_LIGHT_MAX] =
{
{V_SENS_LIGHT_DEV_NAME, 0x00 }, /* Light */
};
static struct rt_sensor_info light_info_tbl[SENS_LIGHT_MAX] =
{
{RT_SENSOR_CLASS_LIGHT, RT_SENSOR_VENDOR_BOSCH, RT_NULL, RT_SENSOR_UNIT_LUX, RT_SENSOR_INTF_SPI, 2048000, 0, 1 },
};
static rt_uint8_t sensor_get_id(rt_uint8_t sens_index)
{
rt_uint8_t chip_id = 0x00;
switch (sens_index)
{
case SENS_LIGHT_01:
chip_id = SENS_LIGHT_01_SENSOR_ID;
break;
default:
break;
}
return chip_id;
}
static int sensor_init(rt_uint8_t index)
{
sens_light_tbl[index].sens_id = sensor_get_id(index);
return RT_EOK;
}
static void* li_sensor_create(struct rt_sensor_intf* intf, rt_uint8_t index)
{
if (sensor_init(index) != RT_EOK)
{
LOG_E("%s:error!", __func__);
}
return 0;
}
static rt_err_t li_sensor_set_odr(rt_sensor_t sensor, rt_uint16_t odr)
{
LOG_D("%s:odr=%d", __func__, odr);
return RT_EOK;
}
static rt_err_t li_sensor_set_range(rt_sensor_t sensor, rt_uint16_t range)
{
LOG_D("%s:range=%d", __func__, range);
return RT_EOK;
}
static rt_err_t li_sensor_set_power(rt_sensor_t sensor, rt_uint8_t power)
{
rt_int8_t rslt = 0;
LOG_D("%s:power=%d", __func__, power);
return rslt;
}
static rt_size_t light_sensor_fetch_data(struct rt_sensor_device* sensor, void* buf, rt_size_t size)
{
struct rt_sensor_data* data = buf;
rt_int16_t max_range = 0;
if (size < 1)
{
LOG_E("%s:read size err! size=%d", __func__, size);
return 0;
}
if (buf == RT_NULL)
{
LOG_E("%s:read buf is NULL!", __func__);
return 0;
}
max_range = light_info_tbl[SENS_LIGHT_01].range_max - light_info_tbl[SENS_LIGHT_01].range_min;
for (int i = 0; i < size; i++)
{
data->type = RT_SENSOR_CLASS_LIGHT;
data->data.light = rand() % max_range + light_info_tbl[SENS_LIGHT_01].range_min;
data->timestamp = rt_sensor_get_ts();
LOG_D("%s:%d", __func__, data->data.light);
data++;
}
return size;
}
static rt_err_t light_sensor_control(struct rt_sensor_device* sensor, int cmd, void* args)
{
rt_err_t result = RT_EOK;
switch (cmd)
{
case RT_SENSOR_CTRL_GET_ID:
*(rt_uint8_t*)args = sens_light_tbl[SENS_LIGHT_01].sens_id;
break;
case RT_SENSOR_CTRL_SET_ODR:
result = li_sensor_set_odr(sensor, (rt_uint32_t)args & 0xffff);
break;
case RT_SENSOR_CTRL_SET_RANGE:
result = li_sensor_set_range(sensor, (rt_uint32_t)args);
break;
case RT_SENSOR_CTRL_SET_POWER:
result = li_sensor_set_power(sensor, (rt_uint32_t)args & 0xff);
break;
case RT_SENSOR_CTRL_SELF_TEST:
/* TODO */
result = -RT_EINVAL;
break;
default:
return -RT_EINVAL;
}
return result;
}
static struct rt_sensor_ops sensor_ops[] =
{
{light_sensor_fetch_data, light_sensor_control},
};
int rt_vd_sens_light_init(void)
{
rt_int8_t result;
rt_uint8_t index = 0;
rt_sensor_t sensor_dat = RT_NULL;
struct rt_sensor_config cfg;
cfg.intf.dev_name = SENS_BUS_NAME;
cfg.intf.user_data = RT_NULL;
cfg.irq_pin.pin = RT_PIN_NONE;
for (index = 0; index < SENS_LIGHT_MAX; index++)
{
li_sensor_create(&cfg.intf, index);
sensor_dat = rt_calloc(1, sizeof(struct rt_sensor_device));
if (sensor_dat == RT_NULL)
{
LOG_E("%s:rt_calloc err!", __func__);
return -RT_ERROR;
}
sensor_dat->info.type = light_info_tbl[index].type;
sensor_dat->info.vendor = light_info_tbl[index].vendor;
sensor_dat->info.model = light_info_tbl[index].model;
sensor_dat->info.unit = light_info_tbl[index].unit;
sensor_dat->info.intf_type = light_info_tbl[index].intf_type;
sensor_dat->info.range_max = light_info_tbl[index].range_max;
sensor_dat->info.range_min = light_info_tbl[index].range_min;
sensor_dat->info.period_min = light_info_tbl[index].period_min;
rt_memcpy(&sensor_dat->config, &cfg, sizeof(struct rt_sensor_config));
sensor_dat->ops = &sensor_ops[index];
result = rt_hw_sensor_register(sensor_dat, sens_light_tbl[index].dev_name, RT_DEVICE_FLAG_RDWR, RT_NULL);
if (result != RT_EOK)
{
LOG_E("%s:device register err code: %d", __func__, result);
rt_free(sensor_dat);
return -RT_ERROR;
}
}
return RT_EOK;
}
INIT_DEVICE_EXPORT(rt_vd_sens_light_init);
#endif