SSCMA-Micro CPP SDK  v2.0.0
SSCMA-Micro is a cross-platform machine learning inference framework designed for embedded devices.
sensor.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 #include <vector>
5 
6 #include "core/ma_core.h"
7 #include "porting/ma_porting.h"
8 #include "resource.hpp"
9 
10 namespace ma::server::callback {
11 
12 using namespace ma;
13 
14 void getAvailableSensors(const std::vector<std::string>& args, Transport& transport, Encoder& encoder) {
15  MA_ASSERT(args.size() >= 1);
16  const auto& cmd = args[0];
17 
18  auto& sensors = static_resource->device->getSensors();
19 
20  encoder.begin(MA_MSG_TYPE_RESP, MA_OK, cmd);
21  encoder.write(sensors);
22  encoder.end();
23  transport.send(reinterpret_cast<const char*>(encoder.data()), encoder.size());
24 }
25 
26 void configureSensor(const std::vector<std::string>& args,
27  Transport& transport,
28  Encoder& encoder,
29  bool called_by_event = false) {
30  MA_ASSERT(args.size() >= 4);
31  const auto& cmd = args[0];
32  const size_t sensor_id = std::atoi(args[1].c_str());
33  const size_t enable = std::atoi(args[2].c_str());
34  const size_t opt_id = std::atoi(args[3].c_str());
35  auto& sensors = static_resource->device->getSensors();
36 
37  ma_err_t ret = MA_OK;
38 
39  auto it = std::find_if(sensors.begin(), sensors.end(), [&](const Sensor* s) { return s->getID() == sensor_id; });
40  if (it == sensors.end()) {
41  ret = MA_ENOENT;
42  goto exit;
43  }
44 
45  switch ((*it)->getType()) {
47  auto* camera = static_cast<Camera*>(*it);
48  if (enable) {
49  if (*camera) {
50  camera->deInit();
51  }
52  ret = camera->init(opt_id);
53  } else {
54  camera->deInit();
55  }
56  } break;
57 
58  default:
59  ret = MA_ENOENT;
60  break;
61  }
62 
63  if (ret != MA_OK) {
64  goto exit;
65  }
66 
67  static_resource->current_sensor_id = sensor_id;
68 
69  if (!called_by_event) {
70  MA_STORAGE_SET_POD(
71  ret, static_resource->device->getStorage(), MA_STORAGE_KEY_SENSOR_ID, static_resource->current_sensor_id);
72  MA_STORAGE_SET_POD(ret, static_resource->device->getStorage(), MA_STORAGE_KEY_SENSOR_OPT_ID, opt_id);
73  }
74 
75 exit:
76  encoder.begin(MA_MSG_TYPE_RESP, ret, cmd);
77  if (it != sensors.end()) {
78  encoder.write(*it, (*it)->currentPresetIdx());
79  }
80  encoder.end();
81  transport.send(reinterpret_cast<const char*>(encoder.data()), encoder.size());
82 }
83 
84 void initDefaultSensor(Encoder& encoder) {
85  if (static_resource->device->getTransports().empty()) {
86  MA_LOGD(MA_TAG, "No transport available");
87  return;
88  }
89  auto& transport = static_resource->device->getTransports().front();
90  if (!transport || !*transport) {
91  MA_LOGD(MA_TAG, "Transport not available");
92  return;
93  }
94 
95  size_t sensor_id = 0;
96  size_t opt_id = 0;
97 
98  MA_STORAGE_GET_POD(static_resource->device->getStorage(), MA_STORAGE_KEY_SENSOR_ID, sensor_id, 0);
99  MA_STORAGE_GET_POD(static_resource->device->getStorage(), MA_STORAGE_KEY_SENSOR_OPT_ID, opt_id, 1);
100 
101  std::vector<std::string> args{
102  "INIT@SENSOR", std::to_string(static_cast<int>(sensor_id)), "1", std::to_string(static_cast<int>(opt_id))};
103  configureSensor(args, *transport, encoder, true);
104 }
105 
106 void getSensorStatus(const std::vector<std::string>& args, Transport& transport, Encoder& encoder) {
107  MA_ASSERT(args.size() >= 1);
108  const auto& cmd = args[0];
109 
110  ma_err_t ret = MA_OK;
111  auto& sensors = static_resource->device->getSensors();
112 
113  auto it = std::find_if(sensors.begin(), sensors.end(), [&](const Sensor* s) {
114  return s->getID() == static_resource->current_sensor_id;
115  });
116  if (it == sensors.end()) {
117  ret = MA_ENOENT;
118  }
119 
120  encoder.begin(MA_MSG_TYPE_RESP, ret, cmd);
121  if (it != sensors.end()) {
122  encoder.write(*it, (*it)->currentPresetIdx());
123  }
124  encoder.end();
125  transport.send(reinterpret_cast<const char*>(encoder.data()), encoder.size());
126 }
127 
128 } // namespace ma::server::callback
Definition: ma_camera.h:13
Definition: ma_codec_base.h:14
virtual ma_err_t end()=0
Encoder type for end.
virtual const void * data() const =0
Encoder type for get data.
virtual ma_err_t begin()=0
Encoder type for begin.
virtual ma_err_t write(const std::string &key, int8_t value)=0
Encoder type for write int8_t value.
virtual const size_t size() const =0
Encoder type for get size.
Definition: ma_sensor.h:12
virtual void deInit() noexcept=0
Definition: ma_transport.h:12
virtual size_t send(const char *data, size_t length) noexcept=0
#define MA_LOGD(TAG,...)
Definition: ma_debug.h:79
#define MA_ASSERT(expr)
Definition: ma_debug.h:119
#define MA_TAG
Definition: ma_debug.h:9
#define MA_STORAGE_KEY_SENSOR_OPT_ID
Definition: ma_definations.h:23
#define MA_STORAGE_KEY_SENSOR_ID
Definition: ma_definations.h:22
@ MA_MSG_TYPE_RESP
Definition: ma_types.h:252
ma_err_t
Definition: ma_types.h:21
@ MA_ENOENT
Definition: ma_types.h:33
@ MA_OK
Definition: ma_types.h:23
Definition: algorithm.hpp:11
void initDefaultSensor(Encoder &encoder)
Definition: sensor.hpp:84
void getSensorStatus(const std::vector< std::string > &args, Transport &transport, Encoder &encoder)
Definition: sensor.hpp:106
void configureSensor(const std::vector< std::string > &args, Transport &transport, Encoder &encoder, bool called_by_event=false)
Definition: sensor.hpp:26
void getAvailableSensors(const std::vector< std::string > &args, Transport &transport, Encoder &encoder)
Definition: sensor.hpp:14
Definition: ma_cv.cpp:7
#define static_resource
Definition: resource.hpp:64