public async Task Run() { var board = await ConnectionService.Instance.GetFirstDeviceAsync(); await board.ConnectAsync(); var driver = new Apa102(board.Spi, 32); driver.AutoFlush = false; var encoder = new RotaryEncoder(board.Pins[13], board.Pins[12], 4); long oldPosition = 0; encoder.PositionChanged += async(s, e) => { var encoderAngle = 360 * e.NewPosition / 20f; // 20 clicks per rotation for (int i = 0; i < driver.Leds.Count; i++) { driver.Leds[i].SetHsl((360f / driver.Leds.Count) * i + encoderAngle, 100, 5); } await driver.FlushAsync(); if (e.NewPosition > oldPosition) { mouse_event(0x0800, 0, 0, -120, 0); } if (e.NewPosition < oldPosition) { mouse_event(0x0800, 0, 0, 120, 0); } oldPosition = e.NewPosition; }; }
void Initialize() { Console.WriteLine("Initialize hardware..."); ISpiBus spiBus = Device.CreateSpiBus(); //Not used but is need to create the SPI Peripheral IDigitalOutputPort spiPeriphChipSelect = Device.CreateDigitalOutputPort(Device.Pins.D04); apa102 = new Apa102(spiBus, spiPeriphChipSelect, 150, Apa102.PixelOrder.BGR); }
void Initialize() { Console.WriteLine("Initialize hardware..."); display = new Apa102(Device.CreateSpiBus(), Apa102.PixelOrder.BGR, false, 32, 8); graphics = new GraphicsLibrary(display); graphics.CurrentFont = new Font4x8(); graphics.Clear(); graphics.DrawText(0, 1, "MEADOW", Color.FromRgb(0, 0.1, 0.1)); graphics.DrawText(24, 1, "F7", Color.FromRgb(0.1, 0, 0)); graphics.Show(); }
void Initialize() { var onboardLed = new RgbPwmLed( device: Device, redPwmPin: Device.Pins.OnboardLedRed, greenPwmPin: Device.Pins.OnboardLedGreen, bluePwmPin: Device.Pins.OnboardLedBlue); onboardLed.SetColor(Color.Red); ISpiBus spiBus = Device.CreateSpiBus(); IDigitalOutputPort spiPeriphChipSelect = Device.CreateDigitalOutputPort(Device.Pins.D04); ledStrip = new Apa102( spiBus: spiBus, numberOfLeds: 18, pixelOrder: Apa102.PixelOrder.BGR); onboardLed.SetColor(Color.Green); }
void Initialize() { var onboardLed = new RgbPwmLed(device: Device, redPwmPin: Device.Pins.OnboardLedRed, greenPwmPin: Device.Pins.OnboardLedGreen, bluePwmPin: Device.Pins.OnboardLedBlue, 3.3f, 3.3f, 3.3f, IRgbLed.CommonType.CommonAnode); onboardLed.SetColor(Color.Red); Console.WriteLine("Initialize hardware..."); ISpiBus spiBus = Device.CreateSpiBus(); IDigitalOutputPort spiPeriphChipSelect = Device.CreateDigitalOutputPort(Device.Pins.D04); ledStrip = new Apa102( spiBus: spiBus, numberOfLeds: 18, pixelOrder: Apa102.PixelOrder.BGR); onboardLed.SetColor(Color.Green); }
static async Task App2() { var board = await ConnectionService.Instance.GetFirstDeviceAsync(); Console.WriteLine($"Connecting to {board} (Version: {board.VersionString})"); await board.ConnectAsync(); Console.WriteLine("Board connected. Starting rainbow demo"); Console.WriteLine("Press any key to clear display and disconnect."); var driver = new Apa102(board.Spi, 1); // change to match the number of LEDs you have connected driver.Brightness = 1; driver.AutoFlush = false; // only send updates when we explicitly Flush() int hueOffset = 0; while (!Console.KeyAvailable) { for (int i = 0; i < driver.Leds.Count; i++) { //driver.Leds[i].SetRgb(0, 0, 255); driver.Leds[i].SetHsl((360f / driver.Leds.Count) * i + hueOffset, 100, 50); } await driver.FlushAsync().ConfigureAwait(false); //await Task.Delay(10).ConfigureAwait(false); hueOffset += 1; } await driver.Clear(); board.Disconnect(); Console.WriteLine("Board disconnected"); }
protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); SetContentView(Resource.Layout.activity_main); _tempValueTxtiew = FindViewById <TextView>(Resource.Id.tempValue); _pressureValueTxtView = FindViewById <TextView>(Resource.Id.pressureValue); _sensorManager = (SensorManager)GetSystemService(SensorService); _dynamicSensorCallback = new DynamicSensorCallback(this); try { _buttonInputDriver = new ButtonInputDriver(BoardDefaults.GetButtonGpioPin(), Google.Android.Things.Contrib.Driver.Button.Button.LogicState.PressedWhenLow, (int)KeyEvent.KeyCodeFromString("KEYCODE_A")); _buttonInputDriver.Register(); Log.Debug(TAG, "Initialized GPIO Button that generates a keypress with KEYCODE_A"); } catch (Exception e) { throw new Exception("Error initializing GPIO button", e); } try { _environmentalSensorDriver = new Bmx280SensorDriver(BoardDefaults.GetI2cBus()); _sensorManager.RegisterDynamicSensorCallback(_dynamicSensorCallback); _environmentalSensorDriver.RegisterTemperatureSensor(); _environmentalSensorDriver.RegisterPressureSensor(); Log.Debug(TAG, "Initialized I2C BMP280"); } catch (Exception e) { throw new Exception("Error initializing BMP280", e); } try { _display = new AlphanumericDisplay(BoardDefaults.GetI2cBus()); _display.SetEnabled(true); _display.Clear(); Log.Debug(TAG, "Initialized I2C Display"); } catch (Exception e) { Log.Error(TAG, "Error initializing display", e); Log.Debug(TAG, "Display disabled"); _display = null; } try { _ledStrip = new Apa102(BoardDefaults.GetSpiBus(), Apa102.Mode.Bgr); _ledStrip.Brightness = LEDSTRIP_BRIGHTNESS; for (int i = 0; i < _rainbow.Length; i++) { float[] hsv = { i * 360f / _rainbow.Length, 1.0f, 1.0f }; _rainbow[i] = Color.HSVToColor(255, hsv); } } catch (Exception e) { Console.WriteLine(e); _ledStrip = null; } try { PeripheralManager pioService = PeripheralManager.Instance; _led = pioService.OpenGpio(BoardDefaults.GetLedGpioPin()); _led.SetEdgeTriggerType(Gpio.EdgeNone); _led.SetDirection(Gpio.DirectionOutInitiallyLow); _led.SetActiveType(Gpio.ActiveHigh); } catch (Exception e) { Console.WriteLine(e); } try { _speaker = new Speaker(BoardDefaults.GetSpeakerPwmPin()); ValueAnimator slide = ValueAnimator.OfFloat(440, 440 * 4); slide.SetDuration(50); slide.RepeatCount = 5; slide.SetInterpolator(new LinearInterpolator()); slide.AddUpdateListener(this); } catch (Exception e) { Console.WriteLine(e); throw; } }
protected override void OnDestroy() { base.OnDestroy(); // Clean up sensor registrations _sensorManager.UnregisterListener(new TemperatureListener(this)); _sensorManager.UnregisterListener(new PressureListener(this)); // Clean up peripheral. if (_environmentalSensorDriver != null) { try { _environmentalSensorDriver.Close(); } catch (IOException e) { e.PrintStackTrace(); } _environmentalSensorDriver = null; } if (_buttonInputDriver != null) { try { _buttonInputDriver.Close(); } catch (IOException e) { e.PrintStackTrace(); } _buttonInputDriver = null; } if (_display != null) { try { _display.Clear(); _display.SetEnabled(false); _display.Close(); } catch (IOException e) { e.PrintStackTrace(); } finally { _display = null; } } if (_ledStrip != null) { try { _ledStrip.Brightness = 0; _ledStrip.Write(new int[7]); _ledStrip.Close(); } catch (IOException e) { e.PrintStackTrace(); } finally { _ledStrip = null; } } if (_led != null) { try { _led.Value = false; _led.Close(); } catch (IOException e) { e.PrintStackTrace(); } finally { _led = null; } } //TODO https://github.com/androidthings/weatherstation/blob/master/app/src/main/java/com/example/androidthings/weatherstation/WeatherStationActivity.java#L304 }
public void Initialize() { if (_useHubs) { InitializeHubs(); } SensorManager = (SensorManager)GetSystemService(SensorService); _dynamicSensorCallback = new WeatherDynamicSensorCallback(this); PubSubHandler.GetInstance().Subscribe <TemperatureMessage>(OnTemperatureMessage); PubSubHandler.GetInstance().Subscribe <PressureMessage>(OnPressureMessage); PubSubHandler.GetInstance().Subscribe <HumidityMessage>(OnHumidityMessage); try { _ledRainbowStrip = new Apa102(BoardDefaults.GetSpiBus(), Apa102.Mode.Bgr); _ledRainbowStrip.Brightness = LedstripBrightness; for (var i = 0; i < _rainbow.Length; i++) { float[] hsv = { i * 360f / _rainbow.Length, 1.0f, 1.0f }; _rainbow[i] = Color.HSVToColor(255, hsv); } } catch (Exception e) { System.Console.WriteLine(e); _ledRainbowStrip = null; } try { var pioService = PeripheralManager.Instance; _led = pioService.OpenGpio(BoardDefaults.GetLedGpioPin()); _led.SetEdgeTriggerType(Gpio.EdgeNone); _led.SetDirection(Gpio.DirectionOutInitiallyLow); _led.SetActiveType(Gpio.ActiveHigh); } catch (Exception e) { System.Console.WriteLine(e); } try { _buttonInputDriver = new ButtonInputDriver(BoardDefaults.GetButtonGpioPin(), Button.LogicState.PressedWhenLow, (int)KeyEvent.KeyCodeFromString("KEYCODE_A")); _buttonInputDriver.Register(); Log.Debug(Tag, "Initialized GPIO Button that generates a keypress with KEYCODE_A"); } catch (Exception e) { throw new Exception("Error initializing GPIO button", e); } try { _bmx280SensorDriver = RainbowHat.CreateSensorDriver(); // Register the drivers with the framework SensorManager.RegisterDynamicSensorCallback(_dynamicSensorCallback); _bmx280SensorDriver.RegisterTemperatureSensor(); _bmx280SensorDriver.RegisterPressureSensor(); _bmx280SensorDriver.RegisterHumiditySensor(); Log.Debug(Tag, "Initialized I2C BMP280"); } catch (IOException e) { throw new RuntimeException("Error initializing BMP280", e); } try { _display = new AlphanumericDisplay(BoardDefaults.GetI2cBus()); _display.SetEnabled(true); _display.Clear(); Log.Debug(Tag, "Initialized I2C Display"); } catch (Exception e) { Log.Error(Tag, "Error initializing display", e); Log.Debug(Tag, "Display disabled"); _display = null; } }
void Initialize() { Console.WriteLine("Initialize hardware..."); apa102 = new Apa102(Device.CreateSpiBus(), 128, Apa102.PixelOrder.BGR); }