forked from ddemidov/ev3dev-lang-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ev3dev.cpp
1401 lines (1093 loc) · 35.3 KB
/
ev3dev.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
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
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* C++ API to the sensors, motors, buttons, LEDs and battery of the ev3dev
* Linux kernel for the LEGO Mindstorms EV3 hardware
*
* Copyright (c) 2014 - Franz Detro
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Modification:
* Add new button for ev3dev Release 02.00.00 (ev3dev-jessie-2014-07-12) - Christophe Chaudelet
*
*/
//-----------------------------------------------------------------------------
//~autogen autogen-header
// Sections of the following code were auto-generated based on spec v1.2.0.
//~autogen
//-----------------------------------------------------------------------------
#include "ev3dev.h"
#include <iostream>
#include <sstream>
#include <fstream>
#include <list>
#include <map>
#include <array>
#include <algorithm>
#include <system_error>
#include <mutex>
#include <chrono>
#include <thread>
#include <stdexcept>
#include <string.h>
#include <math.h>
#include <dirent.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#ifndef SYS_ROOT
#define SYS_ROOT "/sys/class"
#endif
#ifndef FSTREAM_CACHE_SIZE
#define FSTREAM_CACHE_SIZE 16
#endif
#ifndef NO_LINUX_HEADERS
#include <linux/fb.h>
#include <linux/input.h>
#else
#define KEY_CNT 8
#endif
static const int bits_per_long = sizeof(long) * 8;
//-----------------------------------------------------------------------------
namespace ev3dev {
namespace {
// This class implements a small LRU cache. It assumes the number of elements
// is small, and so uses a simple linear search.
template <typename K, typename V>
class lru_cache
{
private:
// typedef st::pair<K, V> item;
// std::pair seems to be missing necessary move constructors :(
struct item
{
K first;
V second;
item(const K &k) : first(k) {}
item(item &&m) : first(std::move(m.first)), second(std::move(m.second)) {}
};
public:
lru_cache(size_t size = 3) : _size(size)
{
}
V &operator[] (const K &k)
{
iterator i = find(k);
if (i != _items.end())
{
// Found the key, bring the item to the front.
_items.splice(_items.begin(), _items, i);
} else {
// If the cache is full, remove oldest items to make room.
while (_items.size() + 1 > _size)
{
_items.pop_back();
}
// Insert a new default constructed value for this new key.
_items.emplace_front(k);
}
// The new item is the most recently used.
return _items.front().second;
}
void clear()
{
_items.clear();
}
private:
typedef typename std::list<item>::iterator iterator;
iterator find(const K &k)
{
return std::find_if(_items.begin(), _items.end(),
[&](const item &i) { return i.first == k; });
}
size_t _size;
std::list<item> _items;
};
// A global cache of files.
std::ifstream& ifstream_cache(const std::string &path) {
static lru_cache<std::string, std::ifstream> cache(FSTREAM_CACHE_SIZE);
static std::mutex mx;
std::lock_guard<std::mutex> lock(mx);
return cache[path];
}
std::ofstream& ofstream_cache(const std::string &path) {
static lru_cache<std::string, std::ofstream> cache(FSTREAM_CACHE_SIZE);
static std::mutex mx;
std::lock_guard<std::mutex> lock(mx);
return cache[path];
}
//-----------------------------------------------------------------------------
std::ofstream &ofstream_open(const std::string &path)
{
std::ofstream &file = ofstream_cache(path);
if (!file.is_open())
{
// Don't buffer writes to avoid latency. Also saves a bit of memory.
file.rdbuf()->pubsetbuf(NULL, 0);
file.open(path);
}
else
{
// Clear the error bits in case something happened.
file.clear();
}
return file;
}
std::ifstream &ifstream_open(const std::string &path)
{
std::ifstream &file = ifstream_cache(path);
if (!file.is_open())
{
file.open(path);
}
else
{
// Clear the flags bits in case something happened (like reaching EOF).
file.clear();
file.seekg(0, std::ios::beg);
}
return file;
}
} // namespace
//-----------------------------------------------------------------------------
bool device::connect(
const std::string &dir,
const std::string &pattern,
const std::map<std::string, std::set<std::string>> &match
) noexcept
{
using namespace std;
const size_t pattern_length = pattern.length();
struct dirent *dp;
DIR *dfd;
if ((dfd = opendir(dir.c_str())) != nullptr)
{
while ((dp = readdir(dfd)) != nullptr)
{
if (strncmp(dp->d_name, pattern.c_str(), pattern_length)==0)
{
try
{
_path = dir + dp->d_name + '/';
bool bMatch = true;
for (auto &m : match)
{
const auto &attribute = m.first;
const auto &matches = m.second;
const auto strValue = get_attr_string(attribute);
if (!matches.empty() && !matches.begin()->empty() &&
(matches.find(strValue) == matches.end()))
{
bMatch = false;
break;
}
}
if (bMatch) {
closedir(dfd);
return true;
}
}
catch (...) { }
_path.clear();
}
}
closedir(dfd);
}
return false;
}
//-----------------------------------------------------------------------------
int device::device_index() const
{
using namespace std;
if (_path.empty())
throw system_error(make_error_code(errc::function_not_supported), "no device connected");
if (_device_index < 0)
{
unsigned f = 1;
_device_index = 0;
for (auto it=_path.rbegin(); it!=_path.rend(); ++it)
{
if(*it =='/')
continue;
if ((*it < '0') || (*it > '9'))
break;
_device_index += (*it -'0') * f;
f *= 10;
}
}
return _device_index;
}
//-----------------------------------------------------------------------------
int device::get_attr_int(const std::string &name) const {
using namespace std;
if (_path.empty())
throw system_error(make_error_code(errc::function_not_supported), "no device connected");
for(int attempt = 0; attempt < 2; ++attempt) {
ifstream &is = ifstream_open(_path + name);
if (is.is_open())
{
int result = 0;
try {
is >> result;
return result;
} catch(...) {
// This could mean the sysfs attribute was recreated and the
// corresponding file handle got stale. Lets close the file and try
// again (once):
if (attempt != 0) throw;
is.close();
is.clear();
}
} else break;
}
throw system_error(make_error_code(errc::no_such_device), _path+name);
}
//-----------------------------------------------------------------------------
void device::set_attr_int(const std::string &name, int value) {
using namespace std;
if (_path.empty())
throw system_error(make_error_code(errc::function_not_supported), "no device connected");
for(int attempt = 0; attempt < 2; ++attempt) {
ofstream &os = ofstream_open(_path + name);
if (os.is_open())
{
if (os << value) return;
// An error could mean that sysfs attribute was recreated and the cached
// file handle is stale. Lets close the file and try again (once):
if (attempt == 0 && errno == ENODEV) {
os.close();
os.clear();
} else {
throw system_error(std::error_code(errno, std::system_category()));
}
} else {
throw system_error(make_error_code(errc::no_such_device), _path + name);
}
}
}
//-----------------------------------------------------------------------------
std::string device::get_attr_string(const std::string &name) const
{
using namespace std;
if (_path.empty())
throw system_error(make_error_code(errc::function_not_supported), "no device connected");
ifstream &is = ifstream_open(_path + name);
if (is.is_open())
{
string result;
is >> result;
return result;
}
throw system_error(make_error_code(errc::no_such_device), _path+name);
}
//-----------------------------------------------------------------------------
void device::set_attr_string(const std::string &name, const std::string &value)
{
using namespace std;
if (_path.empty())
throw system_error(make_error_code(errc::function_not_supported), "no device connected");
ofstream &os = ofstream_open(_path + name);
if (os.is_open())
{
if (!(os << value)) throw system_error(std::error_code(errno, std::system_category()));
return;
}
throw system_error(make_error_code(errc::no_such_device), _path+name);
}
//-----------------------------------------------------------------------------
std::string device::get_attr_line(const std::string &name) const
{
using namespace std;
if (_path.empty())
throw system_error(make_error_code(errc::function_not_supported), "no device connected");
ifstream &is = ifstream_open(_path + name);
if (is.is_open())
{
string result;
getline(is, result);
return result;
}
throw system_error(make_error_code(errc::no_such_device), _path+name);
}
//-----------------------------------------------------------------------------
mode_set device::get_attr_set(const std::string &name,
std::string *pCur) const
{
using namespace std;
string s = get_attr_line(name);
mode_set result;
size_t pos, last_pos = 0;
string t;
do {
pos = s.find(' ', last_pos);
if (pos != string::npos)
{
t = s.substr(last_pos, pos-last_pos);
last_pos = pos+1;
}
else
t = s.substr(last_pos);
if (!t.empty())
{
if (*t.begin()=='[')
{
t = t.substr(1, t.length()-2);
if (pCur)
*pCur = t;
}
result.insert(t);
}
} while (pos!=string::npos);
return result;
}
//-----------------------------------------------------------------------------
std::string device::get_attr_from_set(const std::string &name) const
{
using namespace std;
string s = get_attr_line(name);
size_t pos, last_pos = 0;
string t;
do {
pos = s.find(' ', last_pos);
if (pos != string::npos)
{
t = s.substr(last_pos, pos-last_pos);
last_pos = pos+1;
}
else
t = s.substr(last_pos);
if (!t.empty())
{
if (*t.begin()=='[')
{
return t.substr(1, t.length()-2);
}
}
} while (pos!=string::npos);
return { "none" };
}
//-----------------------------------------------------------------------------
constexpr char sensor::ev3_touch[];
constexpr char sensor::ev3_color[];
constexpr char sensor::ev3_ultrasonic[];
constexpr char sensor::ev3_gyro[];
constexpr char sensor::ev3_infrared[];
constexpr char sensor::nxt_touch[];
constexpr char sensor::nxt_light[];
constexpr char sensor::nxt_sound[];
constexpr char sensor::nxt_ultrasonic[];
constexpr char sensor::nxt_i2c_sensor[];
constexpr char sensor::nxt_analog[];
//-----------------------------------------------------------------------------
sensor::sensor(address_type address)
{
connect({{ "address", { address }}});
}
//-----------------------------------------------------------------------------
sensor::sensor(address_type address, const std::set<sensor_type> &types)
{
connect({{ "address", { address }},
{ "driver_name", types }});
}
//-----------------------------------------------------------------------------
bool sensor::connect(const std::map<std::string, std::set<std::string>> &match) noexcept
{
static const std::string _strClassDir { SYS_ROOT "/lego-sensor/" };
static const std::string _strPattern { "sensor" };
try
{
if (device::connect(_strClassDir, _strPattern, match))
{
return true;
}
}
catch (...) { }
_path.clear();
return false;
}
//-----------------------------------------------------------------------------
std::string sensor::type_name() const
{
auto type = driver_name();
if (type.empty())
{
static const std::string s("<none>");
return s;
}
static const std::map<sensor_type, const std::string> lookup_table {
{ ev3_touch, "EV3 touch" },
{ ev3_color, "EV3 color" },
{ ev3_ultrasonic, "EV3 ultrasonic" },
{ ev3_gyro, "EV3 gyro" },
{ ev3_infrared, "EV3 infrared" },
{ nxt_touch, "NXT touch" },
{ nxt_light, "NXT light" },
{ nxt_sound, "NXT sound" },
{ nxt_ultrasonic, "NXT ultrasonic" },
{ nxt_i2c_sensor, "I2C sensor" },
};
auto s = lookup_table.find(type);
if (s != lookup_table.end())
return s->second;
return type;
}
//-----------------------------------------------------------------------------
int sensor::value(unsigned index) const
{
if (static_cast<int>(index) >= num_values())
throw std::invalid_argument("index");
char svalue[7] = "value0";
svalue[5] += index;
return get_attr_int(svalue);
}
//-----------------------------------------------------------------------------
float sensor::float_value(unsigned index) const
{
return value(index) * powf(10, -decimals());
}
//-----------------------------------------------------------------------------
const std::vector<char>& sensor::bin_data() const
{
using namespace std;
if (_path.empty())
throw system_error(make_error_code(errc::function_not_supported), "no device connected");
if (_bin_data.empty()) {
static const map<string, int> lookup_table {
{"u8", 1},
{"s8", 1},
{"u16", 2},
{"s16", 2},
{"s16_be", 2},
{"s32", 4},
{"float", 4}
};
int value_size = 1;
auto s = lookup_table.find(bin_data_format());
if (s != lookup_table.end())
value_size = s->second;
_bin_data.resize(num_values() * value_size);
}
const string fname = _path + "bin_data";
ifstream &is = ifstream_open(fname);
if (is.is_open())
{
is.read(_bin_data.data(), _bin_data.size());
return _bin_data;
}
throw system_error(make_error_code(errc::no_such_device), fname);
}
//-----------------------------------------------------------------------------
i2c_sensor::i2c_sensor(address_type address, const std::set<sensor_type> &types) :
sensor(address, types)
{
}
//-----------------------------------------------------------------------------
//~autogen generic-define-property-value specialSensorTypes.touchSensor>currentClass
constexpr char touch_sensor::mode_touch[];
//~autogen
touch_sensor::touch_sensor(address_type address) :
sensor(address, { ev3_touch, nxt_touch })
{
}
//-----------------------------------------------------------------------------
//~autogen generic-define-property-value specialSensorTypes.colorSensor>currentClass
constexpr char color_sensor::mode_col_reflect[];
constexpr char color_sensor::mode_col_ambient[];
constexpr char color_sensor::mode_col_color[];
constexpr char color_sensor::mode_ref_raw[];
constexpr char color_sensor::mode_rgb_raw[];
constexpr char color_sensor::color_nocolor[];
constexpr char color_sensor::color_black[];
constexpr char color_sensor::color_blue[];
constexpr char color_sensor::color_green[];
constexpr char color_sensor::color_yellow[];
constexpr char color_sensor::color_red[];
constexpr char color_sensor::color_white[];
constexpr char color_sensor::color_brown[];
//~autogen
color_sensor::color_sensor(address_type address) :
sensor(address, { ev3_color })
{
}
//-----------------------------------------------------------------------------
//~autogen generic-define-property-value specialSensorTypes.ultrasonicSensor>currentClass
constexpr char ultrasonic_sensor::mode_us_dist_cm[];
constexpr char ultrasonic_sensor::mode_us_dist_in[];
constexpr char ultrasonic_sensor::mode_us_listen[];
constexpr char ultrasonic_sensor::mode_us_si_cm[];
constexpr char ultrasonic_sensor::mode_us_si_in[];
//~autogen
ultrasonic_sensor::ultrasonic_sensor(address_type address) :
sensor(address, { ev3_ultrasonic, nxt_ultrasonic })
{
}
ultrasonic_sensor::ultrasonic_sensor(address_type address, const std::set<sensor_type>& sensorTypes) :
sensor(address, sensorTypes)
{
}
//-----------------------------------------------------------------------------
//~autogen generic-define-property-value specialSensorTypes.gyroSensor>currentClass
constexpr char gyro_sensor::mode_gyro_ang[];
constexpr char gyro_sensor::mode_gyro_rate[];
constexpr char gyro_sensor::mode_gyro_fas[];
constexpr char gyro_sensor::mode_gyro_g_a[];
constexpr char gyro_sensor::mode_gyro_cal[];
//~autogen
gyro_sensor::gyro_sensor(address_type address) :
sensor(address, { ev3_gyro })
{
}
//-----------------------------------------------------------------------------
//~autogen generic-define-property-value specialSensorTypes.infraredSensor>currentClass
constexpr char infrared_sensor::mode_ir_prox[];
constexpr char infrared_sensor::mode_ir_seek[];
constexpr char infrared_sensor::mode_ir_remote[];
constexpr char infrared_sensor::mode_ir_rem_a[];
constexpr char infrared_sensor::mode_ir_cal[];
//~autogen
infrared_sensor::infrared_sensor(address_type address) :
sensor(address, { ev3_infrared })
{
}
//-----------------------------------------------------------------------------
//~autogen generic-define-property-value specialSensorTypes.soundSensor>currentClass
constexpr char sound_sensor::mode_db[];
constexpr char sound_sensor::mode_dba[];
//~autogen
sound_sensor::sound_sensor(address_type address) :
sensor(address, { nxt_sound, nxt_analog })
{
if (connected() && driver_name() == nxt_analog) {
lego_port port(address);
if (port.connected()) {
port.set_set_device(nxt_sound);
if (port.status() != nxt_sound) {
// Failed to load lego-nxt-sound friver. Wrong port?
_path.clear();
}
} else {
_path.clear();
}
}
}
//-----------------------------------------------------------------------------
//~autogen generic-define-property-value specialSensorTypes.lightSensor>currentClass
constexpr char light_sensor::mode_reflect[];
constexpr char light_sensor::mode_ambient[];
//~autogen
light_sensor::light_sensor(address_type address) :
sensor(address, { nxt_light })
{
}
//-----------------------------------------------------------------------------
constexpr char motor::motor_large[];
constexpr char motor::motor_medium[];
//~autogen generic-define-property-value classes.motor>currentClass
constexpr char motor::command_run_forever[];
constexpr char motor::command_run_to_abs_pos[];
constexpr char motor::command_run_to_rel_pos[];
constexpr char motor::command_run_timed[];
constexpr char motor::command_run_direct[];
constexpr char motor::command_stop[];
constexpr char motor::command_reset[];
constexpr char motor::encoder_polarity_normal[];
constexpr char motor::encoder_polarity_inversed[];
constexpr char motor::polarity_normal[];
constexpr char motor::polarity_inversed[];
constexpr char motor::state_running[];
constexpr char motor::state_ramping[];
constexpr char motor::state_holding[];
constexpr char motor::state_overloaded[];
constexpr char motor::state_stalled[];
constexpr char motor::stop_action_coast[];
constexpr char motor::stop_action_brake[];
constexpr char motor::stop_action_hold[];
//~autogen
//-----------------------------------------------------------------------------
motor::motor(address_type address)
{
connect({{ "address", { address } }});
}
//-----------------------------------------------------------------------------
motor::motor(address_type address, const motor_type &t)
{
connect({{ "address", { address } }, { "driver_name", { t }}});
}
//-----------------------------------------------------------------------------
bool motor::connect(const std::map<std::string, std::set<std::string>> &match) noexcept
{
static const std::string _strClassDir { SYS_ROOT "/tacho-motor/" };
static const std::string _strPattern { "motor" };
try
{
return device::connect(_strClassDir, _strPattern, match);
}
catch (...) { }
_path.clear();
return false;
}
//-----------------------------------------------------------------------------
medium_motor::medium_motor(address_type address) : motor(address, motor_medium)
{
}
//-----------------------------------------------------------------------------
large_motor::large_motor(address_type address) : motor(address, motor_large)
{
}
//-----------------------------------------------------------------------------
dc_motor::dc_motor(address_type address)
{
static const std::string _strClassDir { SYS_ROOT "/dc-motor/" };
static const std::string _strPattern { "motor" };
connect(_strClassDir, _strPattern, {{ "address", { address }}});
}
//~autogen generic-define-property-value classes.dcMotor>currentClass
constexpr char dc_motor::command_run_forever[];
constexpr char dc_motor::command_run_timed[];
constexpr char dc_motor::command_run_direct[];
constexpr char dc_motor::command_stop[];
constexpr char dc_motor::polarity_normal[];
constexpr char dc_motor::polarity_inversed[];
constexpr char dc_motor::stop_action_coast[];
constexpr char dc_motor::stop_action_brake[];
//~autogen
//-----------------------------------------------------------------------------
servo_motor::servo_motor(address_type address)
{
static const std::string _strClassDir { SYS_ROOT "/servo-motor/" };
static const std::string _strPattern { "motor" };
connect(_strClassDir, _strPattern, {{ "address", { address }}});
}
//~autogen generic-define-property-value classes.servoMotor>currentClass
constexpr char servo_motor::command_run[];
constexpr char servo_motor::command_float[];
constexpr char servo_motor::polarity_normal[];
constexpr char servo_motor::polarity_inversed[];
//~autogen
//-----------------------------------------------------------------------------
led::led(std::string name)
{
static const std::string _strClassDir { SYS_ROOT "/leds/" };
connect(_strClassDir, name, std::map<std::string, std::set<std::string>>());
}
//-----------------------------------------------------------------------------
void led::flash(unsigned on_ms, unsigned off_ms)
{
static const mode_type timer("timer");
set_trigger(timer);
if (on_ms)
{
// A workaround for ev3dev/ev3dev#225.
// It takes some time for delay_{on,off} sysfs attributes to appear after
// led trigger has been set to "timer".
for (int i = 0; ; ++i) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
try {
set_delay_on (on_ms );
set_delay_off(off_ms);
break;
} catch(...) {
if (i >= 5) throw;
}
}
}
}
//-----------------------------------------------------------------------------
#if defined(EV3DEV_PLATFORM_BRICKPI)
//~autogen leds-define platforms.brickpi.led>currentClass
led led::blue_led1{"brickpi:led1:blue:ev3dev"};
led led::blue_led2{"brickpi:led2:blue:ev3dev"};
std::vector<led*> led::led1{ &led::blue_led1 };
std::vector<led*> led::led2{ &led::blue_led2 };
std::vector<float> led::black{ static_cast<float>(0) };
std::vector<float> led::blue{ static_cast<float>(1) };
//-----------------------------------------------------------------------------
void led::all_off() {
blue_led1.off();
blue_led2.off();
}
//~autogen
#elif defined(EV3DEV_PLATFORM_BRICKPI3)
led led::amber_led1{"brickpi3:amber:ev3dev"};
std::vector<led*> led::led1{ &led::amber_led1 };
std::vector<float> led::black{ static_cast<float>(0) };
std::vector<float> led::blue{ static_cast<float>(1) };
//-----------------------------------------------------------------------------
void led::all_off() {
amber_led1.off();
}
#elif defined(EV3DEV_PLATFORM_PISTORMS)
//~autogen leds-define platforms.pistorms.led>currentClass
led led::red_left{"pistorms:BB:red:ev3dev"};
led led::red_right{"pistorms:BA:red:ev3dev"};
led led::green_left{"pistorms:BB:green:ev3dev"};
led led::green_right{"pistorms:BA:green:ev3dev"};
led led::blue_left{"pistorms:BB:blue:ev3dev"};
led led::blue_right{"pistorms:BA:blue:ev3dev"};
std::vector<led*> led::left{ &led::red_left, &led::green_left, &led::blue_left };
std::vector<led*> led::right{ &led::red_right, &led::green_right, &led::blue_right };
std::vector<float> led::black{ static_cast<float>(0), static_cast<float>(0), static_cast<float>(0) };
std::vector<float> led::red{ static_cast<float>(1), static_cast<float>(0), static_cast<float>(0) };
std::vector<float> led::green{ static_cast<float>(0), static_cast<float>(1), static_cast<float>(0) };
std::vector<float> led::blue{ static_cast<float>(0), static_cast<float>(0), static_cast<float>(1) };
std::vector<float> led::yellow{ static_cast<float>(1), static_cast<float>(1), static_cast<float>(0) };
std::vector<float> led::purple{ static_cast<float>(1), static_cast<float>(0), static_cast<float>(1) };
std::vector<float> led::cyan{ static_cast<float>(0), static_cast<float>(1), static_cast<float>(1) };
std::vector<float> led::white{ static_cast<float>(1), static_cast<float>(1), static_cast<float>(1) };
std::vector<float> led::orange{ static_cast<float>(1), static_cast<float>(0.5), static_cast<float>(0) };
//-----------------------------------------------------------------------------
void led::all_off() {
red_left.off();
red_right.off();
green_left.off();
green_right.off();
blue_left.off();
blue_right.off();
}
//~autogen
#else
//~autogen leds-define platforms.ev3.led>currentClass
led led::red_left{"led0:red:brick-status"};
led led::red_right{"led1:red:brick-status"};
led led::green_left{"led0:green:brick-status"};
led led::green_right{"led1:green:brick-status"};
std::vector<led*> led::left{ &led::red_left, &led::green_left };
std::vector<led*> led::right{ &led::red_right, &led::green_right };
std::vector<float> led::black{ static_cast<float>(0), static_cast<float>(0) };
std::vector<float> led::red{ static_cast<float>(1), static_cast<float>(0) };
std::vector<float> led::green{ static_cast<float>(0), static_cast<float>(1) };
std::vector<float> led::amber{ static_cast<float>(1), static_cast<float>(1) };
std::vector<float> led::orange{ static_cast<float>(1), static_cast<float>(0.5) };
std::vector<float> led::yellow{ static_cast<float>(0.1), static_cast<float>(1) };
//-----------------------------------------------------------------------------
void led::all_off() {
red_left.off();
red_right.off();
green_left.off();
green_right.off();
}
//~autogen
#endif