Example #1
0
        private static void OnTelemetryUpdated(object sender, SdkWrapper.TelemetryUpdatedEventArgs e)
        {
            var stopwatch = Stopwatch.StartNew();

            const int IndicatorHalfWidth = 4;
            var       shftInd            = e.TelemetryInfo.ShiftIndicatorPct.Value;

            shftInd = shftInd.Remap(0, 1, 0, IndicatorHalfWidth);

            Console.SetCursorPosition(0, 0);

            Console.Write("Indicator:\t\t");

            byte indicator = 0;

            for (var i = 0; i < IndicatorHalfWidth * 2; i++)
            {
                indicator = (byte)(indicator << 1);
                if (i < shftInd.RoundToInt() || i >= (IndicatorHalfWidth * 2) - shftInd.RoundToInt())
                {
                    indicator |= 1;
                }
            }

            Console.WriteLine(Convert.ToString(indicator, 2).PadLeft(8, '0'));

            driver.Send(new DigitalWriteRequest(10, DigitalValue.Low));
            driver.Send(new ShiftOutRequest(12, 11, BitOrder.MSBFIRST, indicator));
            driver.Send(new DigitalWriteRequest(10, DigitalValue.High));

            stopwatch.Stop();

            Console.WriteLine($"Bitshift duration:\t{stopwatch.ElapsedMilliseconds} ms");
        }
Example #2
0
 private static void Main(string[] args)
 {
     using (var driver = new ArduinoDriver(AttachedArduino, true)) {
         for (var i = 0; i < Melody.Length; i++)
         {
             var noteDuration = 1000 / tempo[i];
             driver.Send(new ToneRequest(DigitalPinBuzzer, (ushort)Melody[i], (uint)noteDuration));
             Thread.Sleep((int)(noteDuration * 1.40));
             driver.Send(new NoToneRequest(DigitalPinBuzzer));
         }
     }
 }
Example #3
0
 private static void Main(string[] args)
 {
     using (var driver = new ArduinoDriver(AttachedArduino, AttachedPort))
     {
         driver.Send(new PinModeRequest(DigitalPinBlink, PinMode.Output));
         while (true)
         {
             driver.Send(new DigitalWriteRequest(DigitalPinBlink, DigitalValue.High));
             Thread.Sleep(BlinkDelayMS);
             driver.Send(new DigitalWriteRequest(DigitalPinBlink, DigitalValue.Low));
             Thread.Sleep(BlinkDelayMS);
         }
     }
 }
Example #4
0
        private static void Main(string[] args)
        {
            var iracing = new SdkWrapper {
                TelemetryUpdateFrequency = 15
            };

            iracing.TelemetryUpdated += OnTelemetryUpdated;

            driver = new ArduinoDriver(ArduinoModel.UnoR3, "COM4", true);

            Console.CursorVisible = false;

            const byte DataPin  = 12;
            const byte ClockPin = 11;
            const byte LatchPin = 10;

            driver.Send(new PinModeRequest(DataPin, PinMode.Output));
            driver.Send(new PinModeRequest(LatchPin, PinMode.Output));
            driver.Send(new PinModeRequest(ClockPin, PinMode.Output));


            driver.Send(new DigitalWriteRequest(LatchPin, DigitalValue.Low));
            var response = driver.Send(new ShiftOutRequest(DataPin, ClockPin, BitOrder.LSBFIRST, 115));

            driver.Send(new DigitalWriteRequest(LatchPin, DigitalValue.High));

            Console.WriteLine($"{response.Value} = {Convert.ToString(response.Value, 2).PadLeft(8, '0')}");

            iracing.Start();
        }
Example #5
0
        internal static actionResult DoDriverAction(string[] actionNameArray, string[] actionParameters)
        {
            var ar = new actionResult();

            if (!Plugin.SelectedPort.IsAlive())
            {
                ar.setError("Current port isn't open.");
                return(ar);
            }
            if (actionParameters.Length < 1)
            {
                ar.setError("Arduino model not specified.");
                return(ar);
            }
            ArduinoModel arduinoModel;

            if (!Enum.TryParse(actionParameters[0], true, out arduinoModel))
            {
                ar.setError("Invalid Arduino model.");
                return(ar);
            }

            switch (actionNameArray[2].ToUpper())
            {
            case "UPLOAD": {     // [Arduino Model], [Path to sketch]
                // TODO connect avrdude.exe to compile & upload sketches without Arduino IDE.
                if (actionParameters.Length < 2)
                {
                    ar.setError("2 parameters expected.");
                    return(ar);
                }
                // Uploading sketch from another thread,
                // Result is returned by Serial.Upload.{Status} event.
                Task.Run(() => UploadSketch(arduinoModel, actionParameters[1], Plugin.SelectedPort.PortName));
                ar.setInfo("Uploading...");
                return(ar);
            }

#if DEBUG
            // BETA ACTIONS, BUT MOST OF THEM ALREADY TESTED ON MY PC W/ ARDUINO UNO.
            case "MARIO": {     // [Arduino model], [Buzzer pin]
                if (actionParameters.Length < 2)
                {
                    ar.setError("2 parameters expected.");
                    return(ar);
                }
                byte pin;
                if (!byte.TryParse(actionParameters[1], out pin))
                {
                    ar.setError("Invalid buzzer pin.");
                    return(ar);
                }

                Task.Run(
                    delegate {
                        using (var driver = new ArduinoDriver(arduinoModel, Plugin.SelectedPort.PortName, true)) {
                            for (var i = 0; i < melody.Length; i++)
                            {
                                int noteDuration = 1000 / tempo[i];
                                driver.Send(new ToneRequest(pin, (ushort)melody[i], (uint)noteDuration));
                                Thread.Sleep((int)(noteDuration * 1.40));
                                driver.Send(new NoToneRequest(pin));
                            }
                        }
                        Plugin.HostInstance.triggerEvent(
                            "Serial.Micro.Mario.Success",
                            new List <string>()
                            );
                    }
                    );
                ar.setInfo("Uploading...");
                break;
            }

            case "TONE": {     // [Arduino model], [Buzzer pin], [Tone], [Duration in ms]
                if (actionParameters.Length < 4)
                {
                    ar.setError("4 parameters expected.");
                    return(ar);
                }
                byte pin;
                if (!byte.TryParse(actionParameters[1], out pin))
                {
                    ar.setError("Invalid pin.");
                    return(ar);
                }
                ushort tone;
                if (!ushort.TryParse(actionParameters[2], out tone))
                {
                    ar.setError("Invalid tone.");
                    return(ar);
                }
                uint duration;
                if (!uint.TryParse(actionParameters[2], out duration))
                {
                    ar.setError("Invalid duration.");
                    return(ar);
                }

                Task.Run(
                    delegate {
                        using (var driver = new ArduinoDriver(arduinoModel, Plugin.SelectedPort.PortName, true)) {
                            driver.Send(new ToneRequest(pin, tone, duration));
                            Thread.Sleep((int)(duration * 1.40));
                            driver.Send(new NoToneRequest(pin));
                        }
                    }
                    );
                ar.setInfo("OK.");
                break;
            }

            case "ANALOGREAD": {     // [Arduino model], [Pin to read]
                if (actionParameters.Length < 2)
                {
                    ar.setError("2 parameters expected.");
                    return(ar);
                }
                byte pin;
                if (!byte.TryParse(actionParameters[1], out pin))
                {
                    ar.setError("Invalid pin.");
                    return(ar);
                }
                using (var driver = new ArduinoDriver(arduinoModel, Plugin.SelectedPort.PortName, true)) {
                    AnalogReadResponse response = driver.Send(new AnalogReadRequest(pin));
                    ar.setSuccess(response.PinValue.ToString());
                }
                break;
            }

            case "DIGITALREAD": {     // [Arduino model], [Pin to read]
                if (actionParameters.Length < 2)
                {
                    ar.setError("2 parameters expected.");
                    return(ar);
                }
                byte pin;
                if (!byte.TryParse(actionParameters[1], out pin))
                {
                    ar.setError("Invalid pin.");
                    return(ar);
                }
                using (var driver = new ArduinoDriver(arduinoModel, Plugin.SelectedPort.PortName, true)) {
                    DigitalReadResponse response = driver.Send(new DigitalReadRequest(pin));
                    ar.setSuccess(response.PinValue.ToString("G").ToUpper());
                }
                break;
            }

            // all other actions need at least 3 parameters.
            default: {
                if (actionParameters.Length < 3)
                {
                    ar.setError("3 parameters expected.");
                    return(ar);
                }
                byte pin;
                if (!byte.TryParse(actionParameters[1], out pin))
                {
                    ar.setError("Invalid pin.");
                    return(ar);
                }
                switch (actionNameArray[2].ToUpper())
                {
                case "ANALOGWRITE": {         // [Arduino model], [Pin to write], [Value to write]
                    byte val;
                    if (!byte.TryParse(actionParameters[2], out val))
                    {
                        ar.setError("Invalid pin value.");
                        return(ar);
                    }
                    using (var driver = new ArduinoDriver(arduinoModel, Plugin.SelectedPort.PortName, true)) {
                        AnalogWriteResponse response = driver.Send(new AnalogWriteRequest(pin, val));
                        ar.setInfo($"OK. pin {response.PinWritten}, value: {val}");
                    }
                    break;
                }

                case "DIGITALWRITE": {         // [Arduino model], [Pin to write], [Value to write]
                    byte val;
                    if (!byte.TryParse(actionParameters[2], out val))
                    {
                        ar.setError("Invalid pin value.");
                        return(ar);
                    }
                    using (var driver = new ArduinoDriver(arduinoModel, Plugin.SelectedPort.PortName, true)) {
                        DigitalWriteReponse response = driver.Send(new DigitalWriteRequest(pin, (DigitalValue)val));
                        ar.setInfo($"OK. pin {response.PinWritten}, value: {val}");
                    }
                    break;
                }

                case "PINMODE": {         // [Arduino model], [Pin to set], [Pin mode]
                    PinMode pinMode;
                    switch (actionParameters[2].ToUpper())
                    {
                    case "IN":
                        pinMode = PinMode.Input;
                        break;

                    case "INPULL":
                        pinMode = PinMode.InputPullup;
                        break;

                    case "OUT":
                        pinMode = PinMode.Output;
                        break;

                    default:
                        ar.setError("Invalid pin mode.");
                        return(ar);
                    }
                    using (var driver = new ArduinoDriver(arduinoModel, Plugin.SelectedPort.PortName, true)) {
                        PinModeResponse response = driver.Send(new PinModeRequest(pin, pinMode));
                        ar.setInfo($"OK. pin {response.Pin}: {response.Mode:G}.");
                    }
                    break;
                }
                }
                break;
            }
#endif
            }
            return(ar);
        }