-
Notifications
You must be signed in to change notification settings - Fork 7
/
ap3216c.c
777 lines (666 loc) · 19.3 KB
/
ap3216c.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
/*
* Copyright (c) 2006-2018, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2018-08-20 Ernest Chen the first version
*/
#include <rtthread.h>
#include <rthw.h>
#include <rtdevice.h>
#include <finsh.h>
#include <string.h>
#include "ap3216c.h"
#ifdef PKG_USING_AP3216C
#define DBG_ENABLE
#define DBG_SECTION_NAME "ap3216c"
#define DBG_LEVEL DBG_LOG
#define DBG_COLOR
#include <rtdbg.h>
//System Register
#define AP3216C_SYS_CONFIGURATION_REG 0x00 //default
#define AP3216C_SYS_INT_STATUS_REG 0x01
#define AP3216C_SYS_INT_CLEAR_MANNER_REG 0x02
#define AP3216C_IR_DATA_L_REG 0x0A
#define AP3216C_IR_DATA_H_REG 0x0B
#define AP3216C_ALS_DATA_L_REG 0x0C
#define AP3216C_ALS_DATA_H_REG 0x0D
#define AP3216C_PS_DATA_L_REG 0x0E
#define AP3216C_PS_DATA_H_REG 0x0F
//ALS Register
#define AP3216C_ALS_CONFIGURATION_REG 0x10 //range 5:4,persist 3:0
#define AP3216C_ALS_CALIBRATION_REG 0x19
#define AP3216C_ALS_THRESHOLD_LOW_L_REG 0x1A //bit 7:0
#define AP3216C_ALS_THRESHOLD_LOW_H_REG 0x1B //bit 15:8
#define AP3216C_ALS_THRESHOLD_HIGH_L_REG 0x1C //bit 7:0
#define AP3216C_ALS_THRESHOLD_HIGH_H_REG 0x1D //bit 15:8
//PS Register
#define AP3216C_PS_CONFIGURATION_REG 0x20
#define AP3216C_PS_LED_DRIVER_REG 0x21
#define AP3216C_PS_INT_FORM_REG 0x22
#define AP3216C_PS_MEAN_TIME_REG 0x23
#define AP3216C_PS_LED_WAITING_TIME_REG 0x24
#define AP3216C_PS_CALIBRATION_L_REG 0x28
#define AP3216C_PS_CALIBRATION_H_REG 0x29
#define AP3216C_PS_THRESHOLD_LOW_L_REG 0x2A //bit 1:0
#define AP3216C_PS_THRESHOLD_LOW_H_REG 0x2B //bit 9:2
#define AP3216C_PS_THRESHOLD_HIGH_L_REG 0x2C //bit 1:0
#define AP3216C_PS_THRESHOLD_HIGH_H_REG 0x2D //bit 9:2
#define AP3216C_ADDR 0x1e /*0x3c=0x1e<<1*/
static rt_err_t write_reg(struct rt_i2c_bus_device *bus, rt_uint8_t reg, rt_uint8_t data)
{
struct rt_i2c_msg msgs;
rt_uint8_t temp[2];
temp[0] = reg;
temp[1] = data;
msgs.addr = AP3216C_ADDR;
msgs.flags = RT_I2C_WR;
msgs.buf = temp;
msgs.len = 2;
if (rt_i2c_transfer(bus, &msgs, 1) == 1)
{
return RT_EOK;
}
else
{
LOG_E("Writing command error");
return -RT_ERROR;
}
}
static rt_err_t read_regs(struct rt_i2c_bus_device *bus, rt_uint8_t reg, rt_uint8_t len, rt_uint8_t *buf)
{
struct rt_i2c_msg msgs[2];
msgs[0].addr = AP3216C_ADDR;
msgs[0].flags = RT_I2C_WR;
msgs[0].buf = ®
msgs[0].len = 1;
msgs[1].addr = AP3216C_ADDR;
msgs[1].flags = RT_I2C_RD;
msgs[1].buf = buf;
msgs[1].len = len;
if (rt_i2c_transfer(bus, msgs, 2) == 2)
{
return RT_EOK;
}
else
{
LOG_E("Reading command error");
return -RT_ERROR;
}
}
rt_err_t ap3216c_reset_sensor(ap3216c_device_t dev)
{
if (dev == RT_NULL)
{
return -RT_EINVAL;
}
write_reg(dev->i2c, AP3216C_SYS_CONFIGURATION_REG, AP3216C_MODE_SW_RESET); //reset
rt_thread_mdelay(15); /* need to wait at least 10ms */
return RT_EOK;
}
/**
* This function is convenient to getting data except including high and low data for this sensor.
* note:after reading lower register first,reading higher add one.
*/
static rt_uint32_t read_low_and_high(ap3216c_device_t dev, rt_uint8_t reg, rt_uint8_t len)
{
rt_uint32_t data;
rt_uint8_t buf = 0;
read_regs(dev->i2c, reg, len, &buf); //low
data = buf;
read_regs(dev->i2c, reg + 1, len, &buf); //high
data = data + (buf << len * 8);
return data;
}
#ifdef AP3216C_USING_HW_INT
/**
* This function is only used to set threshold without filtering times
*
* @param dev the name of ap3216c device
* @param cmd first register , and other cmd count by it.
* @param threshold threshold and filtering times of als threshold
*/
static void set_threshold(ap3216c_device_t dev, ap3216c_cmd_t cmd, ap3216c_threshold_t threshold)
{
ap3216c_set_param(dev, cmd, (threshold.min & 0xff));
ap3216c_set_param(dev, (ap3216c_cmd_t)(cmd + 1), (threshold.min >> 8));
ap3216c_set_param(dev, (ap3216c_cmd_t)(cmd + 2), (threshold.max & 0xff));
ap3216c_set_param(dev, (ap3216c_cmd_t)(cmd + 3), threshold.max >> 8);
}
static void ap3216c_hw_interrupt(void *args)
{
ap3216c_device_t dev = (ap3216c_device_t)args;
if (dev->als_int_cb)
{
dev->als_int_cb(dev->als_int_cb);
}
if (dev->ps_int_cb)
{
dev->ps_int_cb(dev->ps_int_cb);
}
}
static void ap3216c_int_init(ap3216c_device_t dev)
{
RT_ASSERT(dev);
rt_pin_mode(AP3216C_INT_PIN, PIN_MODE_INPUT_PULLUP);
rt_pin_attach_irq(AP3216C_INT_PIN, PIN_IRQ_MODE_FALLING, ap3216c_hw_interrupt, (void *)dev);
rt_pin_irq_enable(AP3216C_INT_PIN, PIN_IRQ_ENABLE);
}
/**
* This function registers als interrupt with callback function
*
* @param dev the name of ap3216c device
* @param enabled enable or disenable als interrupt
* @param threshold threshold and filtering times of als threshold
*
* @param int_cb callback funtion is defined by user.
*/
void ap3216c_int_als_cb(ap3216c_device_t dev, rt_bool_t enabled, ap3216c_threshold_t threshold, ap3216c_int_cb int_cb)
{
RT_ASSERT(dev);
if (enabled)
{
dev->als_int_cb = int_cb;
set_threshold(dev, AP3216C_ALS_LOW_THRESHOLD_L, threshold);
}
else
{
dev->als_int_cb = RT_NULL;
}
}
/**
* This function registers ps interrupt with callback function
*
* @param dev the name of ap3216c device
* @param enabled enable or disenable ps interrupt
* @param threshold threshold and filtering times of ps threshold
*
* @param int_cb callback funtion is defined by user.
*/
void ap3216c_int_ps_cb(ap3216c_device_t dev, rt_bool_t enabled, ap3216c_threshold_t threshold, ap3216c_int_cb int_cb)
{
RT_ASSERT(dev);
if (enabled)
{
dev->ps_int_cb = int_cb;
set_threshold(dev, AP3216C_PS_LOW_THRESHOLD_L, threshold);
}
else
{
dev->ps_int_cb = RT_NULL;
}
}
#endif /* AP3216C_USING_HW_INT */
/**
* This function initializes ap3216c registered device driver
*
* @param dev the name of ap3216c device
*
* @return the ap3216c device.
*/
ap3216c_device_t ap3216c_init(const char *i2c_bus_name)
{
ap3216c_device_t dev;
RT_ASSERT(i2c_bus_name);
dev = rt_calloc(1, sizeof(struct ap3216c_device));
if (dev == RT_NULL)
{
LOG_E("Can't allocate memory for ap3216c device on '%s' ", i2c_bus_name);
rt_free(dev);
return RT_NULL;
}
dev->i2c = rt_i2c_bus_device_find(i2c_bus_name);
if (dev->i2c == RT_NULL)
{
LOG_E("Can't find ap3216c device on '%s'", i2c_bus_name);
rt_free(dev);
return RT_NULL;
}
dev->lock = rt_mutex_create("mutex_ap3216c", RT_IPC_FLAG_FIFO);
if (dev->lock == RT_NULL)
{
LOG_E("Can't create mutex for ap3216c device on '%s'", i2c_bus_name);
rt_free(dev);
return RT_NULL;
}
/* reset ap3216c */
ap3216c_reset_sensor(dev);
ap3216c_set_param(dev, AP3216C_SYSTEM_MODE, AP3216C_MODE_ALS_AND_PS);
#ifdef AP3216C_USING_HW_INT
/* init interrupt mode */
ap3216c_int_init(dev);
#endif /* AP3216C_USING_HW_INT */
return dev;
}
/**
* This function releases memory and deletes mutex lock
*
* @param dev the pointer of device driver structure
*/
void ap3216c_deinit(ap3216c_device_t dev)
{
RT_ASSERT(dev);
rt_mutex_delete(dev->lock);
rt_free(dev);
}
/**
* This function reads temperature by ap3216c sensor measurement
*
* @param dev the pointer of device driver structure
*
* @return the ambient light converted to float data.
*/
float ap3216c_read_ambient_light(ap3216c_device_t dev)
{
float brightness = 0.0; // default error data
rt_err_t result;
rt_uint32_t read_data;
rt_uint8_t temp;
RT_ASSERT(dev);
result = rt_mutex_take(dev->lock, RT_WAITING_FOREVER);
if (result == RT_EOK)
{
read_data = read_low_and_high(dev, AP3216C_ALS_DATA_L_REG, 1);
ap3216c_get_param(dev, AP3216C_ALS_RANGE, &temp);
if (temp == AP3216C_ALS_RANGE_20661)
{
brightness = 0.35 * read_data; //sensor ambient light converse to reality
}
else if (temp == AP3216C_ALS_RANGE_5162)
{
brightness = 0.0788 * read_data; //sensor ambient light converse to reality
}
else if (temp == AP3216C_ALS_RANGE_1291)
{
brightness = 0.0197 * read_data; //sensor ambient light converse to reality
}
else if (temp == AP3216C_ALS_RANGE_323)
{
brightness = 0.0049 * read_data; //sensor ambient light converse to reality
}
else
{
LOG_E("Failed to get range of ap3216c");
}
}
else
{
LOG_E("Failed to reading ambient light");
}
rt_mutex_release(dev->lock);
return brightness;
}
/**
* This function reads temperature by ap3216c sensor measurement
*
* @param dev the pointer of device driver structure
*
* @return the proximity data.
*/
uint16_t ap3216c_read_ps_data(ap3216c_device_t dev)
{
rt_uint16_t proximity = 0;
rt_err_t result;
RT_ASSERT(dev);
result = rt_mutex_take(dev->lock, RT_WAITING_FOREVER);
if (result == RT_EOK)
{
rt_uint32_t read_data;
read_data = read_low_and_high(dev, AP3216C_PS_DATA_L_REG, 1); //read two data
if (1 == ((read_data >> 6) & 0x01 || (read_data >> 14) & 0x01))
{
LOG_I("The data of PS is invalid for high intensive IR light ");
}
proximity = (read_data & 0x000f) + (((read_data >> 8) & 0x3f) << 4); //sensor proximity converse to reality
}
else
{
LOG_E("Failed to reading ps data ");
}
rt_mutex_release(dev->lock);
return proximity;
}
/**
* This function sets parameter of ap3216c sensor
*
* @param dev the pointer of device driver structure
* @param cmd the parameter cmd of device
* @param value for setting value in cmd register
*
* @return the setting parameter status,RT_EOK reprensents setting successfully.
*/
rt_err_t ap3216c_set_param(ap3216c_device_t dev, ap3216c_cmd_t cmd, rt_uint8_t value)
{
RT_ASSERT(dev);
switch (cmd)
{
case AP3216C_SYSTEM_MODE:
{
if (value > AP3216C_MODE_ALS_AND_PS_ONCE)
{
LOG_E("Setting system mode parameter is wrong !");
return -RT_ERROR;
}
/* default 000,power down */
write_reg(dev->i2c, AP3216C_SYS_CONFIGURATION_REG, value);
break;
}
case AP3216C_INT_PARAM:
{
if (value > AP3216C_ALS_CLEAR_MANNER_BY_SOFTWARE)
{
LOG_E("Setting int parameter is wrong !");
return -RT_ERROR;
}
write_reg(dev->i2c, AP3216C_SYS_INT_CLEAR_MANNER_REG, value);
break;
}
case AP3216C_ALS_RANGE:
{
rt_uint8_t args;
if (!(value == AP3216C_ALS_RANGE_20661 || value == AP3216C_ALS_RANGE_5162 || value == AP3216C_ALS_RANGE_1291 || value == AP3216C_ALS_RANGE_323))
{
LOG_E("Setting als dynamic range is wrong, please refer als_range");
return -RT_ERROR;
}
read_regs(dev->i2c, AP3216C_ALS_CONFIGURATION_REG, 1, &args);
args &= 0xcf;
args |= value << 4;
write_reg(dev->i2c, AP3216C_ALS_CONFIGURATION_REG, args);
break;
}
case AP3216C_ALS_PERSIST:
{
rt_uint8_t args = 0;
if (value > 0x0f)
{
LOG_E("Setting als persist overflows ");
return -RT_ERROR;
}
read_regs(dev->i2c, AP3216C_ALS_CONFIGURATION_REG, 1, &args);
args &= 0xf0;
args |= value;
write_reg(dev->i2c, AP3216C_ALS_CONFIGURATION_REG, args);
break;
}
case AP3216C_ALS_LOW_THRESHOLD_L:
{
write_reg(dev->i2c, AP3216C_ALS_THRESHOLD_LOW_L_REG, value);
break;
}
case AP3216C_ALS_LOW_THRESHOLD_H:
{
write_reg(dev->i2c, AP3216C_ALS_THRESHOLD_LOW_H_REG, value);
break;
}
case AP3216C_ALS_HIGH_THRESHOLD_L:
{
write_reg(dev->i2c, AP3216C_ALS_THRESHOLD_HIGH_L_REG, value);
break;
}
case AP3216C_ALS_HIGH_THRESHOLD_H:
{
write_reg(dev->i2c, AP3216C_ALS_THRESHOLD_HIGH_H_REG, value);
break;
}
case AP3216C_PS_GAIN:
{
rt_uint8_t args = 0;
if (value > 0x3)
{
LOG_E("Setting ps again overflows ");
return -RT_ERROR;
}
read_regs(dev->i2c, AP3216C_PS_CONFIGURATION_REG, 1, &args);
args &= 0xf3;
args |= value;
write_reg(dev->i2c, AP3216C_PS_CONFIGURATION_REG, args);
break;
}
case AP3216C_PS_PERSIST:
{
rt_uint8_t args = 0;
if (value > 0x3)
{
LOG_E("Setting ps persist overflows ");
return -RT_ERROR;
}
read_regs(dev->i2c, AP3216C_PS_CONFIGURATION_REG, 1, &args);
args &= 0xfc;
args |= value;
write_reg(dev->i2c, AP3216C_PS_CONFIGURATION_REG, args);
break;
}
case AP3216C_PS_LOW_THRESHOLD_L:
{
if (value > 0x3)
{
LOG_E("Setting ps low threshold of low bit is wrong !");
return -RT_ERROR;
}
write_reg(dev->i2c, AP3216C_PS_THRESHOLD_LOW_L_REG, value);
break;
}
case AP3216C_PS_LOW_THRESHOLD_H:
{
write_reg(dev->i2c, AP3216C_PS_THRESHOLD_LOW_H_REG, value);
break;
}
case AP3216C_PS_HIGH_THRESHOLD_L:
{
if (value > 0x3)
{
LOG_E("Setting ps high threshold of low bit is wrong !");
return -RT_ERROR;
}
write_reg(dev->i2c, AP3216C_PS_THRESHOLD_HIGH_L_REG, value);
break;
}
case AP3216C_PS_HIGH_THRESHOLD_H:
{
write_reg(dev->i2c, AP3216C_PS_THRESHOLD_HIGH_H_REG, value);
break;
}
default:
{
return -RT_ERROR;
}
}
return RT_EOK;
}
/**
* This function gets parameter of ap3216c sensor
*
* @param dev the pointer of device driver structure
* @param cmd the parameter cmd of device
* @param value to get value in cmd register
*
* @return the getting parameter status,RT_EOK reprensents getting successfully.
*/
rt_err_t ap3216c_get_param(ap3216c_device_t dev, ap3216c_cmd_t cmd, rt_uint8_t *value)
{
RT_ASSERT(dev);
switch (cmd)
{
case AP3216C_SYSTEM_MODE:
{
read_regs(dev->i2c, AP3216C_SYS_CONFIGURATION_REG, 1, value);
if (*value > AP3216C_MODE_ALS_AND_PS_ONCE)
{
LOG_E("Getting system mode parameter is wrong !");
return -RT_ERROR;
}
break;
}
case AP3216C_INT_PARAM:
{
read_regs(dev->i2c, AP3216C_SYS_INT_CLEAR_MANNER_REG, 1, value);
if (*value > AP3216C_ALS_CLEAR_MANNER_BY_SOFTWARE)
{
LOG_E("Getting int parameter is wrong !");
return -RT_ERROR;
}
break;
}
case AP3216C_ALS_RANGE:
{
rt_uint8_t temp;
read_regs(dev->i2c, AP3216C_ALS_CONFIGURATION_REG, 1, value);
temp = (*value & 0xff) >> 4;
if (!(temp == AP3216C_ALS_RANGE_20661 || temp == AP3216C_ALS_RANGE_5162 || temp == AP3216C_ALS_RANGE_1291 || temp == AP3216C_ALS_RANGE_323))
{
LOG_E("Getting als dynamic range is wrong, please refer als_range");
return -RT_ERROR;
}
*value = temp;
break;
}
case AP3216C_ALS_PERSIST:
{
rt_uint8_t temp;
read_regs(dev->i2c, AP3216C_ALS_CONFIGURATION_REG, 1, value);
temp = *value & 0x0f;
if (temp > 0x0f)
{
LOG_E("Getting als persist is wrong, please refer als_range");
return -RT_ERROR;
}
*value = temp;
break;
}
case AP3216C_ALS_LOW_THRESHOLD_L:
{
read_regs(dev->i2c, AP3216C_ALS_THRESHOLD_LOW_L_REG, 1, value);
break;
}
case AP3216C_ALS_LOW_THRESHOLD_H:
{
read_regs(dev->i2c, AP3216C_ALS_THRESHOLD_LOW_H_REG, 1, value);
break;
}
case AP3216C_ALS_HIGH_THRESHOLD_L:
{
read_regs(dev->i2c, AP3216C_ALS_THRESHOLD_HIGH_L_REG, 1, value);
break;
}
case AP3216C_ALS_HIGH_THRESHOLD_H:
{
read_regs(dev->i2c, AP3216C_ALS_THRESHOLD_HIGH_H_REG, 1, value);
break;
}
case AP3216C_PS_GAIN:
{
rt_uint8_t temp;
read_regs(dev->i2c, AP3216C_PS_CONFIGURATION_REG, 1, &temp);
*value = (temp & 0xc) >> 2;
break;
}
case AP3216C_PS_PERSIST:
{
rt_uint8_t temp;
read_regs(dev->i2c, AP3216C_PS_CONFIGURATION_REG, 1, &temp);
*value = temp & 0x3;
break;
}
case AP3216C_PS_LOW_THRESHOLD_L:
{
read_regs(dev->i2c, AP3216C_PS_THRESHOLD_LOW_L_REG, 1, value);
if ((*value & 0xff) > 0x3)
{
LOG_E("Getting ps low threshold of low bit is wrong !");
return -RT_ERROR;
}
break;
}
case AP3216C_PS_LOW_THRESHOLD_H:
{
read_regs(dev->i2c, AP3216C_PS_THRESHOLD_LOW_H_REG, 1, value);
break;
}
case AP3216C_PS_HIGH_THRESHOLD_L:
{
read_regs(dev->i2c, AP3216C_PS_THRESHOLD_HIGH_L_REG, 1, value);
if ((*value & 0xff) > 3)
{
LOG_E("Getting ps high threshold of low bit is wrong !");
return -RT_ERROR;
}
break;
}
case AP3216C_PS_HIGH_THRESHOLD_H:
{
read_regs(dev->i2c, AP3216C_PS_THRESHOLD_HIGH_H_REG, 1, value);
break;
}
default:
{
return -RT_ERROR;
}
}
return RT_EOK;
}
void ap3216c(int argc, char *argv[])
{
static ap3216c_device_t dev = RT_NULL;
if (argc > 1)
{
if (!strcmp(argv[1], "probe"))
{
if (argc > 2)
{
/* initialize the sensor when first probe */
if (!dev || strcmp(dev->i2c->parent.parent.name, argv[2]))
{
/* deinit the old device */
if (dev)
{
ap3216c_deinit(dev);
}
dev = ap3216c_init(argv[2]);
}
}
else
{
rt_kprintf("ap3216c probe <dev_name> - probe sensor by given name\n");
}
}
else if (!strcmp(argv[1], "read"))
{
if (dev)
{
rt_uint16_t ps_data;
float brightness;
/* read the sensor */
ps_data = ap3216c_read_ps_data(dev);
if (ps_data == 0)
{
rt_kprintf("object is not proximity of sensor \n");
}
else
{
rt_kprintf("ap3216c read current ps data : %d\n", ps_data);
}
brightness = ap3216c_read_ambient_light(dev);
rt_kprintf("ap3216c measure current brightness: %d.%d(lux) \n", (int)brightness, ((int)(10 * brightness) % 10));
}
else
{
rt_kprintf("Please using 'ap3216c probe <dev_name>' first\n");
}
}
else
{
rt_kprintf("Unknown command. Please enter 'ap3216c' for help\n");
}
}
else
{
rt_kprintf("Usage:\n");
rt_kprintf("ap3216c probe <dev_name> - probe sensor by given name\n");
rt_kprintf("ap3216c read - read sensor ap3216c data\n");
}
}
MSH_CMD_EXPORT(ap3216c, ap3216c sensor function);
#endif /* PKG_USING_AP3216C */