コード例 #1
0
        private async void InitI2C() {
            byte[] i2CWriteBuffer;
            byte[] i2CReadBuffer;
            byte bitMask;

            // Inicjalizacja I2C - urządzenie z RPI2
            string deviceSelector = I2cDevice.GetDeviceSelector();
            var i2cDeviceControllers = await DeviceInformation.FindAllAsync(deviceSelector);
            if (i2cDeviceControllers.Count == 0) {
                return;
            }

            //Ustawienia dla MCP230008
            var i2cSettings = new I2cConnectionSettings(I2C_ADDR_MCP23008);
            i2cSettings.BusSpeed = I2cBusSpeed.FastMode;
            i2cMCP23008 = await I2cDevice.FromIdAsync(i2cDeviceControllers[0].Id, i2cSettings);
            if (i2cMCP23008 == null) {
                return;
            }

            //Ustawienia dla PCF8591
            i2cSettings.SlaveAddress = I2C_ADDR_PCF8591;
            i2cPCF8591 = await I2cDevice.FromIdAsync(i2cDeviceControllers[0].Id, i2cSettings);
            if (i2cPCF8591 == null) {
                return;
            }

            //Ustawienia dla Arduino
            i2cSettings.SlaveAddress = I2C_ADDR_ARDUINO;
            i2cArduino = await I2cDevice.FromIdAsync(i2cDeviceControllers[0].Id, i2cSettings);
            if (i2cArduino == null) {
                return;
            }


            // Inicjalizacja port Expander 
			// Za: https://ms-iot.github.io/content/en-US/win10/samples/I2CPortExpander.htm 
            try {
                i2CReadBuffer = new byte[1];
                i2cMCP23008.WriteRead(new byte[] { MCP23008_IODIR }, i2CReadBuffer);
                iodirRegister = i2CReadBuffer[0];
                i2cMCP23008.WriteRead(new byte[] { MCP23008_GPIO }, i2CReadBuffer);
                gpioRegister = i2CReadBuffer[0];
                i2cMCP23008.WriteRead(new byte[] { MCP23008_OLAT }, i2CReadBuffer);
                olatRegister = i2CReadBuffer[0];

                // Konfiguracja PIN-a z laserem na 1; reszta bez zmian
                olatRegister |= LED_GPIO_PIN;
                i2CWriteBuffer = new byte[] { MCP23008_OLAT, olatRegister };
                i2cMCP23008.Write(i2CWriteBuffer);

                bitMask = (byte)(0xFF ^ LED_GPIO_PIN); // Tylko nasz PIN będzie miał maskę 0 - reszta będzie równa 1, co spowoduje że nie zmienią wartości
                iodirRegister &= bitMask;
                i2CWriteBuffer = new byte[] { MCP23008_IODIR, iodirRegister };
                i2cMCP23008.Write(i2CWriteBuffer);

            } catch (Exception e) {
                return;
            }


            //Komunikacja z Arduino
            byte[] wbuffer = new byte[] { 1, 2, 3, 4, 5, 6 };
            byte[] rbuffer = new byte[2];
			//Wysłanie liczb do dodania
            i2cArduino.Write(wbuffer);
            await Task.Delay(1000);
			//Odczytanie wyniku
            var result = i2cArduino.ReadPartial(rbuffer);
            //Wyświetlenie
			int sum = (((int)rbuffer[1]) << 8) + (int)rbuffer[0];
            Debug.WriteLine($"Suma:{sum}");

            //Błyskanie laserem co sekundę
            m_timer = new DispatcherTimer();
            m_timer.Interval = TimeSpan.FromMilliseconds(1000);
            m_timer.Tick += timer_Tick;
            m_timer.Start();

			//Zapis "trójkąta" do PCF8591 (w pętli, nieskończonej)
            await WritePCF8591();
        }
コード例 #2
0
ファイル: Movements.cs プロジェクト: peejster/hexapet
        // set the PWM frequency for the servos on the specified servo controll board 
        private static void setPWMFreq(I2cDevice board, int freq)
        {
            double preScale = 25000000.0;
            preScale /= 4096;
            preScale /= (float)freq;
            preScale -= 1.0;

            preScale = Math.Floor(preScale + .5);
            var oldMode = board.ReadPartial(new byte[] { (byte)(PCA9685_REG_MODE1) });
            var newMode = (oldMode.BytesTransferred & 0x7F) | 0x10;
            board.Write(new byte[] { (byte)PCA9685_REG_MODE1, (byte)(newMode) });
            board.Write(new byte[] { (byte)PCA9685_REG_PRESCALE, (byte)(Math.Floor(preScale)) });
            board.Write(new byte[] { (byte)PCA9685_REG_MODE1, (byte)(oldMode.BytesTransferred) });
            board.Write(new byte[] { (byte)PCA9685_REG_MODE1, (byte)(oldMode.BytesTransferred | 0x80) });
        }