#pragma once #include #include #include #include #include namespace tt::hal { /** * Base class for HAL-related devices. */ class Device { public: enum class Type { I2c, Display, Touch, SdCard, Keyboard, Power }; typedef uint32_t Id; private: Id id; public: Device(); virtual ~Device() = default; Id getId() const { return id; } /** The type of device. */ virtual Type getType() const = 0; /** The part number or hardware name e.g. TdeckTouch, TdeckDisplay, BQ24295, etc. */ virtual std::string getName() const = 0; /** A short description of what this device does. * e.g. "USB charging controller with I2C interface." */ virtual std::string getDescription() const = 0; }; /** * Adds a device to the registry. * @warning This will leak memory if you want to destroy a device and don't call deregisterDevice()! */ void registerDevice(const std::shared_ptr& device); /** Remove a device from the registry. */ void deregisterDevice(const std::shared_ptr& device); /** Find a single device with a custom filter */ std::shared_ptr _Nullable findDevice(const std::function&)>& filterFunction); /** Find devices with a custom filter */ std::vector> findDevices(const std::function&)>& filterFunction); /** Find a device in the registry by its name. */ std::shared_ptr _Nullable findDevice(std::string name); /** Find a device in the registry by its identifier. */ std::shared_ptr _Nullable findDevice(Device::Id id); /** Find 0, 1 or more devices in the registry by type. */ std::vector> findDevices(Device::Type type); /** Get a copy of the entire device registry in its current state. */ std::vector> getDevices(); /** Find devices of a certain type and cast them to the specified class */ template std::vector> findDevices(Device::Type type) { auto devices = findDevices(type); if (devices.empty()) { return {}; } else { std::vector> result; result.reserve(devices.size()); for (auto& device : devices) { auto target_device = std::static_pointer_cast(device); assert(target_device != nullptr); result.push_back(target_device); } return std::move(result); } } /** Find the first device of the specified type and cast it to the specified class */ template std::shared_ptr findFirstDevice(Device::Type type) { auto devices = findDevices(type); if (devices.empty()) { return {}; } else { auto& first = devices[0]; return std::static_pointer_cast(first); } } }