-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVCNL4000.cs
163 lines (148 loc) · 6.28 KB
/
VCNL4000.cs
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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading;
namespace i2c
{
// ReSharper disable once InconsistentNaming
internal class VCNL4000 : I2C<VCNL4000_Constants>
{
private readonly Action<ProximtyEventArgs> _updateCaller;
// ReSharper disable once InconsistentNaming
public VCNL4000(string busid, int irCurrent_mA, Action<ProximtyEventArgs> updateCaller, Action<string> logger) : base(logger)
{
_updateCaller = updateCaller;
Busid = busid;
Constants = new Dictionary<VCNL4000_Constants, int>
{
{ VCNL4000_Constants.VCNL4000_ADDRESS, 0x13 },
{ VCNL4000_Constants.VCNL4000_COMMAND, 0x80 },
{ VCNL4000_Constants.VCNL4000_PRODUCTID, 0x81},
{ VCNL4000_Constants.VCNL4000_IRLED, 0x83},
{ VCNL4000_Constants.VCNL4000_AMBIENTPARAMETER, 0x84},
{ VCNL4000_Constants.VCNL4000_AMBIENTDATA, 0x85},
{ VCNL4000_Constants.VCNL4000_PROXIMITYDATA_1, 0x87},
{ VCNL4000_Constants.VCNL4000_PROXIMITYDATA_2, 0x88},
{ VCNL4000_Constants.VCNL4000_SIGNALFREQ, 0x89},
{ VCNL4000_Constants.VCNL4000_PROXINITYADJUST, 0x8A},
{ VCNL4000_Constants.VCNL4000_3M125, 0},
{ VCNL4000_Constants.VCNL4000_1M5625, 1},
{ VCNL4000_Constants.VCNL4000_781K25, 2},
{ VCNL4000_Constants.VCNL4000_390K625, 3},
{ VCNL4000_Constants.VCNL4000_MEASUREAMBIENT, 0x10},
{ VCNL4000_Constants.VCNL4000_MEASUREPROXIMITY, 0x08},
{ VCNL4000_Constants.VCNL4000_AMBIENTREADY, 0x40},
{ VCNL4000_Constants.VCNL4000_PROXIMITYREADY, 0x20},
{ VCNL4000_Constants.dx , 5250 },
{ VCNL4000_Constants.dy, 2370 }
};
var irled = Set8(
Busid,
GetConstantAsString(VCNL4000_Constants.VCNL4000_ADDRESS),
GetConstantAsString(VCNL4000_Constants.VCNL4000_IRLED),
irCurrent_mA.ToString(CultureInfo.InvariantCulture),
0);
}
public UInt16 ProductID {
get
{
return GetValue8(VCNL4000_Constants.VCNL4000_ADDRESS, VCNL4000_Constants.VCNL4000_PRODUCTID);
}
}
/// <summary>
/// Returns a decimal conversion from the VCNL4000's proximity data registers
/// </summary>
private Tuple<decimal, int> Proximity {
get
{
var command = GetValue8(VCNL4000_Constants.VCNL4000_ADDRESS, VCNL4000_Constants.VCNL4000_COMMAND);
Set8(
Busid,
GetConstantAsString(VCNL4000_Constants.VCNL4000_ADDRESS),
GetConstantAsString(VCNL4000_Constants.VCNL4000_COMMAND),
(command | GetConstantAsByte(VCNL4000_Constants.VCNL4000_MEASUREPROXIMITY)).ToString(CultureInfo.InvariantCulture),
0);
while (!ProximityReady) { /*wait until proximity data ready*/ }
var data = (UInt16)(GetValue8(VCNL4000_Constants.VCNL4000_ADDRESS, VCNL4000_Constants.VCNL4000_PROXIMITYDATA_1) << 8);
data |= GetValue8(VCNL4000_Constants.VCNL4000_ADDRESS, VCNL4000_Constants.VCNL4000_PROXIMITYDATA_2);
Thread.Sleep(1000);
var distance = GetDistance(data);
return new Tuple<decimal,int>(distance, data);
}
}
private static decimal GetDistance(UInt16 data)
{
// ReSharper disable once InconsistentNaming
var prox_mm = Convert.ToDecimal(GetConstantAsByte(VCNL4000_Constants.dx)) / (Convert.ToDecimal(data) - Convert.ToDecimal(GetConstantAsByte(VCNL4000_Constants.dy)));
return prox_mm;
}
private char[] ReadCommandRegister()
{
var s = Convert.ToString(GetValue8(VCNL4000_Constants.VCNL4000_ADDRESS, VCNL4000_Constants.VCNL4000_COMMAND), 2);
var bits = s.PadLeft(8, '0').ToCharArray();
return bits;
}
private bool ProximityReady
{
get
{
/*
* "10100000" = 0xA0
* Bit 7(0) - Config_lock - Ignore
* Bit 6(1) - als_data_rdy - Ambient Light sensor ready
* Bit 5(2) - prox_data_rdy - Promimity Data ready
* Bit 4(3) - als_od - Ambient Light Reading On-demand
* Bit 3(4) - prox_od - Proximity Reading On-demand
* Bit 2(5) - N/A
* Bit 1(6) - N/A
* Bit 0(7) - N/A
*/
var register = ReadCommandRegister();
return register[2] == '1';
}
}
public bool AmbientLightReady
{
get
{
/*
* "10100000" = 0xA0
* Bit 7(0) - Config_lock - Ignore
* Bit 6(1) - als_data_rdy - Ambient Light sensor ready
* Bit 5(2) - prox_data_rdy - Promimity Data ready
* Bit 4(3) - als_od - Ambient Light Reading On-demand
* Bit 3(4) - prox_od - Proximity Reading On-demand
* Bit 2(5) - N/A
* Bit 1(6) - N/A
* Bit 0(7) - N/A
*/
var register = ReadCommandRegister();
return register[1] == '1';
}
}
internal override void Start()
{
var s = new ThreadStart(ReadProximity);
var work = new Thread(s);
work.Start();
}
internal override void Stop()
{
DoWork = false;
}
private void ReadProximity()
{
DoWork = true;
while (DoWork)
{
var Event = new ProximtyEventArgs { Proximity = Proximity.Item1, RawValue = Proximity.Item2 };
_updateCaller(Event);
}
}
}
public class ProximtyEventArgs : EventArgs
{
public decimal Proximity { get; set; }
public int RawValue { get; set; }
}
}