aboutsummaryrefslogtreecommitdiff
path: root/sensors.cpp
blob: 881752896e1c067b805d11b315a5bb2bb9442327 (plain) (blame)
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
#include <cstdio>
#include <cstring>
#include <variant>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include "utils.hpp"
#include "sensors.hpp"
void SensorBase::parse_type_string(std::string type,scan_t* ti)
{
	char endian[4],sign;
	if(type.find('X')!=type.npos)
		sscanf(type.c_str(),"%2s:%c%hhu/%hhuX%hhu>>%hhu",
			endian,&sign,&ti->bits,&ti->storagebits,&ti->repeat,&ti->shift
		);
	else
	{
		ti->repeat=0;
		sscanf(type.c_str(),"%2s:%c%hhu/%hhu>>%hhu",
			endian,&sign,&ti->bits,&ti->storagebits,&ti->shift
		);
	}
	ti->is_le=std::string(endian)=="le";
	ti->is_signed=sign=='s';
}
void SensorBase::readbuffer()
{
	char* buf=new char[readsize];
	ssize_t sz=read(devfd,buf,readsize);
	if(sz==readsize)
	{
		char *p=buf;
		for(int i=0;p-buf<readsize;++i)
		{
			scan_t ti=std::get<scan_t>(enabled_scan_elem[i]);
			std::string es=std::get<std::string>(enabled_scan_elem[i])+"_value";
			if(ti.is_le)
			switch(ti.storagebits)
			{
				case 8:
					if(ti.is_signed)
					{int8_t t;memcpy(&t,p,1);t>>=ti.shift;dict[es]=t;}
					else
					{uint8_t t;memcpy(&t,p,1);t>>=ti.shift;dict[es]=t;}
					++p;
				break;
				case 16:
					if(ti.is_signed)
					{int16_t t;memcpy(&t,p,2);t>>=ti.shift;dict[es]=t;}
					else
					{uint16_t t;memcpy(&t,p,2);t>>=ti.shift;dict[es]=t;}
					p+=2;
				break;
				case 32:
					if(ti.is_signed)
					{int32_t t;memcpy(&t,p,4);t>>=ti.shift;dict[es]=t;}
					else
					{uint32_t t;memcpy(&t,p,4);t>>=ti.shift;dict[es]=t;}
					p+=4;
				break;
				case 64:
					if(ti.is_signed)
					{int64_t t;memcpy(&t,p,8);t>>=ti.shift;dict[es]=t;}
					else
					{uint64_t t;memcpy(&t,p,8);t>>=ti.shift;dict[es]=t;}
					p+=8;
				break;
			}
		}
	}
	delete[] buf;
}
void SensorBase::enable_buffer()
{
	using filesystem::path;
	
	path buffer_enable_path=sysfspath/"buffer"/"enable";
	writeint(buffer_enable_path.c_str(),1);
}
void SensorBase::enable_scan_element(std::string elem)
{
	using filesystem::path;
	std::string elem_base=sensor_basename+(elem.length()?"_"+elem:"");
	
	path elem_type_path=sysfspath/"scan_elements"/(elem_base+"_type");
	std::string ts;dict[elem_base+"_type"]=ts=readstr(elem_type_path.c_str());
	scan_t st;parse_type_string(ts,&st);
	readsize+=st.storagebits/8;//assume this shit is aligned to byte

	path elem_en_path=sysfspath/"scan_elements"/(elem_base+"_en");
	writeint(elem_en_path.c_str(),1);

	path elem_idx_path=sysfspath/"scan_elements"/(elem_base+"_index");
	int idx;dict[elem_base+"_index"]=idx=readint(elem_idx_path.c_str());
	
	path raw_val_path=sysfspath/(elem_base+"_raw");//initial value
	dict[elem_base+"_value"]=readint(raw_val_path.c_str());
	
	enabled_scan_elem.insert(
		std::upper_bound(enabled_scan_elem.begin(),enabled_scan_elem.end(),
			std::make_tuple(idx,elem_base,st),
			[](const auto&a,const auto&b)->bool{return std::get<0>(a)<std::get<0>(b);}
		),std::make_tuple(idx,elem_base,st)
	);
}
void SensorBase::init(int id,std::string _sensor_basename)
{
	sysfspath=IIODEV_SYSFS_PATH_BASE+std::to_string(id);
	devbufpath=DEV_PATH+std::to_string(id);
	sensor_basename=_sensor_basename;

	using filesystem::path;
	path name_path=sysfspath/"name";
	type=readstr(name_path.c_str());
	path scale_path=sysfspath/(sensor_basename+"_scale");
	dict[sensor_basename+"_scale"]=readfloat(scale_path.c_str());
	path offset_path=sysfspath/(sensor_basename+"_offset");
	dict[sensor_basename+"_offset"]=readfloat(offset_path.c_str());

	readsize=0;
	enabled_scan_elem.clear();
	enable_scan_elements();
	update_values();
	enable_buffer();
	devfd=open(devbufpath.c_str(),O_RDONLY);
	if(!~devfd)LOG('E',"failed to open the iio buffer device: %s",devbufpath.c_str());
}
void SensorBase::deinit()
{
	if(~devfd)close(devfd);
	devfd=-1;
}
void SensorBase::reset()
{
	deinit();
	using filesystem::path;
	
	path buffer_enable_path=sysfspath/"buffer"/"enable";
	writeint(buffer_enable_path.c_str(),0);
	for(auto& ent:filesystem::directory_iterator(sysfspath/"scan_elements"))
		if(ent.path().string().substr(ent.path().string().length()-3)=="_en")
		writeint(ent.path().c_str(),0);
}
void SensorBase::worker()
{
	for(workerquit=0;!workerquit;)
	{
		readbuffer();update_values();
		if(readercb!=nullptr)readercb(this);
	}
}
void SensorBase::quit_worker()
{workerquit=1;}
void SensorBase::set_reader_callback(std::function<void(SensorBase*)> cb)
{readercb=cb;}
std::string SensorBase::get_type(int id)
{
	using filesystem::path;
	path sysfspath=path(IIODEV_SYSFS_PATH_BASE+std::to_string(id));

	path name_path=sysfspath/"name";
	return readstr(name_path.c_str());
}
int SensorBase::detect_sensor(std::string type)
{
	using filesystem::path;
	path sysfsbasepath=path(IIODEV_SYSFS_PATH_BASE).remove_filename();
	for(auto& ent:filesystem::directory_iterator(sysfsbasepath))
	{
		path name_path=ent.path()/"name";
		if(trim(readstr(name_path.c_str()))==type)
		{
			std::string es=ent.path().filename().string();
			size_t i;
			for(i=0;i<es.length()&&(es[i]<'0'||es[i]>'9');++i);
			if(i<es.length())return atoi(es.c_str()+i);
			return -1;
		}
	}
	return -1;
}