// // Public functions // // Binds an LED object to a controller. public void AssociateLed(int startingSlot, Led assoicateLed) { if (assoicateLed == null) { throw new ArgumentNullException("Led can't be null!"); } if(startingSlot < 0) { throw new ArgumentOutOfRangeException("The LED slot can't be < 0!"); } // Get how many slots are needed int slotsNeeded = assoicateLed.GetBase().SlotsRequired; lock (m_ledMap) { // Make sure there is enough room in the position requested to // hold the new LED for (int i = 0; i < slotsNeeded; i++) { // Check if something exists at that key if (m_ledMap.ContainsKey(i + startingSlot)) { // If it does, see if it is still valid Led tempLed; if (m_ledMap[i + startingSlot].TryGetTarget(out tempLed)) { // The LED actually exists, we can't add the LED here throw new ArgumentException("The requested position is already occupied by a LED. Remember RBG LEDs take 3 slots!"); } else { // If we get here we have an entry for a LED that is gone. Remove it m_ledMap.Remove(startingSlot + i); } } } } // Inform the listener that slots need to be added. The listener should throw if // this fails. m_controllerExtener.NotifiySlotsAdded(startingSlot, slotsNeeded); // Now actually add the LEDs to the map. lock(m_ledMap) { for (int i = 0; i < slotsNeeded; i++) { m_ledMap.Add(startingSlot + i, new WeakReference<Led>(assoicateLed)); } } // Inform the LED of the association assoicateLed.GetBase().SetNotificationCallback(startingSlot, this); }
// Breaks the association with a LED and the controller public void DissociateLed(Led dissociateLed) { if(dissociateLed == null) { throw new ArgumentNullException("The LED can't be null!"); } int startingPosition = dissociateLed.GetBase().FirstSlot; int slotsFilled = dissociateLed.GetBase().SlotsRequired; // Remove the LED from the map lock (m_ledMap) { for (int i = 0; i < slotsFilled; i++) { m_ledMap.Remove(startingPosition + i); } } // Remove the association with the LED dissociateLed.GetBase().RemoveNotificationCallback(); // Tell the listener last, if they fail we still want to do our logic m_controllerExtener.NotifiySlotsRevmoed(startingPosition, slotsFilled); }