From a85594bb3e26439a02655611c81c304e8f904d9d Mon Sep 17 00:00:00 2001 From: Curle Date: Fri, 16 Jul 2021 05:25:10 +0100 Subject: [PATCH] Implement devices header --- src/drivers/devices/devices.cpp | 66 +++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 src/drivers/devices/devices.cpp diff --git a/src/drivers/devices/devices.cpp b/src/drivers/devices/devices.cpp new file mode 100644 index 0000000..956e4b8 --- /dev/null +++ b/src/drivers/devices/devices.cpp @@ -0,0 +1,66 @@ +#include +#include + +/************************ + *** Team Kitty, 2021 *** + *** Chroma *** + ***********************/ + +// TODO: increase this +#define MAX_DEVICES 8 +// TODO: increase this +#define MAX_STORAGE_DEVICES 4 + +// Internal storage. TODO: Turn this into some form of search tree structure. +Device::GenericDevice* DevicesArray[MAX_DEVICES]; +// Internal storage. Index into the above array. +size_t CurrentDevice = 0; + +// Internal storage. TODO: Turn this into some form of search tree structure. +Device::GenericStorage* StorageDevicesArray[MAX_STORAGE_DEVICES]; +// Internal storage. Index into the above array. +size_t CurrentStorageDevice = 0; + +// Internal storage. TODO: Make this not a pain to maintain +const char* DeviceNames[] = { "Storage", "Keyboard", "Networking" }; + + +// Add a device pointer to the managed list. +void Device::RegisterDevice(Device::GenericDevice* Device) { + DevicesArray[CurrentDevice] = Device; + Device->DeviceID = CurrentDevice; + CurrentDevice++; + SerialPrintf("[DEVICE] Registered device %d called %s of type %s\r\n", CurrentDevice - 1, Device->GetName(), DeviceNames[Device->GetType()]); +} + +// Retrieve a device pointer from the managed list. +Device::GenericDevice* Device::GetDevice(size_t ID) { + return DevicesArray[ID]; +} + +void Device::RegisterStorageDevice(Device::GenericStorage* Device) { + RegisterDevice(Device); + StorageDevicesArray[CurrentStorageDevice] = Device; + CurrentStorageDevice++; +} + +Device::GenericStorage* Device::GetStorageDevice(size_t ID) { + return StorageDevicesArray[ID]; +} + +// Get the count of registered devices. +size_t Device::GetTotalDevices() { return CurrentDevice; } + +template +// Get the first registered instance of a specific type of device +T* Device::FindDevice() { + for(size_t i = 0; i < CurrentDevice; i++) + if(DevicesArray[i]->GetType() == T::GetRootType()) + return static_cast(DevicesArray[i]); + + SerialPrintf("[DEVICE] Warning: Unable to find a %s device.\r\n", DeviceNames[T::GetRootType()]); + return static_cast(nullptr); +} + +// Get the first registered instance of a Storage device +template Device::GenericStorage* Device::FindDevice(); \ No newline at end of file