1 // 2 // Copyright (c) 2010-2024 Antmicro 3 // Copyright (c) 2011-2015 Realtime Embedded 4 // 5 // This file is licensed under the MIT License. 6 // Full license text is available in 'licenses/MIT.txt'. 7 // 8 using System; 9 using System.Collections.Generic; 10 11 namespace Antmicro.Renode.Core 12 { 13 public class IGPIORedirector : IReadOnlyDictionary<int, IGPIO> 14 { IGPIORedirector(int size, Action<int, IGPIOReceiver, int> connector, Action<int> disconnector = null)15 public IGPIORedirector(int size, Action<int, IGPIOReceiver, int> connector, Action<int> disconnector = null) 16 { 17 Count = size; 18 this.connector = connector; 19 this.disconnector = disconnector; 20 } 21 ContainsKey(int key)22 public bool ContainsKey(int key) 23 { 24 return key >= 0 && key < Count; 25 } 26 TryGetValue(int key, out IGPIO value)27 public bool TryGetValue(int key, out IGPIO value) 28 { 29 if(key >= Count) 30 { 31 value = null; 32 return false; 33 } 34 value = new GPIOWrapper(key, connector, disconnector); 35 return true; 36 } 37 38 public IGPIO this[int index] 39 { 40 get 41 { 42 IGPIO value; 43 if(!TryGetValue(index, out value)) 44 { 45 throw new ArgumentOutOfRangeException(); 46 } 47 return value; 48 } 49 } 50 51 public IEnumerable<int> Keys 52 { 53 get 54 { 55 for(int i = 0; i < Count; i++) 56 { 57 yield return i; 58 } 59 } 60 } 61 62 public IEnumerable<IGPIO> Values 63 { 64 get 65 { 66 foreach(var key in Keys) 67 { 68 yield return new GPIOWrapper(key, connector, disconnector); 69 } 70 } 71 } 72 GetEnumerator()73 public IEnumerator<KeyValuePair<int, IGPIO>> GetEnumerator() 74 { 75 for(int i = 0; i < Count; i++) 76 { 77 yield return new KeyValuePair<int, IGPIO>(i, new GPIOWrapper(i, connector, disconnector)); 78 } 79 } 80 System.Collections.IEnumerable.GetEnumerator()81 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() 82 { 83 return GetEnumerator(); 84 } 85 86 public int Count { get; private set; } 87 88 private readonly Action<int, IGPIOReceiver, int> connector; 89 private readonly Action<int> disconnector; 90 91 private class GPIOWrapper : IGPIO 92 { GPIOWrapper(int id, Action<int, IGPIOReceiver, int> connector, Action<int> disconnector)93 public GPIOWrapper(int id, Action<int, IGPIOReceiver, int> connector, Action<int> disconnector) 94 { 95 this.id = id; 96 this.connector = connector; 97 this.disconnector = disconnector; 98 targets = new List<GPIOEndpoint>(); 99 } 100 Set(bool value)101 public void Set(bool value) 102 { 103 throw new NotImplementedException(); 104 } 105 Toggle()106 public void Toggle() 107 { 108 throw new NotImplementedException(); 109 } 110 Connect(IGPIOReceiver destination, int destinationNumber)111 public void Connect(IGPIOReceiver destination, int destinationNumber) 112 { 113 connector(id, destination, destinationNumber); 114 targets.Add(new GPIOEndpoint(destination, destinationNumber)); 115 } 116 Disconnect()117 public void Disconnect() 118 { 119 if (disconnector != null) 120 { 121 disconnector(id); 122 return; 123 } 124 125 throw new NotImplementedException(); 126 } 127 Disconnect(GPIOEndpoint enpoint)128 public void Disconnect(GPIOEndpoint enpoint) 129 { 130 throw new NotImplementedException(); 131 } 132 133 public bool IsSet 134 { 135 get 136 { 137 throw new NotImplementedException(); 138 } 139 } 140 141 public bool IsConnected 142 { 143 get 144 { 145 throw new NotImplementedException(); 146 } 147 } 148 149 public IList<GPIOEndpoint> Endpoints 150 { 151 get 152 { 153 return targets; 154 } 155 } 156 157 private readonly int id; 158 private readonly Action<int, IGPIOReceiver, int> connector; 159 private readonly Action<int> disconnector; 160 private readonly IList<GPIOEndpoint> targets; 161 } 162 } 163 } 164 165