The Microsoft Surface Pro has a gyroscope and accelerometer, Windows 8, and the full .NET framework.
Most articles I find that talk about the motion API point to the Windows Phone 8 API.
What .NET Framework namespaces and classes should I be using to get gyroscope and accelerometer data from?
I just worked based off the documentation - http://msdn.microsoft.com/en-us/library/ie/windows.devices.sensors
using Windows.Devices.Sensors;
private Accelerometer _accelerometer;
private void DoStuffWithAccel()
{
_accelerometer = Accelerometer.GetDefault();
if (_accelerometer != null)
{
AccelerometerReading reading = _accelerometer.GetCurrentReading();
if (reading != null)
double xreading = reading.AccelerationX;
... etc.
}
}
Haven't tested it, but it should work for any Windows Store App - If you're trying to make it run as a console/windows forms app, you need to change the targetplatform by:
Right Click your project -> Unload Project
Follow the rest of this https://software.intel.com/en-us/articles/using-winrt-apis-from-desktop-applications
For the surface pro you need to use the Windows 8.1 library, instead of the Windows Phone 8.1 library.
It should be in the same Windows.Devices.Sensors namespace.
using Windows.Devices.Sensors;
...
//if you aren't already doing so, and you want the default sensor
private void Init()
{
_accelerometer = Accelerometer.GetDefault();
_gyrometer = Gyrometer.GetDefault();
}
...
private void DisplayAccelReading(object sender, object args)
{
AccelerometerReading reading = _accelerometer.GetCurrentReading();
if (reading == null)
return;
ScenarioOutput_X.Text = String.Format("{0,5:0.00}", reading.AccelerationX);
ScenarioOutput_Y.Text = String.Format("{0,5:0.00}", reading.AccelerationY);
ScenarioOutput_Z.Text = String.Format("{0,5:0.00}", reading.AccelerationZ);
}
...
private void DisplayGyroReading(object sender, object args)
{
GyrometerReading reading = _gyrometer.GetCurrentReading();
if (reading == null)
return;
ScenarioOutput_AngVelX.Text =
String.Format("{0,5:0.00}", reading.AngularVelocityX);
ScenarioOutput_AngVelY.Text =
String.Format("{0,5:0.00}", reading.AngularVelocityY);
ScenarioOutput_AngVelZ.Text =
String.Format("{0,5:0.00}", reading.AngularVelocityZ);
}
Related
I have a program in which I use the Aforge library for viewing a webcam.
This works wonder:
LocalWebcamsCollection = new FilterInfoCollection(FilterCategory.VideoInputDevice);
LocalScannerBarcode = new VideoCaptureDevice(LocalWebcamsCollection[WebcamNumber].MonikerString);
LocalScannerBarcode.NewFrame += LocalScannerBarcode_NewFrame;
LocalScannerBarcode.Start();
and in the new frame event I get the bitmap
System.Drawing.Bitmap frame;
void LocalScannerBarcode_NewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs)
{
frame = (System.Drawing.Bitmap)eventArgs.Frame.Clone();
}
now I have to decode what is seen. Basically I have to pass the bitmap to decode.
So global I have;
ZXing.BarcodeReader bcr;
and into the event LocalScannerBarcode_NewFrame
if (bcr == null)
bcr = new ZXing.BarcodeReader();
but as soon as I put the two lines above the event is not called anymore.
Please notice that in Windows forms that works but I have to do it in WPF.
Thanks
Not sure if this helps but have you tried putting the reference to the ZXing library in another project? Something an Helper.
So in you project you will have:
string strResult = Helper.ReadBarcode(frame);
if (strResult != null)
{
... do stuff with the string
}
and in the helper
static ZXing.BarcodeReader bcr;
public static string ReadBarcode(System.Drawing.Bitmap bmp)
{
if (bcr == null)
bcr = new ZXing.BarcodeReader();
return bcr.Decode(bmp).ToString();
}
I am making an app that utilizes bluetooth function such as scanning devices etc. I checked the scan flag and returns true but not showing the discoverable device that I am testing.
I am using Samsung J7 Pro as my app test device and Samsung J7 as the device I want to see in the list of discovered devices.
J7 already set as discoverable and with bluetooth ON.
I based my codes in Monkey.BluetoothLE
Here is what I have:
Declarations
ObservableCollection<BluetoothViewModel> vm = new ObservableCollection<BluetoothViewModel>();
Android.Bluetooth.BluetoothManager _blManager;
Android.Bluetooth.BluetoothManager _blManager;
Robotics.Mobile.Core.Bluetooth.LE.Adapter _bleAdapter;
Functions
public BluetoothPage()
{
InitializeComponent();
lvInfo.ItemsSource = vm;
var appContext = Android.App.Application.Context;
_blManager = (Android.Bluetooth.BluetoothManager)appContext.GetSystemService("bluetooth");
_blAdapter = _blManager.Adapter;
_bleAdapter = new Robotics.Mobile.Core.Bluetooth.LE.Adapter();
_bleAdapter.DeviceDiscovered += _bleAdapter_DeviceDiscovered;
_bleAdapter.ScanTimeoutElapsed += _bleAdapter_ScanTimeoutElapsed;
}
private void btnScanStopBluetooth_Click(object sender, EventArgs e)
{
if (!_bleAdapter.IsScanning)
{
if (!_blAdapter.IsEnabled)
{
_blAdapter.Enable();
DisplayInformation("Turning on bluetooth...");
while (!_blAdapter.IsEnabled)
{
//do nothing until enabled
}
}
vm.Clear();
btnScan.Text = "Stop Scan";
_bleAdapter.StartScanningForDevices();
}
else
{
btnScan.Text = "Start Scan";
_bleAdapter.StopScanningForDevices();
}
}
private void _bleAdapter_DeviceDiscovered(object sender, Robotics.Mobile.Core.Bluetooth.LE.DeviceDiscoveredEventArgs e)
{
count++;
vm.Add(new BluetoothViewModel
{
Name = e.Device.Name,
ID = e.Device.ID.ToString(),
RSSI = e.Device.Rssi.ToString()
});
}
private void _bleAdapter_ScanTimeoutElapsed(object sender, EventArgs e)
{
DisplayInformation("Scan Timeout");
_bleAdapter.StopScanningForDevices();
btnScan.Text = "Start Scan";
}
private void DisplayInformation(string line)
{
lblStatus.Text = line;
}
A listview is bound to "vm" that will display the discovered device.
It does not show anything, and count is always zero but I checked the scan flag using _bleAdapter.IsScanning, it returns true.
EDIT:
I tried other open-source sample programs for Bluetooth such as
xamarin-bluetooth-le (BLE Explorer)
Bluetooth-Xamarin.Forms (DemoBluetooth)
None of them seem to list the device. When I use my built-in bluetooth app under settings, it lists the device. What am I missing here?
Have you granted permission for bluetooth and location?
You have to grant permission in the Manifest/or Settings and depending on the sdk (23+) also asking the user for extra permission.
https://learn.microsoft.com/en-us/xamarin/android/app-fundamentals/permissions?tabs=windows
I am following this tutorial to learn how to use OpenGL in C#. Everything ran fine until this part: OpenGL 4 with OpenTK in C# Part 10: Asteroid Invaders. I am not using the exact same OpenTK as the one used in the tutorial. I am using this version that is compatible with .NET Core here: https://www.nuget.org/packages/OpenTK.NETCore/
Issue
Seems that OpenTK does not detect my keyboard. I have a simple update loop that handles keyboard input like this:
OnUpdateFrame
protected override void OnUpdateFrame(FrameEventArgs e)
{
base.OnUpdateFrame(e);
this.HandleKeyboard(e.Time);
// {...} Update Logic
}
HandleKeyboard
protected void HandleKeyboard(double delta)
{
Console.WriteLine("Handling keyboard");
var keyState = this.Keyboard.GetState();
if (keyState.IsKeyDown(Key.Escape))
{
this.Exit();
}
if (keyState.IsKeyDown(Key.A))
{
Console.WriteLine("Moving left");
this.player.MoveLeft();
}
if (keyState.IsKeyDown(Key.D) {
Console.WriteLine("Moving right");
this.player.MoveRight();
}
if (!this.gameOver && keyState.IsKeyDown(Key.Space) && this.lastKeyboardState.IsKeyUp(Key.Space))
{
this.gameObjects.Add(this.gameObjectFactory.CreateBullet(this.player.Position));
}
this.lastKeyboardState = keyState;
}
The console shows Handling keyboard many times, when I press A or D it doesn't write Moving left or Moving right. When I debug, and hover over keystate there is a property IsConnected = false. Maybe OpenTK is not recognizing my keyboard?
I am using the laptop keyboard so nothing fancy.
According to the documentation on Keyboard.GetState() it says Gets the primary Keyboard device, or null if no keyboard exists. But it doesn't return null?
How can I solve this? Thanks
Minimal example
I tried this on a separate solution:
using System;
using OpenTK;
using OpenTK.Input;
namespace OpenTKTest
{
class Program
{
[STAThread]
static void Main(string[] args)
{
using (var window = new GameWindow())
{
window.UpdateFrame += (sender, eventArgs) =>
{
var state = window.Keyboard.GetState();
if (state.IsKeyDown(Key.A))
{
Console.WriteLine("KEYSTATE");
}
};
window.KeyDown += (sender, eventArgs) =>
{
if (eventArgs.Key == Key.A)
{
Console.WriteLine("KEYDOWN EVENT");
}
};
window.Run(60);
}
}
}
}
And effectively, when I press the A key, KEYDOWN EVENT gets written but not KEYSTATE. So definitely a bug.
From what I gather (having experienced similar results), OpenTK.Input.Keyboard API is not yet implemented.
It seems exactly like Win10 IoT - RaspBerry Pi2: ValueChanged not called when GPIO change
I have a raspberry pi 2 with win10 IoT (creator version) and have this C# code:
public sealed class StartupTask : IBackgroundTask
{
private const int SENSOR_PIN = 17;
private GpioPin pinSensor;
public void Run(IBackgroundTaskInstance taskInstance)
{
taskInstance.Canceled += TaskInstance_Canceled; // "destructor"
var gpio = GpioController.GetDefault();
if (gpio != null)
{
pinSensor = gpio.OpenPin(SENSOR_PIN); // also tried with GpioSharingMode.SharedReadOnly
var r = pinSensor.Read(); // works and changes if sensor changes. Verified with quickwatch
pinSensor.SetDriveMode(GpioPinDriveMode.Input);
pinSensor.DebounceTimeout = TimeSpan.FromMilliseconds(20);
pinSensor.ValueChanged += PinIn_ValueChanged;
}
}
private void PinIn_ValueChanged(GpioPin sender, GpioPinValueChangedEventArgs args)
{
// never gets hit...
}
private void TaskInstance_Canceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason)
{
pinSensor.Dispose();
}
}
led on sensor and quickwatch say the GpioPinValue does alternate between high and low... so should get hit...
When I retrieve the drive mode after setting it to input. It tells me it actually is set to input:
var dm = pinSensor.GetDriveMode();
as was suggested in the comment of the linked stack overflow issue. So what am I doing wrong? And more important: why?
When the Run method ends, unless a deferral object is created, the
Background Application ends. The common practice, for asynchronous
programming is to take a deferral like this:
var deferval = taskInstance.GetDeferral();
Ref:Developing Background Applications
I want to swipe images in windows phone 7.
Where do I begin from?
You can use the GestureService in the Silverlight Control Toolkit for Windows Phone 7. In your UI element, add the following piece of code (after you have referenced the toolkit's DLL in your WP7 project) -
<toolkit:GestureService.GestureListener>
<toolkit:GestureListener Flick="OnFlick"/>
</toolkit:GestureService.GestureListener>
Implement the handler OnFlick in the code-behind file, like so -
private void OnFlick(object sender, FlickGestureEventArgs e)
{
var vm = DataContext as SelectedCatalogViewModel;
if (vm != null)
{
// User flicked towards left
if (e.HorizontalVelocity < 0)
{
// Load the next image
LoadNextPage(null);
}
// User flicked towards right
if (e.HorizontalVelocity > 0)
{
// Load the previous image
LoadPreviousPage();
}
}
}
Hope this helps,
indyfromoz
If you don't want to use the silverlight toolkit you can use the XNA framework.
http://www.nickharris.net/2010/11/using-touchpanel-for-gestures-in-windows-phone-7/
Try this:
using Microsoft.Phone.Controls;
public partial class MyControl
{
public MyControl()
{
InitializeComponent();
var gl = GestureService.GetGestureListener(asd);
gl.Flick += new EventHandler<FlickGestureEventArgs>(GestureListener_Flick);
}
private void GestureListener_Flick(object sender, FlickGestureEventArgs e)
{
if (e.Direction == Orientation.Horizontal)
{
if (e.HorizontalVelocity < 0) // determine direction (Right > 0)
{
//Some Action
}
else
{
//Some Action
}
}
}
}