示例#1
0
        public SpiStream(PigpiodIf pigpiodIf, UInt32 channel, UInt32 speed, UInt32 flags)
        {
            this.pigpiodIf = pigpiodIf;

            for (int i = 0; i < 3; i++)
            {
                handle = pigpiodIf.spi_open(channel, speed, flags);
                //Console.WriteLine("spi_open: {0}", handle);
                if (handle >= 0)
                {
                    break;
                }
                System.Threading.Thread.Sleep(100);
            }
            if (handle < 0)
            {
                throw new PigpiodIfException(handle, "PigpiodIf: " + pigpiodIf.pigpio_error(handle));
            }
        }
示例#2
0
        private async void buttonRollStart_Click(object sender, EventArgs e)
        {
            buttonRollStart.Enabled = false;
            buttonFastStart.Enabled = false;
            buttonRollStop.Enabled  = true;

            int h = -1;

            try
            {
                rollCts = new CancellationTokenSource();
                var ct = rollCts.Token;
                await Task.Run(() =>
                {
                    h = pigpiodIf.spi_open(2, 1000000, 256 + 0);
                    if (h < 0)
                    {
                        throw new PigpiodIfException(h, "PigpiodIf: " + pigpiodIf.pigpio_error(h));
                    }

                    for (int ch = 0; ch < NUM_ROLL_CHANNELS; ch++)
                    {
                        rollSeries[ch].Points.Clear();
                    }
                    DateTime start = DateTime.Now;
                    while (!ct.IsCancellationRequested)
                    {
                        double[] volts         = new double[NUM_ROLL_CHANNELS];
                        DataPoint[] dataPoints = new DataPoint[NUM_ROLL_CHANNELS];
                        for (int ch = 0; ch < NUM_ROLL_CHANNELS; ch++)
                        {
                            byte[] buf = new byte[] { (byte)(0x06 + (ch >> 2)), (byte)(ch << 6), 0x00 };
                            int b      = pigpiodIf.spi_xfer((UInt32)h, buf, buf);
                            if (b == 3)
                            {
                                TimeSpan ts    = DateTime.Now - start;
                                double volt    = 3.3 * (((buf[1] & 0x0f) * 256) + buf[2]) / 4096.0;
                                volts[ch]      = volt;
                                dataPoints[ch] = new DataPoint(ts.TotalSeconds, volt);
                            }
                        }

                        string s = volts[0].ToString("0.0");
                        for (int ch = 1; ch < NUM_ROLL_CHANNELS; ch++)
                        {
                            s += string.Format(", {0:0.0}", volts[ch]);
                        }
                        Console.WriteLine("{0}", s);

                        Invoke(new Action(() =>
                        {
                            for (int ch = 0; ch < NUM_ROLL_CHANNELS; ch++)
                            {
                                if (rollSeries[ch].Points.Count >= NUM_ROLL_SAMPLES)
                                {
                                    rollSeries[ch].Points.RemoveAt(0);
                                }
                                rollSeries[ch].Points.Add(dataPoints[ch]);
                            }
                            rollPlotModel.InvalidatePlot(true);
                            if (plotView1.Model != rollPlotModel)
                            {
                                plotView1.Model = rollPlotModel;
                            }
                            else
                            {
                                plotView1.Invalidate();
                            }
                        }));
                    }
                }, ct);
            }
            catch (OperationCanceledException)
            {
                // nothing to do
            }
            catch (PigpiodIfException ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if (h >= 0)
                {
                    pigpiodIf.spi_close((UInt32)h);
                }

                rollCts = null;

                buttonRollStart.Enabled = true;
                buttonFastStart.Enabled = true;
                buttonRollStop.Enabled  = false;
            }
        }