Ejemplo n.º 1
0
        public void tc(PigpiodIf pigpiodIf, CancellationToken ct)
        {
            int h, x, b, e;

            byte[] buf = new byte[128];

            Console.WriteLine("\r\nSPI tests.");

            /* this test requires a MCP3202 on SPI1 channel 2 */

            h = pigpiodIf.spi_open(2, 50000, 256 + 0);
            CHECK(12, 1, h, 0, 0, "spi open", ct);


            for (x = 0; x < 5; x++)
            {
                buf = new byte[] { 0x01, 0x80, 0x00 };
                b   = pigpiodIf.spi_xfer((UInt32)h, buf, buf);
                CHECK(12, 2, b, 3, 0, "spi xfer", ct);
                if (b == 3)
                {
                    pigpiodIf.time_sleep(1.0);
                    Console.WriteLine("{0} ", ((buf[1] & 0x0F) * 256) | buf[2]);
                }
            }

            e = pigpiodIf.spi_close((UInt32)h);
            CHECK(12, 99, e, 0, 0, "spi close", ct);
        }
Ejemplo n.º 2
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));
            }
        }
Ejemplo n.º 3
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;
            }
        }