Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Threading;
- using Microsoft.SPOT;
- using Microsoft.SPOT.Hardware;
- using SecretLabs.NETMF.Hardware;
- using SecretLabs.NETMF.Hardware.Netduino;
- using Netduino_USB_HID_Library;
- namespace iTunesHIDRemote
- {
- public class Program
- {
- private static readonly InterruptPort LeftButton = new InterruptPort(Pins.GPIO_PIN_D2, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLow);
- private static DateTime _leftButtonLastPress = DateTime.Now;
- private static readonly InterruptPort CenterButton = new InterruptPort(Pins.GPIO_PIN_D3, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLow);
- private static DateTime _centerButtonLastPress = DateTime.Now;
- private static readonly InterruptPort RightButton = new InterruptPort(Pins.GPIO_PIN_D4, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLow);
- private static DateTime _rightButtonLastPress = DateTime.Now;
- private static readonly AnalogInput Pot = new AnalogInput(Pins.GPIO_PIN_A0);
- private static double _potLastValue;
- private const int PotTolerance = 10;
- private static int PotMax = 1023;
- private static int PotMin = 80;
- public static void Main()
- {
- LeftButton.OnInterrupt += leftButton_OnInterrupt;
- CenterButton.OnInterrupt += centerButton_OnInterrupt;
- RightButton.OnInterrupt += rightButton_OnInterrupt;
- string setupResult = Keyboard.SetUp();
- if (setupResult != "Success")
- return;
- int value, range;
- float inc;
- while(true)
- {
- value = Pot.Read();
- if (CalculateChangeInPotValue(value))
- {
- Debug.Print("Volume: " + value);
- if (value > PotMax)
- PotMax = value;
- if (value < PotMin)
- PotMin = value;
- _potLastValue = value;
- range = PotMax - PotMin;
- inc = range/9;
- if (value < inc * 1)
- Keyboard.Send(16);
- if (value > inc * 1 && value < inc * 2)
- Keyboard.Send(17);
- if (value > inc * 2 && value < inc * 3)
- Keyboard.Send(18);
- if (value > inc * 3 && value < inc * 4)
- Keyboard.Send(19);
- if (value > inc * 4 && value < inc * 5)
- Keyboard.Send(20);
- if (value > inc * 5 && value < inc * 6)
- Keyboard.Send(21);
- if (value > inc * 6 && value < inc * 7)
- Keyboard.Send(22);
- if (value > inc * 7 && value < inc * 8)
- Keyboard.Send(23);
- if (value > inc * 8)
- Keyboard.Send(24);
- }
- Thread.Sleep(25);
- }
- }
- static bool CalculateChangeInPotValue(int value)
- {
- return _potLastValue - PotTolerance > value || _potLastValue + PotTolerance < value;
- }
- static void rightButton_OnInterrupt(uint data1, uint data2, DateTime time)
- {
- if ((DateTime.Now - _rightButtonLastPress).Milliseconds < 100)
- return;
- Debug.Print("Next Song");
- Keyboard.Send(13);
- _rightButtonLastPress = DateTime.Now;
- }
- static void centerButton_OnInterrupt(uint data1, uint data2, DateTime time)
- {
- if ((DateTime.Now - _centerButtonLastPress).Milliseconds < 100)
- return;
- Debug.Print("Play/Pause");
- Keyboard.Send(14);
- _centerButtonLastPress = DateTime.Now;
- }
- static void leftButton_OnInterrupt(uint data1, uint data2, DateTime time)
- {
- if ((DateTime.Now - _leftButtonLastPress).Milliseconds < 100)
- return;
- Debug.Print("Previous Song");
- Keyboard.Send(15);
- _leftButtonLastPress = DateTime.Now;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment