-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensor.cpp
50 lines (40 loc) · 1.02 KB
/
sensor.cpp
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
/*!
* @file sensor.cpp v1.0
* @Copyright © 2018 Nobuhiro Funato and Shogo Arato
* @date 2018.11.16
*
* Released under the MIT license.
* see https://opensource.org/licenses/MIT
*/
#include "sensor.h"
DistanceSensor::DistanceSensor( PinName analog_value ):_analog_value(analog_value){
//_analog_value = 0;
}
int DistanceSensor::get_val(){
return (((_analog_value*3.3) - 2.375)/(-0.0375))*10;
}
gp2y_i2c::gp2y_i2c(PinName sda, PinName scl):
i2c(sda,scl)
{
Distance_adrs[0] = 0x5E;
Distance_adrs[1] = 0x5F;
}
gp2y_i2c::gp2y_i2c(PinName sda, PinName scl, int _frequncy):
i2c(sda,scl)
{
Distance_adrs[0] = 0x5E;
Distance_adrs[1] = 0x5F;
i2c.frequency(_frequncy);
}
char gp2y_i2c::i2c_read(int addr,char regist){
cmd[0] = regist;
i2c.write(addr,cmd,1);
i2c.read(addr,cmd,1);
return cmd[0];
}
void gp2y_i2c::set_frequency(int _val){
i2c.frequency(_val);
}
int gp2y_i2c::distance(char adrs){
return (int((i2c_read(adrs,Distance_adrs[0]))*16+(i2c_read(adrs,Distance_adrs[1]))/16/4));
}