unkwntech

Netduino iTunes HID Control

Jul 15th, 2012
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.28 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3. using Microsoft.SPOT;
  4. using Microsoft.SPOT.Hardware;
  5. using SecretLabs.NETMF.Hardware;
  6. using SecretLabs.NETMF.Hardware.Netduino;
  7. using Netduino_USB_HID_Library;
  8.  
  9. namespace iTunesHIDRemote
  10. {
  11.     public class Program
  12.     {
  13.         private static readonly InterruptPort LeftButton = new InterruptPort(Pins.GPIO_PIN_D2, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLow);
  14.         private static DateTime _leftButtonLastPress = DateTime.Now;
  15.         private static readonly InterruptPort CenterButton = new InterruptPort(Pins.GPIO_PIN_D3, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLow);
  16.         private static DateTime _centerButtonLastPress = DateTime.Now;
  17.         private static readonly InterruptPort RightButton = new InterruptPort(Pins.GPIO_PIN_D4, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLow);
  18.         private static DateTime _rightButtonLastPress = DateTime.Now;
  19.        
  20.         private static readonly AnalogInput Pot = new AnalogInput(Pins.GPIO_PIN_A0);
  21.         private static double _potLastValue;
  22.         private const int PotTolerance = 10;
  23.  
  24.         private static int PotMax = 1023;
  25.         private static int PotMin = 80;
  26.  
  27.         public static void Main()
  28.         {
  29.             LeftButton.OnInterrupt += leftButton_OnInterrupt;
  30.             CenterButton.OnInterrupt += centerButton_OnInterrupt;
  31.             RightButton.OnInterrupt += rightButton_OnInterrupt;
  32.  
  33.             string setupResult = Keyboard.SetUp();
  34.             if (setupResult != "Success")
  35.                 return;
  36.  
  37.             int value, range;
  38.             float inc;
  39.  
  40.             while(true)
  41.             {
  42.                 value = Pot.Read();
  43.                
  44.                 if (CalculateChangeInPotValue(value))
  45.                 {
  46.                     Debug.Print("Volume: " + value);
  47.  
  48.                     if (value > PotMax)
  49.                         PotMax = value;
  50.                     if (value < PotMin)
  51.                         PotMin = value;
  52.  
  53.                     _potLastValue = value;
  54.  
  55.                     range = PotMax - PotMin;
  56.                     inc = range/9;
  57.  
  58.                     if (value < inc * 1)
  59.                         Keyboard.Send(16);
  60.                     if (value > inc * 1 && value < inc * 2)
  61.                         Keyboard.Send(17);
  62.                     if (value > inc * 2 && value < inc * 3)
  63.                         Keyboard.Send(18);
  64.                     if (value > inc * 3 && value < inc * 4)
  65.                         Keyboard.Send(19);
  66.                     if (value > inc * 4 && value < inc * 5)
  67.                         Keyboard.Send(20);
  68.                     if (value > inc * 5 && value < inc * 6)
  69.                         Keyboard.Send(21);
  70.                     if (value > inc * 6 && value < inc * 7)
  71.                         Keyboard.Send(22);
  72.                     if (value > inc * 7 && value < inc * 8)
  73.                         Keyboard.Send(23);
  74.                     if (value > inc * 8)
  75.                         Keyboard.Send(24);
  76.  
  77.  
  78.  
  79.                 }
  80.                 Thread.Sleep(25);
  81.             }
  82.         }
  83.  
  84.         static bool CalculateChangeInPotValue(int value)
  85.         {
  86.             return _potLastValue - PotTolerance > value || _potLastValue + PotTolerance < value;
  87.         }
  88.  
  89.         static void rightButton_OnInterrupt(uint data1, uint data2, DateTime time)
  90.         {
  91.             if ((DateTime.Now - _rightButtonLastPress).Milliseconds < 100)
  92.                 return;
  93.             Debug.Print("Next Song");
  94.             Keyboard.Send(13);
  95.             _rightButtonLastPress = DateTime.Now;
  96.         }
  97.  
  98.         static void centerButton_OnInterrupt(uint data1, uint data2, DateTime time)
  99.         {
  100.             if ((DateTime.Now - _centerButtonLastPress).Milliseconds < 100)
  101.                 return;
  102.             Debug.Print("Play/Pause");
  103.             Keyboard.Send(14);
  104.             _centerButtonLastPress = DateTime.Now;
  105.         }
  106.  
  107.         static void leftButton_OnInterrupt(uint data1, uint data2, DateTime time)
  108.         {
  109.             if ((DateTime.Now - _leftButtonLastPress).Milliseconds < 100)
  110.                 return;
  111.             Debug.Print("Previous Song");
  112.             Keyboard.Send(15);
  113.             _leftButtonLastPress = DateTime.Now;
  114.         }
  115.  
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment