Created
August 13, 2013 20:22
-
-
Save Redth/6225289 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using Android.App; | |
| using Android.Content; | |
| using Android.Runtime; | |
| using Android.Views; | |
| using Android.Widget; | |
| using Android.OS; | |
| using Android.Hardware; | |
| namespace HeyKarlWakeUp | |
| { | |
| [Activity (Label = "HeyKarlWakeUp", MainLauncher = true)] | |
| public class MainActivity : Activity | |
| { | |
| Camera camera; | |
| Camera.Parameters cameraParameters; | |
| private bool isFlashOn = false; | |
| protected override void OnCreate (Bundle bundle) | |
| { | |
| base.OnCreate (bundle); | |
| // Set our view from the "main" layout resource | |
| SetContentView (Resource.Layout.Main); | |
| // Get our button from the layout resource, | |
| // and attach an event to it | |
| Button button = FindViewById<Button> (Resource.Id.myButton); | |
| button.Click += (sender, e) => PlayMarioPattern(delay => { | |
| Toggle(true); | |
| System.Threading.Thread.Sleep(100); | |
| Toggle(false); | |
| System.Threading.Thread.Sleep(delay); | |
| }); | |
| try | |
| { | |
| camera = Camera.Open(); | |
| cameraParameters = camera.GetParameters (); | |
| camera.StartPreview (); | |
| } | |
| catch (Exception ex) | |
| { | |
| Android.Util.Log.Debug ("CAM", ex.ToString()); | |
| camera = null; | |
| } | |
| } | |
| protected override void OnStop () | |
| { | |
| base.OnStop (); | |
| if (camera != null) { | |
| if (isFlashOn) | |
| Toggle (false); | |
| camera.StopPreview (); | |
| camera.Release(); | |
| } | |
| } | |
| void Toggle(bool flashOn) | |
| { | |
| try | |
| { | |
| if (flashOn) | |
| { | |
| var fm = Camera.Parameters.FlashModeTorch; | |
| cameraParameters = camera.GetParameters (); | |
| if (!cameraParameters.SupportedFlashModes.Contains (Camera.Parameters.FlashModeTorch)) | |
| fm = Camera.Parameters.FlashModeOn; | |
| try | |
| { | |
| cameraParameters.FlashMode = fm; | |
| camera.SetParameters(cameraParameters); | |
| } | |
| catch (Exception ex) | |
| { | |
| Android.Util.Log.Debug ("CAM", ex.ToString()); | |
| } | |
| } else { | |
| try | |
| { | |
| cameraParameters.FlashMode = Camera.Parameters.FlashModeOff; | |
| camera.SetParameters(cameraParameters); | |
| } | |
| catch (Exception ex) | |
| { | |
| Android.Util.Log.Debug ("CAM", ex.ToString()); | |
| } | |
| } | |
| } | |
| catch (Exception ex) | |
| { | |
| Android.Util.Log.Debug ("CAM", ex.ToString()); | |
| } | |
| } | |
| void PlayMarioPattern(Action<int> flash) | |
| { | |
| flash(75); | |
| flash(150); | |
| flash(150); | |
| flash(50); | |
| flash(150); | |
| flash(275); | |
| flash(287); | |
| flash(225); | |
| flash(200); | |
| flash(250); | |
| flash(150); | |
| flash(165); | |
| flash(75); | |
| flash(150); | |
| flash(100); | |
| flash(100); | |
| flash(75); | |
| flash(150); | |
| flash(75); | |
| flash(175); | |
| flash(150); | |
| flash(75); | |
| flash(75); | |
| flash(175); | |
| flash(275); | |
| flash(200); | |
| flash(250); | |
| flash(150); | |
| flash(165); | |
| flash(75); | |
| flash(150); | |
| flash(100); | |
| flash(100); | |
| flash(75); | |
| flash(150); | |
| flash(75); | |
| flash(175); | |
| flash(150); | |
| flash(75); | |
| flash(75); | |
| flash(250); | |
| flash(150); | |
| flash(50); | |
| flash(75); | |
| flash(75); | |
| flash(150); | |
| flash(150); | |
| flash(75); | |
| flash(75); | |
| flash(150); | |
| flash(75); | |
| flash(50); | |
| flash(110); | |
| flash(150); | |
| flash(50); | |
| flash(75); | |
| flash(75); | |
| flash(150); | |
| flash(150); | |
| flash(150); | |
| flash(75); | |
| flash(150); | |
| flash(150); | |
| flash(150); | |
| flash(50); | |
| flash(75); | |
| flash(75); | |
| flash(150); | |
| flash(150); | |
| flash(75); | |
| flash(75); | |
| flash(150); | |
| flash(75); | |
| flash(50); | |
| flash(110); | |
| flash(150); | |
| flash(50); | |
| flash(75); | |
| flash(75); | |
| flash(150); | |
| flash(150); | |
| flash(150); | |
| flash(75); | |
| flash(150); | |
| flash(150); | |
| flash(150); | |
| flash(50); | |
| flash(75); | |
| flash(75); | |
| flash(150); | |
| flash(150); | |
| flash(75); | |
| flash(75); | |
| flash(1000); | |
| flash(200); | |
| flash(200); | |
| flash(200); | |
| flash(200); | |
| flash(2000); | |
| } | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment