jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/
|
| 2 | /* Copyright (c) FIRST 2008. All Rights Reserved. */
|
| 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */
|
| 4 | /* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
|
| 5 | /*----------------------------------------------------------------------------*/
|
| 6 |
|
| 7 | #include "Module.h"
|
| 8 | #include "AnalogModule.h"
|
| 9 | #include "DigitalModule.h"
|
| 10 | //#include "SolenoidModule.h"
|
| 11 |
|
| 12 | Module* Module::m_modules[kMaxModules] = {NULL};
|
| 13 |
|
| 14 | /**
|
| 15 | * Constructor.
|
| 16 | *
|
| 17 | * @param type The type of module represented.
|
| 18 | * @param number The module index within the module type.
|
| 19 | */
|
| 20 | Module::Module(nLoadOut::tModuleType type, UINT8 number)
|
| 21 | : m_moduleType (type)
|
| 22 | , m_moduleNumber (number)
|
| 23 | {
|
| 24 | m_modules[ToIndex(type, number)] = this;
|
| 25 | }
|
| 26 |
|
| 27 | /**
|
| 28 | * Destructor.
|
| 29 | */
|
| 30 | Module::~Module()
|
| 31 | {
|
| 32 | }
|
| 33 |
|
| 34 | /**
|
| 35 | * Static module singleton factory.
|
| 36 | *
|
| 37 | * @param type The type of module represented.
|
| 38 | * @param number The module index within the module type.
|
| 39 | */
|
| 40 | Module* Module::GetModule(nLoadOut::tModuleType type, UINT8 number)
|
| 41 | {
|
| 42 | if (m_modules[ToIndex(type, number)] == NULL)
|
| 43 | {
|
| 44 | switch(type)
|
| 45 | {
|
| 46 | case nLoadOut::kModuleType_Analog:
|
| 47 | new AnalogModule(number);
|
| 48 | break;
|
| 49 | case nLoadOut::kModuleType_Digital:
|
| 50 | new DigitalModule(number);
|
| 51 | break;
|
| 52 | /*
|
| 53 | case nLoadOut::kModuleType_Solenoid:
|
| 54 | new SolenoidModule(number);
|
| 55 | break;
|
| 56 | */
|
| 57 | default:
|
| 58 | return NULL;
|
| 59 | }
|
| 60 | }
|
| 61 | return m_modules[ToIndex(type, number)];
|
| 62 | }
|
| 63 |
|
| 64 | /**
|
| 65 | * Create an index into the m_modules array based on type and number
|
| 66 | *
|
| 67 | * @param type The type of module represented.
|
| 68 | * @param number The module index within the module type.
|
| 69 | * @return The index into m_modules.
|
| 70 | */
|
| 71 | UINT8 Module::ToIndex(nLoadOut::tModuleType type, UINT8 number)
|
| 72 | {
|
| 73 | if (number == 0 || number > kMaxModuleNumber) return 0;
|
| 74 | if (type < nLoadOut::kModuleType_Analog || type > nLoadOut::kModuleType_Solenoid) return 0;
|
| 75 | return (type * kMaxModuleNumber) + (number - 1);
|
| 76 | }
|