Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Threading;
- using Microsoft.SPOT;
- using Microsoft.SPOT.Presentation;
- using Microsoft.SPOT.Presentation.Controls;
- using Microsoft.SPOT.Presentation.Media;
- using Microsoft.SPOT.Touch;
- using Gadgeteer.Networking;
- using GT = Gadgeteer;
- using GTM = Gadgeteer.Modules;
- using Gadgeteer.Modules.GHIElectronics;
- namespace Gadgeteer_Joystick_Blink_02
- {
- public partial class Program
- {
- // Variable holders that are available everywhere
- TimeSpan blinkTick = new TimeSpan(0, 0, 0, 0, 1000);
- // led
- TimeSpan ledBlinkLength = new TimeSpan(0, 0, 0, 0, 100); // create a timebased variable holder for the length of the blinks
- int ledBlinkOffset = 500;
- // led1
- TimeSpan led1BlinkLength = new TimeSpan(0, 0, 0, 0, 100); // create a timebased variable holder for the length of the blinks
- int led1BlinkOffset = 500;
- // led2
- TimeSpan led2BlinkLength = new TimeSpan(0, 0, 0, 0, 100); // create a timebased variable holder for the length of the blinks
- int led2BlinkOffset = 500;
- // led color set to cold
- GT.Color hotColdColor = new GT.Color(0, 0, 255);
- int hotColdColorRed = 0;
- int hotColdColorGreen = 0;
- int hotColdColorBlue = 255;
- // create the main timer event robot to trigger repeated tasks
- GT.Timer mainTimer = new GT.Timer(1000, GT.Timer.BehaviorType.RunContinuously);
- // This method is run when the mainboard is powered up or reset.
- void ProgramStarted()
- {
- // here we go!
- Debug.Print("Program Started");
- // wake up with a start the timer event robot
- mainTimer.Tick += new GT.Timer.TickEventHandler(mainTimer_Tick);
- mainTimer.Start();
- // enable us to ask the joystick questions
- joystick.DebugPrintEnabled = true;
- // flip the green and blue!!
- led.GreenBlueSwapped = true;
- led1.GreenBlueSwapped = true;
- led2.GreenBlueSwapped = true;
- }
- void mainTimer_Tick(GT.Timer timer)
- {
- // this is what the event handling robot will do when it sees an event
- // tell the robot to look at the joystick and get it's position
- double joystickPosX = joystick.GetJoystickPostion().X; // assigns the x-direction from 0 to 1 to the variable holder joystickPosX
- double joystickPosY = joystick.GetJoystickPostion().Y; // assigns the y-direction from 0 to 1 to the variable holder joystickPosY
- // print out the numbers coming in from the joystick
- //Debug.Print(((int)(joystickPosX * 1000)).ToString()); // prints the current joystick position * 255 in the x-direction
- //Debug.Print(((int)(joystickPosY * 1000)).ToString()); // prints the current joystick position * 255 in the y-direction
- // convert the joystick's X position into milliseconds
- ledBlinkOffset = (int)(joystickPosX * 667);
- led1BlinkOffset = 667 - ledBlinkOffset;
- ledBlinkOffset += 166;
- led1BlinkOffset += 166;
- // convert the joystick's Y position into colors
- hotColdColorRed = (int)(joystickPosY * 255);
- hotColdColorBlue = 255 - hotColdColorRed;
- hotColdColor = GT.Color.FromRGB((byte)hotColdColorRed, (byte)hotColdColorGreen, (byte)hotColdColorBlue);
- // add blink time
- ledBlinkLength = TimeSpan.FromTicks((100 + (int)(joystickPosY * 200)) * 1000);
- // create a new led timer event robot
- GT.Timer ledTimer = new GT.Timer(ledBlinkOffset, GT.Timer.BehaviorType.RunOnce);
- ledTimer.Tick += new GT.Timer.TickEventHandler(ledTimer_Tick);
- // create a new led1 timer event robot
- GT.Timer led1Timer = new GT.Timer(led1BlinkOffset, GT.Timer.BehaviorType.RunOnce);
- led1Timer.Tick += new GT.Timer.TickEventHandler(led1timer_Tick);
- // create a new led1 timer event robot
- GT.Timer led2Timer = new GT.Timer(led2BlinkOffset, GT.Timer.BehaviorType.RunOnce);
- led2Timer.Tick += new GT.Timer.TickEventHandler(led2timer_Tick);
- // start the new led timer event robots
- ledTimer.Start();
- led1Timer.Start();
- led2Timer.Start();
- }
- // led timer event robot's instructions
- void ledTimer_Tick(GT.Timer timer)
- {
- led.BlinkOnce(hotColdColor, ledBlinkLength);
- timer.Stop();
- }
- // led1 timer event robot's instructions
- void led1timer_Tick(GT.Timer timer)
- {
- led1.BlinkOnce(hotColdColor, ledBlinkLength);
- timer.Stop();
- }
- // led2 timer event robot's instructions
- void led2timer_Tick(GT.Timer timer)
- {
- led2.BlinkOnce(hotColdColor, ledBlinkLength);
- timer.Stop();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment