diff options
Diffstat (limited to 'peripheral')
4 files changed, 43 insertions, 43 deletions
diff --git a/peripheral/sensors/edison_arduino/SensorDescriptionFactory.cpp b/peripheral/sensors/edison_arduino/SensorDescriptionFactory.cpp index ebee43c..a7385a1 100644 --- a/peripheral/sensors/edison_arduino/SensorDescriptionFactory.cpp +++ b/peripheral/sensors/edison_arduino/SensorDescriptionFactory.cpp @@ -18,24 +18,8 @@ #include "SensorDescriptionFactory.hpp" #include "sensors/Sensors.hpp" -SensorDescriptionFactory::SensorDescriptionFactory() { - for (int i = 0; i < Sensor::Type::kNumTypes; i++) { - descriptions[i] = *SensorDescriptionFactory::getDescription( - static_cast<Sensor::Type>(i)); - } -} - -struct sensor_t const * SensorDescriptionFactory::getDescriptions() { - return descriptions; -} - -bool SensorDescriptionFactory::areFlagsSet(int handle, uint32_t flags) { - if ((handle < 0) || (handle >= Sensor::Type::kNumTypes)) { - return false; - } - - return (descriptions[handle].flags & flags) == flags; -} +bool SensorDescriptionFactory::initialized = false; +struct sensor_t SensorDescriptionFactory::descriptions[Sensor::Type::kNumTypes]; struct sensor_t const * SensorDescriptionFactory::getDescription(Sensor::Type type) { @@ -58,3 +42,29 @@ SensorDescriptionFactory::getDescription(Sensor::Type type) { } return nullptr; } + +struct sensor_t const * SensorDescriptionFactory::getDescriptions() { + init(); + + return descriptions; +} + +bool SensorDescriptionFactory::areFlagsSet(int handle, uint32_t flags) { + if ((handle < 0) || (handle >= Sensor::Type::kNumTypes)) { + return false; + } + + init(); + + return (descriptions[handle].flags & flags) == flags; +} + +void SensorDescriptionFactory::init() { + if (!SensorDescriptionFactory::initialized) { + for (int i = 0; i < Sensor::Type::kNumTypes; i++) { + descriptions[i] = *SensorDescriptionFactory::getDescription( + static_cast<Sensor::Type>(i)); + } + SensorDescriptionFactory::initialized = true; + } +} diff --git a/peripheral/sensors/edison_arduino/SensorDescriptionFactory.hpp b/peripheral/sensors/edison_arduino/SensorDescriptionFactory.hpp index 3d77c8a..18807e5 100644 --- a/peripheral/sensors/edison_arduino/SensorDescriptionFactory.hpp +++ b/peripheral/sensors/edison_arduino/SensorDescriptionFactory.hpp @@ -29,34 +29,35 @@ class SensorDescriptionFactory { public: /** - * SensorDescriptionFactory constructor - */ - SensorDescriptionFactory(); + * Retrieve a sensor description of a certain type + * @param type sensor type + * @return pointer to a description data structure + */ + static struct sensor_t const * getDescription(Sensor::Type type); /** - * get all available sensor descriptions + * Get all available sensor descriptions * @return array of sensor description data structures */ - struct sensor_t const * getDescriptions(); + static struct sensor_t const * getDescriptions(); /** - * check if a sensor description has certain flags set + * Check if a sensor description has certain flags set * @param handle sensor type identifier * @param flags flags to check * @return true if flags are set, false otherwise */ - bool areFlagsSet(int handle, uint32_t flags); + static bool areFlagsSet(int handle, uint32_t flags); private: /* - * Retrieve a sensor description of a certain type - * @param type sensor type - * @return pointer to a description data structure + * Initialize static members */ - static struct sensor_t const * getDescription(Sensor::Type type); + static void init(); - struct sensor_t descriptions[Sensor::Type::kNumTypes]; + static struct sensor_t descriptions[Sensor::Type::kNumTypes]; + static bool initialized; }; #endif // SENSOR_DESCRIPTION_FACTORY_HPP diff --git a/peripheral/sensors/edison_arduino/SensorsHAL.cpp b/peripheral/sensors/edison_arduino/SensorsHAL.cpp index 74ca3d8..1863f1c 100644 --- a/peripheral/sensors/edison_arduino/SensorsHAL.cpp +++ b/peripheral/sensors/edison_arduino/SensorsHAL.cpp @@ -26,8 +26,6 @@ int gPollFd; -SensorDescriptionFactory SensorContext::sensorDescriptionFactory; - SensorContext::SensorContext(const hw_module_t* module) { memset(&device, 0, sizeof(device)); @@ -152,7 +150,7 @@ int SensorContext::flush(int handle) { } /* flush doesn't apply to one-shot sensors */ - if (SensorContext::sensorDescriptionFactory.areFlagsSet( + if (SensorDescriptionFactory::areFlagsSet( handle, SENSOR_FLAG_ONE_SHOT_MODE)) return -EINVAL; @@ -225,7 +223,7 @@ static struct hw_module_methods_t sensors_module_methods = { static int get_sensors_list(struct sensors_module_t* module, struct sensor_t const** list) { if (!list) return 0; - *list = SensorContext::getSensorDescriptions(); + *list = SensorDescriptionFactory::getDescriptions(); return Sensor::Type::kNumTypes; } diff --git a/peripheral/sensors/edison_arduino/SensorsHAL.hpp b/peripheral/sensors/edison_arduino/SensorsHAL.hpp index 08b43d3..d238c58 100644 --- a/peripheral/sensors/edison_arduino/SensorsHAL.hpp +++ b/peripheral/sensors/edison_arduino/SensorsHAL.hpp @@ -42,14 +42,6 @@ class SensorContext { */ ~SensorContext(); - /** - * Get all supported sensor descriptions - * @return array of sensor descriptions - */ - static struct sensor_t const * getSensorDescriptions() { - return SensorContext::sensorDescriptionFactory.getDescriptions(); - } - private: int activate(int handle, int enabled); int setDelay(int handle, int64_t ns); @@ -71,7 +63,6 @@ class SensorContext { static int FlushWrapper(sensors_poll_device_1_t* dev, int handle); Sensor * sensors[Sensor::Type::kNumTypes]; - static SensorDescriptionFactory sensorDescriptionFactory; }; #endif // SENSORS_HAL_HPP |