Example #1
0
 public Motor(string motorType, LegoPort port, Dictionary <string, string> attributes = null)
 {
     if (attributes == null)
     {
         attributes = new Dictionary <string, string>();
     }
     if (attributes.ContainsKey("deviceroot_startswith"))
     {
         Console.WriteLine("WARNING: The 'deviceroot_startswith' attribute is automatically included when using the Motor class and it should not be defined manually. Overriding...");
         attributes["deviceroot_startswith"] = "motor";
     }
     else
     {
         attributes.Add("deviceroot_startswith", "motor");
     }
     if (attributes.ContainsKey("driver_name"))
     {
         Console.WriteLine("WARNING: The 'driver_name' attribute is automatically included when using the Motor class and it should not be defined manually. If you wish to use a custom motor type, please modify the 'motorType' argument in the Motor class constructor accordingly. Overriding...");
         attributes["driver_name"] = motorType;
     }
     else
     {
         attributes["driver_name"] = motorType;
     }
     InitDevice("tacho-motor", port, attributes);
     Command      = "stop";
     _motorType   = motorType;
     _commands    = GetAttributeString("commands").Split(' ');
     _countPerRot = GetAttributeInt("count_per_rot");
     _maxSpeed    = GetAttributeInt("max_speed");
 }
        public void InitDevice(string classDir, LegoPort port, Dictionary <string, string> attributes)
        {
            bool device_found = false;

            foreach (string dir in IOError_Workaround.GetDirectories(Path.Combine(ROOT, "class", classDir)))
            {
                path = dir;
                if (Utils.String2LegoPort(GetAttributeString("address")) != port)
                {
                    continue;
                }
                bool attributes_match = true;
                foreach (KeyValuePair <string, string> attribute in attributes)
                {
                    if (attribute.Key == "deviceroot_startswith")
                    {
                        if (!Path.GetFileName(dir).StartsWith(attributes["deviceroot_startswith"]))
                        {
                            attributes_match = false;
                            break;
                        }
                    }
                    else if (GetAttributeString(attribute.Key) != attribute.Value)
                    {
                        attributes_match = false;
                        break;
                    }
                }

                if (!attributes_match)
                {
                    continue;
                }

                // Found the desired device
                Class        = classDir;
                Address      = port;
                DriverName   = GetAttributeString("driver_name");
                device_found = true;
                break;
            }

            if (!device_found)
            {
                path = null;
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("No devices matching the attributes below:");
                sb.AppendLine($"Class: {classDir}");
                sb.AppendLine($"Port: {port.ToString()} ({Utils.LegoPort2String(port)}");
                sb.AppendLine("Attributes:");
                foreach (KeyValuePair <string, string> kvp in attributes)
                {
                    sb.AppendLine($"   '{kvp.Key}'='{kvp.Value}'");
                }

                throw new Exception(sb.ToString());
            }
        }
Example #3
0
 public LargeMotor(LegoPort port, Dictionary <string, string> attributes = null)
     : base("lego-ev3-l-motor", port, attributes)
 {
 }
 public Device(string classDir, LegoPort port, Dictionary <string, string> attributes)
 {
     InitDevice(classDir, port, attributes);
 }
 public MediumMotor(LegoPort port, Dictionary <string, string> attributes = null)
     : base("lego-ev3-m-motor", port, attributes)
 {
 }
 public static string LegoPort2String(LegoPort port)
 {
     return(LegoPortMap[port]);
 }