|
| 1 | +/* |
| 2 | + * PerfectGyroscopeI2CSensor.hxx |
| 3 | + * Copyright 2020 ItJustWorksTM |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +#ifndef SMARTCAR_EMUL_PERFECTGYROSCOPEI2CSENSOR_HXX |
| 19 | +#define SMARTCAR_EMUL_PERFECTGYROSCOPEI2CSENSOR_HXX |
| 20 | + |
| 21 | +#include <array> |
| 22 | +#include <bit> |
| 23 | +#include <rapidjson/include/rapidjson/document.h> |
| 24 | +#include <Urho3D/Scene/LogicComponent.h> |
| 25 | +#include <Urho3D/Core/Context.h> |
| 26 | +#include "BoardData.hxx" |
| 27 | +#include "DeviceMap.hxx" |
| 28 | + |
| 29 | + |
| 30 | +class PerfectGyroscopeI2CSensor : public Urho3D::LogicComponent { |
| 31 | + URHO3D_OBJECT(PerfectGyroscopeI2CSensor, Urho3D::LogicComponent); |
| 32 | + |
| 33 | + std::function<void()> ticker; |
| 34 | + |
| 35 | + enum Registers { |
| 36 | + WHO_AM_I = 0xF0, |
| 37 | + CTRL_REG1 [[maybe_unused]] = 0x20, |
| 38 | + CTRL_REG2 [[maybe_unused]] = 0x21, |
| 39 | + CTRL_REG3 [[maybe_unused]] = 0x22, |
| 40 | + CTRL_REG4 [[maybe_unused]] = 0x23, |
| 41 | + CTRL_REG5 [[maybe_unused]] = 0x24, |
| 42 | + REFERENCE [[maybe_unused]] = 0x25, |
| 43 | + OUT_TEMP = 0x26, |
| 44 | + STATUS_REG [[maybe_unused]] = 0x27, |
| 45 | + OUT_X_L = 0x28, |
| 46 | + OUT_X_H = 0x29, |
| 47 | + OUT_Y_L = 0x2A, |
| 48 | + OUT_Y_H = 0x2B, |
| 49 | + OUT_Z_L = 0x2C, |
| 50 | + OUT_Z_H = 0x2D, |
| 51 | + }; |
| 52 | + |
| 53 | + enum class PowerMode { |
| 54 | + powered_down, |
| 55 | + sleeping, |
| 56 | + normal, |
| 57 | + }; |
| 58 | + enum class DataRate { |
| 59 | + hz100 [[maybe_unused]], |
| 60 | + hz200 [[maybe_unused]], |
| 61 | + hz400 [[maybe_unused]], |
| 62 | + hz800 [[maybe_unused]], |
| 63 | + }; |
| 64 | + enum class FullScale { |
| 65 | + dps250, |
| 66 | + dps500, |
| 67 | + dps2000, |
| 68 | + }; |
| 69 | + enum Enabled { |
| 70 | + x = 1, |
| 71 | + y = 2, |
| 72 | + z = 4, |
| 73 | + }; |
| 74 | + |
| 75 | + // Full scale to sensitivity |
| 76 | + constexpr static std::array fs2sens{0.00875f, 0.0175f, 0.07f}; |
| 77 | + |
| 78 | + using RDev = regmon::Device<PerfectGyroscopeI2CSensor>; |
| 79 | + const RDev::DeviceMap gy = RDev::make_device( |
| 80 | + RDev::DefaultsTo<WHO_AM_I>{0b11010011}, |
| 81 | + RDev::DefaultsTo<OUT_TEMP>{20} // °C |
| 82 | + ); |
| 83 | + |
| 84 | + RDev::DeviceStorage store{}; |
| 85 | + I2cBus* bus; |
| 86 | + |
| 87 | + [[maybe_unused]] [[nodiscard]] bool is_powered_up() const noexcept; |
| 88 | + [[nodiscard]] PowerMode get_power_mode() const noexcept; |
| 89 | + [[maybe_unused]] [[nodiscard]] DataRate get_data_rate() const noexcept; |
| 90 | + [[nodiscard]] FullScale get_full_scale() const noexcept; |
| 91 | + [[nodiscard]] std::endian get_data_endianness() const noexcept; |
| 92 | + public: |
| 93 | + PerfectGyroscopeI2CSensor(BoardData& bd, Urho3D::Node* node, const rapidjson::Value& pin); |
| 94 | + void UpdatePy(Urho3D::StringHash, Urho3D::VariantMap& event_data); |
| 95 | + |
| 96 | +}; |
| 97 | + |
| 98 | +#endif // SMARTCAR_EMUL_PERFECTGYROSCOPEI2CSENSOR_HXX |
0 commit comments