Пример #1
0
        public byte[] SpiXfer(int handle, byte[] txBuff)
        {
            byte[]   rxBuff = new byte[txBuff.Length];
            GCHandle tx     = GCHandle.Alloc(txBuff, GCHandleType.Pinned);

            try
            {
                GCHandle rx = GCHandle.Alloc(rxBuff, GCHandleType.Pinned);
                try
                {
                    short ret = PiGpioNativeMethods.spiXfer((ushort)handle, tx.AddrOfPinnedObject(), tx.AddrOfPinnedObject(), (ushort)txBuff.Length);
                    if (ret < 0)
                    {
                        throw new PiGPIOException(ret);
                    }
                }
                finally
                {
                    rx.Free();
                }
            }
            finally
            {
                tx.Free();
            }
            return(rxBuff);
        }
Пример #2
0
        byte[] IPiGPIO.BSpiXfer(int gpioCS, byte[] txBuffer)
        {
            byte[]   rxBuffer = new byte[txBuffer.Length];
            GCHandle tx       = GCHandle.Alloc(tx, GCHandleType.Pinned);

            try
            {
                GCHandle rx = GCHandle.Alloc(rxBuffer, GCHandleType.Pinned);
                try
                {
                    short ret = PiGpioNativeMethods.bspiXfer((ushort)gpioCS, tx.AddrOfPinnedObject(), rx.AddrOfPinnedObject(), (ushort)txBuffer.Length);
                    if (ret < 0)
                    {
                        throw new PiGPIOException(ret);
                    }
                }
                finally
                {
                    rx.Free();
                }
            }
            finally
            {
                tx.Free();
            }
            return(rxBuffer);
        }
Пример #3
0
        public byte[] I2CReadBytes(int handle, int num)
        {
            byte[]   data = new byte[num];
            GCHandle h    = GCHandle.Alloc(data, GCHandleType.Pinned);

            try
            {
                short ret = PiGpioNativeMethods.i2cReadDevice((ushort)handle, h.AddrOfPinnedObject(), (ushort)num);
                if (ret < 0)
                {
                    throw new PiGPIOException(ret);
                }
                if (ret < num)
                {
                    byte[] real = new byte[ret];
                    Array.Copy(data, real, ret);
                    return(real);
                }
                else
                {
                    return(data);
                }
            }
            finally
            {
                h.Free();
            }
        }
Пример #4
0
        public CallbackInfo AddCallback(int gpio, Edge edge, GpioCallback callback)
        {
            List <CallbackInfo> callbacks;

            lock (this.m_callbacks)
            {
                if (this.m_callbacks.ContainsKey(gpio))
                {
                    callbacks = this.m_callbacks[gpio];
                }
                else
                {
                    short ret = PiGpioNativeMethods.SetAlertFunc((ushort)gpio, this.GpioAlertCallback);
                    if (ret < 0)
                    {
                        throw new PiGPIOException(ret);
                    }
                    callbacks = new List <CallbackInfo>();
                    this.m_callbacks.Add(gpio, callbacks);
                }
            }

            CallbackInfo cbi = new CallbackInfo((uint)gpio, 0, edge, callback);

            lock (callbacks)
            {
                callbacks.Add(cbi);
            }
            return(cbi);
        }
Пример #5
0
        public void Write(int gpio, bool value)
        {
            short ret = PiGpioNativeMethods.Write((ushort)gpio, (ushort)(value ? 1 : 0));

            if (ret < 0)
            {
                throw new PiGPIOException(ret);
            }
        }
Пример #6
0
        /// <inheritDoc />
        public void SetPullUpDown(int gpio, PullUpDown pud)
        {
            short ret = PiGpioNativeMethods.SetPullUpDown((ushort)gpio, (ushort)pud);

            if (ret < 0)
            {
                throw new PiGPIOException(ret);
            }
        }
Пример #7
0
        /// <inheritDoc />
        public void GlitchFilter(int gpio, int steady)
        {
            short ret = PiGpioNativeMethods.GlitchFilter((ushort)gpio, (ushort)steady);

            if (ret < 0)
            {
                throw new PiGPIOException(ret);
            }
        }
Пример #8
0
        public void BSpiOpen(int gpioCS, int gpioMiso, int gpioMosi, int gpioClk, int bauds, int flags)
        {
            short ret = PiGpioNativeMethods.bspiOpen((ushort)gpioCS, (ushort)gpioMiso, (ushort)gpioMosi, (ushort)gpioClk, (ushort)bauds, unchecked ((ushort)flags));

            if (ret < 0)
            {
                throw new PiGPIOException(ret);
            }
        }
Пример #9
0
        public void ClearBits_32_53(int bits)
        {
            short ret = PiGpioNativeMethods.clearBits_32_53(unchecked ((uint)bits));

            if (ret < 0)
            {
                throw new PiGPIOException(ret);
            }
        }
Пример #10
0
        /// <inheritDoc />
        public void NoiseFilter(int gpio, int steady, int active)
        {
            short ret = PiGpioNativeMethods.NoiseFilter((ushort)gpio, (ushort)steady, (ushort)active);

            if (ret < 0)
            {
                throw new PiGPIOException(ret);
            }
        }
Пример #11
0
        public void BSpiClose(int gpioCS)
        {
            short ret = PiGpioNativeMethods.bspiClose((ushort)gpioCS);

            if (ret < 0)
            {
                throw new PiGPIOException(ret);
            }
        }
Пример #12
0
        public void SetBits_0_31(int bits)
        {
            short ret = PiGpioNativeMethods.setBits_0_31(unchecked ((uint)bits));

            if (ret < 0)
            {
                throw new PiGPIOException(ret);
            }
        }
Пример #13
0
        /// <inheritDoc />
        public void SetMode(int gpio, Mode mode)
        {
            short ret = PiGpioNativeMethods.SetMode((ushort)gpio, (ushort)mode);

            if (ret < 0)
            {
                throw new PiGPIOException(ret);
            }
        }
Пример #14
0
        public void SpiClose(int handle)
        {
            short ret = PiGpioNativeMethods.spiClose((ushort)handle);

            if (ret < 0)
            {
                throw new PiGPIOException(ret);
            }
        }
Пример #15
0
        /// <inheritDoc />
        public Mode GetMode(int gpio)
        {
            short value = PiGpioNativeMethods.GetMode((ushort)gpio);

            if (value < 0)
            {
                throw new PiGPIOException(value);
            }
            return((Mode)value);
        }
Пример #16
0
        public bool Read(int gpio)
        {
            short ret = PiGpioNativeMethods.Read((ushort)gpio);

            if (ret < 0)
            {
                throw new PiGPIOException(ret);
            }
            return(ret != 0);
        }
Пример #17
0
        public int FileOpen(string file, int mode)
        {
            short ret = PiGpioNativeMethods.fileOpen(file, (short)mode);

            if (ret < 0)
            {
                throw new PiGPIOException(ret);
            }
            return(ret);
        }
Пример #18
0
        public int I2COpen(int bus, int address, int flags)
        {
            short ret = PiGpioNativeMethods.I2COpen((ushort)bus, (ushort)address, (ushort)flags);

            if (ret < 0)
            {
                throw new PiGPIOException(ret);
            }
            return(ret);
        }
Пример #19
0
        public int SpiOpen(int chan, int baud, int flags)
        {
            short ret = PiGpioNativeMethods.spiOpen((ushort)chan, (ushort)baud, (ushort)flags);

            if (ret < 0)
            {
                throw new PiGPIOException(ret);
            }
            return(ret);
        }
Пример #20
0
        public void SpiWrite(int handle, byte[] data)
        {
            GCHandle h = GCHandle.Alloc(data, GCHandleType.Pinned);

            try
            {
                short ret = PiGpioNativeMethods.spiWrite((ushort)handle, h.AddrOfPinnedObject(), (ushort)data.Length);
                if (ret < 0)
                {
                    throw new PiGPIOException(ret);
                }
            }
            finally
            {
                h.Free();
            }
        }
Пример #21
0
        public byte[] SpiRead(int handle, int count)
        {
            byte[]   data = new byte[count];
            GCHandle h    = GCHandle.Alloc(data, GCHandleType.Pinned);

            try
            {
                short ret = PiGpioNativeMethods.spiRead((ushort)handle, h.AddrOfPinnedObject(), (ushort)data.Length);
                if (ret < 0)
                {
                    throw new PiGPIOException(ret);
                }
            }
            finally
            {
                h.Free();
            }
            return(data);
        }
Пример #22
0
        public PiGpio()
        {
            //Disable internal signal handling
            uint cfg = PiGpioNativeMethods.cfgGetInternals();

            cfg = unchecked ((uint)(cfg & ~PiGpioNativeMethods.PI_CFG_SIGHANDLER));
            short cfgRet = PiGpioNativeMethods.cfgSetInternals(cfg);

            if (cfgRet < 0)
            {
                throw new PiGPIOException(cfgRet);
            }

            int ret = PiGpioNativeMethods.Initialise();

            if (ret < 0)
            {
                throw new PiGPIOException(ret);
            }
        }
Пример #23
0
 public int ReadBits_32_53()
 {
     return(unchecked ((int)PiGpioNativeMethods.readBits_32_53()));
 }
Пример #24
0
 public int PigpioVersion()
 {
     return(unchecked ((int)PiGpioNativeMethods.Version()));
 }
Пример #25
0
 public void Dispose()
 {
     PiGpioNativeMethods.Terminate();
 }
Пример #26
0
 public int HardwareRevision()
 {
     return(unchecked ((int)PiGpioNativeMethods.HardwareRevision()));
 }